quantum.c 27 KB

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