quantum.c 36 KB

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