quantum.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #include "quantum.h"
  2. #include "timer.h"
  3. __attribute__ ((weak))
  4. void matrix_init_kb(void) {}
  5. __attribute__ ((weak))
  6. void matrix_scan_kb(void) {}
  7. __attribute__ ((weak))
  8. bool process_action_kb(keyrecord_t *record) {
  9. return true;
  10. }
  11. __attribute__ ((weak))
  12. void leader_start(void) {}
  13. __attribute__ ((weak))
  14. void leader_end(void) {}
  15. uint8_t starting_note = 0x0C;
  16. int offset = 7;
  17. #ifdef AUDIO_ENABLE
  18. bool music_activated = false;
  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_music_scale();
  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. return false;
  160. }
  161. if (keycode == AU_OFF && record->event.pressed) {
  162. audio_off();
  163. return false;
  164. }
  165. if (keycode == AU_TOG && record->event.pressed) {
  166. if (is_audio_on())
  167. {
  168. audio_off();
  169. }
  170. else
  171. {
  172. audio_on();
  173. }
  174. return false;
  175. }
  176. if (keycode == MU_ON && record->event.pressed) {
  177. music_on();
  178. return false;
  179. }
  180. if (keycode == MU_OFF && record->event.pressed) {
  181. music_off();
  182. return false;
  183. }
  184. if (keycode == MU_TOG && record->event.pressed) {
  185. if (music_activated)
  186. {
  187. music_off();
  188. }
  189. else
  190. {
  191. music_on();
  192. }
  193. return false;
  194. }
  195. if (keycode == MUV_IN && record->event.pressed) {
  196. voice_iterate();
  197. play_music_scale();
  198. return false;
  199. }
  200. if (keycode == MUV_DE && record->event.pressed) {
  201. voice_deiterate();
  202. play_music_scale();
  203. return false;
  204. }
  205. if (music_activated) {
  206. if (keycode == KC_LCTL && record->event.pressed) { // Start recording
  207. stop_all_notes();
  208. music_sequence_recording = true;
  209. music_sequence_playing = false;
  210. music_sequence_count = 0;
  211. return false;
  212. }
  213. if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing
  214. stop_all_notes();
  215. music_sequence_recording = false;
  216. music_sequence_playing = false;
  217. return false;
  218. }
  219. if (keycode == KC_LGUI && record->event.pressed) { // Start playing
  220. stop_all_notes();
  221. music_sequence_recording = false;
  222. music_sequence_playing = true;
  223. music_sequence_position = 0;
  224. music_sequence_timer = 0;
  225. return false;
  226. }
  227. if (keycode == KC_UP) {
  228. if (record->event.pressed)
  229. music_sequence_interval-=10;
  230. return false;
  231. }
  232. if (keycode == KC_DOWN) {
  233. if (record->event.pressed)
  234. music_sequence_interval+=10;
  235. return false;
  236. }
  237. 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));
  238. if (record->event.pressed) {
  239. play_note(freq, 0xF);
  240. if (music_sequence_recording) {
  241. music_sequence[music_sequence_count] = freq;
  242. music_sequence_count++;
  243. }
  244. } else {
  245. stop_note(freq);
  246. }
  247. if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
  248. return false;
  249. }
  250. #endif
  251. #ifndef DISABLE_LEADER
  252. // Leader key set-up
  253. if (record->event.pressed) {
  254. if (!leading && keycode == KC_LEAD) {
  255. leader_start();
  256. leading = true;
  257. leader_time = timer_read();
  258. leader_sequence_size = 0;
  259. leader_sequence[0] = 0;
  260. leader_sequence[1] = 0;
  261. leader_sequence[2] = 0;
  262. return false;
  263. }
  264. if (leading && timer_elapsed(leader_time) < LEADER_TIMEOUT) {
  265. leader_sequence[leader_sequence_size] = keycode;
  266. leader_sequence_size++;
  267. return false;
  268. }
  269. }
  270. #endif
  271. #define DISABLE_CHORDING
  272. #ifndef DISABLE_CHORDING
  273. if (keycode >= 0x5700 && keycode <= 0x57FF) {
  274. if (record->event.pressed) {
  275. if (!chording) {
  276. chording = true;
  277. for (uint8_t i = 0; i < CHORDING_MAX; i++)
  278. chord_keys[i] = 0;
  279. chord_key_count = 0;
  280. chord_key_down = 0;
  281. }
  282. chord_keys[chord_key_count] = (keycode & 0xFF);
  283. chord_key_count++;
  284. chord_key_down++;
  285. return false;
  286. } else {
  287. if (chording) {
  288. chord_key_down--;
  289. if (chord_key_down == 0) {
  290. chording = false;
  291. // Chord Dictionary
  292. if (keys_chord((uint8_t[]){KC_ENTER, KC_SPACE})) {
  293. register_code(KC_A);
  294. unregister_code(KC_A);
  295. return false;
  296. }
  297. for (uint8_t i = 0; i < chord_key_count; i++) {
  298. register_code(chord_keys[i]);
  299. unregister_code(chord_keys[i]);
  300. return false;
  301. }
  302. }
  303. }
  304. }
  305. }
  306. #endif
  307. return process_action_kb(record);
  308. }
  309. void matrix_init_quantum() {
  310. matrix_init_kb();
  311. }
  312. void matrix_scan_quantum() {
  313. #ifdef AUDIO_ENABLE
  314. if (music_sequence_playing) {
  315. if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
  316. music_sequence_timer = timer_read();
  317. stop_note(music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]);
  318. play_note(music_sequence[music_sequence_position], 0xF);
  319. music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
  320. }
  321. }
  322. #endif
  323. matrix_scan_kb();
  324. }
  325. bool is_music_on(void) {
  326. return (music_activated != 0);
  327. }
  328. void music_toggle(void) {
  329. if (!music_activated) {
  330. music_on();
  331. } else {
  332. music_off();
  333. }
  334. }
  335. void music_on(void) {
  336. music_activated = 1;
  337. music_on_user();
  338. }
  339. void music_off(void) {
  340. music_activated = 0;
  341. stop_all_notes();
  342. }
  343. __attribute__ ((weak))
  344. void music_on_user() {}