quantum.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  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. #if !defined(RGBLIGHT_ENABLE) && !defined(RGB_MATRIX_ENABLE)
  18. #include "rgb.h"
  19. #endif
  20. #ifdef PROTOCOL_LUFA
  21. #include "outputselect.h"
  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 VELOCIKEY_ENABLE
  38. #include "velocikey.h"
  39. #endif
  40. #ifdef HAPTIC_ENABLE
  41. #include "haptic.h"
  42. #endif
  43. #ifdef ENCODER_ENABLE
  44. #include "encoder.h"
  45. #endif
  46. #ifdef AUDIO_ENABLE
  47. #ifndef GOODBYE_SONG
  48. #define GOODBYE_SONG SONG(GOODBYE_SOUND)
  49. #endif
  50. #ifndef AG_NORM_SONG
  51. #define AG_NORM_SONG SONG(AG_NORM_SOUND)
  52. #endif
  53. #ifndef AG_SWAP_SONG
  54. #define AG_SWAP_SONG SONG(AG_SWAP_SOUND)
  55. #endif
  56. float goodbye_song[][2] = GOODBYE_SONG;
  57. float ag_norm_song[][2] = AG_NORM_SONG;
  58. float ag_swap_song[][2] = AG_SWAP_SONG;
  59. #ifdef DEFAULT_LAYER_SONGS
  60. float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
  61. #endif
  62. #endif
  63. static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
  64. switch (code) {
  65. case QK_MODS ... QK_MODS_MAX:
  66. break;
  67. default:
  68. return;
  69. }
  70. if (code & QK_LCTL)
  71. f(KC_LCTL);
  72. if (code & QK_LSFT)
  73. f(KC_LSFT);
  74. if (code & QK_LALT)
  75. f(KC_LALT);
  76. if (code & QK_LGUI)
  77. f(KC_LGUI);
  78. if (code < QK_RMODS_MIN) return;
  79. if (code & QK_RCTL)
  80. f(KC_RCTL);
  81. if (code & QK_RSFT)
  82. f(KC_RSFT);
  83. if (code & QK_RALT)
  84. f(KC_RALT);
  85. if (code & QK_RGUI)
  86. f(KC_RGUI);
  87. }
  88. static inline void qk_register_weak_mods(uint8_t kc) {
  89. add_weak_mods(MOD_BIT(kc));
  90. send_keyboard_report();
  91. }
  92. static inline void qk_unregister_weak_mods(uint8_t kc) {
  93. del_weak_mods(MOD_BIT(kc));
  94. send_keyboard_report();
  95. }
  96. static inline void qk_register_mods(uint8_t kc) {
  97. add_weak_mods(MOD_BIT(kc));
  98. send_keyboard_report();
  99. }
  100. static inline void qk_unregister_mods(uint8_t kc) {
  101. del_weak_mods(MOD_BIT(kc));
  102. send_keyboard_report();
  103. }
  104. void register_code16 (uint16_t code) {
  105. if (IS_MOD(code) || code == KC_NO) {
  106. do_code16 (code, qk_register_mods);
  107. } else {
  108. do_code16 (code, qk_register_weak_mods);
  109. }
  110. register_code (code);
  111. }
  112. void unregister_code16 (uint16_t code) {
  113. unregister_code (code);
  114. if (IS_MOD(code) || code == KC_NO) {
  115. do_code16 (code, qk_unregister_mods);
  116. } else {
  117. do_code16 (code, qk_unregister_weak_mods);
  118. }
  119. }
  120. void tap_code16(uint16_t code) {
  121. register_code16(code);
  122. #if TAP_CODE_DELAY > 0
  123. wait_ms(TAP_CODE_DELAY);
  124. #endif
  125. unregister_code16(code);
  126. }
  127. __attribute__ ((weak))
  128. bool process_action_kb(keyrecord_t *record) {
  129. return true;
  130. }
  131. __attribute__ ((weak))
  132. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  133. return process_record_user(keycode, record);
  134. }
  135. __attribute__ ((weak))
  136. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  137. return true;
  138. }
  139. void reset_keyboard(void) {
  140. clear_keyboard();
  141. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  142. process_midi_all_notes_off();
  143. #endif
  144. #ifdef AUDIO_ENABLE
  145. #ifndef NO_MUSIC_MODE
  146. music_all_notes_off();
  147. #endif
  148. uint16_t timer_start = timer_read();
  149. PLAY_SONG(goodbye_song);
  150. shutdown_user();
  151. while(timer_elapsed(timer_start) < 250)
  152. wait_ms(1);
  153. stop_all_notes();
  154. #else
  155. shutdown_user();
  156. wait_ms(250);
  157. #endif
  158. #ifdef HAPTIC_ENABLE
  159. haptic_shutdown();
  160. #endif
  161. // this is also done later in bootloader.c - not sure if it's neccesary here
  162. #ifdef BOOTLOADER_CATERINA
  163. *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
  164. #endif
  165. bootloader_jump();
  166. }
  167. /* true if the last press of GRAVE_ESC was shifted (i.e. GUI or SHIFT were pressed), false otherwise.
  168. * Used to ensure that the correct keycode is released if the key is released.
  169. */
  170. static bool grave_esc_was_shifted = false;
  171. /* Convert record into usable keycode via the contained event. */
  172. uint16_t get_record_keycode(keyrecord_t *record) {
  173. return get_event_keycode(record->event);
  174. }
  175. /* Convert event into usable keycode. Checks the layer cache to ensure that it
  176. * retains the correct keycode after a layer change, if the key is still pressed.
  177. */
  178. uint16_t get_event_keycode(keyevent_t event) {
  179. #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
  180. /* TODO: Use store_or_get_action() or a similar function. */
  181. if (!disable_action_cache) {
  182. uint8_t layer;
  183. if (event.pressed) {
  184. layer = layer_switch_get_layer(event.key);
  185. update_source_layers_cache(event.key, layer);
  186. } else {
  187. layer = read_source_layers_cache(event.key);
  188. }
  189. return keymap_key_to_keycode(layer, event.key);
  190. } else
  191. #endif
  192. return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key);
  193. }
  194. /* Main keycode processing function. Hands off handling to other functions,
  195. * then processes internal Quantum keycodes, then processes ACTIONs.
  196. */
  197. bool process_record_quantum(keyrecord_t *record) {
  198. uint16_t keycode = get_record_keycode(record);
  199. // This is how you use actions here
  200. // if (keycode == KC_LEAD) {
  201. // action_t action;
  202. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  203. // process_action(record, action);
  204. // return false;
  205. // }
  206. #ifdef VELOCIKEY_ENABLE
  207. if (velocikey_enabled() && record->event.pressed) { velocikey_accelerate(); }
  208. #endif
  209. #ifdef TAP_DANCE_ENABLE
  210. preprocess_tap_dance(keycode, record);
  211. #endif
  212. if (!(
  213. #if defined(KEY_LOCK_ENABLE)
  214. // Must run first to be able to mask key_up events.
  215. process_key_lock(&keycode, record) &&
  216. #endif
  217. #if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
  218. process_clicky(keycode, record) &&
  219. #endif //AUDIO_CLICKY
  220. #ifdef HAPTIC_ENABLE
  221. process_haptic(keycode, record) &&
  222. #endif //HAPTIC_ENABLE
  223. #if defined(RGB_MATRIX_ENABLE)
  224. process_rgb_matrix(keycode, record) &&
  225. #endif
  226. process_record_kb(keycode, record) &&
  227. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  228. process_midi(keycode, record) &&
  229. #endif
  230. #ifdef AUDIO_ENABLE
  231. process_audio(keycode, record) &&
  232. #endif
  233. #ifdef STENO_ENABLE
  234. process_steno(keycode, record) &&
  235. #endif
  236. #if (defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)
  237. process_music(keycode, record) &&
  238. #endif
  239. #ifdef TAP_DANCE_ENABLE
  240. process_tap_dance(keycode, record) &&
  241. #endif
  242. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  243. process_unicode_common(keycode, record) &&
  244. #endif
  245. #ifdef LEADER_ENABLE
  246. process_leader(keycode, record) &&
  247. #endif
  248. #ifdef COMBO_ENABLE
  249. process_combo(keycode, record) &&
  250. #endif
  251. #ifdef PRINTING_ENABLE
  252. process_printer(keycode, record) &&
  253. #endif
  254. #ifdef AUTO_SHIFT_ENABLE
  255. process_auto_shift(keycode, record) &&
  256. #endif
  257. #ifdef TERMINAL_ENABLE
  258. process_terminal(keycode, record) &&
  259. #endif
  260. #ifdef SPACE_CADET_ENABLE
  261. process_space_cadet(keycode, record) &&
  262. #endif
  263. true)) {
  264. return false;
  265. }
  266. // Shift / paren setup
  267. switch(keycode) {
  268. case RESET:
  269. if (record->event.pressed) {
  270. reset_keyboard();
  271. }
  272. return false;
  273. case DEBUG:
  274. if (record->event.pressed) {
  275. debug_enable ^= 1;
  276. if (debug_enable) {
  277. print("DEBUG: enabled.\n");
  278. } else {
  279. print("DEBUG: disabled.\n");
  280. }
  281. }
  282. return false;
  283. case EEPROM_RESET:
  284. if (record->event.pressed) {
  285. eeconfig_init();
  286. }
  287. return false;
  288. #ifdef FAUXCLICKY_ENABLE
  289. case FC_TOG:
  290. if (record->event.pressed) {
  291. FAUXCLICKY_TOGGLE;
  292. }
  293. return false;
  294. case FC_ON:
  295. if (record->event.pressed) {
  296. FAUXCLICKY_ON;
  297. }
  298. return false;
  299. case FC_OFF:
  300. if (record->event.pressed) {
  301. FAUXCLICKY_OFF;
  302. }
  303. return false;
  304. #endif
  305. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  306. case RGB_TOG:
  307. // Split keyboards need to trigger on key-up for edge-case issue
  308. #ifndef SPLIT_KEYBOARD
  309. if (record->event.pressed) {
  310. #else
  311. if (!record->event.pressed) {
  312. #endif
  313. rgblight_toggle();
  314. }
  315. return false;
  316. case RGB_MODE_FORWARD:
  317. if (record->event.pressed) {
  318. uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT));
  319. if(shifted) {
  320. rgblight_step_reverse();
  321. }
  322. else {
  323. rgblight_step();
  324. }
  325. }
  326. return false;
  327. case RGB_MODE_REVERSE:
  328. if (record->event.pressed) {
  329. uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT));
  330. if(shifted) {
  331. rgblight_step();
  332. }
  333. else {
  334. rgblight_step_reverse();
  335. }
  336. }
  337. return false;
  338. case RGB_HUI:
  339. // Split keyboards need to trigger on key-up for edge-case issue
  340. #ifndef SPLIT_KEYBOARD
  341. if (record->event.pressed) {
  342. #else
  343. if (!record->event.pressed) {
  344. #endif
  345. rgblight_increase_hue();
  346. }
  347. return false;
  348. case RGB_HUD:
  349. // Split keyboards need to trigger on key-up for edge-case issue
  350. #ifndef SPLIT_KEYBOARD
  351. if (record->event.pressed) {
  352. #else
  353. if (!record->event.pressed) {
  354. #endif
  355. rgblight_decrease_hue();
  356. }
  357. return false;
  358. case RGB_SAI:
  359. // Split keyboards need to trigger on key-up for edge-case issue
  360. #ifndef SPLIT_KEYBOARD
  361. if (record->event.pressed) {
  362. #else
  363. if (!record->event.pressed) {
  364. #endif
  365. rgblight_increase_sat();
  366. }
  367. return false;
  368. case RGB_SAD:
  369. // Split keyboards need to trigger on key-up for edge-case issue
  370. #ifndef SPLIT_KEYBOARD
  371. if (record->event.pressed) {
  372. #else
  373. if (!record->event.pressed) {
  374. #endif
  375. rgblight_decrease_sat();
  376. }
  377. return false;
  378. case RGB_VAI:
  379. // Split keyboards need to trigger on key-up for edge-case issue
  380. #ifndef SPLIT_KEYBOARD
  381. if (record->event.pressed) {
  382. #else
  383. if (!record->event.pressed) {
  384. #endif
  385. rgblight_increase_val();
  386. }
  387. return false;
  388. case RGB_VAD:
  389. // Split keyboards need to trigger on key-up for edge-case issue
  390. #ifndef SPLIT_KEYBOARD
  391. if (record->event.pressed) {
  392. #else
  393. if (!record->event.pressed) {
  394. #endif
  395. rgblight_decrease_val();
  396. }
  397. return false;
  398. case RGB_SPI:
  399. if (record->event.pressed) {
  400. rgblight_increase_speed();
  401. }
  402. return false;
  403. case RGB_SPD:
  404. if (record->event.pressed) {
  405. rgblight_decrease_speed();
  406. }
  407. return false;
  408. case RGB_MODE_PLAIN:
  409. if (record->event.pressed) {
  410. rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
  411. }
  412. return false;
  413. case RGB_MODE_BREATHE:
  414. #ifdef RGBLIGHT_EFFECT_BREATHING
  415. if (record->event.pressed) {
  416. if ((RGBLIGHT_MODE_BREATHING <= rgblight_get_mode()) &&
  417. (rgblight_get_mode() < RGBLIGHT_MODE_BREATHING_end)) {
  418. rgblight_step();
  419. } else {
  420. rgblight_mode(RGBLIGHT_MODE_BREATHING);
  421. }
  422. }
  423. #endif
  424. return false;
  425. case RGB_MODE_RAINBOW:
  426. #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
  427. if (record->event.pressed) {
  428. if ((RGBLIGHT_MODE_RAINBOW_MOOD <= rgblight_get_mode()) &&
  429. (rgblight_get_mode() < RGBLIGHT_MODE_RAINBOW_MOOD_end)) {
  430. rgblight_step();
  431. } else {
  432. rgblight_mode(RGBLIGHT_MODE_RAINBOW_MOOD);
  433. }
  434. }
  435. #endif
  436. return false;
  437. case RGB_MODE_SWIRL:
  438. #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
  439. if (record->event.pressed) {
  440. if ((RGBLIGHT_MODE_RAINBOW_SWIRL <= rgblight_get_mode()) &&
  441. (rgblight_get_mode() < RGBLIGHT_MODE_RAINBOW_SWIRL_end)) {
  442. rgblight_step();
  443. } else {
  444. rgblight_mode(RGBLIGHT_MODE_RAINBOW_SWIRL);
  445. }
  446. }
  447. #endif
  448. return false;
  449. case RGB_MODE_SNAKE:
  450. #ifdef RGBLIGHT_EFFECT_SNAKE
  451. if (record->event.pressed) {
  452. if ((RGBLIGHT_MODE_SNAKE <= rgblight_get_mode()) &&
  453. (rgblight_get_mode() < RGBLIGHT_MODE_SNAKE_end)) {
  454. rgblight_step();
  455. } else {
  456. rgblight_mode(RGBLIGHT_MODE_SNAKE);
  457. }
  458. }
  459. #endif
  460. return false;
  461. case RGB_MODE_KNIGHT:
  462. #ifdef RGBLIGHT_EFFECT_KNIGHT
  463. if (record->event.pressed) {
  464. if ((RGBLIGHT_MODE_KNIGHT <= rgblight_get_mode()) &&
  465. (rgblight_get_mode() < RGBLIGHT_MODE_KNIGHT_end)) {
  466. rgblight_step();
  467. } else {
  468. rgblight_mode(RGBLIGHT_MODE_KNIGHT);
  469. }
  470. }
  471. #endif
  472. return false;
  473. case RGB_MODE_XMAS:
  474. #ifdef RGBLIGHT_EFFECT_CHRISTMAS
  475. if (record->event.pressed) {
  476. rgblight_mode(RGBLIGHT_MODE_CHRISTMAS);
  477. }
  478. #endif
  479. return false;
  480. case RGB_MODE_GRADIENT:
  481. #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
  482. if (record->event.pressed) {
  483. if ((RGBLIGHT_MODE_STATIC_GRADIENT <= rgblight_get_mode()) &&
  484. (rgblight_get_mode() < RGBLIGHT_MODE_STATIC_GRADIENT_end)) {
  485. rgblight_step();
  486. } else {
  487. rgblight_mode(RGBLIGHT_MODE_STATIC_GRADIENT);
  488. }
  489. }
  490. #endif
  491. return false;
  492. case RGB_MODE_RGBTEST:
  493. #ifdef RGBLIGHT_EFFECT_RGB_TEST
  494. if (record->event.pressed) {
  495. rgblight_mode(RGBLIGHT_MODE_RGB_TEST);
  496. }
  497. #endif
  498. return false;
  499. #endif // defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  500. #ifdef VELOCIKEY_ENABLE
  501. case VLK_TOG:
  502. if (record->event.pressed) {
  503. velocikey_toggle();
  504. }
  505. return false;
  506. #endif
  507. #ifdef PROTOCOL_LUFA
  508. case OUT_AUTO:
  509. if (record->event.pressed) {
  510. set_output(OUTPUT_AUTO);
  511. }
  512. return false;
  513. case OUT_USB:
  514. if (record->event.pressed) {
  515. set_output(OUTPUT_USB);
  516. }
  517. return false;
  518. #ifdef BLUETOOTH_ENABLE
  519. case OUT_BT:
  520. if (record->event.pressed) {
  521. set_output(OUTPUT_BLUETOOTH);
  522. }
  523. return false;
  524. #endif
  525. #endif
  526. case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
  527. if (record->event.pressed) {
  528. // MAGIC actions (BOOTMAGIC without the boot)
  529. if (!eeconfig_is_enabled()) {
  530. eeconfig_init();
  531. }
  532. /* keymap config */
  533. keymap_config.raw = eeconfig_read_keymap();
  534. switch (keycode)
  535. {
  536. case MAGIC_SWAP_CONTROL_CAPSLOCK:
  537. keymap_config.swap_control_capslock = true;
  538. break;
  539. case MAGIC_CAPSLOCK_TO_CONTROL:
  540. keymap_config.capslock_to_control = true;
  541. break;
  542. case MAGIC_SWAP_LALT_LGUI:
  543. keymap_config.swap_lalt_lgui = true;
  544. break;
  545. case MAGIC_SWAP_RALT_RGUI:
  546. keymap_config.swap_ralt_rgui = true;
  547. break;
  548. case MAGIC_NO_GUI:
  549. keymap_config.no_gui = true;
  550. break;
  551. case MAGIC_SWAP_GRAVE_ESC:
  552. keymap_config.swap_grave_esc = true;
  553. break;
  554. case MAGIC_SWAP_BACKSLASH_BACKSPACE:
  555. keymap_config.swap_backslash_backspace = true;
  556. break;
  557. case MAGIC_HOST_NKRO:
  558. keymap_config.nkro = true;
  559. break;
  560. case MAGIC_SWAP_ALT_GUI:
  561. keymap_config.swap_lalt_lgui = true;
  562. keymap_config.swap_ralt_rgui = true;
  563. #ifdef AUDIO_ENABLE
  564. PLAY_SONG(ag_swap_song);
  565. #endif
  566. break;
  567. case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
  568. keymap_config.swap_control_capslock = false;
  569. break;
  570. case MAGIC_UNCAPSLOCK_TO_CONTROL:
  571. keymap_config.capslock_to_control = false;
  572. break;
  573. case MAGIC_UNSWAP_LALT_LGUI:
  574. keymap_config.swap_lalt_lgui = false;
  575. break;
  576. case MAGIC_UNSWAP_RALT_RGUI:
  577. keymap_config.swap_ralt_rgui = false;
  578. break;
  579. case MAGIC_UNNO_GUI:
  580. keymap_config.no_gui = false;
  581. break;
  582. case MAGIC_UNSWAP_GRAVE_ESC:
  583. keymap_config.swap_grave_esc = false;
  584. break;
  585. case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
  586. keymap_config.swap_backslash_backspace = false;
  587. break;
  588. case MAGIC_UNHOST_NKRO:
  589. keymap_config.nkro = false;
  590. break;
  591. case MAGIC_UNSWAP_ALT_GUI:
  592. keymap_config.swap_lalt_lgui = false;
  593. keymap_config.swap_ralt_rgui = false;
  594. #ifdef AUDIO_ENABLE
  595. PLAY_SONG(ag_norm_song);
  596. #endif
  597. break;
  598. case MAGIC_TOGGLE_ALT_GUI:
  599. keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
  600. keymap_config.swap_ralt_rgui = !keymap_config.swap_ralt_rgui;
  601. #ifdef AUDIO_ENABLE
  602. if (keymap_config.swap_ralt_rgui) {
  603. PLAY_SONG(ag_swap_song);
  604. } else {
  605. PLAY_SONG(ag_norm_song);
  606. }
  607. #endif
  608. break;
  609. case MAGIC_TOGGLE_NKRO:
  610. keymap_config.nkro = !keymap_config.nkro;
  611. break;
  612. default:
  613. break;
  614. }
  615. eeconfig_update_keymap(keymap_config.raw);
  616. clear_keyboard(); // clear to prevent stuck keys
  617. return false;
  618. }
  619. break;
  620. case GRAVE_ESC: {
  621. uint8_t shifted = get_mods() & ((MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)
  622. |MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)));
  623. #ifdef GRAVE_ESC_ALT_OVERRIDE
  624. // if ALT is pressed, ESC is always sent
  625. // this is handy for the cmd+opt+esc shortcut on macOS, among other things.
  626. if (get_mods() & (MOD_BIT(KC_LALT) | MOD_BIT(KC_RALT))) {
  627. shifted = 0;
  628. }
  629. #endif
  630. #ifdef GRAVE_ESC_CTRL_OVERRIDE
  631. // if CTRL is pressed, ESC is always sent
  632. // this is handy for the ctrl+shift+esc shortcut on windows, among other things.
  633. if (get_mods() & (MOD_BIT(KC_LCTL) | MOD_BIT(KC_RCTL))) {
  634. shifted = 0;
  635. }
  636. #endif
  637. #ifdef GRAVE_ESC_GUI_OVERRIDE
  638. // if GUI is pressed, ESC is always sent
  639. if (get_mods() & (MOD_BIT(KC_LGUI) | MOD_BIT(KC_RGUI))) {
  640. shifted = 0;
  641. }
  642. #endif
  643. #ifdef GRAVE_ESC_SHIFT_OVERRIDE
  644. // if SHIFT is pressed, ESC is always sent
  645. if (get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) {
  646. shifted = 0;
  647. }
  648. #endif
  649. if (record->event.pressed) {
  650. grave_esc_was_shifted = shifted;
  651. add_key(shifted ? KC_GRAVE : KC_ESCAPE);
  652. }
  653. else {
  654. del_key(grave_esc_was_shifted ? KC_GRAVE : KC_ESCAPE);
  655. }
  656. send_keyboard_report();
  657. return false;
  658. }
  659. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_BREATHING)
  660. case BL_BRTG: {
  661. if (record->event.pressed)
  662. breathing_toggle();
  663. return false;
  664. }
  665. #endif
  666. }
  667. return process_action_kb(record);
  668. }
  669. __attribute__ ((weak))
  670. const bool ascii_to_shift_lut[0x80] PROGMEM = {
  671. 0, 0, 0, 0, 0, 0, 0, 0,
  672. 0, 0, 0, 0, 0, 0, 0, 0,
  673. 0, 0, 0, 0, 0, 0, 0, 0,
  674. 0, 0, 0, 0, 0, 0, 0, 0,
  675. 0, 1, 1, 1, 1, 1, 1, 0,
  676. 1, 1, 1, 1, 0, 0, 0, 0,
  677. 0, 0, 0, 0, 0, 0, 0, 0,
  678. 0, 0, 1, 0, 1, 0, 1, 1,
  679. 1, 1, 1, 1, 1, 1, 1, 1,
  680. 1, 1, 1, 1, 1, 1, 1, 1,
  681. 1, 1, 1, 1, 1, 1, 1, 1,
  682. 1, 1, 1, 0, 0, 0, 1, 1,
  683. 0, 0, 0, 0, 0, 0, 0, 0,
  684. 0, 0, 0, 0, 0, 0, 0, 0,
  685. 0, 0, 0, 0, 0, 0, 0, 0,
  686. 0, 0, 0, 1, 1, 1, 1, 0
  687. };
  688. __attribute__ ((weak))
  689. const bool ascii_to_altgr_lut[0x80] PROGMEM = {
  690. 0, 0, 0, 0, 0, 0, 0, 0,
  691. 0, 0, 0, 0, 0, 0, 0, 0,
  692. 0, 0, 0, 0, 0, 0, 0, 0,
  693. 0, 0, 0, 0, 0, 0, 0, 0,
  694. 0, 0, 0, 0, 0, 0, 0, 0,
  695. 0, 0, 0, 0, 0, 0, 0, 0,
  696. 0, 0, 0, 0, 0, 0, 0, 0,
  697. 0, 0, 0, 0, 0, 0, 0, 0,
  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, 0, 0, 0, 0, 0, 0, 0,
  703. 0, 0, 0, 0, 0, 0, 0, 0,
  704. 0, 0, 0, 0, 0, 0, 0, 0,
  705. 0, 0, 0, 0, 0, 0, 0, 0
  706. };
  707. __attribute__ ((weak))
  708. const uint8_t ascii_to_keycode_lut[0x80] PROGMEM = {
  709. 0, 0, 0, 0, 0, 0, 0, 0,
  710. KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
  711. 0, 0, 0, 0, 0, 0, 0, 0,
  712. 0, 0, 0, KC_ESC, 0, 0, 0, 0,
  713. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  714. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  715. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  716. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  717. KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  718. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  719. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  720. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  721. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  722. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  723. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  724. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  725. };
  726. void send_string(const char *str) {
  727. send_string_with_delay(str, 0);
  728. }
  729. void send_string_P(const char *str) {
  730. send_string_with_delay_P(str, 0);
  731. }
  732. void send_string_with_delay(const char *str, uint8_t interval) {
  733. while (1) {
  734. char ascii_code = *str;
  735. if (!ascii_code) break;
  736. if (ascii_code == SS_TAP_CODE) {
  737. // tap
  738. uint8_t keycode = *(++str);
  739. register_code(keycode);
  740. unregister_code(keycode);
  741. } else if (ascii_code == SS_DOWN_CODE) {
  742. // down
  743. uint8_t keycode = *(++str);
  744. register_code(keycode);
  745. } else if (ascii_code == SS_UP_CODE) {
  746. // up
  747. uint8_t keycode = *(++str);
  748. unregister_code(keycode);
  749. } else {
  750. send_char(ascii_code);
  751. }
  752. ++str;
  753. // interval
  754. { uint8_t ms = interval; while (ms--) wait_ms(1); }
  755. }
  756. }
  757. void send_string_with_delay_P(const char *str, uint8_t interval) {
  758. while (1) {
  759. char ascii_code = pgm_read_byte(str);
  760. if (!ascii_code) break;
  761. if (ascii_code == SS_TAP_CODE) {
  762. // tap
  763. uint8_t keycode = pgm_read_byte(++str);
  764. register_code(keycode);
  765. unregister_code(keycode);
  766. } else if (ascii_code == SS_DOWN_CODE) {
  767. // down
  768. uint8_t keycode = pgm_read_byte(++str);
  769. register_code(keycode);
  770. } else if (ascii_code == SS_UP_CODE) {
  771. // up
  772. uint8_t keycode = pgm_read_byte(++str);
  773. unregister_code(keycode);
  774. } else {
  775. send_char(ascii_code);
  776. }
  777. ++str;
  778. // interval
  779. { uint8_t ms = interval; while (ms--) wait_ms(1); }
  780. }
  781. }
  782. void send_char(char ascii_code) {
  783. uint8_t keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
  784. bool is_shifted = pgm_read_byte(&ascii_to_shift_lut[(uint8_t)ascii_code]);
  785. bool is_altgred = pgm_read_byte(&ascii_to_altgr_lut[(uint8_t)ascii_code]);
  786. if (is_shifted) {
  787. register_code(KC_LSFT);
  788. }
  789. if (is_altgred) {
  790. register_code(KC_RALT);
  791. }
  792. tap_code(keycode);
  793. if (is_altgred) {
  794. unregister_code(KC_RALT);
  795. }
  796. if (is_shifted) {
  797. unregister_code(KC_LSFT);
  798. }
  799. }
  800. void set_single_persistent_default_layer(uint8_t default_layer) {
  801. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  802. PLAY_SONG(default_layer_songs[default_layer]);
  803. #endif
  804. eeconfig_update_default_layer(1U<<default_layer);
  805. default_layer_set(1U<<default_layer);
  806. }
  807. uint32_t update_tri_layer_state(uint32_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  808. uint32_t mask12 = (1UL << layer1) | (1UL << layer2);
  809. uint32_t mask3 = 1UL << layer3;
  810. return (state & mask12) == mask12 ? (state | mask3) : (state & ~mask3);
  811. }
  812. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  813. layer_state_set(update_tri_layer_state(layer_state, layer1, layer2, layer3));
  814. }
  815. void tap_random_base64(void) {
  816. #if defined(__AVR_ATmega32U4__)
  817. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  818. #else
  819. uint8_t key = rand() % 64;
  820. #endif
  821. switch (key) {
  822. case 0 ... 25:
  823. register_code(KC_LSFT);
  824. register_code(key + KC_A);
  825. unregister_code(key + KC_A);
  826. unregister_code(KC_LSFT);
  827. break;
  828. case 26 ... 51:
  829. register_code(key - 26 + KC_A);
  830. unregister_code(key - 26 + KC_A);
  831. break;
  832. case 52:
  833. register_code(KC_0);
  834. unregister_code(KC_0);
  835. break;
  836. case 53 ... 61:
  837. register_code(key - 53 + KC_1);
  838. unregister_code(key - 53 + KC_1);
  839. break;
  840. case 62:
  841. register_code(KC_LSFT);
  842. register_code(KC_EQL);
  843. unregister_code(KC_EQL);
  844. unregister_code(KC_LSFT);
  845. break;
  846. case 63:
  847. register_code(KC_SLSH);
  848. unregister_code(KC_SLSH);
  849. break;
  850. }
  851. }
  852. __attribute__((weak))
  853. void bootmagic_lite(void) {
  854. // The lite version of TMK's bootmagic based on Wilba.
  855. // 100% less potential for accidentally making the
  856. // keyboard do stupid things.
  857. // We need multiple scans because debouncing can't be turned off.
  858. matrix_scan();
  859. #if defined(DEBOUNCING_DELAY) && DEBOUNCING_DELAY > 0
  860. wait_ms(DEBOUNCING_DELAY * 2);
  861. #elif defined(DEBOUNCE) && DEBOUNCE > 0
  862. wait_ms(DEBOUNCE * 2);
  863. #else
  864. wait_ms(30);
  865. #endif
  866. matrix_scan();
  867. // If the Esc and space bar are held down on power up,
  868. // reset the EEPROM valid state and jump to bootloader.
  869. // Assumes Esc is at [0,0].
  870. // This isn't very generalized, but we need something that doesn't
  871. // rely on user's keymaps in firmware or EEPROM.
  872. if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
  873. eeconfig_disable();
  874. // Jump to bootloader.
  875. bootloader_jump();
  876. }
  877. }
  878. void matrix_init_quantum() {
  879. #ifdef BOOTMAGIC_LITE
  880. bootmagic_lite();
  881. #endif
  882. if (!eeconfig_is_enabled()) {
  883. eeconfig_init();
  884. }
  885. #ifdef BACKLIGHT_ENABLE
  886. #ifdef LED_MATRIX_ENABLE
  887. led_matrix_init();
  888. #else
  889. backlight_init_ports();
  890. #endif
  891. #endif
  892. #ifdef AUDIO_ENABLE
  893. audio_init();
  894. #endif
  895. #ifdef RGB_MATRIX_ENABLE
  896. rgb_matrix_init();
  897. #endif
  898. #ifdef ENCODER_ENABLE
  899. encoder_init();
  900. #endif
  901. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  902. unicode_input_mode_init();
  903. #endif
  904. #ifdef HAPTIC_ENABLE
  905. haptic_init();
  906. #endif
  907. #ifdef OUTPUT_AUTO_ENABLE
  908. set_output(OUTPUT_AUTO);
  909. #endif
  910. matrix_init_kb();
  911. }
  912. void matrix_scan_quantum() {
  913. #if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE)
  914. matrix_scan_music();
  915. #endif
  916. #ifdef TAP_DANCE_ENABLE
  917. matrix_scan_tap_dance();
  918. #endif
  919. #ifdef COMBO_ENABLE
  920. matrix_scan_combo();
  921. #endif
  922. #if defined(BACKLIGHT_ENABLE)
  923. #if defined(LED_MATRIX_ENABLE)
  924. led_matrix_task();
  925. #elif defined(BACKLIGHT_PIN)
  926. backlight_task();
  927. #endif
  928. #endif
  929. #ifdef RGB_MATRIX_ENABLE
  930. rgb_matrix_task();
  931. #endif
  932. #ifdef ENCODER_ENABLE
  933. encoder_read();
  934. #endif
  935. #ifdef HAPTIC_ENABLE
  936. haptic_task();
  937. #endif
  938. matrix_scan_kb();
  939. }
  940. #if defined(BACKLIGHT_ENABLE) && (defined(BACKLIGHT_PIN) || defined(BACKLIGHT_PINS))
  941. // The logic is a bit complex, we support 3 setups:
  942. // 1. hardware PWM when backlight is wired to a PWM pin
  943. // depending on this pin, we use a different output compare unit
  944. // 2. software PWM with hardware timers, but the used timer depends
  945. // on the audio setup (audio wins other backlight)
  946. // 3. full software PWM
  947. #if BACKLIGHT_PIN == B7
  948. # define HARDWARE_PWM
  949. # define TCCRxA TCCR1A
  950. # define TCCRxB TCCR1B
  951. # define COMxx1 COM1C1
  952. # define OCRxx OCR1C
  953. # define TIMERx_OVF_vect TIMER1_OVF_vect
  954. # define TOIEx TOIE1
  955. # define ICRx ICR1
  956. # define TIMSKx TIMSK1
  957. #elif BACKLIGHT_PIN == B6
  958. # define HARDWARE_PWM
  959. # define TCCRxA TCCR1A
  960. # define TCCRxB TCCR1B
  961. # define COMxx1 COM1B1
  962. # define OCRxx OCR1B
  963. # define TIMERx_OVF_vect TIMER1_OVF_vect
  964. # define TOIEx TOIE1
  965. # define ICRx ICR1
  966. # define TIMSKx TIMSK1
  967. #elif BACKLIGHT_PIN == B5
  968. # define HARDWARE_PWM
  969. # define TCCRxA TCCR1A
  970. # define TCCRxB TCCR1B
  971. # define COMxx1 COM1A1
  972. # define OCRxx OCR1A
  973. # define TIMERx_OVF_vect TIMER1_OVF_vect
  974. # define TOIEx TOIE1
  975. # define ICRx ICR1
  976. # define TIMSKx TIMSK1
  977. #elif BACKLIGHT_PIN == C6
  978. # define HARDWARE_PWM
  979. # define TCCRxA TCCR3A
  980. # define TCCRxB TCCR3B
  981. # define COMxx1 COM3A1
  982. # define OCRxx OCR3A
  983. # define TIMERx_OVF_vect TIMER3_OVF_vect
  984. # define TOIEx TOIE3
  985. # define ICRx ICR3
  986. # define TIMSKx TIMSK3
  987. #elif defined(__AVR_ATmega32A__) && BACKLIGHT_PIN == D4
  988. # define TCCRxA TCCR1A
  989. # define TCCRxB TCCR1B
  990. # define COMxx1 COM1B1
  991. # define OCRxx OCR1B
  992. # define TIMERx_OVF_vect TIMER1_OVF_vect
  993. # define TOIEx TOIE1
  994. # define ICRx ICR1
  995. # define TIMSKx TIMSK1
  996. #else
  997. # if !defined(BACKLIGHT_CUSTOM_DRIVER)
  998. # if !defined(B5_AUDIO) && !defined(B6_AUDIO) && !defined(B7_AUDIO)
  999. // timer 1 is not used by audio , backlight can use it
  1000. #pragma message "Using hardware timer 1 with software PWM"
  1001. # define HARDWARE_PWM
  1002. # define BACKLIGHT_PWM_TIMER
  1003. # define TCCRxA TCCR1A
  1004. # define TCCRxB TCCR1B
  1005. # define OCRxx OCR1A
  1006. # define TIMERx_COMPA_vect TIMER1_COMPA_vect
  1007. # define TIMERx_OVF_vect TIMER1_OVF_vect
  1008. # define OCIExA OCIE1A
  1009. # define TOIEx TOIE1
  1010. # define ICRx ICR1
  1011. # if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register
  1012. # define TIMSKx TIMSK
  1013. # else
  1014. # define TIMSKx TIMSK1
  1015. # endif
  1016. # elif !defined(C6_AUDIO) && !defined(C5_AUDIO) && !defined(C4_AUDIO)
  1017. #pragma message "Using hardware timer 3 with software PWM"
  1018. // timer 3 is not used by audio, backlight can use it
  1019. # define HARDWARE_PWM
  1020. # define BACKLIGHT_PWM_TIMER
  1021. # define TCCRxA TCCR3A
  1022. # define TCCRxB TCCR3B
  1023. # define OCRxx OCR3A
  1024. # define TIMERx_COMPA_vect TIMER3_COMPA_vect
  1025. # define TIMERx_OVF_vect TIMER3_OVF_vect
  1026. # define OCIExA OCIE3A
  1027. # define TOIEx TOIE3
  1028. # define ICRx ICR1
  1029. # define TIMSKx TIMSK3
  1030. # else
  1031. #pragma message "Audio in use - using pure software PWM"
  1032. #define NO_HARDWARE_PWM
  1033. # endif
  1034. # else
  1035. #pragma message "Custom driver defined - using pure software PWM"
  1036. #define NO_HARDWARE_PWM
  1037. # endif
  1038. #endif
  1039. #ifndef BACKLIGHT_ON_STATE
  1040. #define BACKLIGHT_ON_STATE 0
  1041. #endif
  1042. void backlight_on(uint8_t backlight_pin) {
  1043. #if BACKLIGHT_ON_STATE == 0
  1044. writePinLow(backlight_pin);
  1045. #else
  1046. writePinHigh(backlight_pin);
  1047. #endif
  1048. }
  1049. void backlight_off(uint8_t backlight_pin) {
  1050. #if BACKLIGHT_ON_STATE == 0
  1051. writePinHigh(backlight_pin);
  1052. #else
  1053. writePinLow(backlight_pin);
  1054. #endif
  1055. }
  1056. #if defined(NO_HARDWARE_PWM) || defined(BACKLIGHT_PWM_TIMER) // pwm through software
  1057. // we support multiple backlight pins
  1058. #ifndef BACKLIGHT_LED_COUNT
  1059. #define BACKLIGHT_LED_COUNT 1
  1060. #endif
  1061. #if BACKLIGHT_LED_COUNT == 1
  1062. #define BACKLIGHT_PIN_INIT { BACKLIGHT_PIN }
  1063. #else
  1064. #define BACKLIGHT_PIN_INIT BACKLIGHT_PINS
  1065. #endif
  1066. #define FOR_EACH_LED(x) \
  1067. for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) \
  1068. { \
  1069. uint8_t backlight_pin = backlight_pins[i]; \
  1070. { \
  1071. x \
  1072. } \
  1073. }
  1074. static const uint8_t backlight_pins[BACKLIGHT_LED_COUNT] = BACKLIGHT_PIN_INIT;
  1075. #else // full hardware PWM
  1076. // we support only one backlight pin
  1077. static const uint8_t backlight_pin = BACKLIGHT_PIN;
  1078. #define FOR_EACH_LED(x) x
  1079. #endif
  1080. #ifdef NO_HARDWARE_PWM
  1081. __attribute__((weak))
  1082. void backlight_init_ports(void)
  1083. {
  1084. // Setup backlight pin as output and output to on state.
  1085. FOR_EACH_LED(
  1086. setPinOutput(backlight_pin);
  1087. backlight_on(backlight_pin);
  1088. )
  1089. }
  1090. __attribute__ ((weak))
  1091. void backlight_set(uint8_t level) {}
  1092. uint8_t backlight_tick = 0;
  1093. #ifndef BACKLIGHT_CUSTOM_DRIVER
  1094. void backlight_task(void) {
  1095. if ((0xFFFF >> ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
  1096. FOR_EACH_LED(
  1097. backlight_on(backlight_pin);
  1098. )
  1099. }
  1100. else {
  1101. FOR_EACH_LED(
  1102. backlight_off(backlight_pin);
  1103. )
  1104. }
  1105. backlight_tick = (backlight_tick + 1) % 16;
  1106. }
  1107. #endif
  1108. #ifdef BACKLIGHT_BREATHING
  1109. #ifndef BACKLIGHT_CUSTOM_DRIVER
  1110. #error "Backlight breathing only available with hardware PWM. Please disable."
  1111. #endif
  1112. #endif
  1113. #else // hardware pwm through timer
  1114. #ifdef BACKLIGHT_PWM_TIMER
  1115. // The idea of software PWM assisted by hardware timers is the following
  1116. // we use the hardware timer in fast PWM mode like for hardware PWM, but
  1117. // instead of letting the Output Match Comparator control the led pin
  1118. // (which is not possible since the backlight is not wired to PWM pins on the
  1119. // CPU), we do the LED on/off by oursleves.
  1120. // The timer is setup to count up to 0xFFFF, and we set the Output Compare
  1121. // register to the current 16bits backlight level (after CIE correction).
  1122. // This means the CPU will trigger a compare match interrupt when the counter
  1123. // reaches the backlight level, where we turn off the LEDs,
  1124. // but also an overflow interrupt when the counter rolls back to 0,
  1125. // in which we're going to turn on the LEDs.
  1126. // The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz.
  1127. // Triggered when the counter reaches the OCRx value
  1128. ISR(TIMERx_COMPA_vect) {
  1129. FOR_EACH_LED(
  1130. backlight_off(backlight_pin);
  1131. )
  1132. }
  1133. // Triggered when the counter reaches the TOP value
  1134. // this one triggers at F_CPU/65536 =~ 244 Hz
  1135. ISR(TIMERx_OVF_vect) {
  1136. #ifdef BACKLIGHT_BREATHING
  1137. breathing_task();
  1138. #endif
  1139. // for very small values of OCRxx (or backlight level)
  1140. // we can't guarantee this whole code won't execute
  1141. // at the same time as the compare match interrupt
  1142. // which means that we might turn on the leds while
  1143. // trying to turn them off, leading to flickering
  1144. // artifacts (especially while breathing, because breathing_task
  1145. // takes many computation cycles).
  1146. // so better not turn them on while the counter TOP is very low.
  1147. if (OCRxx > 256) {
  1148. FOR_EACH_LED(
  1149. backlight_on(backlight_pin);
  1150. )
  1151. }
  1152. }
  1153. #endif
  1154. #define TIMER_TOP 0xFFFFU
  1155. // See http://jared.geek.nz/2013/feb/linear-led-pwm
  1156. static uint16_t cie_lightness(uint16_t v) {
  1157. if (v <= 5243) // if below 8% of max
  1158. return v / 9; // same as dividing by 900%
  1159. else {
  1160. uint32_t y = (((uint32_t) v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
  1161. // to get a useful result with integer division, we shift left in the expression above
  1162. // and revert what we've done again after squaring.
  1163. y = y * y * y >> 8;
  1164. if (y > 0xFFFFUL) // prevent overflow
  1165. return 0xFFFFU;
  1166. else
  1167. return (uint16_t) y;
  1168. }
  1169. }
  1170. // range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val.
  1171. static inline void set_pwm(uint16_t val) {
  1172. OCRxx = val;
  1173. }
  1174. #ifndef BACKLIGHT_CUSTOM_DRIVER
  1175. __attribute__ ((weak))
  1176. void backlight_set(uint8_t level) {
  1177. if (level > BACKLIGHT_LEVELS)
  1178. level = BACKLIGHT_LEVELS;
  1179. if (level == 0) {
  1180. #ifdef BACKLIGHT_PWM_TIMER
  1181. if (OCRxx) {
  1182. TIMSKx &= ~(_BV(OCIExA));
  1183. TIMSKx &= ~(_BV(TOIEx));
  1184. FOR_EACH_LED(
  1185. backlight_off(backlight_pin);
  1186. )
  1187. }
  1188. #else
  1189. // Turn off PWM control on backlight pin
  1190. TCCRxA &= ~(_BV(COMxx1));
  1191. #endif
  1192. } else {
  1193. #ifdef BACKLIGHT_PWM_TIMER
  1194. if (!OCRxx) {
  1195. TIMSKx |= _BV(OCIExA);
  1196. TIMSKx |= _BV(TOIEx);
  1197. }
  1198. #else
  1199. // Turn on PWM control of backlight pin
  1200. TCCRxA |= _BV(COMxx1);
  1201. #endif
  1202. }
  1203. // Set the brightness
  1204. set_pwm(cie_lightness(TIMER_TOP * (uint32_t)level / BACKLIGHT_LEVELS));
  1205. }
  1206. void backlight_task(void) {}
  1207. #endif // BACKLIGHT_CUSTOM_DRIVER
  1208. #ifdef BACKLIGHT_BREATHING
  1209. #define BREATHING_NO_HALT 0
  1210. #define BREATHING_HALT_OFF 1
  1211. #define BREATHING_HALT_ON 2
  1212. #define BREATHING_STEPS 128
  1213. static uint8_t breathing_period = BREATHING_PERIOD;
  1214. static uint8_t breathing_halt = BREATHING_NO_HALT;
  1215. static uint16_t breathing_counter = 0;
  1216. #ifdef BACKLIGHT_PWM_TIMER
  1217. static bool breathing = false;
  1218. bool is_breathing(void) {
  1219. return breathing;
  1220. }
  1221. #define breathing_interrupt_enable() do { breathing = true; } while (0)
  1222. #define breathing_interrupt_disable() do { breathing = false; } while (0)
  1223. #else
  1224. bool is_breathing(void) {
  1225. return !!(TIMSKx & _BV(TOIEx));
  1226. }
  1227. #define breathing_interrupt_enable() do {TIMSKx |= _BV(TOIEx);} while (0)
  1228. #define breathing_interrupt_disable() do {TIMSKx &= ~_BV(TOIEx);} while (0)
  1229. #endif
  1230. #define breathing_min() do {breathing_counter = 0;} while (0)
  1231. #define breathing_max() do {breathing_counter = breathing_period * 244 / 2;} while (0)
  1232. void breathing_enable(void)
  1233. {
  1234. breathing_counter = 0;
  1235. breathing_halt = BREATHING_NO_HALT;
  1236. breathing_interrupt_enable();
  1237. }
  1238. void breathing_pulse(void)
  1239. {
  1240. if (get_backlight_level() == 0)
  1241. breathing_min();
  1242. else
  1243. breathing_max();
  1244. breathing_halt = BREATHING_HALT_ON;
  1245. breathing_interrupt_enable();
  1246. }
  1247. void breathing_disable(void)
  1248. {
  1249. breathing_interrupt_disable();
  1250. // Restore backlight level
  1251. backlight_set(get_backlight_level());
  1252. }
  1253. void breathing_self_disable(void)
  1254. {
  1255. if (get_backlight_level() == 0)
  1256. breathing_halt = BREATHING_HALT_OFF;
  1257. else
  1258. breathing_halt = BREATHING_HALT_ON;
  1259. }
  1260. void breathing_toggle(void) {
  1261. if (is_breathing())
  1262. breathing_disable();
  1263. else
  1264. breathing_enable();
  1265. }
  1266. void breathing_period_set(uint8_t value)
  1267. {
  1268. if (!value)
  1269. value = 1;
  1270. breathing_period = value;
  1271. }
  1272. void breathing_period_default(void) {
  1273. breathing_period_set(BREATHING_PERIOD);
  1274. }
  1275. void breathing_period_inc(void)
  1276. {
  1277. breathing_period_set(breathing_period+1);
  1278. }
  1279. void breathing_period_dec(void)
  1280. {
  1281. breathing_period_set(breathing_period-1);
  1282. }
  1283. /* To generate breathing curve in python:
  1284. * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
  1285. */
  1286. 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};
  1287. // Use this before the cie_lightness function.
  1288. static inline uint16_t scale_backlight(uint16_t v) {
  1289. return v / BACKLIGHT_LEVELS * get_backlight_level();
  1290. }
  1291. #ifdef BACKLIGHT_PWM_TIMER
  1292. void breathing_task(void)
  1293. #else
  1294. /* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run
  1295. * about 244 times per second.
  1296. */
  1297. ISR(TIMERx_OVF_vect)
  1298. #endif
  1299. {
  1300. uint16_t interval = (uint16_t) breathing_period * 244 / BREATHING_STEPS;
  1301. // resetting after one period to prevent ugly reset at overflow.
  1302. breathing_counter = (breathing_counter + 1) % (breathing_period * 244);
  1303. uint8_t index = breathing_counter / interval % BREATHING_STEPS;
  1304. if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) ||
  1305. ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1)))
  1306. {
  1307. breathing_interrupt_disable();
  1308. }
  1309. set_pwm(cie_lightness(scale_backlight((uint16_t) pgm_read_byte(&breathing_table[index]) * 0x0101U)));
  1310. }
  1311. #endif // BACKLIGHT_BREATHING
  1312. __attribute__ ((weak))
  1313. void backlight_init_ports(void)
  1314. {
  1315. // Setup backlight pin as output and output to on state.
  1316. FOR_EACH_LED(
  1317. setPinOutput(backlight_pin);
  1318. backlight_on(backlight_pin);
  1319. )
  1320. // I could write a wall of text here to explain... but TL;DW
  1321. // Go read the ATmega32u4 datasheet.
  1322. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  1323. #ifdef BACKLIGHT_PWM_TIMER
  1324. // TimerX setup, Fast PWM mode count to TOP set in ICRx
  1325. TCCRxA = _BV(WGM11); // = 0b00000010;
  1326. // clock select clk/1
  1327. TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  1328. #else // hardware PWM
  1329. // Pin PB7 = OCR1C (Timer 1, Channel C)
  1330. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  1331. // (i.e. start high, go low when counter matches.)
  1332. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  1333. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  1334. /*
  1335. 14.8.3:
  1336. "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 [..]."
  1337. "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)."
  1338. */
  1339. TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010;
  1340. TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  1341. #endif
  1342. // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0.
  1343. ICRx = TIMER_TOP;
  1344. backlight_init();
  1345. #ifdef BACKLIGHT_BREATHING
  1346. breathing_enable();
  1347. #endif
  1348. }
  1349. #endif // hardware backlight
  1350. #else // no backlight
  1351. __attribute__ ((weak))
  1352. void backlight_init_ports(void) {}
  1353. __attribute__ ((weak))
  1354. void backlight_set(uint8_t level) {}
  1355. #endif // backlight
  1356. #ifdef HD44780_ENABLED
  1357. #include "hd44780.h"
  1358. #endif
  1359. // Functions for spitting out values
  1360. //
  1361. void send_dword(uint32_t number) { // this might not actually work
  1362. uint16_t word = (number >> 16);
  1363. send_word(word);
  1364. send_word(number & 0xFFFFUL);
  1365. }
  1366. void send_word(uint16_t number) {
  1367. uint8_t byte = number >> 8;
  1368. send_byte(byte);
  1369. send_byte(number & 0xFF);
  1370. }
  1371. void send_byte(uint8_t number) {
  1372. uint8_t nibble = number >> 4;
  1373. send_nibble(nibble);
  1374. send_nibble(number & 0xF);
  1375. }
  1376. void send_nibble(uint8_t number) {
  1377. switch (number) {
  1378. case 0:
  1379. register_code(KC_0);
  1380. unregister_code(KC_0);
  1381. break;
  1382. case 1 ... 9:
  1383. register_code(KC_1 + (number - 1));
  1384. unregister_code(KC_1 + (number - 1));
  1385. break;
  1386. case 0xA ... 0xF:
  1387. register_code(KC_A + (number - 0xA));
  1388. unregister_code(KC_A + (number - 0xA));
  1389. break;
  1390. }
  1391. }
  1392. __attribute__((weak))
  1393. uint16_t hex_to_keycode(uint8_t hex)
  1394. {
  1395. hex = hex & 0xF;
  1396. if (hex == 0x0) {
  1397. return KC_0;
  1398. } else if (hex < 0xA) {
  1399. return KC_1 + (hex - 0x1);
  1400. } else {
  1401. return KC_A + (hex - 0xA);
  1402. }
  1403. }
  1404. void api_send_unicode(uint32_t unicode) {
  1405. #ifdef API_ENABLE
  1406. uint8_t chunk[4];
  1407. dword_to_bytes(unicode, chunk);
  1408. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  1409. #endif
  1410. }
  1411. __attribute__ ((weak))
  1412. void led_set_user(uint8_t usb_led) {
  1413. }
  1414. __attribute__ ((weak))
  1415. void led_set_kb(uint8_t usb_led) {
  1416. led_set_user(usb_led);
  1417. }
  1418. __attribute__ ((weak))
  1419. void led_init_ports(void)
  1420. {
  1421. }
  1422. __attribute__ ((weak))
  1423. void led_set(uint8_t usb_led)
  1424. {
  1425. // Example LED Code
  1426. //
  1427. // // Using PE6 Caps Lock LED
  1428. // if (usb_led & (1<<USB_LED_CAPS_LOCK))
  1429. // {
  1430. // // Output high.
  1431. // DDRE |= (1<<6);
  1432. // PORTE |= (1<<6);
  1433. // }
  1434. // else
  1435. // {
  1436. // // Output low.
  1437. // DDRE &= ~(1<<6);
  1438. // PORTE &= ~(1<<6);
  1439. // }
  1440. #if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
  1441. // Use backlight as Caps Lock indicator
  1442. uint8_t bl_toggle_lvl = 0;
  1443. if (IS_LED_ON(usb_led, USB_LED_CAPS_LOCK) && !backlight_config.enable) {
  1444. // Turning Caps Lock ON and backlight is disabled in config
  1445. // Toggling backlight to the brightest level
  1446. bl_toggle_lvl = BACKLIGHT_LEVELS;
  1447. } else if (IS_LED_OFF(usb_led, USB_LED_CAPS_LOCK) && backlight_config.enable) {
  1448. // Turning Caps Lock OFF and backlight is enabled in config
  1449. // Toggling backlight and restoring config level
  1450. bl_toggle_lvl = backlight_config.level;
  1451. }
  1452. // Set level without modify backlight_config to keep ability to restore state
  1453. backlight_set(bl_toggle_lvl);
  1454. #endif
  1455. led_set_kb(usb_led);
  1456. }
  1457. //------------------------------------------------------------------------------
  1458. // Override these functions in your keymap file to play different tunes on
  1459. // different events such as startup and bootloader jump
  1460. __attribute__ ((weak))
  1461. void startup_user() {}
  1462. __attribute__ ((weak))
  1463. void shutdown_user() {}
  1464. //------------------------------------------------------------------------------