quantum.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /* Copyright 2016-2017 Jack Humbert
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "quantum.h"
  17. #ifdef PROTOCOL_LUFA
  18. # include "outputselect.h"
  19. #endif
  20. #ifdef BACKLIGHT_ENABLE
  21. # include "backlight.h"
  22. extern backlight_config_t backlight_config;
  23. #endif
  24. #ifdef FAUXCLICKY_ENABLE
  25. # include "fauxclicky.h"
  26. #endif
  27. #ifdef API_ENABLE
  28. # include "api.h"
  29. #endif
  30. #ifdef MIDI_ENABLE
  31. # include "process_midi.h"
  32. #endif
  33. #ifdef VELOCIKEY_ENABLE
  34. # include "velocikey.h"
  35. #endif
  36. #ifdef HAPTIC_ENABLE
  37. # include "haptic.h"
  38. #endif
  39. #ifdef ENCODER_ENABLE
  40. # include "encoder.h"
  41. #endif
  42. #ifdef AUDIO_ENABLE
  43. # ifndef GOODBYE_SONG
  44. # define GOODBYE_SONG SONG(GOODBYE_SOUND)
  45. # endif
  46. float goodbye_song[][2] = GOODBYE_SONG;
  47. # ifdef DEFAULT_LAYER_SONGS
  48. float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
  49. # endif
  50. # ifdef SENDSTRING_BELL
  51. float bell_song[][2] = SONG(TERMINAL_SOUND);
  52. # endif
  53. #endif
  54. static void do_code16(uint16_t code, void (*f)(uint8_t)) {
  55. switch (code) {
  56. case QK_MODS ... QK_MODS_MAX:
  57. break;
  58. default:
  59. return;
  60. }
  61. uint8_t mods_to_send = 0;
  62. if (code & QK_RMODS_MIN) { // Right mod flag is set
  63. if (code & QK_LCTL) mods_to_send |= MOD_BIT(KC_RCTL);
  64. if (code & QK_LSFT) mods_to_send |= MOD_BIT(KC_RSFT);
  65. if (code & QK_LALT) mods_to_send |= MOD_BIT(KC_RALT);
  66. if (code & QK_LGUI) mods_to_send |= MOD_BIT(KC_RGUI);
  67. } else {
  68. if (code & QK_LCTL) mods_to_send |= MOD_BIT(KC_LCTL);
  69. if (code & QK_LSFT) mods_to_send |= MOD_BIT(KC_LSFT);
  70. if (code & QK_LALT) mods_to_send |= MOD_BIT(KC_LALT);
  71. if (code & QK_LGUI) mods_to_send |= MOD_BIT(KC_LGUI);
  72. }
  73. f(mods_to_send);
  74. }
  75. void register_code16(uint16_t code) {
  76. if (IS_MOD(code) || code == KC_NO) {
  77. do_code16(code, register_mods);
  78. } else {
  79. do_code16(code, register_weak_mods);
  80. }
  81. register_code(code);
  82. }
  83. void unregister_code16(uint16_t code) {
  84. unregister_code(code);
  85. if (IS_MOD(code) || code == KC_NO) {
  86. do_code16(code, unregister_mods);
  87. } else {
  88. do_code16(code, unregister_weak_mods);
  89. }
  90. }
  91. void tap_code16(uint16_t code) {
  92. register_code16(code);
  93. #if TAP_CODE_DELAY > 0
  94. wait_ms(TAP_CODE_DELAY);
  95. #endif
  96. unregister_code16(code);
  97. }
  98. __attribute__((weak)) bool process_action_kb(keyrecord_t *record) { return true; }
  99. __attribute__((weak)) bool process_record_kb(uint16_t keycode, keyrecord_t *record) { return process_record_user(keycode, record); }
  100. __attribute__((weak)) bool process_record_user(uint16_t keycode, keyrecord_t *record) { return true; }
  101. void reset_keyboard(void) {
  102. clear_keyboard();
  103. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  104. process_midi_all_notes_off();
  105. #endif
  106. #ifdef AUDIO_ENABLE
  107. # ifndef NO_MUSIC_MODE
  108. music_all_notes_off();
  109. # endif
  110. uint16_t timer_start = timer_read();
  111. PLAY_SONG(goodbye_song);
  112. shutdown_user();
  113. while (timer_elapsed(timer_start) < 250) wait_ms(1);
  114. stop_all_notes();
  115. #else
  116. shutdown_user();
  117. wait_ms(250);
  118. #endif
  119. #ifdef HAPTIC_ENABLE
  120. haptic_shutdown();
  121. #endif
  122. // this is also done later in bootloader.c - not sure if it's neccesary here
  123. #ifdef BOOTLOADER_CATERINA
  124. *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
  125. #endif
  126. bootloader_jump();
  127. }
  128. /* Convert record into usable keycode via the contained event. */
  129. uint16_t get_record_keycode(keyrecord_t *record) { return get_event_keycode(record->event); }
  130. /* Convert event into usable keycode. Checks the layer cache to ensure that it
  131. * retains the correct keycode after a layer change, if the key is still pressed.
  132. */
  133. uint16_t get_event_keycode(keyevent_t event) {
  134. #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
  135. /* TODO: Use store_or_get_action() or a similar function. */
  136. if (!disable_action_cache) {
  137. uint8_t layer;
  138. if (event.pressed) {
  139. layer = layer_switch_get_layer(event.key);
  140. update_source_layers_cache(event.key, layer);
  141. } else {
  142. layer = read_source_layers_cache(event.key);
  143. }
  144. return keymap_key_to_keycode(layer, event.key);
  145. } else
  146. #endif
  147. return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key);
  148. }
  149. /* Main keycode processing function. Hands off handling to other functions,
  150. * then processes internal Quantum keycodes, then processes ACTIONs.
  151. */
  152. bool process_record_quantum(keyrecord_t *record) {
  153. uint16_t keycode = get_record_keycode(record);
  154. // This is how you use actions here
  155. // if (keycode == KC_LEAD) {
  156. // action_t action;
  157. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  158. // process_action(record, action);
  159. // return false;
  160. // }
  161. #ifdef VELOCIKEY_ENABLE
  162. if (velocikey_enabled() && record->event.pressed) {
  163. velocikey_accelerate();
  164. }
  165. #endif
  166. #ifdef TAP_DANCE_ENABLE
  167. preprocess_tap_dance(keycode, record);
  168. #endif
  169. if (!(
  170. #if defined(KEY_LOCK_ENABLE)
  171. // Must run first to be able to mask key_up events.
  172. process_key_lock(&keycode, record) &&
  173. #endif
  174. #if defined(DYNAMIC_MACRO_ENABLE) && !defined(DYNAMIC_MACRO_USER_CALL)
  175. // Must run asap to ensure all keypresses are recorded.
  176. process_dynamic_macro(keycode, record) &&
  177. #endif
  178. #if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
  179. process_clicky(keycode, record) &&
  180. #endif // AUDIO_CLICKY
  181. #ifdef HAPTIC_ENABLE
  182. process_haptic(keycode, record) &&
  183. #endif // HAPTIC_ENABLE
  184. #if defined(RGB_MATRIX_ENABLE)
  185. process_rgb_matrix(keycode, record) &&
  186. #endif
  187. #if defined(VIA_ENABLE)
  188. process_record_via(keycode, record) &&
  189. #endif
  190. process_record_kb(keycode, record) &&
  191. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  192. process_midi(keycode, record) &&
  193. #endif
  194. #ifdef AUDIO_ENABLE
  195. process_audio(keycode, record) &&
  196. #endif
  197. #ifdef STENO_ENABLE
  198. process_steno(keycode, record) &&
  199. #endif
  200. #if (defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)
  201. process_music(keycode, record) &&
  202. #endif
  203. #ifdef TAP_DANCE_ENABLE
  204. process_tap_dance(keycode, record) &&
  205. #endif
  206. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  207. process_unicode_common(keycode, record) &&
  208. #endif
  209. #ifdef LEADER_ENABLE
  210. process_leader(keycode, record) &&
  211. #endif
  212. #ifdef COMBO_ENABLE
  213. process_combo(keycode, record) &&
  214. #endif
  215. #ifdef PRINTING_ENABLE
  216. process_printer(keycode, record) &&
  217. #endif
  218. #ifdef AUTO_SHIFT_ENABLE
  219. process_auto_shift(keycode, record) &&
  220. #endif
  221. #ifdef TERMINAL_ENABLE
  222. process_terminal(keycode, record) &&
  223. #endif
  224. #ifdef SPACE_CADET_ENABLE
  225. process_space_cadet(keycode, record) &&
  226. #endif
  227. #ifdef MAGIC_KEYCODE_ENABLE
  228. process_magic(keycode, record) &&
  229. #endif
  230. #ifdef GRAVE_ESC_ENABLE
  231. process_grave_esc(keycode, record) &&
  232. #endif
  233. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  234. process_rgb(keycode, record) &&
  235. #endif
  236. true)) {
  237. return false;
  238. }
  239. if (record->event.pressed) {
  240. switch (keycode) {
  241. case RESET:
  242. reset_keyboard();
  243. return false;
  244. #ifndef NO_DEBUG
  245. case DEBUG:
  246. debug_enable ^= 1;
  247. if (debug_enable) {
  248. print("DEBUG: enabled.\n");
  249. } else {
  250. print("DEBUG: disabled.\n");
  251. }
  252. #endif
  253. return false;
  254. case EEPROM_RESET:
  255. eeconfig_init();
  256. return false;
  257. #ifdef FAUXCLICKY_ENABLE
  258. case FC_TOG:
  259. FAUXCLICKY_TOGGLE;
  260. return false;
  261. case FC_ON:
  262. FAUXCLICKY_ON;
  263. return false;
  264. case FC_OFF:
  265. FAUXCLICKY_OFF;
  266. return false;
  267. #endif
  268. #ifdef VELOCIKEY_ENABLE
  269. case VLK_TOG:
  270. velocikey_toggle();
  271. return false;
  272. #endif
  273. #ifdef BLUETOOTH_ENABLE
  274. case OUT_AUTO:
  275. set_output(OUTPUT_AUTO);
  276. return false;
  277. case OUT_USB:
  278. set_output(OUTPUT_USB);
  279. return false;
  280. case OUT_BT:
  281. set_output(OUTPUT_BLUETOOTH);
  282. return false;
  283. #endif
  284. #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_BREATHING)
  285. case BL_BRTG:
  286. backlight_toggle_breathing();
  287. return false;
  288. #endif
  289. }
  290. }
  291. return process_action_kb(record);
  292. }
  293. __attribute__((weak)) const bool ascii_to_shift_lut[128] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  294. 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0};
  295. __attribute__((weak)) const bool ascii_to_altgr_lut[128] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  296. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  297. // clang-format off
  298. __attribute__((weak)) const uint8_t ascii_to_keycode_lut[128] PROGMEM = {// NUL SOH STX ETX EOT ENQ ACK BEL
  299. XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  300. // BS TAB LF VT FF CR SO SI
  301. KC_BSPC, KC_TAB, KC_ENT, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  302. // DLE DC1 DC2 DC3 DC4 NAK SYN ETB
  303. XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  304. // CAN EM SUB ESC FS GS RS US
  305. XXXXXXX, XXXXXXX, XXXXXXX, KC_ESC, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
  306. // ! " # $ % & '
  307. KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
  308. // ( ) * + , - . /
  309. KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
  310. // 0 1 2 3 4 5 6 7
  311. KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
  312. // 8 9 : ; < = > ?
  313. KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
  314. // @ A B C D E F G
  315. KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  316. // H I J K L M N O
  317. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  318. // P Q R S T U V W
  319. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  320. // X Y Z [ \ ] ^ _
  321. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
  322. // ` a b c d e f g
  323. KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
  324. // h i j k l m n o
  325. KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
  326. // p q r s t u v w
  327. KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
  328. // x y z { | } ~ DEL
  329. KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL};
  330. // clang-format on
  331. void send_string(const char *str) { send_string_with_delay(str, 0); }
  332. void send_string_P(const char *str) { send_string_with_delay_P(str, 0); }
  333. void send_string_with_delay(const char *str, uint8_t interval) {
  334. while (1) {
  335. char ascii_code = *str;
  336. if (!ascii_code) break;
  337. if (ascii_code == SS_TAP_CODE) {
  338. // tap
  339. uint8_t keycode = *(++str);
  340. register_code(keycode);
  341. unregister_code(keycode);
  342. } else if (ascii_code == SS_DOWN_CODE) {
  343. // down
  344. uint8_t keycode = *(++str);
  345. register_code(keycode);
  346. } else if (ascii_code == SS_UP_CODE) {
  347. // up
  348. uint8_t keycode = *(++str);
  349. unregister_code(keycode);
  350. } else {
  351. send_char(ascii_code);
  352. }
  353. ++str;
  354. // interval
  355. {
  356. uint8_t ms = interval;
  357. while (ms--) wait_ms(1);
  358. }
  359. }
  360. }
  361. void send_string_with_delay_P(const char *str, uint8_t interval) {
  362. while (1) {
  363. char ascii_code = pgm_read_byte(str);
  364. if (!ascii_code) break;
  365. if (ascii_code == SS_TAP_CODE) {
  366. // tap
  367. uint8_t keycode = pgm_read_byte(++str);
  368. register_code(keycode);
  369. unregister_code(keycode);
  370. } else if (ascii_code == SS_DOWN_CODE) {
  371. // down
  372. uint8_t keycode = pgm_read_byte(++str);
  373. register_code(keycode);
  374. } else if (ascii_code == SS_UP_CODE) {
  375. // up
  376. uint8_t keycode = pgm_read_byte(++str);
  377. unregister_code(keycode);
  378. } else {
  379. send_char(ascii_code);
  380. }
  381. ++str;
  382. // interval
  383. {
  384. uint8_t ms = interval;
  385. while (ms--) wait_ms(1);
  386. }
  387. }
  388. }
  389. void send_char(char ascii_code) {
  390. #if defined(AUDIO_ENABLE) && defined(SENDSTRING_BELL)
  391. if (ascii_code == '\a') { // BEL
  392. PLAY_SONG(bell_song);
  393. return;
  394. }
  395. #endif
  396. uint8_t keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
  397. bool is_shifted = pgm_read_byte(&ascii_to_shift_lut[(uint8_t)ascii_code]);
  398. bool is_altgred = pgm_read_byte(&ascii_to_altgr_lut[(uint8_t)ascii_code]);
  399. if (is_shifted) {
  400. register_code(KC_LSFT);
  401. }
  402. if (is_altgred) {
  403. register_code(KC_RALT);
  404. }
  405. tap_code(keycode);
  406. if (is_altgred) {
  407. unregister_code(KC_RALT);
  408. }
  409. if (is_shifted) {
  410. unregister_code(KC_LSFT);
  411. }
  412. }
  413. void set_single_persistent_default_layer(uint8_t default_layer) {
  414. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  415. PLAY_SONG(default_layer_songs[default_layer]);
  416. #endif
  417. eeconfig_update_default_layer(1U << default_layer);
  418. default_layer_set(1U << default_layer);
  419. }
  420. layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  421. layer_state_t mask12 = (1UL << layer1) | (1UL << layer2);
  422. layer_state_t mask3 = 1UL << layer3;
  423. return (state & mask12) == mask12 ? (state | mask3) : (state & ~mask3);
  424. }
  425. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) { layer_state_set(update_tri_layer_state(layer_state, layer1, layer2, layer3)); }
  426. void tap_random_base64(void) {
  427. #if defined(__AVR_ATmega32U4__)
  428. uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
  429. #else
  430. uint8_t key = rand() % 64;
  431. #endif
  432. switch (key) {
  433. case 0 ... 25:
  434. register_code(KC_LSFT);
  435. register_code(key + KC_A);
  436. unregister_code(key + KC_A);
  437. unregister_code(KC_LSFT);
  438. break;
  439. case 26 ... 51:
  440. register_code(key - 26 + KC_A);
  441. unregister_code(key - 26 + KC_A);
  442. break;
  443. case 52:
  444. register_code(KC_0);
  445. unregister_code(KC_0);
  446. break;
  447. case 53 ... 61:
  448. register_code(key - 53 + KC_1);
  449. unregister_code(key - 53 + KC_1);
  450. break;
  451. case 62:
  452. register_code(KC_LSFT);
  453. register_code(KC_EQL);
  454. unregister_code(KC_EQL);
  455. unregister_code(KC_LSFT);
  456. break;
  457. case 63:
  458. register_code(KC_SLSH);
  459. unregister_code(KC_SLSH);
  460. break;
  461. }
  462. }
  463. __attribute__((weak)) void bootmagic_lite(void) {
  464. // The lite version of TMK's bootmagic based on Wilba.
  465. // 100% less potential for accidentally making the
  466. // keyboard do stupid things.
  467. // We need multiple scans because debouncing can't be turned off.
  468. matrix_scan();
  469. #if defined(DEBOUNCE) && DEBOUNCE > 0
  470. wait_ms(DEBOUNCE * 2);
  471. #else
  472. wait_ms(30);
  473. #endif
  474. matrix_scan();
  475. // If the Esc and space bar are held down on power up,
  476. // reset the EEPROM valid state and jump to bootloader.
  477. // Assumes Esc is at [0,0].
  478. // This isn't very generalized, but we need something that doesn't
  479. // rely on user's keymaps in firmware or EEPROM.
  480. if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
  481. eeconfig_disable();
  482. // Jump to bootloader.
  483. bootloader_jump();
  484. }
  485. }
  486. void matrix_init_quantum() {
  487. #ifdef BOOTMAGIC_LITE
  488. bootmagic_lite();
  489. #endif
  490. if (!eeconfig_is_enabled()) {
  491. eeconfig_init();
  492. }
  493. #ifdef BACKLIGHT_ENABLE
  494. # ifdef LED_MATRIX_ENABLE
  495. led_matrix_init();
  496. # else
  497. backlight_init_ports();
  498. # endif
  499. #endif
  500. #ifdef AUDIO_ENABLE
  501. audio_init();
  502. #endif
  503. #ifdef RGB_MATRIX_ENABLE
  504. rgb_matrix_init();
  505. #endif
  506. #ifdef ENCODER_ENABLE
  507. encoder_init();
  508. #endif
  509. #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
  510. unicode_input_mode_init();
  511. #endif
  512. #ifdef HAPTIC_ENABLE
  513. haptic_init();
  514. #endif
  515. #ifdef OUTPUT_AUTO_ENABLE
  516. set_output(OUTPUT_AUTO);
  517. #endif
  518. #ifdef DIP_SWITCH_ENABLE
  519. dip_switch_init();
  520. #endif
  521. matrix_init_kb();
  522. }
  523. void matrix_scan_quantum() {
  524. #if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE)
  525. matrix_scan_music();
  526. #endif
  527. #ifdef TAP_DANCE_ENABLE
  528. matrix_scan_tap_dance();
  529. #endif
  530. #ifdef COMBO_ENABLE
  531. matrix_scan_combo();
  532. #endif
  533. #ifdef LED_MATRIX_ENABLE
  534. led_matrix_task();
  535. #endif
  536. #ifdef RGB_MATRIX_ENABLE
  537. rgb_matrix_task();
  538. #endif
  539. #ifdef ENCODER_ENABLE
  540. encoder_read();
  541. #endif
  542. #ifdef HAPTIC_ENABLE
  543. haptic_task();
  544. #endif
  545. #ifdef DIP_SWITCH_ENABLE
  546. dip_switch_read(false);
  547. #endif
  548. matrix_scan_kb();
  549. }
  550. #ifdef HD44780_ENABLED
  551. # include "hd44780.h"
  552. #endif
  553. // Functions for spitting out values
  554. //
  555. void send_dword(uint32_t number) { // this might not actually work
  556. uint16_t word = (number >> 16);
  557. send_word(word);
  558. send_word(number & 0xFFFFUL);
  559. }
  560. void send_word(uint16_t number) {
  561. uint8_t byte = number >> 8;
  562. send_byte(byte);
  563. send_byte(number & 0xFF);
  564. }
  565. void send_byte(uint8_t number) {
  566. uint8_t nibble = number >> 4;
  567. send_nibble(nibble);
  568. send_nibble(number & 0xF);
  569. }
  570. void send_nibble(uint8_t number) {
  571. switch (number) {
  572. case 0:
  573. register_code(KC_0);
  574. unregister_code(KC_0);
  575. break;
  576. case 1 ... 9:
  577. register_code(KC_1 + (number - 1));
  578. unregister_code(KC_1 + (number - 1));
  579. break;
  580. case 0xA ... 0xF:
  581. register_code(KC_A + (number - 0xA));
  582. unregister_code(KC_A + (number - 0xA));
  583. break;
  584. }
  585. }
  586. __attribute__((weak)) uint16_t hex_to_keycode(uint8_t hex) {
  587. hex = hex & 0xF;
  588. if (hex == 0x0) {
  589. return KC_0;
  590. } else if (hex < 0xA) {
  591. return KC_1 + (hex - 0x1);
  592. } else {
  593. return KC_A + (hex - 0xA);
  594. }
  595. }
  596. void api_send_unicode(uint32_t unicode) {
  597. #ifdef API_ENABLE
  598. uint8_t chunk[4];
  599. dword_to_bytes(unicode, chunk);
  600. MT_SEND_DATA(DT_UNICODE, chunk, 5);
  601. #endif
  602. }
  603. /** \brief Lock LED set callback - keymap/user level
  604. *
  605. * \deprecated Use led_update_user() instead.
  606. */
  607. __attribute__((weak)) void led_set_user(uint8_t usb_led) {}
  608. /** \brief Lock LED set callback - keyboard level
  609. *
  610. * \deprecated Use led_update_kb() instead.
  611. */
  612. __attribute__((weak)) void led_set_kb(uint8_t usb_led) { led_set_user(usb_led); }
  613. /** \brief Lock LED update callback - keymap/user level
  614. *
  615. * \return True if led_update_kb() should run its own code, false otherwise.
  616. */
  617. __attribute__((weak)) bool led_update_user(led_t led_state) { return true; }
  618. /** \brief Lock LED update callback - keyboard level
  619. *
  620. * \return Ignored for now.
  621. */
  622. __attribute__((weak)) bool led_update_kb(led_t led_state) { return led_update_user(led_state); }
  623. __attribute__((weak)) void led_init_ports(void) {}
  624. __attribute__((weak)) void led_set(uint8_t usb_led) {
  625. #if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
  626. // Use backlight as Caps Lock indicator
  627. uint8_t bl_toggle_lvl = 0;
  628. if (IS_LED_ON(usb_led, USB_LED_CAPS_LOCK) && !backlight_config.enable) {
  629. // Turning Caps Lock ON and backlight is disabled in config
  630. // Toggling backlight to the brightest level
  631. bl_toggle_lvl = BACKLIGHT_LEVELS;
  632. } else if (IS_LED_OFF(usb_led, USB_LED_CAPS_LOCK) && backlight_config.enable) {
  633. // Turning Caps Lock OFF and backlight is enabled in config
  634. // Toggling backlight and restoring config level
  635. bl_toggle_lvl = backlight_config.level;
  636. }
  637. // Set level without modify backlight_config to keep ability to restore state
  638. backlight_set(bl_toggle_lvl);
  639. #endif
  640. led_set_kb(usb_led);
  641. led_update_kb((led_t)usb_led);
  642. }
  643. //------------------------------------------------------------------------------
  644. // Override these functions in your keymap file to play different tunes on
  645. // different events such as startup and bootloader jump
  646. __attribute__((weak)) void startup_user() {}
  647. __attribute__((weak)) void shutdown_user() {}
  648. //------------------------------------------------------------------------------