quantum.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  12. return process_record_user(keycode, record);
  13. }
  14. __attribute__ ((weak))
  15. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  16. return true;
  17. }
  18. __attribute__ ((weak))
  19. void leader_start(void) {}
  20. __attribute__ ((weak))
  21. void leader_end(void) {}
  22. uint8_t starting_note = 0x0C;
  23. int offset = 7;
  24. #ifdef AUDIO_ENABLE
  25. bool music_activated = false;
  26. // music sequencer
  27. static bool music_sequence_recording = false;
  28. static bool music_sequence_playing = false;
  29. static float music_sequence[16] = {0};
  30. static uint8_t music_sequence_count = 0;
  31. static uint8_t music_sequence_position = 0;
  32. static uint16_t music_sequence_timer = 0;
  33. static uint16_t music_sequence_interval = 100;
  34. #endif
  35. #ifdef MIDI_ENABLE
  36. bool midi_activated = false;
  37. #endif
  38. // Leader key stuff
  39. bool leading = false;
  40. uint16_t leader_time = 0;
  41. uint16_t leader_sequence[5] = {0, 0, 0, 0, 0};
  42. uint8_t leader_sequence_size = 0;
  43. // Chording stuff
  44. #define CHORDING_MAX 4
  45. bool chording = false;
  46. uint8_t chord_keys[CHORDING_MAX] = {0};
  47. uint8_t chord_key_count = 0;
  48. uint8_t chord_key_down = 0;
  49. #ifdef UNICODE_ENABLE
  50. static uint8_t input_mode;
  51. #endif
  52. // Shift / paren setup
  53. #ifndef LSPO_KEY
  54. #define LSPO_KEY KC_9
  55. #endif
  56. #ifndef RSPC_KEY
  57. #define RSPC_KEY KC_0
  58. #endif
  59. static bool shift_interrupted[2] = {0, 0};
  60. bool keys_chord(uint8_t keys[]) {
  61. uint8_t keys_size = sizeof(keys)/sizeof(keys[0]);
  62. bool pass = true;
  63. uint8_t in = 0;
  64. for (uint8_t i = 0; i < chord_key_count; i++) {
  65. bool found = false;
  66. for (uint8_t j = 0; j < keys_size; j++) {
  67. if (chord_keys[i] == (keys[j] & 0xFF)) {
  68. in++; // detects key in chord
  69. found = true;
  70. break;
  71. }
  72. }
  73. if (found)
  74. continue;
  75. if (chord_keys[i] != 0) {
  76. pass = false; // makes sure rest are blank
  77. }
  78. }
  79. return (pass && (in == keys_size));
  80. }
  81. #ifdef UNICODE_ENABLE
  82. uint16_t hex_to_keycode(uint8_t hex)
  83. {
  84. if (hex == 0x0) {
  85. return KC_0;
  86. } else if (hex < 0xA) {
  87. return KC_1 + (hex - 0x1);
  88. } else {
  89. return KC_A + (hex - 0xA);
  90. }
  91. }
  92. void set_unicode_mode(uint8_t os_target)
  93. {
  94. input_mode = os_target;
  95. }
  96. #endif
  97. bool process_record_quantum(keyrecord_t *record) {
  98. /* This gets the keycode from the key pressed */
  99. keypos_t key = record->event.key;
  100. uint16_t keycode;
  101. #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
  102. uint8_t layer;
  103. if (record->event.pressed) {
  104. layer = layer_switch_get_layer(key);
  105. update_source_layers_cache(key, layer);
  106. } else {
  107. layer = read_source_layers_cache(key);
  108. }
  109. keycode = keymap_key_to_keycode(layer, key);
  110. #else
  111. keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
  112. #endif
  113. if (!process_record_kb(keycode, record))
  114. return false;
  115. // This is how you use actions here
  116. // if (keycode == KC_LEAD) {
  117. // action_t action;
  118. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  119. // process_action(record, action);
  120. // return false;
  121. // }
  122. #ifdef MIDI_ENABLE
  123. if (keycode == MI_ON && record->event.pressed) {
  124. midi_activated = true;
  125. music_scale_user();
  126. return false;
  127. }
  128. if (keycode == MI_OFF && record->event.pressed) {
  129. midi_activated = false;
  130. midi_send_cc(&midi_device, 0, 0x7B, 0);
  131. return false;
  132. }
  133. if (midi_activated) {
  134. if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1)) {
  135. if (record->event.pressed) {
  136. starting_note++; // Change key
  137. midi_send_cc(&midi_device, 0, 0x7B, 0);
  138. }
  139. return false;
  140. }
  141. if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1)) {
  142. if (record->event.pressed) {
  143. starting_note--; // Change key
  144. midi_send_cc(&midi_device, 0, 0x7B, 0);
  145. }
  146. return false;
  147. }
  148. if (record->event.key.col == (MATRIX_COLS - 3) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
  149. offset++; // Change scale
  150. midi_send_cc(&midi_device, 0, 0x7B, 0);
  151. return false;
  152. }
  153. if (record->event.key.col == (MATRIX_COLS - 4) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
  154. offset--; // Change scale
  155. midi_send_cc(&midi_device, 0, 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. music_scale_user();
  219. return false;
  220. }
  221. if (keycode == MUV_DE && record->event.pressed) {
  222. voice_deiterate();
  223. music_scale_user();
  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. leader_sequence[3] = 0;
  284. leader_sequence[4] = 0;
  285. return false;
  286. }
  287. if (leading && timer_elapsed(leader_time) < LEADER_TIMEOUT) {
  288. leader_sequence[leader_sequence_size] = keycode;
  289. leader_sequence_size++;
  290. return false;
  291. }
  292. }
  293. #endif
  294. #define DISABLE_CHORDING
  295. #ifndef DISABLE_CHORDING
  296. if (keycode >= QK_CHORDING && keycode <= QK_CHORDING_MAX) {
  297. if (record->event.pressed) {
  298. if (!chording) {
  299. chording = true;
  300. for (uint8_t i = 0; i < CHORDING_MAX; i++)
  301. chord_keys[i] = 0;
  302. chord_key_count = 0;
  303. chord_key_down = 0;
  304. }
  305. chord_keys[chord_key_count] = (keycode & 0xFF);
  306. chord_key_count++;
  307. chord_key_down++;
  308. return false;
  309. } else {
  310. if (chording) {
  311. chord_key_down--;
  312. if (chord_key_down == 0) {
  313. chording = false;
  314. // Chord Dictionary
  315. if (keys_chord((uint8_t[]){KC_ENTER, KC_SPACE})) {
  316. register_code(KC_A);
  317. unregister_code(KC_A);
  318. return false;
  319. }
  320. for (uint8_t i = 0; i < chord_key_count; i++) {
  321. register_code(chord_keys[i]);
  322. unregister_code(chord_keys[i]);
  323. return false;
  324. }
  325. }
  326. }
  327. }
  328. }
  329. #endif
  330. #ifdef UNICODE_ENABLE
  331. if (keycode > QK_UNICODE && record->event.pressed) {
  332. uint16_t unicode = keycode & 0x7FFF;
  333. switch(input_mode) {
  334. case UC_OSX:
  335. register_code(KC_LALT);
  336. break;
  337. case UC_LNX:
  338. register_code(KC_LCTL);
  339. register_code(KC_LSFT);
  340. register_code(KC_U);
  341. unregister_code(KC_U);
  342. break;
  343. case UC_WIN:
  344. register_code(KC_LALT);
  345. register_code(KC_PPLS);
  346. unregister_code(KC_PPLS);
  347. break;
  348. }
  349. for(int i = 3; i >= 0; i--) {
  350. uint8_t digit = ((unicode >> (i*4)) & 0xF);
  351. register_code(hex_to_keycode(digit));
  352. unregister_code(hex_to_keycode(digit));
  353. }
  354. switch(input_mode) {
  355. case UC_OSX:
  356. case UC_WIN:
  357. unregister_code(KC_LALT);
  358. break;
  359. case UC_LNX:
  360. unregister_code(KC_LCTL);
  361. unregister_code(KC_LSFT);
  362. break;
  363. }
  364. }
  365. #endif
  366. // Shift / paren setup
  367. switch(keycode) {
  368. case RESET:
  369. if (record->event.pressed) {
  370. clear_keyboard();
  371. #ifdef AUDIO_ENABLE
  372. stop_all_notes();
  373. shutdown_user();
  374. #endif
  375. _delay_ms(250);
  376. #ifdef ATREUS_ASTAR
  377. *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
  378. #endif
  379. bootloader_jump();
  380. return false;
  381. }
  382. break;
  383. case DEBUG:
  384. if (record->event.pressed) {
  385. print("\nDEBUG: enabled.\n");
  386. debug_enable = true;
  387. return false;
  388. }
  389. break;
  390. case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_UNSWAP_ALT_GUI:
  391. if (record->event.pressed) {
  392. // MAGIC actions (BOOTMAGIC without the boot)
  393. if (!eeconfig_is_enabled()) {
  394. eeconfig_init();
  395. }
  396. /* keymap config */
  397. keymap_config.raw = eeconfig_read_keymap();
  398. if (keycode == MAGIC_SWAP_CONTROL_CAPSLOCK) {
  399. keymap_config.swap_control_capslock = 1;
  400. } else if (keycode == MAGIC_CAPSLOCK_TO_CONTROL) {
  401. keymap_config.capslock_to_control = 1;
  402. } else if (keycode == MAGIC_SWAP_LALT_LGUI) {
  403. keymap_config.swap_lalt_lgui = 1;
  404. } else if (keycode == MAGIC_SWAP_RALT_RGUI) {
  405. keymap_config.swap_ralt_rgui = 1;
  406. } else if (keycode == MAGIC_NO_GUI) {
  407. keymap_config.no_gui = 1;
  408. } else if (keycode == MAGIC_SWAP_GRAVE_ESC) {
  409. keymap_config.swap_grave_esc = 1;
  410. } else if (keycode == MAGIC_SWAP_BACKSLASH_BACKSPACE) {
  411. keymap_config.swap_backslash_backspace = 1;
  412. } else if (keycode == MAGIC_HOST_NKRO) {
  413. keymap_config.nkro = 1;
  414. } else if (keycode == MAGIC_SWAP_ALT_GUI) {
  415. keymap_config.swap_lalt_lgui = 1;
  416. keymap_config.swap_ralt_rgui = 1;
  417. }
  418. /* UNs */
  419. else if (keycode == MAGIC_UNSWAP_CONTROL_CAPSLOCK) {
  420. keymap_config.swap_control_capslock = 0;
  421. } else if (keycode == MAGIC_UNCAPSLOCK_TO_CONTROL) {
  422. keymap_config.capslock_to_control = 0;
  423. } else if (keycode == MAGIC_UNSWAP_LALT_LGUI) {
  424. keymap_config.swap_lalt_lgui = 0;
  425. } else if (keycode == MAGIC_UNSWAP_RALT_RGUI) {
  426. keymap_config.swap_ralt_rgui = 0;
  427. } else if (keycode == MAGIC_UNNO_GUI) {
  428. keymap_config.no_gui = 0;
  429. } else if (keycode == MAGIC_UNSWAP_GRAVE_ESC) {
  430. keymap_config.swap_grave_esc = 0;
  431. } else if (keycode == MAGIC_UNSWAP_BACKSLASH_BACKSPACE) {
  432. keymap_config.swap_backslash_backspace = 0;
  433. } else if (keycode == MAGIC_UNHOST_NKRO) {
  434. keymap_config.nkro = 0;
  435. } else if (keycode == MAGIC_UNSWAP_ALT_GUI) {
  436. keymap_config.swap_lalt_lgui = 0;
  437. keymap_config.swap_ralt_rgui = 0;
  438. }
  439. eeconfig_update_keymap(keymap_config.raw);
  440. return false;
  441. }
  442. break;
  443. case KC_LSPO: {
  444. if (record->event.pressed) {
  445. shift_interrupted[0] = false;
  446. register_mods(MOD_BIT(KC_LSFT));
  447. }
  448. else {
  449. if (!shift_interrupted[0]) {
  450. register_code(LSPO_KEY);
  451. unregister_code(LSPO_KEY);
  452. }
  453. unregister_mods(MOD_BIT(KC_LSFT));
  454. }
  455. return false;
  456. break;
  457. }
  458. case KC_RSPC: {
  459. if (record->event.pressed) {
  460. shift_interrupted[1] = false;
  461. register_mods(MOD_BIT(KC_RSFT));
  462. }
  463. else {
  464. if (!shift_interrupted[1]) {
  465. register_code(RSPC_KEY);
  466. unregister_code(RSPC_KEY);
  467. }
  468. unregister_mods(MOD_BIT(KC_RSFT));
  469. }
  470. return false;
  471. break;
  472. }
  473. default: {
  474. shift_interrupted[0] = true;
  475. shift_interrupted[1] = true;
  476. break;
  477. }
  478. }
  479. return process_action_kb(record);
  480. }
  481. const bool ascii_to_qwerty_shift_lut[0x80] PROGMEM = {
  482. 0, 0, 0, 0, 0, 0, 0, 0,
  483. 0, 0, 0, 0, 0, 0, 0, 0,
  484. 0, 0, 0, 0, 0, 0, 0, 0,
  485. 0, 0, 0, 0, 0, 0, 0, 0,
  486. 0, 1, 1, 1, 1, 1, 1, 0,
  487. 1, 1, 1, 1, 0, 0, 0, 0,
  488. 0, 0, 0, 0, 0, 0, 0, 0,
  489. 0, 0, 1, 0, 1, 0, 1, 1,
  490. 1, 1, 1, 1, 1, 1, 1, 1,
  491. 1, 1, 1, 1, 1, 1, 1, 1,
  492. 1, 1, 1, 1, 1, 1, 1, 1,
  493. 1, 1, 1, 0, 0, 0, 1, 1,
  494. 0, 0, 0, 0, 0, 0, 0, 0,
  495. 0, 0, 0, 0, 0, 0, 0, 0,
  496. 0, 0, 0, 0, 0, 0, 0, 0,
  497. 0, 0, 0, 1, 1, 1, 1, 0
  498. };
  499. const uint8_t ascii_to_qwerty_keycode_lut[0x80] PROGMEM = {
  500. 0, 0, 0, 0, 0, 0, 0, 0,
  501. KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
  502. 0, 0, 0, 0, 0, 0, 0, 0,
  503. 0, 0, 0, KC_ESC, 0, 0, 0, 0,
  504. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  505. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  506. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  507. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  508. KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  509. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  510. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  511. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  512. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  513. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  514. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  515. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  516. };
  517. /* for users whose OSes are set to Colemak */
  518. #if 0
  519. #include "keymap_colemak.h"
  520. const bool ascii_to_colemak_shift_lut[0x80] PROGMEM = {
  521. 0, 0, 0, 0, 0, 0, 0, 0,
  522. 0, 0, 0, 0, 0, 0, 0, 0,
  523. 0, 0, 0, 0, 0, 0, 0, 0,
  524. 0, 0, 0, 0, 0, 0, 0, 0,
  525. 0, 1, 1, 1, 1, 1, 1, 0,
  526. 1, 1, 1, 1, 0, 0, 0, 0,
  527. 0, 0, 0, 0, 0, 0, 0, 0,
  528. 0, 0, 1, 0, 1, 0, 1, 1,
  529. 1, 1, 1, 1, 1, 1, 1, 1,
  530. 1, 1, 1, 1, 1, 1, 1, 1,
  531. 1, 1, 1, 1, 1, 1, 1, 1,
  532. 1, 1, 1, 0, 0, 0, 1, 1,
  533. 0, 0, 0, 0, 0, 0, 0, 0,
  534. 0, 0, 0, 0, 0, 0, 0, 0,
  535. 0, 0, 0, 0, 0, 0, 0, 0,
  536. 0, 0, 0, 1, 1, 1, 1, 0
  537. };
  538. const uint8_t ascii_to_colemak_keycode_lut[0x80] PROGMEM = {
  539. 0, 0, 0, 0, 0, 0, 0, 0,
  540. KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
  541. 0, 0, 0, 0, 0, 0, 0, 0,
  542. 0, 0, 0, KC_ESC, 0, 0, 0, 0,
  543. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  544. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  545. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  546. KC_8, KC_9, CM_SCLN, CM_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  547. KC_2, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
  548. CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
  549. CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
  550. CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  551. KC_GRV, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
  552. CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
  553. CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
  554. CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  555. };
  556. #endif
  557. void send_string(const char *str) {
  558. while (1) {
  559. uint8_t keycode;
  560. uint8_t ascii_code = pgm_read_byte(str);
  561. if (!ascii_code) break;
  562. keycode = pgm_read_byte(&ascii_to_qwerty_keycode_lut[ascii_code]);
  563. if (pgm_read_byte(&ascii_to_qwerty_shift_lut[ascii_code])) {
  564. register_code(KC_LSFT);
  565. register_code(keycode);
  566. unregister_code(keycode);
  567. unregister_code(KC_LSFT);
  568. }
  569. else {
  570. register_code(keycode);
  571. unregister_code(keycode);
  572. }
  573. ++str;
  574. }
  575. }
  576. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  577. if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
  578. layer_on(layer3);
  579. } else {
  580. layer_off(layer3);
  581. }
  582. }
  583. void matrix_init_quantum() {
  584. #ifdef BACKLIGHT_ENABLE
  585. backlight_init_ports();
  586. #endif
  587. matrix_init_kb();
  588. }
  589. void matrix_scan_quantum() {
  590. #ifdef AUDIO_ENABLE
  591. if (music_sequence_playing) {
  592. if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
  593. music_sequence_timer = timer_read();
  594. stop_note(music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]);
  595. play_note(music_sequence[music_sequence_position], 0xF);
  596. music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
  597. }
  598. }
  599. #endif
  600. matrix_scan_kb();
  601. }
  602. #ifdef AUDIO_ENABLE
  603. bool is_music_on(void) {
  604. return (music_activated != 0);
  605. }
  606. void music_toggle(void) {
  607. if (!music_activated) {
  608. music_on();
  609. } else {
  610. music_off();
  611. }
  612. }
  613. void music_on(void) {
  614. music_activated = 1;
  615. music_on_user();
  616. }
  617. void music_off(void) {
  618. music_activated = 0;
  619. stop_all_notes();
  620. }
  621. #endif
  622. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  623. static const uint8_t backlight_pin = BACKLIGHT_PIN;
  624. #if BACKLIGHT_PIN == B7
  625. # define COM1x1 COM1C1
  626. # define OCR1x OCR1C
  627. #elif BACKLIGHT_PIN == B6
  628. # define COM1x1 COM1B1
  629. # define OCR1x OCR1B
  630. #elif BACKLIGHT_PIN == B5
  631. # define COM1x1 COM1A1
  632. # define OCR1x OCR1A
  633. #else
  634. # error "Backlight pin not supported - use B5, B6, or B7"
  635. #endif
  636. __attribute__ ((weak))
  637. void backlight_init_ports(void)
  638. {
  639. // Setup backlight pin as output and output low.
  640. // DDRx |= n
  641. _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
  642. // PORTx &= ~n
  643. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  644. // Use full 16-bit resolution.
  645. ICR1 = 0xFFFF;
  646. // I could write a wall of text here to explain... but TL;DW
  647. // Go read the ATmega32u4 datasheet.
  648. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  649. // Pin PB7 = OCR1C (Timer 1, Channel C)
  650. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  651. // (i.e. start high, go low when counter matches.)
  652. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  653. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  654. TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
  655. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  656. backlight_init();
  657. #ifdef BACKLIGHT_BREATHING
  658. breathing_defaults();
  659. #endif
  660. }
  661. __attribute__ ((weak))
  662. void backlight_set(uint8_t level)
  663. {
  664. // Prevent backlight blink on lowest level
  665. // PORTx &= ~n
  666. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  667. if ( level == 0 ) {
  668. // Turn off PWM control on backlight pin, revert to output low.
  669. TCCR1A &= ~(_BV(COM1x1));
  670. OCR1x = 0x0;
  671. } else if ( level == BACKLIGHT_LEVELS ) {
  672. // Turn on PWM control of backlight pin
  673. TCCR1A |= _BV(COM1x1);
  674. // Set the brightness
  675. OCR1x = 0xFFFF;
  676. } else {
  677. // Turn on PWM control of backlight pin
  678. TCCR1A |= _BV(COM1x1);
  679. // Set the brightness
  680. OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
  681. }
  682. #ifdef BACKLIGHT_BREATHING
  683. breathing_intensity_default();
  684. #endif
  685. }
  686. #ifdef BACKLIGHT_BREATHING
  687. #define BREATHING_NO_HALT 0
  688. #define BREATHING_HALT_OFF 1
  689. #define BREATHING_HALT_ON 2
  690. static uint8_t breath_intensity;
  691. static uint8_t breath_speed;
  692. static uint16_t breathing_index;
  693. static uint8_t breathing_halt;
  694. void breathing_enable(void)
  695. {
  696. if (get_backlight_level() == 0)
  697. {
  698. breathing_index = 0;
  699. }
  700. else
  701. {
  702. // Set breathing_index to be at the midpoint (brightest point)
  703. breathing_index = 0x20 << breath_speed;
  704. }
  705. breathing_halt = BREATHING_NO_HALT;
  706. // Enable breathing interrupt
  707. TIMSK1 |= _BV(OCIE1A);
  708. }
  709. void breathing_pulse(void)
  710. {
  711. if (get_backlight_level() == 0)
  712. {
  713. breathing_index = 0;
  714. }
  715. else
  716. {
  717. // Set breathing_index to be at the midpoint + 1 (brightest point)
  718. breathing_index = 0x21 << breath_speed;
  719. }
  720. breathing_halt = BREATHING_HALT_ON;
  721. // Enable breathing interrupt
  722. TIMSK1 |= _BV(OCIE1A);
  723. }
  724. void breathing_disable(void)
  725. {
  726. // Disable breathing interrupt
  727. TIMSK1 &= ~_BV(OCIE1A);
  728. backlight_set(get_backlight_level());
  729. }
  730. void breathing_self_disable(void)
  731. {
  732. if (get_backlight_level() == 0)
  733. {
  734. breathing_halt = BREATHING_HALT_OFF;
  735. }
  736. else
  737. {
  738. breathing_halt = BREATHING_HALT_ON;
  739. }
  740. //backlight_set(get_backlight_level());
  741. }
  742. void breathing_toggle(void)
  743. {
  744. if (!is_breathing())
  745. {
  746. if (get_backlight_level() == 0)
  747. {
  748. breathing_index = 0;
  749. }
  750. else
  751. {
  752. // Set breathing_index to be at the midpoint + 1 (brightest point)
  753. breathing_index = 0x21 << breath_speed;
  754. }
  755. breathing_halt = BREATHING_NO_HALT;
  756. }
  757. // Toggle breathing interrupt
  758. TIMSK1 ^= _BV(OCIE1A);
  759. // Restore backlight level
  760. if (!is_breathing())
  761. {
  762. backlight_set(get_backlight_level());
  763. }
  764. }
  765. bool is_breathing(void)
  766. {
  767. return (TIMSK1 && _BV(OCIE1A));
  768. }
  769. void breathing_intensity_default(void)
  770. {
  771. //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
  772. breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
  773. }
  774. void breathing_intensity_set(uint8_t value)
  775. {
  776. breath_intensity = value;
  777. }
  778. void breathing_speed_default(void)
  779. {
  780. breath_speed = 4;
  781. }
  782. void breathing_speed_set(uint8_t value)
  783. {
  784. bool is_breathing_now = is_breathing();
  785. uint8_t old_breath_speed = breath_speed;
  786. if (is_breathing_now)
  787. {
  788. // Disable breathing interrupt
  789. TIMSK1 &= ~_BV(OCIE1A);
  790. }
  791. breath_speed = value;
  792. if (is_breathing_now)
  793. {
  794. // Adjust index to account for new speed
  795. breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
  796. // Enable breathing interrupt
  797. TIMSK1 |= _BV(OCIE1A);
  798. }
  799. }
  800. void breathing_speed_inc(uint8_t value)
  801. {
  802. if ((uint16_t)(breath_speed - value) > 10 )
  803. {
  804. breathing_speed_set(0);
  805. }
  806. else
  807. {
  808. breathing_speed_set(breath_speed - value);
  809. }
  810. }
  811. void breathing_speed_dec(uint8_t value)
  812. {
  813. if ((uint16_t)(breath_speed + value) > 10 )
  814. {
  815. breathing_speed_set(10);
  816. }
  817. else
  818. {
  819. breathing_speed_set(breath_speed + value);
  820. }
  821. }
  822. void breathing_defaults(void)
  823. {
  824. breathing_intensity_default();
  825. breathing_speed_default();
  826. breathing_halt = BREATHING_NO_HALT;
  827. }
  828. /* Breathing Sleep LED brighness(PWM On period) table
  829. * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
  830. *
  831. * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
  832. * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
  833. */
  834. static const uint8_t breathing_table[64] PROGMEM = {
  835. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
  836. 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
  837. 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
  838. 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  839. };
  840. ISR(TIMER1_COMPA_vect)
  841. {
  842. // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
  843. uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
  844. if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
  845. {
  846. // Disable breathing interrupt
  847. TIMSK1 &= ~_BV(OCIE1A);
  848. }
  849. OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
  850. }
  851. #endif // breathing
  852. #else // backlight
  853. __attribute__ ((weak))
  854. void backlight_init_ports(void)
  855. {
  856. }
  857. __attribute__ ((weak))
  858. void backlight_set(uint8_t level)
  859. {
  860. }
  861. #endif // backlight
  862. __attribute__ ((weak))
  863. void led_set_user(uint8_t usb_led) {
  864. }
  865. __attribute__ ((weak))
  866. void led_set_kb(uint8_t usb_led) {
  867. led_set_user(usb_led);
  868. }
  869. __attribute__ ((weak))
  870. void led_init_ports(void)
  871. {
  872. }
  873. __attribute__ ((weak))
  874. void led_set(uint8_t usb_led)
  875. {
  876. // Example LED Code
  877. //
  878. // // Using PE6 Caps Lock LED
  879. // if (usb_led & (1<<USB_LED_CAPS_LOCK))
  880. // {
  881. // // Output high.
  882. // DDRE |= (1<<6);
  883. // PORTE |= (1<<6);
  884. // }
  885. // else
  886. // {
  887. // // Output low.
  888. // DDRE &= ~(1<<6);
  889. // PORTE &= ~(1<<6);
  890. // }
  891. led_set_kb(usb_led);
  892. }
  893. //------------------------------------------------------------------------------
  894. // Override these functions in your keymap file to play different tunes on
  895. // different events such as startup and bootloader jump
  896. __attribute__ ((weak))
  897. void startup_user() {}
  898. __attribute__ ((weak))
  899. void shutdown_user() {}
  900. __attribute__ ((weak))
  901. void music_on_user() {}
  902. __attribute__ ((weak))
  903. void audio_on_user() {}
  904. __attribute__ ((weak))
  905. void music_scale_user() {}
  906. //------------------------------------------------------------------------------