quantum.c 23 KB

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