quantum.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 = 7;
  17. bool music_activated = false;
  18. float music_scale[][2] = SONG(MUSIC_SCALE_SOUND);
  19. #endif
  20. #ifdef MIDI_ENABLE
  21. bool midi_activated = false;
  22. #endif
  23. // Leader key stuff
  24. bool leading = false;
  25. uint16_t leader_time = 0;
  26. uint16_t leader_sequence[3] = {0, 0, 0};
  27. uint8_t leader_sequence_size = 0;
  28. // Chording stuff
  29. #define CHORDING_MAX 4
  30. bool chording = false;
  31. uint8_t chord_keys[CHORDING_MAX] = {0};
  32. uint8_t chord_key_count = 0;
  33. uint8_t chord_key_down = 0;
  34. bool keys_chord(uint8_t keys[]) {
  35. uint8_t keys_size = sizeof(keys)/sizeof(keys[0]);
  36. bool pass = true;
  37. uint8_t in = 0;
  38. for (uint8_t i = 0; i < chord_key_count; i++) {
  39. bool found = false;
  40. for (uint8_t j = 0; j < keys_size; j++) {
  41. if (chord_keys[i] == (keys[j] & 0xFF)) {
  42. in++; // detects key in chord
  43. found = true;
  44. break;
  45. }
  46. }
  47. if (found)
  48. continue;
  49. if (chord_keys[i] != 0) {
  50. pass = false; // makes sure rest are blank
  51. }
  52. }
  53. return (pass && (in == keys_size));
  54. }
  55. static bool music_sequence_recording = false;
  56. static bool music_sequence_playing = false;
  57. static float music_sequence[16] = {0};
  58. static uint8_t music_sequence_count = 0;
  59. static uint8_t music_sequence_position = 0;
  60. static uint16_t music_sequence_timer = 0;
  61. static uint16_t music_sequence_interval = 100;
  62. bool process_record_quantum(keyrecord_t *record) {
  63. /* This gets the keycode from the key pressed */
  64. keypos_t key = record->event.key;
  65. uint16_t keycode;
  66. #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
  67. uint8_t layer;
  68. if (record->event.pressed) {
  69. layer = layer_switch_get_layer(key);
  70. update_source_layers_cache(key, layer);
  71. } else {
  72. layer = read_source_layers_cache(key);
  73. }
  74. keycode = keymap_key_to_keycode(layer, key);
  75. #else
  76. keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
  77. #endif
  78. // This is how you use actions here
  79. // if (keycode == KC_LEAD) {
  80. // action_t action;
  81. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  82. // process_action(record, action);
  83. // return false;
  84. // }
  85. #ifdef MIDI_ENABLE
  86. if (keycode == MI_ON && record->event.pressed) {
  87. midi_activated = true;
  88. PLAY_NOTE_ARRAY(music_scale, false, 0);
  89. return false;
  90. }
  91. if (keycode == MI_OFF && record->event.pressed) {
  92. midi_activated = false;
  93. midi_send_cc(&midi_device, 0, 0x7B, 0);
  94. return false;
  95. }
  96. if (midi_activated) {
  97. if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1)) {
  98. if (record->event.pressed) {
  99. starting_note++; // Change key
  100. midi_send_cc(&midi_device, 0, 0x7B, 0);
  101. // midi_send_cc(&midi_device, 1, 0x7B, 0);
  102. // midi_send_cc(&midi_device, 2, 0x7B, 0);
  103. // midi_send_cc(&midi_device, 3, 0x7B, 0);
  104. // midi_send_cc(&midi_device, 4, 0x7B, 0);
  105. }
  106. return false;
  107. }
  108. if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1)) {
  109. if (record->event.pressed) {
  110. starting_note--; // Change key
  111. midi_send_cc(&midi_device, 0, 0x7B, 0);
  112. // midi_send_cc(&midi_device, 1, 0x7B, 0);
  113. // midi_send_cc(&midi_device, 2, 0x7B, 0);
  114. // midi_send_cc(&midi_device, 3, 0x7B, 0);
  115. // midi_send_cc(&midi_device, 4, 0x7B, 0);
  116. }
  117. return false;
  118. }
  119. if (record->event.key.col == (MATRIX_COLS - 3) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
  120. offset++; // Change scale
  121. midi_send_cc(&midi_device, 0, 0x7B, 0);
  122. // midi_send_cc(&midi_device, 1, 0x7B, 0);
  123. // midi_send_cc(&midi_device, 2, 0x7B, 0);
  124. // midi_send_cc(&midi_device, 3, 0x7B, 0);
  125. // midi_send_cc(&midi_device, 4, 0x7B, 0);
  126. return false;
  127. }
  128. if (record->event.key.col == (MATRIX_COLS - 4) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
  129. offset--; // Change scale
  130. midi_send_cc(&midi_device, 0, 0x7B, 0);
  131. // midi_send_cc(&midi_device, 1, 0x7B, 0);
  132. // midi_send_cc(&midi_device, 2, 0x7B, 0);
  133. // midi_send_cc(&midi_device, 3, 0x7B, 0);
  134. // midi_send_cc(&midi_device, 4, 0x7B, 0);
  135. return false;
  136. }
  137. // basic
  138. // uint8_t note = (starting_note + SCALE[record->event.key.col + offset])+12*(MATRIX_ROWS - record->event.key.row);
  139. // advanced
  140. // uint8_t note = (starting_note + record->event.key.col + offset)+12*(MATRIX_ROWS - record->event.key.row);
  141. // guitar
  142. uint8_t note = (starting_note + record->event.key.col + offset)+5*(MATRIX_ROWS - record->event.key.row);
  143. // violin
  144. // uint8_t note = (starting_note + record->event.key.col + offset)+7*(MATRIX_ROWS - record->event.key.row);
  145. if (record->event.pressed) {
  146. // midi_send_noteon(&midi_device, record->event.key.row, starting_note + SCALE[record->event.key.col], 127);
  147. midi_send_noteon(&midi_device, 0, note, 127);
  148. } else {
  149. // midi_send_noteoff(&midi_device, record->event.key.row, starting_note + SCALE[record->event.key.col], 127);
  150. midi_send_noteoff(&midi_device, 0, note, 127);
  151. }
  152. if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
  153. return false;
  154. }
  155. #endif
  156. #ifdef AUDIO_ENABLE
  157. if (keycode == AU_ON && record->event.pressed) {
  158. audio_on();
  159. audio_on_callback();
  160. return false;
  161. }
  162. if (keycode == AU_OFF && record->event.pressed) {
  163. audio_off();
  164. return false;
  165. }
  166. if (keycode == MU_ON && record->event.pressed) {
  167. music_activated = true;
  168. PLAY_NOTE_ARRAY(music_scale, false, 0);
  169. return false;
  170. }
  171. if (keycode == MU_OFF && record->event.pressed) {
  172. music_activated = false;
  173. stop_all_notes();
  174. return false;
  175. }
  176. if (keycode == MUV_IN && record->event.pressed) {
  177. voice_iterate();
  178. PLAY_NOTE_ARRAY(music_scale, false, 0);
  179. return false;
  180. }
  181. if (keycode == MUV_DE && record->event.pressed) {
  182. voice_deiterate();
  183. PLAY_NOTE_ARRAY(music_scale, false, 0);
  184. return false;
  185. }
  186. if (music_activated) {
  187. if (keycode == KC_LCTL && record->event.pressed) { // Start recording
  188. stop_all_notes();
  189. music_sequence_recording = true;
  190. music_sequence_playing = false;
  191. music_sequence_count = 0;
  192. return false;
  193. }
  194. if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing
  195. stop_all_notes();
  196. music_sequence_recording = false;
  197. music_sequence_playing = false;
  198. return false;
  199. }
  200. if (keycode == KC_LGUI && record->event.pressed) { // Start playing
  201. stop_all_notes();
  202. music_sequence_recording = false;
  203. music_sequence_playing = true;
  204. music_sequence_position = 0;
  205. music_sequence_timer = 0;
  206. return false;
  207. }
  208. if (keycode == KC_UP) {
  209. if (record->event.pressed)
  210. music_sequence_interval-=10;
  211. return false;
  212. }
  213. if (keycode == KC_DOWN) {
  214. if (record->event.pressed)
  215. music_sequence_interval+=10;
  216. return false;
  217. }
  218. float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(starting_note + SCALE[record->event.key.col + offset])/12.0+(MATRIX_ROWS - record->event.key.row));
  219. if (record->event.pressed) {
  220. play_note(freq, 0xF);
  221. if (music_sequence_recording) {
  222. music_sequence[music_sequence_count] = freq;
  223. music_sequence_count++;
  224. }
  225. } else {
  226. stop_note(freq);
  227. }
  228. if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
  229. return false;
  230. }
  231. #endif
  232. #ifndef DISABLE_LEADER
  233. // Leader key set-up
  234. if (record->event.pressed) {
  235. if (!leading && keycode == KC_LEAD) {
  236. leader_start();
  237. leading = true;
  238. leader_time = timer_read();
  239. leader_sequence_size = 0;
  240. leader_sequence[0] = 0;
  241. leader_sequence[1] = 0;
  242. leader_sequence[2] = 0;
  243. return false;
  244. }
  245. if (leading && timer_elapsed(leader_time) < LEADER_TIMEOUT) {
  246. leader_sequence[leader_sequence_size] = keycode;
  247. leader_sequence_size++;
  248. return false;
  249. }
  250. }
  251. #endif
  252. #define DISABLE_CHORDING
  253. #ifndef DISABLE_CHORDING
  254. if (keycode >= 0x5700 && keycode <= 0x57FF) {
  255. if (record->event.pressed) {
  256. if (!chording) {
  257. chording = true;
  258. for (uint8_t i = 0; i < CHORDING_MAX; i++)
  259. chord_keys[i] = 0;
  260. chord_key_count = 0;
  261. chord_key_down = 0;
  262. }
  263. chord_keys[chord_key_count] = (keycode & 0xFF);
  264. chord_key_count++;
  265. chord_key_down++;
  266. return false;
  267. } else {
  268. if (chording) {
  269. chord_key_down--;
  270. if (chord_key_down == 0) {
  271. chording = false;
  272. // Chord Dictionary
  273. if (keys_chord((uint8_t[]){KC_ENTER, KC_SPACE})) {
  274. register_code(KC_A);
  275. unregister_code(KC_A);
  276. return false;
  277. }
  278. for (uint8_t i = 0; i < chord_key_count; i++) {
  279. register_code(chord_keys[i]);
  280. unregister_code(chord_keys[i]);
  281. return false;
  282. }
  283. }
  284. }
  285. }
  286. }
  287. #endif
  288. return process_action_kb(record);
  289. }
  290. void matrix_init_quantum() {
  291. matrix_init_kb();
  292. }
  293. void matrix_scan_quantum() {
  294. #ifdef AUDIO_ENABLE
  295. if (music_sequence_playing) {
  296. if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
  297. music_sequence_timer = timer_read();
  298. stop_note(music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]);
  299. play_note(music_sequence[music_sequence_position], 0xF);
  300. music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
  301. }
  302. }
  303. #endif
  304. matrix_scan_kb();
  305. }