quantum.c 41 KB

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