quantum.c 38 KB

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