quantum.c 24 KB

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