quantum.c 25 KB

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