quantum.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "quantum.h"
  2. __attribute__ ((weak))
  3. void matrix_init_kb(void) {}
  4. __attribute__ ((weak))
  5. void matrix_scan_kb(void) {}
  6. __attribute__ ((weak))
  7. bool process_action_kb(keyrecord_t *record) {
  8. return true;
  9. }
  10. __attribute__ ((weak))
  11. void leader_start(void) {}
  12. __attribute__ ((weak))
  13. void leader_end(void) {}
  14. #ifdef AUDIO_ENABLE
  15. uint8_t starting_note = 0x0C;
  16. int offset = 0;
  17. bool music_activated = false;
  18. float music_scale[][2] = SONG(MUSIC_SCALE_SOUND);
  19. #endif
  20. // Leader key stuff
  21. bool leading = false;
  22. uint16_t leader_time = 0;
  23. uint16_t leader_sequence[3] = {0, 0, 0};
  24. uint8_t leader_sequence_size = 0;
  25. // Chording stuff
  26. #define CHORDING_MAX 4
  27. bool chording = false;
  28. uint8_t chord_keys[CHORDING_MAX] = {0};
  29. uint8_t chord_key_count = 0;
  30. uint8_t chord_key_down = 0;
  31. bool keys_chord(uint8_t keys[]) {
  32. uint8_t keys_size = sizeof(keys)/sizeof(keys[0]);
  33. bool pass = true;
  34. uint8_t in = 0;
  35. for (uint8_t i = 0; i < chord_key_count; i++) {
  36. bool found = false;
  37. for (uint8_t j = 0; j < keys_size; j++) {
  38. if (chord_keys[i] == (keys[j] & 0xFF)) {
  39. in++; // detects key in chord
  40. found = true;
  41. break;
  42. }
  43. }
  44. if (found)
  45. continue;
  46. if (chord_keys[i] != 0) {
  47. pass = false; // makes sure rest are blank
  48. }
  49. }
  50. return (pass && (in == keys_size));
  51. }
  52. static bool music_sequence_recording = false;
  53. static bool music_sequence_playing = false;
  54. static float music_sequence[16] = {0};
  55. static uint8_t music_sequence_count = 0;
  56. static uint8_t music_sequence_position = 0;
  57. static uint16_t music_sequence_timer = 0;
  58. static uint16_t music_sequence_interval = 100;
  59. bool process_record_quantum(keyrecord_t *record) {
  60. /* This gets the keycode from the key pressed */
  61. keypos_t key = record->event.key;
  62. uint16_t keycode;
  63. #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
  64. uint8_t layer;
  65. if (record->event.pressed) {
  66. layer = layer_switch_get_layer(key);
  67. update_source_layers_cache(key, layer);
  68. } else {
  69. layer = read_source_layers_cache(key);
  70. }
  71. keycode = keymap_key_to_keycode(layer, key);
  72. #else
  73. keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
  74. #endif
  75. // This is how you use actions here
  76. // if (keycode == KC_LEAD) {
  77. // action_t action;
  78. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  79. // process_action(record, action);
  80. // return false;
  81. // }
  82. #ifdef AUDIO_ENABLE
  83. if (keycode == AU_ON && record->event.pressed) {
  84. audio_on();
  85. audio_on_callback();
  86. return false;
  87. }
  88. if (keycode == AU_OFF && record->event.pressed) {
  89. audio_off();
  90. return false;
  91. }
  92. if (keycode == MU_ON && record->event.pressed) {
  93. music_activated = true;
  94. PLAY_NOTE_ARRAY(music_scale, false, 0);
  95. return false;
  96. }
  97. if (keycode == MU_OFF && record->event.pressed) {
  98. music_activated = false;
  99. stop_all_notes();
  100. return false;
  101. }
  102. if (keycode == MUV_IN && record->event.pressed) {
  103. voice_iterate();
  104. PLAY_NOTE_ARRAY(music_scale, false, 0);
  105. return false;
  106. }
  107. if (keycode == MUV_DE && record->event.pressed) {
  108. voice_deiterate();
  109. PLAY_NOTE_ARRAY(music_scale, false, 0);
  110. return false;
  111. }
  112. if (music_activated) {
  113. if (keycode == KC_LCTL && record->event.pressed) { // Start recording
  114. stop_all_notes();
  115. music_sequence_recording = true;
  116. music_sequence_playing = false;
  117. music_sequence_count = 0;
  118. return false;
  119. }
  120. if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing
  121. stop_all_notes();
  122. music_sequence_recording = false;
  123. music_sequence_playing = false;
  124. return false;
  125. }
  126. if (keycode == KC_LGUI && record->event.pressed) { // Start playing
  127. stop_all_notes();
  128. music_sequence_recording = false;
  129. music_sequence_playing = true;
  130. music_sequence_position = 0;
  131. music_sequence_timer = 0;
  132. return false;
  133. }
  134. if (keycode == KC_UP) {
  135. if (record->event.pressed)
  136. music_sequence_interval-=10;
  137. return false;
  138. }
  139. if (keycode == KC_DOWN) {
  140. if (record->event.pressed)
  141. music_sequence_interval+=10;
  142. return false;
  143. }
  144. float freq = ((float)220.0)*pow(2.0, -4.0)*pow(2.0,(starting_note + SCALE[record->event.key.col + offset])/12.0+(MATRIX_ROWS - record->event.key.row));
  145. if (record->event.pressed) {
  146. play_note(freq, 0xF);
  147. if (music_sequence_recording) {
  148. music_sequence[music_sequence_count] = freq;
  149. music_sequence_count++;
  150. }
  151. } else {
  152. stop_note(freq);
  153. }
  154. if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
  155. return false;
  156. }
  157. #endif
  158. #ifndef DISABLE_LEADER
  159. // Leader key set-up
  160. if (record->event.pressed) {
  161. if (!leading && keycode == KC_LEAD) {
  162. leader_start();
  163. leading = true;
  164. leader_time = timer_read();
  165. leader_sequence_size = 0;
  166. leader_sequence[0] = 0;
  167. leader_sequence[1] = 0;
  168. leader_sequence[2] = 0;
  169. return false;
  170. }
  171. if (leading && timer_elapsed(leader_time) < LEADER_TIMEOUT) {
  172. leader_sequence[leader_sequence_size] = keycode;
  173. leader_sequence_size++;
  174. return false;
  175. }
  176. }
  177. #endif
  178. #define DISABLE_CHORDING
  179. #ifndef DISABLE_CHORDING
  180. if (keycode >= 0x5700 && keycode <= 0x57FF) {
  181. if (record->event.pressed) {
  182. if (!chording) {
  183. chording = true;
  184. for (uint8_t i = 0; i < CHORDING_MAX; i++)
  185. chord_keys[i] = 0;
  186. chord_key_count = 0;
  187. chord_key_down = 0;
  188. }
  189. chord_keys[chord_key_count] = (keycode & 0xFF);
  190. chord_key_count++;
  191. chord_key_down++;
  192. return false;
  193. } else {
  194. if (chording) {
  195. chord_key_down--;
  196. if (chord_key_down == 0) {
  197. chording = false;
  198. // Chord Dictionary
  199. if (keys_chord((uint8_t[]){KC_ENTER, KC_SPACE})) {
  200. register_code(KC_A);
  201. unregister_code(KC_A);
  202. return false;
  203. }
  204. for (uint8_t i = 0; i < chord_key_count; i++) {
  205. register_code(chord_keys[i]);
  206. unregister_code(chord_keys[i]);
  207. return false;
  208. }
  209. }
  210. }
  211. }
  212. }
  213. #endif
  214. return process_action_kb(record);
  215. }
  216. void matrix_init_quantum() {
  217. matrix_init_kb();
  218. }
  219. void matrix_scan_quantum() {
  220. #ifdef AUDIO_ENABLE
  221. if (music_sequence_playing) {
  222. if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
  223. music_sequence_timer = timer_read();
  224. stop_note(music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]);
  225. play_note(music_sequence[music_sequence_position], 0xF);
  226. music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
  227. }
  228. }
  229. #endif
  230. matrix_scan_kb();
  231. }