quantum.c 33 KB

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