quantum.c 48 KB

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