quantum.c 14 KB

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