quantum.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. float music_scale[][2] = SONG(MUSIC_SCALE_SOUND);
  20. // music sequencer
  21. static bool music_sequence_recording = false;
  22. static bool music_sequence_playing = false;
  23. static float music_sequence[16] = {0};
  24. static uint8_t music_sequence_count = 0;
  25. static uint8_t music_sequence_position = 0;
  26. static uint16_t music_sequence_timer = 0;
  27. static uint16_t music_sequence_interval = 100;
  28. #endif
  29. #ifdef MIDI_ENABLE
  30. bool midi_activated = false;
  31. #endif
  32. // Leader key stuff
  33. bool leading = false;
  34. uint16_t leader_time = 0;
  35. uint16_t leader_sequence[3] = {0, 0, 0};
  36. uint8_t leader_sequence_size = 0;
  37. // Chording stuff
  38. #define CHORDING_MAX 4
  39. bool chording = false;
  40. uint8_t chord_keys[CHORDING_MAX] = {0};
  41. uint8_t chord_key_count = 0;
  42. uint8_t chord_key_down = 0;
  43. #ifdef UNICODE_ENABLE
  44. static uint8_t input_mode;
  45. #endif
  46. bool keys_chord(uint8_t keys[]) {
  47. uint8_t keys_size = sizeof(keys)/sizeof(keys[0]);
  48. bool pass = true;
  49. uint8_t in = 0;
  50. for (uint8_t i = 0; i < chord_key_count; i++) {
  51. bool found = false;
  52. for (uint8_t j = 0; j < keys_size; j++) {
  53. if (chord_keys[i] == (keys[j] & 0xFF)) {
  54. in++; // detects key in chord
  55. found = true;
  56. break;
  57. }
  58. }
  59. if (found)
  60. continue;
  61. if (chord_keys[i] != 0) {
  62. pass = false; // makes sure rest are blank
  63. }
  64. }
  65. return (pass && (in == keys_size));
  66. }
  67. #ifdef UNICODE_ENABLE
  68. uint16_t hex_to_keycode(uint8_t hex)
  69. {
  70. if (hex == 0x0) {
  71. return KC_0;
  72. } else if (hex < 0xA) {
  73. return KC_1 + (hex - 0x1);
  74. } else {
  75. return KC_A + (hex - 0xA);
  76. }
  77. }
  78. void set_unicode_mode(uint8_t os_target)
  79. {
  80. input_mode = os_target;
  81. }
  82. #endif
  83. bool process_record_quantum(keyrecord_t *record) {
  84. /* This gets the keycode from the key pressed */
  85. keypos_t key = record->event.key;
  86. uint16_t keycode;
  87. #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
  88. uint8_t layer;
  89. if (record->event.pressed) {
  90. layer = layer_switch_get_layer(key);
  91. update_source_layers_cache(key, layer);
  92. } else {
  93. layer = read_source_layers_cache(key);
  94. }
  95. keycode = keymap_key_to_keycode(layer, key);
  96. #else
  97. keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
  98. #endif
  99. // This is how you use actions here
  100. // if (keycode == KC_LEAD) {
  101. // action_t action;
  102. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  103. // process_action(record, action);
  104. // return false;
  105. // }
  106. #ifdef MIDI_ENABLE
  107. if (keycode == MI_ON && record->event.pressed) {
  108. midi_activated = true;
  109. play_music_scale();
  110. return false;
  111. }
  112. if (keycode == MI_OFF && record->event.pressed) {
  113. midi_activated = false;
  114. midi_send_cc(&midi_device, 0, 0x7B, 0);
  115. return false;
  116. }
  117. if (midi_activated) {
  118. if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1)) {
  119. if (record->event.pressed) {
  120. starting_note++; // Change key
  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. }
  127. return false;
  128. }
  129. if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1)) {
  130. if (record->event.pressed) {
  131. starting_note--; // Change key
  132. midi_send_cc(&midi_device, 0, 0x7B, 0);
  133. // midi_send_cc(&midi_device, 1, 0x7B, 0);
  134. // midi_send_cc(&midi_device, 2, 0x7B, 0);
  135. // midi_send_cc(&midi_device, 3, 0x7B, 0);
  136. // midi_send_cc(&midi_device, 4, 0x7B, 0);
  137. }
  138. return false;
  139. }
  140. if (record->event.key.col == (MATRIX_COLS - 3) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
  141. offset++; // Change scale
  142. midi_send_cc(&midi_device, 0, 0x7B, 0);
  143. // midi_send_cc(&midi_device, 1, 0x7B, 0);
  144. // midi_send_cc(&midi_device, 2, 0x7B, 0);
  145. // midi_send_cc(&midi_device, 3, 0x7B, 0);
  146. // midi_send_cc(&midi_device, 4, 0x7B, 0);
  147. return false;
  148. }
  149. if (record->event.key.col == (MATRIX_COLS - 4) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
  150. offset--; // Change scale
  151. midi_send_cc(&midi_device, 0, 0x7B, 0);
  152. // midi_send_cc(&midi_device, 1, 0x7B, 0);
  153. // midi_send_cc(&midi_device, 2, 0x7B, 0);
  154. // midi_send_cc(&midi_device, 3, 0x7B, 0);
  155. // midi_send_cc(&midi_device, 4, 0x7B, 0);
  156. return false;
  157. }
  158. // basic
  159. // uint8_t note = (starting_note + SCALE[record->event.key.col + offset])+12*(MATRIX_ROWS - record->event.key.row);
  160. // advanced
  161. // uint8_t note = (starting_note + record->event.key.col + offset)+12*(MATRIX_ROWS - record->event.key.row);
  162. // guitar
  163. uint8_t note = (starting_note + record->event.key.col + offset)+5*(MATRIX_ROWS - record->event.key.row);
  164. // violin
  165. // uint8_t note = (starting_note + record->event.key.col + offset)+7*(MATRIX_ROWS - record->event.key.row);
  166. if (record->event.pressed) {
  167. // midi_send_noteon(&midi_device, record->event.key.row, starting_note + SCALE[record->event.key.col], 127);
  168. midi_send_noteon(&midi_device, 0, note, 127);
  169. } else {
  170. // midi_send_noteoff(&midi_device, record->event.key.row, starting_note + SCALE[record->event.key.col], 127);
  171. midi_send_noteoff(&midi_device, 0, note, 127);
  172. }
  173. if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
  174. return false;
  175. }
  176. #endif
  177. #ifdef AUDIO_ENABLE
  178. if (keycode == AU_ON && record->event.pressed) {
  179. audio_on();
  180. return false;
  181. }
  182. if (keycode == AU_OFF && record->event.pressed) {
  183. audio_off();
  184. return false;
  185. }
  186. if (keycode == AU_TOG && record->event.pressed) {
  187. if (is_audio_on())
  188. {
  189. audio_off();
  190. }
  191. else
  192. {
  193. audio_on();
  194. }
  195. return false;
  196. }
  197. if (keycode == MU_ON && record->event.pressed) {
  198. music_on();
  199. return false;
  200. }
  201. if (keycode == MU_OFF && record->event.pressed) {
  202. music_off();
  203. return false;
  204. }
  205. if (keycode == MU_TOG && record->event.pressed) {
  206. if (music_activated)
  207. {
  208. music_off();
  209. }
  210. else
  211. {
  212. music_on();
  213. }
  214. return false;
  215. }
  216. if (keycode == MUV_IN && record->event.pressed) {
  217. voice_iterate();
  218. play_music_scale();
  219. return false;
  220. }
  221. if (keycode == MUV_DE && record->event.pressed) {
  222. voice_deiterate();
  223. play_music_scale();
  224. return false;
  225. }
  226. if (music_activated) {
  227. if (keycode == KC_LCTL && record->event.pressed) { // Start recording
  228. stop_all_notes();
  229. music_sequence_recording = true;
  230. music_sequence_playing = false;
  231. music_sequence_count = 0;
  232. return false;
  233. }
  234. if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing
  235. stop_all_notes();
  236. music_sequence_recording = false;
  237. music_sequence_playing = false;
  238. return false;
  239. }
  240. if (keycode == KC_LGUI && record->event.pressed) { // Start playing
  241. stop_all_notes();
  242. music_sequence_recording = false;
  243. music_sequence_playing = true;
  244. music_sequence_position = 0;
  245. music_sequence_timer = 0;
  246. return false;
  247. }
  248. if (keycode == KC_UP) {
  249. if (record->event.pressed)
  250. music_sequence_interval-=10;
  251. return false;
  252. }
  253. if (keycode == KC_DOWN) {
  254. if (record->event.pressed)
  255. music_sequence_interval+=10;
  256. return false;
  257. }
  258. 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));
  259. if (record->event.pressed) {
  260. play_note(freq, 0xF);
  261. if (music_sequence_recording) {
  262. music_sequence[music_sequence_count] = freq;
  263. music_sequence_count++;
  264. }
  265. } else {
  266. stop_note(freq);
  267. }
  268. if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
  269. return false;
  270. }
  271. #endif
  272. #ifndef DISABLE_LEADER
  273. // Leader key set-up
  274. if (record->event.pressed) {
  275. if (!leading && keycode == KC_LEAD) {
  276. leader_start();
  277. leading = true;
  278. leader_time = timer_read();
  279. leader_sequence_size = 0;
  280. leader_sequence[0] = 0;
  281. leader_sequence[1] = 0;
  282. leader_sequence[2] = 0;
  283. return false;
  284. }
  285. if (leading && timer_elapsed(leader_time) < LEADER_TIMEOUT) {
  286. leader_sequence[leader_sequence_size] = keycode;
  287. leader_sequence_size++;
  288. return false;
  289. }
  290. }
  291. #endif
  292. #define DISABLE_CHORDING
  293. #ifndef DISABLE_CHORDING
  294. if (keycode >= 0x5700 && keycode <= 0x57FF) {
  295. if (record->event.pressed) {
  296. if (!chording) {
  297. chording = true;
  298. for (uint8_t i = 0; i < CHORDING_MAX; i++)
  299. chord_keys[i] = 0;
  300. chord_key_count = 0;
  301. chord_key_down = 0;
  302. }
  303. chord_keys[chord_key_count] = (keycode & 0xFF);
  304. chord_key_count++;
  305. chord_key_down++;
  306. return false;
  307. } else {
  308. if (chording) {
  309. chord_key_down--;
  310. if (chord_key_down == 0) {
  311. chording = false;
  312. // Chord Dictionary
  313. if (keys_chord((uint8_t[]){KC_ENTER, KC_SPACE})) {
  314. register_code(KC_A);
  315. unregister_code(KC_A);
  316. return false;
  317. }
  318. for (uint8_t i = 0; i < chord_key_count; i++) {
  319. register_code(chord_keys[i]);
  320. unregister_code(chord_keys[i]);
  321. return false;
  322. }
  323. }
  324. }
  325. }
  326. }
  327. #endif
  328. #ifdef UNICODE_ENABLE
  329. if (keycode > UNICODE(0) && record->event.pressed) {
  330. uint16_t unicode = keycode & 0x7FFF;
  331. switch(input_mode) {
  332. case UC_OSX:
  333. register_code(KC_LALT);
  334. break;
  335. case UC_LNX:
  336. register_code(KC_LCTL);
  337. register_code(KC_LSFT);
  338. register_code(KC_U);
  339. unregister_code(KC_U);
  340. break;
  341. case UC_WIN:
  342. register_code(KC_LALT);
  343. register_code(KC_PPLS);
  344. unregister_code(KC_PPLS);
  345. break;
  346. }
  347. for(int i = 3; i >= 0; i--) {
  348. uint8_t digit = ((unicode >> (i*4)) & 0xF);
  349. register_code(hex_to_keycode(digit));
  350. unregister_code(hex_to_keycode(digit));
  351. }
  352. switch(input_mode) {
  353. case UC_OSX:
  354. case UC_WIN:
  355. unregister_code(KC_LALT);
  356. break;
  357. case UC_LNX:
  358. unregister_code(KC_LCTL);
  359. unregister_code(KC_LSFT);
  360. break;
  361. }
  362. }
  363. #endif
  364. return process_action_kb(record);
  365. }
  366. void matrix_init_quantum() {
  367. matrix_init_kb();
  368. }
  369. void matrix_scan_quantum() {
  370. #ifdef AUDIO_ENABLE
  371. if (music_sequence_playing) {
  372. if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
  373. music_sequence_timer = timer_read();
  374. stop_note(music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]);
  375. play_note(music_sequence[music_sequence_position], 0xF);
  376. music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
  377. }
  378. }
  379. #endif
  380. matrix_scan_kb();
  381. }
  382. bool is_music_on(void) {
  383. return (music_activated != 0);
  384. }
  385. void music_toggle(void) {
  386. if (!music_activated) {
  387. music_on();
  388. } else {
  389. music_off();
  390. }
  391. }
  392. void music_on(void) {
  393. music_activated = 1;
  394. music_on_user();
  395. }
  396. void music_off(void) {
  397. music_activated = 0;
  398. stop_all_notes();
  399. }
  400. __attribute__ ((weak))
  401. void music_on_user() {}