quantum.c 13 KB

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