quantum.c 38 KB

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