quantum.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  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 = keymap_config.swap_ralt_rgui = true;
  577. #ifdef AUDIO_ENABLE
  578. PLAY_SONG(ag_swap_song);
  579. #endif
  580. break;
  581. case MAGIC_SWAP_CTL_GUI:
  582. keymap_config.swap_lctl_lgui = keymap_config.swap_rctl_rgui = true;
  583. #ifdef AUDIO_ENABLE
  584. PLAY_SONG(cg_swap_song);
  585. #endif
  586. break;
  587. case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
  588. keymap_config.swap_control_capslock = false;
  589. break;
  590. case MAGIC_UNCAPSLOCK_TO_CONTROL:
  591. keymap_config.capslock_to_control = false;
  592. break;
  593. case MAGIC_UNSWAP_LALT_LGUI:
  594. keymap_config.swap_lalt_lgui = false;
  595. break;
  596. case MAGIC_UNSWAP_RALT_RGUI:
  597. keymap_config.swap_ralt_rgui = false;
  598. break;
  599. case MAGIC_UNSWAP_LCTL_LGUI:
  600. keymap_config.swap_lctl_lgui = false;
  601. break;
  602. case MAGIC_UNSWAP_RCTL_RGUI:
  603. keymap_config.swap_rctl_rgui = false;
  604. break;
  605. case MAGIC_UNNO_GUI:
  606. keymap_config.no_gui = false;
  607. break;
  608. case MAGIC_UNSWAP_GRAVE_ESC:
  609. keymap_config.swap_grave_esc = false;
  610. break;
  611. case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
  612. keymap_config.swap_backslash_backspace = false;
  613. break;
  614. case MAGIC_UNHOST_NKRO:
  615. keymap_config.nkro = false;
  616. break;
  617. case MAGIC_UNSWAP_ALT_GUI:
  618. keymap_config.swap_lalt_lgui = keymap_config.swap_ralt_rgui = false;
  619. #ifdef AUDIO_ENABLE
  620. PLAY_SONG(ag_norm_song);
  621. #endif
  622. break;
  623. case MAGIC_UNSWAP_CTL_GUI:
  624. keymap_config.swap_lctl_lgui = keymap_config.swap_rctl_rgui = false;
  625. #ifdef AUDIO_ENABLE
  626. PLAY_SONG(cg_norm_song);
  627. #endif
  628. break;
  629. case MAGIC_TOGGLE_ALT_GUI:
  630. keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
  631. keymap_config.swap_ralt_rgui = keymap_config.swap_lalt_lgui;
  632. #ifdef AUDIO_ENABLE
  633. if (keymap_config.swap_ralt_rgui) {
  634. PLAY_SONG(ag_swap_song);
  635. } else {
  636. PLAY_SONG(ag_norm_song);
  637. }
  638. #endif
  639. break;
  640. case MAGIC_TOGGLE_CTL_GUI:
  641. keymap_config.swap_lctl_lgui = !keymap_config.swap_lctl_lgui;
  642. keymap_config.swap_rctl_rgui = keymap_config.swap_lctl_lgui;
  643. #ifdef AUDIO_ENABLE
  644. if (keymap_config.swap_rctl_rgui) {
  645. PLAY_SONG(cg_swap_song);
  646. } else {
  647. PLAY_SONG(cg_norm_song);
  648. }
  649. #endif
  650. break;
  651. case MAGIC_TOGGLE_NKRO:
  652. keymap_config.nkro = !keymap_config.nkro;
  653. break;
  654. default:
  655. break;
  656. }
  657. eeconfig_update_keymap(keymap_config.raw);
  658. clear_keyboard(); // clear to prevent stuck keys
  659. return false;
  660. }
  661. break;
  662. case GRAVE_ESC: {
  663. uint8_t shifted = get_mods() & ((MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)
  664. |MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)));
  665. #ifdef GRAVE_ESC_ALT_OVERRIDE
  666. // if ALT is pressed, ESC is always sent
  667. // this is handy for the cmd+opt+esc shortcut on macOS, among other things.
  668. if (get_mods() & (MOD_BIT(KC_LALT) | MOD_BIT(KC_RALT))) {
  669. shifted = 0;
  670. }
  671. #endif
  672. #ifdef GRAVE_ESC_CTRL_OVERRIDE
  673. // if CTRL is pressed, ESC is always sent
  674. // this is handy for the ctrl+shift+esc shortcut on windows, among other things.
  675. if (get_mods() & (MOD_BIT(KC_LCTL) | MOD_BIT(KC_RCTL))) {
  676. shifted = 0;
  677. }
  678. #endif
  679. #ifdef GRAVE_ESC_GUI_OVERRIDE
  680. // if GUI is pressed, ESC is always sent
  681. if (get_mods() & (MOD_BIT(KC_LGUI) | MOD_BIT(KC_RGUI))) {
  682. shifted = 0;
  683. }
  684. #endif
  685. #ifdef GRAVE_ESC_SHIFT_OVERRIDE
  686. // if SHIFT is pressed, ESC is always sent
  687. if (get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) {
  688. shifted = 0;
  689. }
  690. #endif
  691. if (record->event.pressed) {
  692. grave_esc_was_shifted = shifted;
  693. add_key(shifted ? KC_GRAVE : KC_ESCAPE);
  694. }
  695. else {
  696. del_key(grave_esc_was_shifted ? KC_GRAVE : KC_ESCAPE);
  697. }
  698. send_keyboard_report();
  699. return false;
  700. }
  701. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_BREATHING)
  702. case BL_BRTG: {
  703. if (record->event.pressed) {
  704. backlight_toggle_breathing();
  705. }
  706. return false;
  707. }
  708. #endif
  709. }
  710. return process_action_kb(record);
  711. }
  712. __attribute__ ((weak))
  713. const bool ascii_to_shift_lut[128] PROGMEM = {
  714. 0, 0, 0, 0, 0, 0, 0, 0,
  715. 0, 0, 0, 0, 0, 0, 0, 0,
  716. 0, 0, 0, 0, 0, 0, 0, 0,
  717. 0, 0, 0, 0, 0, 0, 0, 0,
  718. 0, 1, 1, 1, 1, 1, 1, 0,
  719. 1, 1, 1, 1, 0, 0, 0, 0,
  720. 0, 0, 0, 0, 0, 0, 0, 0,
  721. 0, 0, 1, 0, 1, 0, 1, 1,
  722. 1, 1, 1, 1, 1, 1, 1, 1,
  723. 1, 1, 1, 1, 1, 1, 1, 1,
  724. 1, 1, 1, 1, 1, 1, 1, 1,
  725. 1, 1, 1, 0, 0, 0, 1, 1,
  726. 0, 0, 0, 0, 0, 0, 0, 0,
  727. 0, 0, 0, 0, 0, 0, 0, 0,
  728. 0, 0, 0, 0, 0, 0, 0, 0,
  729. 0, 0, 0, 1, 1, 1, 1, 0
  730. };
  731. __attribute__ ((weak))
  732. const bool ascii_to_altgr_lut[128] PROGMEM = {
  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, 0, 0, 0, 0, 0, 0, 0,
  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. };
  750. __attribute__ ((weak))
  751. const uint8_t ascii_to_keycode_lut[128] PROGMEM = {
  752. // NUL SOH STX ETX EOT ENQ ACK BEL
  753. XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  754. // BS TAB LF VT FF CR SO SI
  755. KC_BSPC, KC_TAB, KC_ENT, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  756. // DLE DC1 DC2 DC3 DC4 NAK SYN ETB
  757. XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  758. // CAN EM SUB ESC FS GS RS US
  759. XXXXXXX, XXXXXXX, XXXXXXX, KC_ESC, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  760. // ! " # $ % & '
  761. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  762. // ( ) * + , - . /
  763. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  764. // 0 1 2 3 4 5 6 7
  765. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  766. // 8 9 : ; < = > ?
  767. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  768. // @ A B C D E F G
  769. KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  770. // H I J K L M N O
  771. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  772. // P Q R S T U V W
  773. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  774. // X Y Z [ \ ] ^ _
  775. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  776. // ` a b c d e f g
  777. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  778. // h i j k l m n o
  779. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  780. // p q r s t u v w
  781. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  782. // x y z { | } ~ DEL
  783. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
  784. };
  785. void send_string(const char *str) {
  786. send_string_with_delay(str, 0);
  787. }
  788. void send_string_P(const char *str) {
  789. send_string_with_delay_P(str, 0);
  790. }
  791. void send_string_with_delay(const char *str, uint8_t interval) {
  792. while (1) {
  793. char ascii_code = *str;
  794. if (!ascii_code) break;
  795. if (ascii_code == SS_TAP_CODE) {
  796. // tap
  797. uint8_t keycode = *(++str);
  798. register_code(keycode);
  799. unregister_code(keycode);
  800. } else if (ascii_code == SS_DOWN_CODE) {
  801. // down
  802. uint8_t keycode = *(++str);
  803. register_code(keycode);
  804. } else if (ascii_code == SS_UP_CODE) {
  805. // up
  806. uint8_t keycode = *(++str);
  807. unregister_code(keycode);
  808. } else {
  809. send_char(ascii_code);
  810. }
  811. ++str;
  812. // interval
  813. { uint8_t ms = interval; while (ms--) wait_ms(1); }
  814. }
  815. }
  816. void send_string_with_delay_P(const char *str, uint8_t interval) {
  817. while (1) {
  818. char ascii_code = pgm_read_byte(str);
  819. if (!ascii_code) break;
  820. if (ascii_code == SS_TAP_CODE) {
  821. // tap
  822. uint8_t keycode = pgm_read_byte(++str);
  823. register_code(keycode);
  824. unregister_code(keycode);
  825. } else if (ascii_code == SS_DOWN_CODE) {
  826. // down
  827. uint8_t keycode = pgm_read_byte(++str);
  828. register_code(keycode);
  829. } else if (ascii_code == SS_UP_CODE) {
  830. // up
  831. uint8_t keycode = pgm_read_byte(++str);
  832. unregister_code(keycode);
  833. } else {
  834. send_char(ascii_code);
  835. }
  836. ++str;
  837. // interval
  838. { uint8_t ms = interval; while (ms--) wait_ms(1); }
  839. }
  840. }
  841. void send_char(char ascii_code) {
  842. uint8_t keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
  843. bool is_shifted = pgm_read_byte(&ascii_to_shift_lut[(uint8_t)ascii_code]);
  844. bool is_altgred = pgm_read_byte(&ascii_to_altgr_lut[(uint8_t)ascii_code]);
  845. if (is_shifted) {
  846. register_code(KC_LSFT);
  847. }
  848. if (is_altgred) {
  849. register_code(KC_RALT);
  850. }
  851. tap_code(keycode);
  852. if (is_altgred) {
  853. unregister_code(KC_RALT);
  854. }
  855. if (is_shifted) {
  856. unregister_code(KC_LSFT);
  857. }
  858. }
  859. void set_single_persistent_default_layer(uint8_t default_layer) {
  860. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  861. PLAY_SONG(default_layer_songs[default_layer]);
  862. #endif
  863. eeconfig_update_default_layer(1U<<default_layer);
  864. default_layer_set(1U<<default_layer);
  865. }
  866. layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  867. layer_state_t mask12 = (1UL << layer1) | (1UL << layer2);
  868. layer_state_t mask3 = 1UL << layer3;
  869. return (state & mask12) == mask12 ? (state | mask3) : (state & ~mask3);
  870. }
  871. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  872. layer_state_set(update_tri_layer_state(layer_state, layer1, layer2, layer3));
  873. }
  874. void tap_random_base64(void) {
  875. #if defined(__AVR_ATmega32U4__)
  876. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  877. #else
  878. uint8_t key = rand() % 64;
  879. #endif
  880. switch (key) {
  881. case 0 ... 25:
  882. register_code(KC_LSFT);
  883. register_code(key + KC_A);
  884. unregister_code(key + KC_A);
  885. unregister_code(KC_LSFT);
  886. break;
  887. case 26 ... 51:
  888. register_code(key - 26 + KC_A);
  889. unregister_code(key - 26 + KC_A);
  890. break;
  891. case 52:
  892. register_code(KC_0);
  893. unregister_code(KC_0);
  894. break;
  895. case 53 ... 61:
  896. register_code(key - 53 + KC_1);
  897. unregister_code(key - 53 + KC_1);
  898. break;
  899. case 62:
  900. register_code(KC_LSFT);
  901. register_code(KC_EQL);
  902. unregister_code(KC_EQL);
  903. unregister_code(KC_LSFT);
  904. break;
  905. case 63:
  906. register_code(KC_SLSH);
  907. unregister_code(KC_SLSH);
  908. break;
  909. }
  910. }
  911. __attribute__((weak))
  912. void bootmagic_lite(void) {
  913. // The lite version of TMK's bootmagic based on Wilba.
  914. // 100% less potential for accidentally making the
  915. // keyboard do stupid things.
  916. // We need multiple scans because debouncing can't be turned off.
  917. matrix_scan();
  918. #if defined(DEBOUNCING_DELAY) && DEBOUNCING_DELAY > 0
  919. wait_ms(DEBOUNCING_DELAY * 2);
  920. #elif defined(DEBOUNCE) && DEBOUNCE > 0
  921. wait_ms(DEBOUNCE * 2);
  922. #else
  923. wait_ms(30);
  924. #endif
  925. matrix_scan();
  926. // If the Esc and space bar are held down on power up,
  927. // reset the EEPROM valid state and jump to bootloader.
  928. // Assumes Esc is at [0,0].
  929. // This isn't very generalized, but we need something that doesn't
  930. // rely on user's keymaps in firmware or EEPROM.
  931. if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
  932. eeconfig_disable();
  933. // Jump to bootloader.
  934. bootloader_jump();
  935. }
  936. }
  937. void matrix_init_quantum() {
  938. #ifdef BOOTMAGIC_LITE
  939. bootmagic_lite();
  940. #endif
  941. if (!eeconfig_is_enabled()) {
  942. eeconfig_init();
  943. }
  944. #ifdef BACKLIGHT_ENABLE
  945. #ifdef LED_MATRIX_ENABLE
  946. led_matrix_init();
  947. #else
  948. backlight_init_ports();
  949. #endif
  950. #endif
  951. #ifdef AUDIO_ENABLE
  952. audio_init();
  953. #endif
  954. #ifdef RGB_MATRIX_ENABLE
  955. rgb_matrix_init();
  956. #endif
  957. #ifdef ENCODER_ENABLE
  958. encoder_init();
  959. #endif
  960. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  961. unicode_input_mode_init();
  962. #endif
  963. #ifdef HAPTIC_ENABLE
  964. haptic_init();
  965. #endif
  966. #ifdef OUTPUT_AUTO_ENABLE
  967. set_output(OUTPUT_AUTO);
  968. #endif
  969. matrix_init_kb();
  970. }
  971. void matrix_scan_quantum() {
  972. #if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE)
  973. matrix_scan_music();
  974. #endif
  975. #ifdef TAP_DANCE_ENABLE
  976. matrix_scan_tap_dance();
  977. #endif
  978. #ifdef COMBO_ENABLE
  979. matrix_scan_combo();
  980. #endif
  981. #if defined(BACKLIGHT_ENABLE)
  982. #if defined(LED_MATRIX_ENABLE)
  983. led_matrix_task();
  984. #elif defined(BACKLIGHT_PIN)
  985. backlight_task();
  986. #endif
  987. #endif
  988. #ifdef RGB_MATRIX_ENABLE
  989. rgb_matrix_task();
  990. #endif
  991. #ifdef ENCODER_ENABLE
  992. encoder_read();
  993. #endif
  994. #ifdef HAPTIC_ENABLE
  995. haptic_task();
  996. #endif
  997. matrix_scan_kb();
  998. }
  999. #if defined(BACKLIGHT_ENABLE) && (defined(BACKLIGHT_PIN) || defined(BACKLIGHT_PINS))
  1000. // This logic is a bit complex, we support 3 setups:
  1001. //
  1002. // 1. Hardware PWM when backlight is wired to a PWM pin.
  1003. // Depending on this pin, we use a different output compare unit.
  1004. // 2. Software PWM with hardware timers, but the used timer
  1005. // depends on the Audio setup (Audio wins over Backlight).
  1006. // 3. Full software PWM, driven by the matrix scan, if both timers are used by Audio.
  1007. #if (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) \
  1008. || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) \
  1009. || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) \
  1010. && (BACKLIGHT_PIN == B5 || BACKLIGHT_PIN == B6 || BACKLIGHT_PIN == B7)
  1011. #define HARDWARE_PWM
  1012. #define ICRx ICR1
  1013. #define TCCRxA TCCR1A
  1014. #define TCCRxB TCCR1B
  1015. #define TIMERx_OVF_vect TIMER1_OVF_vect
  1016. #define TIMSKx TIMSK1
  1017. #define TOIEx TOIE1
  1018. #if BACKLIGHT_PIN == B5
  1019. #define COMxx1 COM1A1
  1020. #define OCRxx OCR1A
  1021. #elif BACKLIGHT_PIN == B6
  1022. #define COMxx1 COM1B1
  1023. #define OCRxx OCR1B
  1024. #elif BACKLIGHT_PIN == B7
  1025. #define COMxx1 COM1C1
  1026. #define OCRxx OCR1C
  1027. #endif
  1028. #elif (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) \
  1029. || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) \
  1030. || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) \
  1031. && (BACKLIGHT_PIN == C4 || BACKLIGHT_PIN == C5 || BACKLIGHT_PIN == C6)
  1032. #define HARDWARE_PWM
  1033. #define ICRx ICR3
  1034. #define TCCRxA TCCR3A
  1035. #define TCCRxB TCCR3B
  1036. #define TIMERx_OVF_vect TIMER3_OVF_vect
  1037. #define TIMSKx TIMSK3
  1038. #define TOIEx TOIE3
  1039. #if BACKLIGHT_PIN == C4
  1040. #if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
  1041. #error This MCU has no C4 pin!
  1042. #else
  1043. #define COMxx1 COM3C1
  1044. #define OCRxx OCR3C
  1045. #endif
  1046. #elif BACKLIGHT_PIN == C5
  1047. #if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
  1048. #error This MCU has no C5 pin!
  1049. #else
  1050. #define COMxx1 COM3B1
  1051. #define OCRxx OCR3B
  1052. #endif
  1053. #elif BACKLIGHT_PIN == C6
  1054. #define COMxx1 COM3A1
  1055. #define OCRxx OCR3A
  1056. #endif
  1057. #elif (defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__)) \
  1058. && (BACKLIGHT_PIN == B7 || BACKLIGHT_PIN == C5 || BACKLIGHT_PIN == C6)
  1059. #define HARDWARE_PWM
  1060. #define ICRx ICR1
  1061. #define TCCRxA TCCR1A
  1062. #define TCCRxB TCCR1B
  1063. #define TIMERx_OVF_vect TIMER1_OVF_vect
  1064. #define TIMSKx TIMSK1
  1065. #define TOIEx TOIE1
  1066. #if BACKLIGHT_PIN == B7
  1067. #define COMxx1 COM1C1
  1068. #define OCRxx OCR1C
  1069. #elif BACKLIGHT_PIN == C5
  1070. #define COMxx1 COM1B1
  1071. #define OCRxx OCR1B
  1072. #elif BACKLIGHT_PIN == C6
  1073. #define COMxx1 COM1A1
  1074. #define OCRxx OCR1A
  1075. #endif
  1076. #elif defined(__AVR_ATmega32A__) \
  1077. && (BACKLIGHT_PIN == D4 || BACKLIGHT_PIN == D5)
  1078. #define HARDWARE_PWM
  1079. #define ICRx ICR1
  1080. #define TCCRxA TCCR1A
  1081. #define TCCRxB TCCR1B
  1082. #define TIMERx_OVF_vect TIMER1_OVF_vect
  1083. #define TIMSKx TIMSK
  1084. #define TOIEx TOIE1
  1085. #if BACKLIGHT_PIN == D4
  1086. #define COMxx1 COM1B1
  1087. #define OCRxx OCR1B
  1088. #elif BACKLIGHT_PIN == D5
  1089. #define COMxx1 COM1A1
  1090. #define OCRxx OCR1A
  1091. #endif
  1092. #else
  1093. #if !defined(BACKLIGHT_CUSTOM_DRIVER)
  1094. #if !defined(B5_AUDIO) && !defined(B6_AUDIO) && !defined(B7_AUDIO)
  1095. // Timer 1 is not in use by Audio feature, Backlight can use it
  1096. #pragma message "Using hardware timer 1 with software PWM"
  1097. #define HARDWARE_PWM
  1098. #define BACKLIGHT_PWM_TIMER
  1099. #define ICRx ICR1
  1100. #define TCCRxA TCCR1A
  1101. #define TCCRxB TCCR1B
  1102. #define TIMERx_COMPA_vect TIMER1_COMPA_vect
  1103. #define TIMERx_OVF_vect TIMER1_OVF_vect
  1104. #if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register
  1105. #define TIMSKx TIMSK
  1106. #else
  1107. #define TIMSKx TIMSK1
  1108. #endif
  1109. #define TOIEx TOIE1
  1110. #define OCIExA OCIE1A
  1111. #define OCRxx OCR1A
  1112. #elif !defined(C6_AUDIO) && !defined(C5_AUDIO) && !defined(C4_AUDIO)
  1113. #pragma message "Using hardware timer 3 with software PWM"
  1114. // Timer 3 is not in use by Audio feature, Backlight can use it
  1115. #define HARDWARE_PWM
  1116. #define BACKLIGHT_PWM_TIMER
  1117. #define ICRx ICR1
  1118. #define TCCRxA TCCR3A
  1119. #define TCCRxB TCCR3B
  1120. #define TIMERx_COMPA_vect TIMER3_COMPA_vect
  1121. #define TIMERx_OVF_vect TIMER3_OVF_vect
  1122. #define TIMSKx TIMSK3
  1123. #define TOIEx TOIE3
  1124. #define OCIExA OCIE3A
  1125. #define OCRxx OCR3A
  1126. #else
  1127. #pragma message "Audio in use - using pure software PWM"
  1128. #define NO_HARDWARE_PWM
  1129. #endif
  1130. #else
  1131. #pragma message "Custom driver defined - using pure software PWM"
  1132. #define NO_HARDWARE_PWM
  1133. #endif
  1134. #endif
  1135. #ifndef BACKLIGHT_ON_STATE
  1136. #define BACKLIGHT_ON_STATE 0
  1137. #endif
  1138. void backlight_on(uint8_t backlight_pin) {
  1139. #if BACKLIGHT_ON_STATE == 0
  1140. writePinLow(backlight_pin);
  1141. #else
  1142. writePinHigh(backlight_pin);
  1143. #endif
  1144. }
  1145. void backlight_off(uint8_t backlight_pin) {
  1146. #if BACKLIGHT_ON_STATE == 0
  1147. writePinHigh(backlight_pin);
  1148. #else
  1149. writePinLow(backlight_pin);
  1150. #endif
  1151. }
  1152. #if defined(NO_HARDWARE_PWM) || defined(BACKLIGHT_PWM_TIMER) // pwm through software
  1153. // we support multiple backlight pins
  1154. #ifndef BACKLIGHT_LED_COUNT
  1155. #define BACKLIGHT_LED_COUNT 1
  1156. #endif
  1157. #if BACKLIGHT_LED_COUNT == 1
  1158. #define BACKLIGHT_PIN_INIT { BACKLIGHT_PIN }
  1159. #else
  1160. #define BACKLIGHT_PIN_INIT BACKLIGHT_PINS
  1161. #endif
  1162. #define FOR_EACH_LED(x) \
  1163. for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) \
  1164. { \
  1165. uint8_t backlight_pin = backlight_pins[i]; \
  1166. { \
  1167. x \
  1168. } \
  1169. }
  1170. static const uint8_t backlight_pins[BACKLIGHT_LED_COUNT] = BACKLIGHT_PIN_INIT;
  1171. #else // full hardware PWM
  1172. // we support only one backlight pin
  1173. static const uint8_t backlight_pin = BACKLIGHT_PIN;
  1174. #define FOR_EACH_LED(x) x
  1175. #endif
  1176. #ifdef NO_HARDWARE_PWM
  1177. __attribute__((weak))
  1178. void backlight_init_ports(void)
  1179. {
  1180. // Setup backlight pin as output and output to on state.
  1181. FOR_EACH_LED(
  1182. setPinOutput(backlight_pin);
  1183. backlight_on(backlight_pin);
  1184. )
  1185. #ifdef BACKLIGHT_BREATHING
  1186. if (is_backlight_breathing()) {
  1187. breathing_enable();
  1188. }
  1189. #endif
  1190. }
  1191. __attribute__ ((weak))
  1192. void backlight_set(uint8_t level) {}
  1193. uint8_t backlight_tick = 0;
  1194. #ifndef BACKLIGHT_CUSTOM_DRIVER
  1195. void backlight_task(void) {
  1196. if ((0xFFFF >> ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
  1197. FOR_EACH_LED(
  1198. backlight_on(backlight_pin);
  1199. )
  1200. }
  1201. else {
  1202. FOR_EACH_LED(
  1203. backlight_off(backlight_pin);
  1204. )
  1205. }
  1206. backlight_tick = (backlight_tick + 1) % 16;
  1207. }
  1208. #endif
  1209. #ifdef BACKLIGHT_BREATHING
  1210. #ifndef BACKLIGHT_CUSTOM_DRIVER
  1211. #error "Backlight breathing only available with hardware PWM. Please disable."
  1212. #endif
  1213. #endif
  1214. #else // hardware pwm through timer
  1215. #ifdef BACKLIGHT_PWM_TIMER
  1216. // The idea of software PWM assisted by hardware timers is the following
  1217. // we use the hardware timer in fast PWM mode like for hardware PWM, but
  1218. // instead of letting the Output Match Comparator control the led pin
  1219. // (which is not possible since the backlight is not wired to PWM pins on the
  1220. // CPU), we do the LED on/off by oursleves.
  1221. // The timer is setup to count up to 0xFFFF, and we set the Output Compare
  1222. // register to the current 16bits backlight level (after CIE correction).
  1223. // This means the CPU will trigger a compare match interrupt when the counter
  1224. // reaches the backlight level, where we turn off the LEDs,
  1225. // but also an overflow interrupt when the counter rolls back to 0,
  1226. // in which we're going to turn on the LEDs.
  1227. // The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz.
  1228. // Triggered when the counter reaches the OCRx value
  1229. ISR(TIMERx_COMPA_vect) {
  1230. FOR_EACH_LED(
  1231. backlight_off(backlight_pin);
  1232. )
  1233. }
  1234. // Triggered when the counter reaches the TOP value
  1235. // this one triggers at F_CPU/65536 =~ 244 Hz
  1236. ISR(TIMERx_OVF_vect) {
  1237. #ifdef BACKLIGHT_BREATHING
  1238. if(is_breathing()) {
  1239. breathing_task();
  1240. }
  1241. #endif
  1242. // for very small values of OCRxx (or backlight level)
  1243. // we can't guarantee this whole code won't execute
  1244. // at the same time as the compare match interrupt
  1245. // which means that we might turn on the leds while
  1246. // trying to turn them off, leading to flickering
  1247. // artifacts (especially while breathing, because breathing_task
  1248. // takes many computation cycles).
  1249. // so better not turn them on while the counter TOP is very low.
  1250. if (OCRxx > 256) {
  1251. FOR_EACH_LED(
  1252. backlight_on(backlight_pin);
  1253. )
  1254. }
  1255. }
  1256. #endif
  1257. #define TIMER_TOP 0xFFFFU
  1258. // See http://jared.geek.nz/2013/feb/linear-led-pwm
  1259. static uint16_t cie_lightness(uint16_t v) {
  1260. if (v <= 5243) // if below 8% of max
  1261. return v / 9; // same as dividing by 900%
  1262. else {
  1263. uint32_t y = (((uint32_t) v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
  1264. // to get a useful result with integer division, we shift left in the expression above
  1265. // and revert what we've done again after squaring.
  1266. y = y * y * y >> 8;
  1267. if (y > 0xFFFFUL) // prevent overflow
  1268. return 0xFFFFU;
  1269. else
  1270. return (uint16_t) y;
  1271. }
  1272. }
  1273. // range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val.
  1274. static inline void set_pwm(uint16_t val) {
  1275. OCRxx = val;
  1276. }
  1277. #ifndef BACKLIGHT_CUSTOM_DRIVER
  1278. __attribute__ ((weak))
  1279. void backlight_set(uint8_t level) {
  1280. if (level > BACKLIGHT_LEVELS)
  1281. level = BACKLIGHT_LEVELS;
  1282. if (level == 0) {
  1283. #ifdef BACKLIGHT_PWM_TIMER
  1284. if (OCRxx) {
  1285. TIMSKx &= ~(_BV(OCIExA));
  1286. TIMSKx &= ~(_BV(TOIEx));
  1287. FOR_EACH_LED(
  1288. backlight_off(backlight_pin);
  1289. )
  1290. }
  1291. #else
  1292. // Turn off PWM control on backlight pin
  1293. TCCRxA &= ~(_BV(COMxx1));
  1294. #endif
  1295. } else {
  1296. #ifdef BACKLIGHT_PWM_TIMER
  1297. if (!OCRxx) {
  1298. TIMSKx |= _BV(OCIExA);
  1299. TIMSKx |= _BV(TOIEx);
  1300. }
  1301. #else
  1302. // Turn on PWM control of backlight pin
  1303. TCCRxA |= _BV(COMxx1);
  1304. #endif
  1305. }
  1306. // Set the brightness
  1307. set_pwm(cie_lightness(TIMER_TOP * (uint32_t)level / BACKLIGHT_LEVELS));
  1308. }
  1309. void backlight_task(void) {}
  1310. #endif // BACKLIGHT_CUSTOM_DRIVER
  1311. #ifdef BACKLIGHT_BREATHING
  1312. #define BREATHING_NO_HALT 0
  1313. #define BREATHING_HALT_OFF 1
  1314. #define BREATHING_HALT_ON 2
  1315. #define BREATHING_STEPS 128
  1316. static uint8_t breathing_period = BREATHING_PERIOD;
  1317. static uint8_t breathing_halt = BREATHING_NO_HALT;
  1318. static uint16_t breathing_counter = 0;
  1319. #ifdef BACKLIGHT_PWM_TIMER
  1320. static bool breathing = false;
  1321. bool is_breathing(void) {
  1322. return breathing;
  1323. }
  1324. #define breathing_interrupt_enable() do { breathing = true; } while (0)
  1325. #define breathing_interrupt_disable() do { breathing = false; } while (0)
  1326. #else
  1327. bool is_breathing(void) {
  1328. return !!(TIMSKx & _BV(TOIEx));
  1329. }
  1330. #define breathing_interrupt_enable() do {TIMSKx |= _BV(TOIEx);} while (0)
  1331. #define breathing_interrupt_disable() do {TIMSKx &= ~_BV(TOIEx);} while (0)
  1332. #endif
  1333. #define breathing_min() do {breathing_counter = 0;} while (0)
  1334. #define breathing_max() do {breathing_counter = breathing_period * 244 / 2;} while (0)
  1335. void breathing_enable(void)
  1336. {
  1337. breathing_counter = 0;
  1338. breathing_halt = BREATHING_NO_HALT;
  1339. breathing_interrupt_enable();
  1340. }
  1341. void breathing_pulse(void)
  1342. {
  1343. if (get_backlight_level() == 0)
  1344. breathing_min();
  1345. else
  1346. breathing_max();
  1347. breathing_halt = BREATHING_HALT_ON;
  1348. breathing_interrupt_enable();
  1349. }
  1350. void breathing_disable(void)
  1351. {
  1352. breathing_interrupt_disable();
  1353. // Restore backlight level
  1354. backlight_set(get_backlight_level());
  1355. }
  1356. void breathing_self_disable(void)
  1357. {
  1358. if (get_backlight_level() == 0)
  1359. breathing_halt = BREATHING_HALT_OFF;
  1360. else
  1361. breathing_halt = BREATHING_HALT_ON;
  1362. }
  1363. void breathing_toggle(void) {
  1364. if (is_breathing())
  1365. breathing_disable();
  1366. else
  1367. breathing_enable();
  1368. }
  1369. void breathing_period_set(uint8_t value)
  1370. {
  1371. if (!value)
  1372. value = 1;
  1373. breathing_period = value;
  1374. }
  1375. void breathing_period_default(void) {
  1376. breathing_period_set(BREATHING_PERIOD);
  1377. }
  1378. void breathing_period_inc(void)
  1379. {
  1380. breathing_period_set(breathing_period+1);
  1381. }
  1382. void breathing_period_dec(void)
  1383. {
  1384. breathing_period_set(breathing_period-1);
  1385. }
  1386. /* To generate breathing curve in python:
  1387. * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
  1388. */
  1389. 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};
  1390. // Use this before the cie_lightness function.
  1391. static inline uint16_t scale_backlight(uint16_t v) {
  1392. return v / BACKLIGHT_LEVELS * get_backlight_level();
  1393. }
  1394. #ifdef BACKLIGHT_PWM_TIMER
  1395. void breathing_task(void)
  1396. #else
  1397. /* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run
  1398. * about 244 times per second.
  1399. */
  1400. ISR(TIMERx_OVF_vect)
  1401. #endif
  1402. {
  1403. uint16_t interval = (uint16_t) breathing_period * 244 / BREATHING_STEPS;
  1404. // resetting after one period to prevent ugly reset at overflow.
  1405. breathing_counter = (breathing_counter + 1) % (breathing_period * 244);
  1406. uint8_t index = breathing_counter / interval % BREATHING_STEPS;
  1407. if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) ||
  1408. ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1)))
  1409. {
  1410. breathing_interrupt_disable();
  1411. }
  1412. set_pwm(cie_lightness(scale_backlight((uint16_t) pgm_read_byte(&breathing_table[index]) * 0x0101U)));
  1413. }
  1414. #endif // BACKLIGHT_BREATHING
  1415. __attribute__ ((weak))
  1416. void backlight_init_ports(void)
  1417. {
  1418. // Setup backlight pin as output and output to on state.
  1419. FOR_EACH_LED(
  1420. setPinOutput(backlight_pin);
  1421. backlight_on(backlight_pin);
  1422. )
  1423. // I could write a wall of text here to explain... but TL;DW
  1424. // Go read the ATmega32u4 datasheet.
  1425. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  1426. #ifdef BACKLIGHT_PWM_TIMER
  1427. // TimerX setup, Fast PWM mode count to TOP set in ICRx
  1428. TCCRxA = _BV(WGM11); // = 0b00000010;
  1429. // clock select clk/1
  1430. TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  1431. #else // hardware PWM
  1432. // Pin PB7 = OCR1C (Timer 1, Channel C)
  1433. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  1434. // (i.e. start high, go low when counter matches.)
  1435. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  1436. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  1437. /*
  1438. 14.8.3:
  1439. "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 [..]."
  1440. "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)."
  1441. */
  1442. TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010;
  1443. TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  1444. #endif
  1445. // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0.
  1446. ICRx = TIMER_TOP;
  1447. backlight_init();
  1448. #ifdef BACKLIGHT_BREATHING
  1449. if (is_backlight_breathing()) {
  1450. breathing_enable();
  1451. }
  1452. #endif
  1453. }
  1454. #endif // hardware backlight
  1455. #else // no backlight
  1456. __attribute__ ((weak))
  1457. void backlight_init_ports(void) {}
  1458. __attribute__ ((weak))
  1459. void backlight_set(uint8_t level) {}
  1460. #endif // backlight
  1461. #ifdef HD44780_ENABLED
  1462. #include "hd44780.h"
  1463. #endif
  1464. // Functions for spitting out values
  1465. //
  1466. void send_dword(uint32_t number) { // this might not actually work
  1467. uint16_t word = (number >> 16);
  1468. send_word(word);
  1469. send_word(number & 0xFFFFUL);
  1470. }
  1471. void send_word(uint16_t number) {
  1472. uint8_t byte = number >> 8;
  1473. send_byte(byte);
  1474. send_byte(number & 0xFF);
  1475. }
  1476. void send_byte(uint8_t number) {
  1477. uint8_t nibble = number >> 4;
  1478. send_nibble(nibble);
  1479. send_nibble(number & 0xF);
  1480. }
  1481. void send_nibble(uint8_t number) {
  1482. switch (number) {
  1483. case 0:
  1484. register_code(KC_0);
  1485. unregister_code(KC_0);
  1486. break;
  1487. case 1 ... 9:
  1488. register_code(KC_1 + (number - 1));
  1489. unregister_code(KC_1 + (number - 1));
  1490. break;
  1491. case 0xA ... 0xF:
  1492. register_code(KC_A + (number - 0xA));
  1493. unregister_code(KC_A + (number - 0xA));
  1494. break;
  1495. }
  1496. }
  1497. __attribute__((weak))
  1498. uint16_t hex_to_keycode(uint8_t hex)
  1499. {
  1500. hex = hex & 0xF;
  1501. if (hex == 0x0) {
  1502. return KC_0;
  1503. } else if (hex < 0xA) {
  1504. return KC_1 + (hex - 0x1);
  1505. } else {
  1506. return KC_A + (hex - 0xA);
  1507. }
  1508. }
  1509. void api_send_unicode(uint32_t unicode) {
  1510. #ifdef API_ENABLE
  1511. uint8_t chunk[4];
  1512. dword_to_bytes(unicode, chunk);
  1513. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  1514. #endif
  1515. }
  1516. __attribute__ ((weak))
  1517. void led_set_user(uint8_t usb_led) {
  1518. }
  1519. __attribute__ ((weak))
  1520. void led_set_kb(uint8_t usb_led) {
  1521. led_set_user(usb_led);
  1522. }
  1523. __attribute__ ((weak))
  1524. void led_init_ports(void)
  1525. {
  1526. }
  1527. __attribute__ ((weak))
  1528. void led_set(uint8_t usb_led)
  1529. {
  1530. #if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
  1531. // Use backlight as Caps Lock indicator
  1532. uint8_t bl_toggle_lvl = 0;
  1533. if (IS_LED_ON(usb_led, USB_LED_CAPS_LOCK) && !backlight_config.enable) {
  1534. // Turning Caps Lock ON and backlight is disabled in config
  1535. // Toggling backlight to the brightest level
  1536. bl_toggle_lvl = BACKLIGHT_LEVELS;
  1537. } else if (IS_LED_OFF(usb_led, USB_LED_CAPS_LOCK) && backlight_config.enable) {
  1538. // Turning Caps Lock OFF and backlight is enabled in config
  1539. // Toggling backlight and restoring config level
  1540. bl_toggle_lvl = backlight_config.level;
  1541. }
  1542. // Set level without modify backlight_config to keep ability to restore state
  1543. backlight_set(bl_toggle_lvl);
  1544. #endif
  1545. led_set_kb(usb_led);
  1546. }
  1547. //------------------------------------------------------------------------------
  1548. // Override these functions in your keymap file to play different tunes on
  1549. // different events such as startup and bootloader jump
  1550. __attribute__ ((weak))
  1551. void startup_user() {}
  1552. __attribute__ ((weak))
  1553. void shutdown_user() {}
  1554. //------------------------------------------------------------------------------