quantum.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. /* Copyright 2016-2017 Jack Humbert
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "quantum.h"
  17. #ifdef PROTOCOL_LUFA
  18. #include "outputselect.h"
  19. #endif
  20. #ifndef TAPPING_TERM
  21. #define TAPPING_TERM 200
  22. #endif
  23. #include "backlight.h"
  24. extern backlight_config_t backlight_config;
  25. #ifdef FAUXCLICKY_ENABLE
  26. #include "fauxclicky.h"
  27. #endif
  28. #ifdef AUDIO_ENABLE
  29. #ifndef GOODBYE_SONG
  30. #define GOODBYE_SONG SONG(GOODBYE_SOUND)
  31. #endif
  32. #ifndef AG_NORM_SONG
  33. #define AG_NORM_SONG SONG(AG_NORM_SOUND)
  34. #endif
  35. #ifndef AG_SWAP_SONG
  36. #define AG_SWAP_SONG SONG(AG_SWAP_SOUND)
  37. #endif
  38. float goodbye_song[][2] = GOODBYE_SONG;
  39. float ag_norm_song[][2] = AG_NORM_SONG;
  40. float ag_swap_song[][2] = AG_SWAP_SONG;
  41. #ifdef DEFAULT_LAYER_SONGS
  42. float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
  43. #endif
  44. #endif
  45. static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
  46. switch (code) {
  47. case QK_MODS ... QK_MODS_MAX:
  48. break;
  49. default:
  50. return;
  51. }
  52. if (code & QK_LCTL)
  53. f(KC_LCTL);
  54. if (code & QK_LSFT)
  55. f(KC_LSFT);
  56. if (code & QK_LALT)
  57. f(KC_LALT);
  58. if (code & QK_LGUI)
  59. f(KC_LGUI);
  60. if (code < QK_RMODS_MIN) return;
  61. if (code & QK_RCTL)
  62. f(KC_RCTL);
  63. if (code & QK_RSFT)
  64. f(KC_RSFT);
  65. if (code & QK_RALT)
  66. f(KC_RALT);
  67. if (code & QK_RGUI)
  68. f(KC_RGUI);
  69. }
  70. static inline void qk_register_weak_mods(uint8_t kc) {
  71. add_weak_mods(MOD_BIT(kc));
  72. send_keyboard_report();
  73. }
  74. static inline void qk_unregister_weak_mods(uint8_t kc) {
  75. del_weak_mods(MOD_BIT(kc));
  76. send_keyboard_report();
  77. }
  78. static inline void qk_register_mods(uint8_t kc) {
  79. add_weak_mods(MOD_BIT(kc));
  80. send_keyboard_report();
  81. }
  82. static inline void qk_unregister_mods(uint8_t kc) {
  83. del_weak_mods(MOD_BIT(kc));
  84. send_keyboard_report();
  85. }
  86. void register_code16 (uint16_t code) {
  87. if (IS_MOD(code) || code == KC_NO) {
  88. do_code16 (code, qk_register_mods);
  89. } else {
  90. do_code16 (code, qk_register_weak_mods);
  91. }
  92. register_code (code);
  93. }
  94. void unregister_code16 (uint16_t code) {
  95. unregister_code (code);
  96. if (IS_MOD(code) || code == KC_NO) {
  97. do_code16 (code, qk_unregister_mods);
  98. } else {
  99. do_code16 (code, qk_unregister_weak_mods);
  100. }
  101. }
  102. __attribute__ ((weak))
  103. bool process_action_kb(keyrecord_t *record) {
  104. return true;
  105. }
  106. __attribute__ ((weak))
  107. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  108. return process_record_user(keycode, record);
  109. }
  110. __attribute__ ((weak))
  111. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  112. return true;
  113. }
  114. void reset_keyboard(void) {
  115. clear_keyboard();
  116. #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_ENABLE_BASIC))
  117. music_all_notes_off();
  118. uint16_t timer_start = timer_read();
  119. PLAY_SONG(goodbye_song);
  120. shutdown_user();
  121. while(timer_elapsed(timer_start) < 250)
  122. wait_ms(1);
  123. stop_all_notes();
  124. #else
  125. wait_ms(250);
  126. #endif
  127. // this is also done later in bootloader.c - not sure if it's neccesary here
  128. #ifdef BOOTLOADER_CATERINA
  129. *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
  130. #endif
  131. bootloader_jump();
  132. }
  133. // Shift / paren setup
  134. #ifndef LSPO_KEY
  135. #define LSPO_KEY KC_9
  136. #endif
  137. #ifndef RSPC_KEY
  138. #define RSPC_KEY KC_0
  139. #endif
  140. static bool shift_interrupted[2] = {0, 0};
  141. static uint16_t scs_timer[2] = {0, 0};
  142. /* true if the last press of GRAVE_ESC was shifted (i.e. GUI or SHIFT were pressed), false otherwise.
  143. * Used to ensure that the correct keycode is released if the key is released.
  144. */
  145. static bool grave_esc_was_shifted = false;
  146. bool process_record_quantum(keyrecord_t *record) {
  147. /* This gets the keycode from the key pressed */
  148. keypos_t key = record->event.key;
  149. uint16_t keycode;
  150. #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
  151. /* TODO: Use store_or_get_action() or a similar function. */
  152. if (!disable_action_cache) {
  153. uint8_t layer;
  154. if (record->event.pressed) {
  155. layer = layer_switch_get_layer(key);
  156. update_source_layers_cache(key, layer);
  157. } else {
  158. layer = read_source_layers_cache(key);
  159. }
  160. keycode = keymap_key_to_keycode(layer, key);
  161. } else
  162. #endif
  163. keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
  164. // This is how you use actions here
  165. // if (keycode == KC_LEAD) {
  166. // action_t action;
  167. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  168. // process_action(record, action);
  169. // return false;
  170. // }
  171. if (!(
  172. #if defined(KEY_LOCK_ENABLE)
  173. // Must run first to be able to mask key_up events.
  174. process_key_lock(&keycode, record) &&
  175. #endif
  176. process_record_kb(keycode, record) &&
  177. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  178. process_midi(keycode, record) &&
  179. #endif
  180. #ifdef AUDIO_ENABLE
  181. process_audio(keycode, record) &&
  182. #endif
  183. #ifdef STENO_ENABLE
  184. process_steno(keycode, record) &&
  185. #endif
  186. #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
  187. process_music(keycode, record) &&
  188. #endif
  189. #ifdef TAP_DANCE_ENABLE
  190. process_tap_dance(keycode, record) &&
  191. #endif
  192. #ifndef DISABLE_LEADER
  193. process_leader(keycode, record) &&
  194. #endif
  195. #ifndef DISABLE_CHORDING
  196. process_chording(keycode, record) &&
  197. #endif
  198. #ifdef COMBO_ENABLE
  199. process_combo(keycode, record) &&
  200. #endif
  201. #ifdef UNICODE_ENABLE
  202. process_unicode(keycode, record) &&
  203. #endif
  204. #ifdef UCIS_ENABLE
  205. process_ucis(keycode, record) &&
  206. #endif
  207. #ifdef PRINTING_ENABLE
  208. process_printer(keycode, record) &&
  209. #endif
  210. #ifdef AUTO_SHIFT_ENABLE
  211. process_auto_shift(keycode, record) &&
  212. #endif
  213. #ifdef UNICODEMAP_ENABLE
  214. process_unicode_map(keycode, record) &&
  215. #endif
  216. #ifdef TERMINAL_ENABLE
  217. process_terminal(keycode, record) &&
  218. #endif
  219. true)) {
  220. return false;
  221. }
  222. // Shift / paren setup
  223. switch(keycode) {
  224. case RESET:
  225. if (record->event.pressed) {
  226. reset_keyboard();
  227. }
  228. return false;
  229. case DEBUG:
  230. if (record->event.pressed) {
  231. debug_enable = true;
  232. print("DEBUG: enabled.\n");
  233. }
  234. return false;
  235. #ifdef FAUXCLICKY_ENABLE
  236. case FC_TOG:
  237. if (record->event.pressed) {
  238. FAUXCLICKY_TOGGLE;
  239. }
  240. return false;
  241. case FC_ON:
  242. if (record->event.pressed) {
  243. FAUXCLICKY_ON;
  244. }
  245. return false;
  246. case FC_OFF:
  247. if (record->event.pressed) {
  248. FAUXCLICKY_OFF;
  249. }
  250. return false;
  251. #endif
  252. #ifdef RGBLIGHT_ENABLE
  253. case RGB_TOG:
  254. if (record->event.pressed) {
  255. rgblight_toggle();
  256. }
  257. return false;
  258. case RGB_MOD:
  259. if (record->event.pressed) {
  260. rgblight_step();
  261. }
  262. return false;
  263. case RGB_SMOD:
  264. // same as RBG_MOD, but if shift is pressed, it will use the reverese direction instead.
  265. if (record->event.pressed) {
  266. uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT));
  267. if(shifted) {
  268. rgblight_step_reverse();
  269. }
  270. else {
  271. rgblight_step();
  272. }
  273. }
  274. return false;
  275. case RGB_HUI:
  276. if (record->event.pressed) {
  277. rgblight_increase_hue();
  278. }
  279. return false;
  280. case RGB_HUD:
  281. if (record->event.pressed) {
  282. rgblight_decrease_hue();
  283. }
  284. return false;
  285. case RGB_SAI:
  286. if (record->event.pressed) {
  287. rgblight_increase_sat();
  288. }
  289. return false;
  290. case RGB_SAD:
  291. if (record->event.pressed) {
  292. rgblight_decrease_sat();
  293. }
  294. return false;
  295. case RGB_VAI:
  296. if (record->event.pressed) {
  297. rgblight_increase_val();
  298. }
  299. return false;
  300. case RGB_VAD:
  301. if (record->event.pressed) {
  302. rgblight_decrease_val();
  303. }
  304. return false;
  305. case RGB_MODE_PLAIN:
  306. if (record->event.pressed) {
  307. rgblight_mode(1);
  308. }
  309. return false;
  310. case RGB_MODE_BREATHE:
  311. if (record->event.pressed) {
  312. if ((2 <= rgblight_get_mode()) && (rgblight_get_mode() < 5)) {
  313. rgblight_step();
  314. } else {
  315. rgblight_mode(2);
  316. }
  317. }
  318. return false;
  319. case RGB_MODE_RAINBOW:
  320. if (record->event.pressed) {
  321. if ((6 <= rgblight_get_mode()) && (rgblight_get_mode() < 8)) {
  322. rgblight_step();
  323. } else {
  324. rgblight_mode(6);
  325. }
  326. }
  327. return false;
  328. case RGB_MODE_SWIRL:
  329. if (record->event.pressed) {
  330. if ((9 <= rgblight_get_mode()) && (rgblight_get_mode() < 14)) {
  331. rgblight_step();
  332. } else {
  333. rgblight_mode(9);
  334. }
  335. }
  336. return false;
  337. case RGB_MODE_SNAKE:
  338. if (record->event.pressed) {
  339. if ((15 <= rgblight_get_mode()) && (rgblight_get_mode() < 20)) {
  340. rgblight_step();
  341. } else {
  342. rgblight_mode(15);
  343. }
  344. }
  345. return false;
  346. case RGB_MODE_KNIGHT:
  347. if (record->event.pressed) {
  348. if ((21 <= rgblight_get_mode()) && (rgblight_get_mode() < 23)) {
  349. rgblight_step();
  350. } else {
  351. rgblight_mode(21);
  352. }
  353. }
  354. return false;
  355. case RGB_MODE_XMAS:
  356. if (record->event.pressed) {
  357. rgblight_mode(24);
  358. }
  359. return false;
  360. case RGB_MODE_GRADIENT:
  361. if (record->event.pressed) {
  362. if ((25 <= rgblight_get_mode()) && (rgblight_get_mode() < 34)) {
  363. rgblight_step();
  364. } else {
  365. rgblight_mode(25);
  366. }
  367. }
  368. return false;
  369. #endif
  370. #ifdef PROTOCOL_LUFA
  371. case OUT_AUTO:
  372. if (record->event.pressed) {
  373. set_output(OUTPUT_AUTO);
  374. }
  375. return false;
  376. case OUT_USB:
  377. if (record->event.pressed) {
  378. set_output(OUTPUT_USB);
  379. }
  380. return false;
  381. #ifdef BLUETOOTH_ENABLE
  382. case OUT_BT:
  383. if (record->event.pressed) {
  384. set_output(OUTPUT_BLUETOOTH);
  385. }
  386. return false;
  387. #endif
  388. #endif
  389. case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
  390. if (record->event.pressed) {
  391. // MAGIC actions (BOOTMAGIC without the boot)
  392. if (!eeconfig_is_enabled()) {
  393. eeconfig_init();
  394. }
  395. /* keymap config */
  396. keymap_config.raw = eeconfig_read_keymap();
  397. switch (keycode)
  398. {
  399. case MAGIC_SWAP_CONTROL_CAPSLOCK:
  400. keymap_config.swap_control_capslock = true;
  401. break;
  402. case MAGIC_CAPSLOCK_TO_CONTROL:
  403. keymap_config.capslock_to_control = true;
  404. break;
  405. case MAGIC_SWAP_LALT_LGUI:
  406. keymap_config.swap_lalt_lgui = true;
  407. break;
  408. case MAGIC_SWAP_RALT_RGUI:
  409. keymap_config.swap_ralt_rgui = true;
  410. break;
  411. case MAGIC_NO_GUI:
  412. keymap_config.no_gui = true;
  413. break;
  414. case MAGIC_SWAP_GRAVE_ESC:
  415. keymap_config.swap_grave_esc = true;
  416. break;
  417. case MAGIC_SWAP_BACKSLASH_BACKSPACE:
  418. keymap_config.swap_backslash_backspace = true;
  419. break;
  420. case MAGIC_HOST_NKRO:
  421. keymap_config.nkro = true;
  422. break;
  423. case MAGIC_SWAP_ALT_GUI:
  424. keymap_config.swap_lalt_lgui = true;
  425. keymap_config.swap_ralt_rgui = true;
  426. #ifdef AUDIO_ENABLE
  427. PLAY_SONG(ag_swap_song);
  428. #endif
  429. break;
  430. case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
  431. keymap_config.swap_control_capslock = false;
  432. break;
  433. case MAGIC_UNCAPSLOCK_TO_CONTROL:
  434. keymap_config.capslock_to_control = false;
  435. break;
  436. case MAGIC_UNSWAP_LALT_LGUI:
  437. keymap_config.swap_lalt_lgui = false;
  438. break;
  439. case MAGIC_UNSWAP_RALT_RGUI:
  440. keymap_config.swap_ralt_rgui = false;
  441. break;
  442. case MAGIC_UNNO_GUI:
  443. keymap_config.no_gui = false;
  444. break;
  445. case MAGIC_UNSWAP_GRAVE_ESC:
  446. keymap_config.swap_grave_esc = false;
  447. break;
  448. case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
  449. keymap_config.swap_backslash_backspace = false;
  450. break;
  451. case MAGIC_UNHOST_NKRO:
  452. keymap_config.nkro = false;
  453. break;
  454. case MAGIC_UNSWAP_ALT_GUI:
  455. keymap_config.swap_lalt_lgui = false;
  456. keymap_config.swap_ralt_rgui = false;
  457. #ifdef AUDIO_ENABLE
  458. PLAY_SONG(ag_norm_song);
  459. #endif
  460. break;
  461. case MAGIC_TOGGLE_NKRO:
  462. keymap_config.nkro = !keymap_config.nkro;
  463. break;
  464. default:
  465. break;
  466. }
  467. eeconfig_update_keymap(keymap_config.raw);
  468. clear_keyboard(); // clear to prevent stuck keys
  469. return false;
  470. }
  471. break;
  472. case KC_LSPO: {
  473. if (record->event.pressed) {
  474. shift_interrupted[0] = false;
  475. scs_timer[0] = timer_read ();
  476. register_mods(MOD_BIT(KC_LSFT));
  477. }
  478. else {
  479. #ifdef DISABLE_SPACE_CADET_ROLLOVER
  480. if (get_mods() & MOD_BIT(KC_RSFT)) {
  481. shift_interrupted[0] = true;
  482. shift_interrupted[1] = true;
  483. }
  484. #endif
  485. if (!shift_interrupted[0] && timer_elapsed(scs_timer[0]) < TAPPING_TERM) {
  486. register_code(LSPO_KEY);
  487. unregister_code(LSPO_KEY);
  488. }
  489. unregister_mods(MOD_BIT(KC_LSFT));
  490. }
  491. return false;
  492. }
  493. case KC_RSPC: {
  494. if (record->event.pressed) {
  495. shift_interrupted[1] = false;
  496. scs_timer[1] = timer_read ();
  497. register_mods(MOD_BIT(KC_RSFT));
  498. }
  499. else {
  500. #ifdef DISABLE_SPACE_CADET_ROLLOVER
  501. if (get_mods() & MOD_BIT(KC_LSFT)) {
  502. shift_interrupted[0] = true;
  503. shift_interrupted[1] = true;
  504. }
  505. #endif
  506. if (!shift_interrupted[1] && timer_elapsed(scs_timer[1]) < TAPPING_TERM) {
  507. register_code(RSPC_KEY);
  508. unregister_code(RSPC_KEY);
  509. }
  510. unregister_mods(MOD_BIT(KC_RSFT));
  511. }
  512. return false;
  513. }
  514. case GRAVE_ESC: {
  515. uint8_t shifted = get_mods() & ((MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)
  516. |MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)));
  517. #ifdef GRAVE_ESC_ALT_OVERRIDE
  518. // if ALT is pressed, ESC is always sent
  519. // this is handy for the cmd+opt+esc shortcut on macOS, among other things.
  520. if (get_mods() & (MOD_BIT(KC_LALT) | MOD_BIT(KC_RALT))) {
  521. shifted = 0;
  522. }
  523. #endif
  524. #ifdef GRAVE_ESC_CTRL_OVERRIDE
  525. // if CTRL is pressed, ESC is always sent
  526. // this is handy for the ctrl+shift+esc shortcut on windows, among other things.
  527. if (get_mods() & (MOD_BIT(KC_LCTL) | MOD_BIT(KC_RCTL))) {
  528. shifted = 0;
  529. }
  530. #endif
  531. #ifdef GRAVE_ESC_GUI_OVERRIDE
  532. // if GUI is pressed, ESC is always sent
  533. if (get_mods() & (MOD_BIT(KC_LGUI) | MOD_BIT(KC_RGUI))) {
  534. shifted = 0;
  535. }
  536. #endif
  537. #ifdef GRAVE_ESC_SHIFT_OVERRIDE
  538. // if SHIFT is pressed, ESC is always sent
  539. if (get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) {
  540. shifted = 0;
  541. }
  542. #endif
  543. if (record->event.pressed) {
  544. grave_esc_was_shifted = shifted;
  545. add_key(shifted ? KC_GRAVE : KC_ESCAPE);
  546. }
  547. else {
  548. del_key(grave_esc_was_shifted ? KC_GRAVE : KC_ESCAPE);
  549. }
  550. send_keyboard_report();
  551. }
  552. default: {
  553. shift_interrupted[0] = true;
  554. shift_interrupted[1] = true;
  555. break;
  556. }
  557. }
  558. return process_action_kb(record);
  559. }
  560. __attribute__ ((weak))
  561. const bool ascii_to_shift_lut[0x80] PROGMEM = {
  562. 0, 0, 0, 0, 0, 0, 0, 0,
  563. 0, 0, 0, 0, 0, 0, 0, 0,
  564. 0, 0, 0, 0, 0, 0, 0, 0,
  565. 0, 0, 0, 0, 0, 0, 0, 0,
  566. 0, 1, 1, 1, 1, 1, 1, 0,
  567. 1, 1, 1, 1, 0, 0, 0, 0,
  568. 0, 0, 0, 0, 0, 0, 0, 0,
  569. 0, 0, 1, 0, 1, 0, 1, 1,
  570. 1, 1, 1, 1, 1, 1, 1, 1,
  571. 1, 1, 1, 1, 1, 1, 1, 1,
  572. 1, 1, 1, 1, 1, 1, 1, 1,
  573. 1, 1, 1, 0, 0, 0, 1, 1,
  574. 0, 0, 0, 0, 0, 0, 0, 0,
  575. 0, 0, 0, 0, 0, 0, 0, 0,
  576. 0, 0, 0, 0, 0, 0, 0, 0,
  577. 0, 0, 0, 1, 1, 1, 1, 0
  578. };
  579. __attribute__ ((weak))
  580. const uint8_t ascii_to_keycode_lut[0x80] PROGMEM = {
  581. 0, 0, 0, 0, 0, 0, 0, 0,
  582. KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
  583. 0, 0, 0, 0, 0, 0, 0, 0,
  584. 0, 0, 0, KC_ESC, 0, 0, 0, 0,
  585. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  586. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  587. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  588. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  589. KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  590. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  591. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  592. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  593. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  594. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  595. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  596. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  597. };
  598. void send_string(const char *str) {
  599. send_string_with_delay(str, 0);
  600. }
  601. void send_string_P(const char *str) {
  602. send_string_with_delay_P(str, 0);
  603. }
  604. void send_string_with_delay(const char *str, uint8_t interval) {
  605. while (1) {
  606. char ascii_code = *str;
  607. if (!ascii_code) break;
  608. if (ascii_code == 1) {
  609. // tap
  610. uint8_t keycode = *(++str);
  611. register_code(keycode);
  612. unregister_code(keycode);
  613. } else if (ascii_code == 2) {
  614. // down
  615. uint8_t keycode = *(++str);
  616. register_code(keycode);
  617. } else if (ascii_code == 3) {
  618. // up
  619. uint8_t keycode = *(++str);
  620. unregister_code(keycode);
  621. } else {
  622. send_char(ascii_code);
  623. }
  624. ++str;
  625. // interval
  626. { uint8_t ms = interval; while (ms--) wait_ms(1); }
  627. }
  628. }
  629. void send_string_with_delay_P(const char *str, uint8_t interval) {
  630. while (1) {
  631. char ascii_code = pgm_read_byte(str);
  632. if (!ascii_code) break;
  633. if (ascii_code == 1) {
  634. // tap
  635. uint8_t keycode = pgm_read_byte(++str);
  636. register_code(keycode);
  637. unregister_code(keycode);
  638. } else if (ascii_code == 2) {
  639. // down
  640. uint8_t keycode = pgm_read_byte(++str);
  641. register_code(keycode);
  642. } else if (ascii_code == 3) {
  643. // up
  644. uint8_t keycode = pgm_read_byte(++str);
  645. unregister_code(keycode);
  646. } else {
  647. send_char(ascii_code);
  648. }
  649. ++str;
  650. // interval
  651. { uint8_t ms = interval; while (ms--) wait_ms(1); }
  652. }
  653. }
  654. void send_char(char ascii_code) {
  655. uint8_t keycode;
  656. keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
  657. if (pgm_read_byte(&ascii_to_shift_lut[(uint8_t)ascii_code])) {
  658. register_code(KC_LSFT);
  659. register_code(keycode);
  660. unregister_code(keycode);
  661. unregister_code(KC_LSFT);
  662. } else {
  663. register_code(keycode);
  664. unregister_code(keycode);
  665. }
  666. }
  667. void set_single_persistent_default_layer(uint8_t default_layer) {
  668. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  669. PLAY_SONG(default_layer_songs[default_layer]);
  670. #endif
  671. eeconfig_update_default_layer(1U<<default_layer);
  672. default_layer_set(1U<<default_layer);
  673. }
  674. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  675. if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
  676. layer_on(layer3);
  677. } else {
  678. layer_off(layer3);
  679. }
  680. }
  681. void tap_random_base64(void) {
  682. #if defined(__AVR_ATmega32U4__)
  683. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  684. #else
  685. uint8_t key = rand() % 64;
  686. #endif
  687. switch (key) {
  688. case 0 ... 25:
  689. register_code(KC_LSFT);
  690. register_code(key + KC_A);
  691. unregister_code(key + KC_A);
  692. unregister_code(KC_LSFT);
  693. break;
  694. case 26 ... 51:
  695. register_code(key - 26 + KC_A);
  696. unregister_code(key - 26 + KC_A);
  697. break;
  698. case 52:
  699. register_code(KC_0);
  700. unregister_code(KC_0);
  701. break;
  702. case 53 ... 61:
  703. register_code(key - 53 + KC_1);
  704. unregister_code(key - 53 + KC_1);
  705. break;
  706. case 62:
  707. register_code(KC_LSFT);
  708. register_code(KC_EQL);
  709. unregister_code(KC_EQL);
  710. unregister_code(KC_LSFT);
  711. break;
  712. case 63:
  713. register_code(KC_SLSH);
  714. unregister_code(KC_SLSH);
  715. break;
  716. }
  717. }
  718. void matrix_init_quantum() {
  719. #ifdef BACKLIGHT_ENABLE
  720. backlight_init_ports();
  721. #endif
  722. #ifdef AUDIO_ENABLE
  723. audio_init();
  724. #endif
  725. matrix_init_kb();
  726. }
  727. void matrix_scan_quantum() {
  728. #ifdef AUDIO_ENABLE
  729. matrix_scan_music();
  730. #endif
  731. #ifdef TAP_DANCE_ENABLE
  732. matrix_scan_tap_dance();
  733. #endif
  734. #ifdef COMBO_ENABLE
  735. matrix_scan_combo();
  736. #endif
  737. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  738. backlight_task();
  739. #endif
  740. matrix_scan_kb();
  741. }
  742. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
  743. static const uint8_t backlight_pin = BACKLIGHT_PIN;
  744. #if BACKLIGHT_PIN == B7
  745. # define COM1x1 COM1C1
  746. # define OCR1x OCR1C
  747. #elif BACKLIGHT_PIN == B6
  748. # define COM1x1 COM1B1
  749. # define OCR1x OCR1B
  750. #elif BACKLIGHT_PIN == B5
  751. # define COM1x1 COM1A1
  752. # define OCR1x OCR1A
  753. #else
  754. # define NO_BACKLIGHT_CLOCK
  755. #endif
  756. #ifndef BACKLIGHT_ON_STATE
  757. #define BACKLIGHT_ON_STATE 0
  758. #endif
  759. __attribute__ ((weak))
  760. void backlight_init_ports(void)
  761. {
  762. // Setup backlight pin as output and output to on state.
  763. // DDRx |= n
  764. _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
  765. #if BACKLIGHT_ON_STATE == 0
  766. // PORTx &= ~n
  767. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  768. #else
  769. // PORTx |= n
  770. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  771. #endif
  772. #ifndef NO_BACKLIGHT_CLOCK
  773. // Use full 16-bit resolution.
  774. ICR1 = 0xFFFF;
  775. // I could write a wall of text here to explain... but TL;DW
  776. // Go read the ATmega32u4 datasheet.
  777. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  778. // Pin PB7 = OCR1C (Timer 1, Channel C)
  779. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  780. // (i.e. start high, go low when counter matches.)
  781. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  782. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  783. TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
  784. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  785. #endif
  786. backlight_init();
  787. #ifdef BACKLIGHT_BREATHING
  788. breathing_defaults();
  789. #endif
  790. }
  791. __attribute__ ((weak))
  792. void backlight_set(uint8_t level)
  793. {
  794. // Prevent backlight blink on lowest level
  795. // #if BACKLIGHT_ON_STATE == 0
  796. // // PORTx &= ~n
  797. // _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  798. // #else
  799. // // PORTx |= n
  800. // _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  801. // #endif
  802. if ( level == 0 ) {
  803. #ifndef NO_BACKLIGHT_CLOCK
  804. // Turn off PWM control on backlight pin, revert to output low.
  805. TCCR1A &= ~(_BV(COM1x1));
  806. OCR1x = 0x0;
  807. #else
  808. // #if BACKLIGHT_ON_STATE == 0
  809. // // PORTx |= n
  810. // _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  811. // #else
  812. // // PORTx &= ~n
  813. // _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  814. // #endif
  815. #endif
  816. }
  817. #ifndef NO_BACKLIGHT_CLOCK
  818. else if ( level == BACKLIGHT_LEVELS ) {
  819. // Turn on PWM control of backlight pin
  820. TCCR1A |= _BV(COM1x1);
  821. // Set the brightness
  822. OCR1x = 0xFFFF;
  823. }
  824. else {
  825. // Turn on PWM control of backlight pin
  826. TCCR1A |= _BV(COM1x1);
  827. // Set the brightness
  828. OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
  829. }
  830. #endif
  831. #ifdef BACKLIGHT_BREATHING
  832. breathing_intensity_default();
  833. #endif
  834. }
  835. uint8_t backlight_tick = 0;
  836. void backlight_task(void) {
  837. #ifdef NO_BACKLIGHT_CLOCK
  838. if ((0xFFFF >> ((BACKLIGHT_LEVELS - backlight_config.level) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
  839. #if BACKLIGHT_ON_STATE == 0
  840. // PORTx &= ~n
  841. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  842. #else
  843. // PORTx |= n
  844. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  845. #endif
  846. } else {
  847. #if BACKLIGHT_ON_STATE == 0
  848. // PORTx |= n
  849. _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
  850. #else
  851. // PORTx &= ~n
  852. _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
  853. #endif
  854. }
  855. backlight_tick = (backlight_tick + 1) % 16;
  856. #endif
  857. }
  858. #ifdef BACKLIGHT_BREATHING
  859. #define BREATHING_NO_HALT 0
  860. #define BREATHING_HALT_OFF 1
  861. #define BREATHING_HALT_ON 2
  862. static uint8_t breath_intensity;
  863. static uint8_t breath_speed;
  864. static uint16_t breathing_index;
  865. static uint8_t breathing_halt;
  866. void breathing_enable(void)
  867. {
  868. if (get_backlight_level() == 0)
  869. {
  870. breathing_index = 0;
  871. }
  872. else
  873. {
  874. // Set breathing_index to be at the midpoint (brightest point)
  875. breathing_index = 0x20 << breath_speed;
  876. }
  877. breathing_halt = BREATHING_NO_HALT;
  878. // Enable breathing interrupt
  879. TIMSK1 |= _BV(OCIE1A);
  880. }
  881. void breathing_pulse(void)
  882. {
  883. if (get_backlight_level() == 0)
  884. {
  885. breathing_index = 0;
  886. }
  887. else
  888. {
  889. // Set breathing_index to be at the midpoint + 1 (brightest point)
  890. breathing_index = 0x21 << breath_speed;
  891. }
  892. breathing_halt = BREATHING_HALT_ON;
  893. // Enable breathing interrupt
  894. TIMSK1 |= _BV(OCIE1A);
  895. }
  896. void breathing_disable(void)
  897. {
  898. // Disable breathing interrupt
  899. TIMSK1 &= ~_BV(OCIE1A);
  900. backlight_set(get_backlight_level());
  901. }
  902. void breathing_self_disable(void)
  903. {
  904. if (get_backlight_level() == 0)
  905. {
  906. breathing_halt = BREATHING_HALT_OFF;
  907. }
  908. else
  909. {
  910. breathing_halt = BREATHING_HALT_ON;
  911. }
  912. //backlight_set(get_backlight_level());
  913. }
  914. void breathing_toggle(void)
  915. {
  916. if (!is_breathing())
  917. {
  918. if (get_backlight_level() == 0)
  919. {
  920. breathing_index = 0;
  921. }
  922. else
  923. {
  924. // Set breathing_index to be at the midpoint + 1 (brightest point)
  925. breathing_index = 0x21 << breath_speed;
  926. }
  927. breathing_halt = BREATHING_NO_HALT;
  928. }
  929. // Toggle breathing interrupt
  930. TIMSK1 ^= _BV(OCIE1A);
  931. // Restore backlight level
  932. if (!is_breathing())
  933. {
  934. backlight_set(get_backlight_level());
  935. }
  936. }
  937. bool is_breathing(void)
  938. {
  939. return (TIMSK1 && _BV(OCIE1A));
  940. }
  941. void breathing_intensity_default(void)
  942. {
  943. //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
  944. breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
  945. }
  946. void breathing_intensity_set(uint8_t value)
  947. {
  948. breath_intensity = value;
  949. }
  950. void breathing_speed_default(void)
  951. {
  952. breath_speed = 4;
  953. }
  954. void breathing_speed_set(uint8_t value)
  955. {
  956. bool is_breathing_now = is_breathing();
  957. uint8_t old_breath_speed = breath_speed;
  958. if (is_breathing_now)
  959. {
  960. // Disable breathing interrupt
  961. TIMSK1 &= ~_BV(OCIE1A);
  962. }
  963. breath_speed = value;
  964. if (is_breathing_now)
  965. {
  966. // Adjust index to account for new speed
  967. breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
  968. // Enable breathing interrupt
  969. TIMSK1 |= _BV(OCIE1A);
  970. }
  971. }
  972. void breathing_speed_inc(uint8_t value)
  973. {
  974. if ((uint16_t)(breath_speed - value) > 10 )
  975. {
  976. breathing_speed_set(0);
  977. }
  978. else
  979. {
  980. breathing_speed_set(breath_speed - value);
  981. }
  982. }
  983. void breathing_speed_dec(uint8_t value)
  984. {
  985. if ((uint16_t)(breath_speed + value) > 10 )
  986. {
  987. breathing_speed_set(10);
  988. }
  989. else
  990. {
  991. breathing_speed_set(breath_speed + value);
  992. }
  993. }
  994. void breathing_defaults(void)
  995. {
  996. breathing_intensity_default();
  997. breathing_speed_default();
  998. breathing_halt = BREATHING_NO_HALT;
  999. }
  1000. /* Breathing Sleep LED brighness(PWM On period) table
  1001. * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
  1002. *
  1003. * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
  1004. * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
  1005. */
  1006. static const uint8_t breathing_table[64] PROGMEM = {
  1007. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
  1008. 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
  1009. 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
  1010. 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1011. };
  1012. ISR(TIMER1_COMPA_vect)
  1013. {
  1014. // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
  1015. uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
  1016. if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
  1017. {
  1018. // Disable breathing interrupt
  1019. TIMSK1 &= ~_BV(OCIE1A);
  1020. }
  1021. OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
  1022. }
  1023. #endif // breathing
  1024. #else // backlight
  1025. __attribute__ ((weak))
  1026. void backlight_init_ports(void)
  1027. {
  1028. }
  1029. __attribute__ ((weak))
  1030. void backlight_set(uint8_t level)
  1031. {
  1032. }
  1033. #endif // backlight
  1034. // Functions for spitting out values
  1035. //
  1036. void send_dword(uint32_t number) { // this might not actually work
  1037. uint16_t word = (number >> 16);
  1038. send_word(word);
  1039. send_word(number & 0xFFFFUL);
  1040. }
  1041. void send_word(uint16_t number) {
  1042. uint8_t byte = number >> 8;
  1043. send_byte(byte);
  1044. send_byte(number & 0xFF);
  1045. }
  1046. void send_byte(uint8_t number) {
  1047. uint8_t nibble = number >> 4;
  1048. send_nibble(nibble);
  1049. send_nibble(number & 0xF);
  1050. }
  1051. void send_nibble(uint8_t number) {
  1052. switch (number) {
  1053. case 0:
  1054. register_code(KC_0);
  1055. unregister_code(KC_0);
  1056. break;
  1057. case 1 ... 9:
  1058. register_code(KC_1 + (number - 1));
  1059. unregister_code(KC_1 + (number - 1));
  1060. break;
  1061. case 0xA ... 0xF:
  1062. register_code(KC_A + (number - 0xA));
  1063. unregister_code(KC_A + (number - 0xA));
  1064. break;
  1065. }
  1066. }
  1067. __attribute__((weak))
  1068. uint16_t hex_to_keycode(uint8_t hex)
  1069. {
  1070. hex = hex & 0xF;
  1071. if (hex == 0x0) {
  1072. return KC_0;
  1073. } else if (hex < 0xA) {
  1074. return KC_1 + (hex - 0x1);
  1075. } else {
  1076. return KC_A + (hex - 0xA);
  1077. }
  1078. }
  1079. void api_send_unicode(uint32_t unicode) {
  1080. #ifdef API_ENABLE
  1081. uint8_t chunk[4];
  1082. dword_to_bytes(unicode, chunk);
  1083. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  1084. #endif
  1085. }
  1086. __attribute__ ((weak))
  1087. void led_set_user(uint8_t usb_led) {
  1088. }
  1089. __attribute__ ((weak))
  1090. void led_set_kb(uint8_t usb_led) {
  1091. led_set_user(usb_led);
  1092. }
  1093. __attribute__ ((weak))
  1094. void led_init_ports(void)
  1095. {
  1096. }
  1097. __attribute__ ((weak))
  1098. void led_set(uint8_t usb_led)
  1099. {
  1100. // Example LED Code
  1101. //
  1102. // // Using PE6 Caps Lock LED
  1103. // if (usb_led & (1<<USB_LED_CAPS_LOCK))
  1104. // {
  1105. // // Output high.
  1106. // DDRE |= (1<<6);
  1107. // PORTE |= (1<<6);
  1108. // }
  1109. // else
  1110. // {
  1111. // // Output low.
  1112. // DDRE &= ~(1<<6);
  1113. // PORTE &= ~(1<<6);
  1114. // }
  1115. led_set_kb(usb_led);
  1116. }
  1117. //------------------------------------------------------------------------------
  1118. // Override these functions in your keymap file to play different tunes on
  1119. // different events such as startup and bootloader jump
  1120. __attribute__ ((weak))
  1121. void startup_user() {}
  1122. __attribute__ ((weak))
  1123. void shutdown_user() {}
  1124. //------------------------------------------------------------------------------