quantum.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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 BACKLIGHT_ENABLE
  18. # include "process_backlight.h"
  19. #endif
  20. #ifdef BLUETOOTH_ENABLE
  21. # include "process_connection.h"
  22. #endif
  23. #ifdef GRAVE_ESC_ENABLE
  24. # include "process_grave_esc.h"
  25. #endif
  26. #ifdef HAPTIC_ENABLE
  27. # include "process_haptic.h"
  28. #endif
  29. #ifdef JOYSTICK_ENABLE
  30. # include "process_joystick.h"
  31. #endif
  32. #ifdef LEADER_ENABLE
  33. # include "process_leader.h"
  34. #endif
  35. #ifdef LED_MATRIX_ENABLE
  36. # include "process_led_matrix.h"
  37. #endif
  38. #ifdef MAGIC_ENABLE
  39. # include "process_magic.h"
  40. #endif
  41. #ifdef MIDI_ENABLE
  42. # include "process_midi.h"
  43. #endif
  44. #ifdef PROGRAMMABLE_BUTTON_ENABLE
  45. # include "process_programmable_button.h"
  46. #endif
  47. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  48. # include "process_rgb.h"
  49. #endif
  50. #ifdef SECURE_ENABLE
  51. # include "process_secure.h"
  52. #endif
  53. #ifdef TRI_LAYER_ENABLE
  54. # include "process_tri_layer.h"
  55. #endif
  56. #ifdef UNICODE_COMMON_ENABLE
  57. # include "process_unicode_common.h"
  58. #endif
  59. #ifdef AUDIO_ENABLE
  60. # ifndef GOODBYE_SONG
  61. # define GOODBYE_SONG SONG(GOODBYE_SOUND)
  62. # endif
  63. float goodbye_song[][2] = GOODBYE_SONG;
  64. # ifdef DEFAULT_LAYER_SONGS
  65. float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
  66. # endif
  67. #endif
  68. uint8_t extract_mod_bits(uint16_t code) {
  69. switch (code) {
  70. case QK_MODS ... QK_MODS_MAX:
  71. break;
  72. default:
  73. return 0;
  74. }
  75. uint8_t mods_to_send = 0;
  76. if (code & QK_RMODS_MIN) { // Right mod flag is set
  77. if (code & QK_LCTL) mods_to_send |= MOD_BIT(KC_RIGHT_CTRL);
  78. if (code & QK_LSFT) mods_to_send |= MOD_BIT(KC_RIGHT_SHIFT);
  79. if (code & QK_LALT) mods_to_send |= MOD_BIT(KC_RIGHT_ALT);
  80. if (code & QK_LGUI) mods_to_send |= MOD_BIT(KC_RIGHT_GUI);
  81. } else {
  82. if (code & QK_LCTL) mods_to_send |= MOD_BIT(KC_LEFT_CTRL);
  83. if (code & QK_LSFT) mods_to_send |= MOD_BIT(KC_LEFT_SHIFT);
  84. if (code & QK_LALT) mods_to_send |= MOD_BIT(KC_LEFT_ALT);
  85. if (code & QK_LGUI) mods_to_send |= MOD_BIT(KC_LEFT_GUI);
  86. }
  87. return mods_to_send;
  88. }
  89. void do_code16(uint16_t code, void (*f)(uint8_t)) {
  90. f(extract_mod_bits(code));
  91. }
  92. __attribute__((weak)) void register_code16(uint16_t code) {
  93. if (IS_MODIFIER_KEYCODE(code) || code == KC_NO) {
  94. do_code16(code, register_mods);
  95. } else {
  96. do_code16(code, register_weak_mods);
  97. }
  98. register_code(code);
  99. }
  100. __attribute__((weak)) void unregister_code16(uint16_t code) {
  101. unregister_code(code);
  102. if (IS_MODIFIER_KEYCODE(code) || code == KC_NO) {
  103. do_code16(code, unregister_mods);
  104. } else {
  105. do_code16(code, unregister_weak_mods);
  106. }
  107. }
  108. /** \brief Tap a keycode with a delay.
  109. *
  110. * \param code The modded keycode to tap.
  111. * \param delay The amount of time in milliseconds to leave the keycode registered, before unregistering it.
  112. */
  113. __attribute__((weak)) void tap_code16_delay(uint16_t code, uint16_t delay) {
  114. register_code16(code);
  115. for (uint16_t i = delay; i > 0; i--) {
  116. wait_ms(1);
  117. }
  118. unregister_code16(code);
  119. }
  120. /** \brief Tap a keycode with the default delay.
  121. *
  122. * \param code The modded keycode to tap. If `code` is `KC_CAPS_LOCK`, the delay will be `TAP_HOLD_CAPS_DELAY`, otherwise `TAP_CODE_DELAY`, if defined.
  123. */
  124. __attribute__((weak)) void tap_code16(uint16_t code) {
  125. tap_code16_delay(code, code == KC_CAPS_LOCK ? TAP_HOLD_CAPS_DELAY : TAP_CODE_DELAY);
  126. }
  127. __attribute__((weak)) bool pre_process_record_kb(uint16_t keycode, keyrecord_t *record) {
  128. return pre_process_record_user(keycode, record);
  129. }
  130. __attribute__((weak)) bool pre_process_record_user(uint16_t keycode, keyrecord_t *record) {
  131. return true;
  132. }
  133. __attribute__((weak)) bool process_action_kb(keyrecord_t *record) {
  134. return true;
  135. }
  136. __attribute__((weak)) bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  137. return process_record_user(keycode, record);
  138. }
  139. __attribute__((weak)) bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  140. return true;
  141. }
  142. __attribute__((weak)) void post_process_record_kb(uint16_t keycode, keyrecord_t *record) {
  143. post_process_record_user(keycode, record);
  144. }
  145. __attribute__((weak)) void post_process_record_user(uint16_t keycode, keyrecord_t *record) {}
  146. void shutdown_quantum(bool jump_to_bootloader) {
  147. clear_keyboard();
  148. #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
  149. process_midi_all_notes_off();
  150. #endif
  151. #ifdef AUDIO_ENABLE
  152. # ifndef NO_MUSIC_MODE
  153. music_all_notes_off();
  154. # endif
  155. uint16_t timer_start = timer_read();
  156. PLAY_SONG(goodbye_song);
  157. shutdown_kb(jump_to_bootloader);
  158. while (timer_elapsed(timer_start) < 250)
  159. wait_ms(1);
  160. stop_all_notes();
  161. #else
  162. shutdown_kb(jump_to_bootloader);
  163. wait_ms(250);
  164. #endif
  165. #ifdef HAPTIC_ENABLE
  166. haptic_shutdown();
  167. #endif
  168. }
  169. void reset_keyboard(void) {
  170. shutdown_quantum(true);
  171. bootloader_jump();
  172. }
  173. void soft_reset_keyboard(void) {
  174. shutdown_quantum(false);
  175. mcu_reset();
  176. }
  177. /* Convert record into usable keycode via the contained event. */
  178. uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache) {
  179. #if defined(COMBO_ENABLE) || defined(REPEAT_KEY_ENABLE)
  180. if (record->keycode) {
  181. return record->keycode;
  182. }
  183. #endif
  184. return get_event_keycode(record->event, update_layer_cache);
  185. }
  186. /* Convert event into usable keycode. Checks the layer cache to ensure that it
  187. * retains the correct keycode after a layer change, if the key is still pressed.
  188. * "update_layer_cache" is to ensure that it only updates the layer cache when
  189. * appropriate, otherwise, it will update it and cause layer tap (and other keys)
  190. * from triggering properly.
  191. */
  192. uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache) {
  193. #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
  194. /* TODO: Use store_or_get_action() or a similar function. */
  195. if (!disable_action_cache) {
  196. uint8_t layer;
  197. if (event.pressed && update_layer_cache) {
  198. layer = layer_switch_get_layer(event.key);
  199. update_source_layers_cache(event.key, layer);
  200. } else {
  201. layer = read_source_layers_cache(event.key);
  202. }
  203. return keymap_key_to_keycode(layer, event.key);
  204. } else
  205. #endif
  206. return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key);
  207. }
  208. /* Get keycode, and then process pre tapping functionality */
  209. bool pre_process_record_quantum(keyrecord_t *record) {
  210. return pre_process_record_kb(get_record_keycode(record, true), record) &&
  211. #ifdef COMBO_ENABLE
  212. process_combo(get_record_keycode(record, true), record) &&
  213. #endif
  214. true;
  215. }
  216. /* Get keycode, and then call keyboard function */
  217. void post_process_record_quantum(keyrecord_t *record) {
  218. uint16_t keycode = get_record_keycode(record, false);
  219. post_process_record_kb(keycode, record);
  220. }
  221. /* Core keycode function, hands off handling to other functions,
  222. then processes internal quantum keycodes, and then processes
  223. ACTIONs. */
  224. bool process_record_quantum(keyrecord_t *record) {
  225. uint16_t keycode = get_record_keycode(record, true);
  226. // This is how you use actions here
  227. // if (keycode == QK_LEADER) {
  228. // action_t action;
  229. // action.code = ACTION_DEFAULT_LAYER_SET(0);
  230. // process_action(record, action);
  231. // return false;
  232. // }
  233. #if defined(SECURE_ENABLE)
  234. if (!preprocess_secure(keycode, record)) {
  235. return false;
  236. }
  237. #endif
  238. #ifdef TAP_DANCE_ENABLE
  239. if (preprocess_tap_dance(keycode, record)) {
  240. // The tap dance might have updated the layer state, therefore the
  241. // result of the keycode lookup might change.
  242. keycode = get_record_keycode(record, true);
  243. }
  244. #endif
  245. #ifdef RGBLIGHT_ENABLE
  246. if (record->event.pressed) {
  247. preprocess_rgblight();
  248. }
  249. #endif
  250. #ifdef WPM_ENABLE
  251. if (record->event.pressed) {
  252. update_wpm(keycode);
  253. }
  254. #endif
  255. if (!(
  256. #if defined(KEY_LOCK_ENABLE)
  257. // Must run first to be able to mask key_up events.
  258. process_key_lock(&keycode, record) &&
  259. #endif
  260. #if defined(DYNAMIC_MACRO_ENABLE) && !defined(DYNAMIC_MACRO_USER_CALL)
  261. // Must run asap to ensure all keypresses are recorded.
  262. process_dynamic_macro(keycode, record) &&
  263. #endif
  264. #ifdef REPEAT_KEY_ENABLE
  265. process_last_key(keycode, record) && process_repeat_key(keycode, record) &&
  266. #endif
  267. #if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
  268. process_clicky(keycode, record) &&
  269. #endif
  270. #ifdef HAPTIC_ENABLE
  271. process_haptic(keycode, record) &&
  272. #endif
  273. #if defined(VIA_ENABLE)
  274. process_record_via(keycode, record) &&
  275. #endif
  276. #if defined(POINTING_DEVICE_ENABLE) && defined(POINTING_DEVICE_AUTO_MOUSE_ENABLE)
  277. process_auto_mouse(keycode, record) &&
  278. #endif
  279. process_record_kb(keycode, record) &&
  280. #if defined(SECURE_ENABLE)
  281. process_secure(keycode, record) &&
  282. #endif
  283. #if defined(SEQUENCER_ENABLE)
  284. process_sequencer(keycode, record) &&
  285. #endif
  286. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  287. process_midi(keycode, record) &&
  288. #endif
  289. #ifdef AUDIO_ENABLE
  290. process_audio(keycode, record) &&
  291. #endif
  292. #if defined(BACKLIGHT_ENABLE)
  293. process_backlight(keycode, record) &&
  294. #endif
  295. #if defined(LED_MATRIX_ENABLE)
  296. process_led_matrix(keycode, record) &&
  297. #endif
  298. #ifdef STENO_ENABLE
  299. process_steno(keycode, record) &&
  300. #endif
  301. #if (defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)
  302. process_music(keycode, record) &&
  303. #endif
  304. #ifdef CAPS_WORD_ENABLE
  305. process_caps_word(keycode, record) &&
  306. #endif
  307. #ifdef KEY_OVERRIDE_ENABLE
  308. process_key_override(keycode, record) &&
  309. #endif
  310. #ifdef TAP_DANCE_ENABLE
  311. process_tap_dance(keycode, record) &&
  312. #endif
  313. #if defined(UNICODE_COMMON_ENABLE)
  314. process_unicode_common(keycode, record) &&
  315. #endif
  316. #ifdef LEADER_ENABLE
  317. process_leader(keycode, record) &&
  318. #endif
  319. #ifdef AUTO_SHIFT_ENABLE
  320. process_auto_shift(keycode, record) &&
  321. #endif
  322. #ifdef DYNAMIC_TAPPING_TERM_ENABLE
  323. process_dynamic_tapping_term(keycode, record) &&
  324. #endif
  325. #ifdef SPACE_CADET_ENABLE
  326. process_space_cadet(keycode, record) &&
  327. #endif
  328. #ifdef MAGIC_ENABLE
  329. process_magic(keycode, record) &&
  330. #endif
  331. #ifdef GRAVE_ESC_ENABLE
  332. process_grave_esc(keycode, record) &&
  333. #endif
  334. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  335. process_rgb(keycode, record) &&
  336. #endif
  337. #ifdef JOYSTICK_ENABLE
  338. process_joystick(keycode, record) &&
  339. #endif
  340. #ifdef PROGRAMMABLE_BUTTON_ENABLE
  341. process_programmable_button(keycode, record) &&
  342. #endif
  343. #ifdef AUTOCORRECT_ENABLE
  344. process_autocorrect(keycode, record) &&
  345. #endif
  346. #ifdef TRI_LAYER_ENABLE
  347. process_tri_layer(keycode, record) &&
  348. #endif
  349. #ifdef BLUETOOTH_ENABLE
  350. process_connection(keycode, record) &&
  351. #endif
  352. true)) {
  353. return false;
  354. }
  355. if (record->event.pressed) {
  356. switch (keycode) {
  357. #ifndef NO_RESET
  358. case QK_BOOTLOADER:
  359. reset_keyboard();
  360. return false;
  361. case QK_REBOOT:
  362. soft_reset_keyboard();
  363. return false;
  364. #endif
  365. #ifndef NO_DEBUG
  366. case QK_DEBUG_TOGGLE:
  367. debug_enable ^= 1;
  368. if (debug_enable) {
  369. print("DEBUG: enabled.\n");
  370. } else {
  371. print("DEBUG: disabled.\n");
  372. }
  373. #endif
  374. return false;
  375. case QK_CLEAR_EEPROM:
  376. #ifdef NO_RESET
  377. eeconfig_init();
  378. #else
  379. eeconfig_disable();
  380. soft_reset_keyboard();
  381. #endif
  382. return false;
  383. #ifdef VELOCIKEY_ENABLE
  384. case QK_VELOCIKEY_TOGGLE:
  385. velocikey_toggle();
  386. return false;
  387. #endif
  388. #ifndef NO_ACTION_ONESHOT
  389. case QK_ONE_SHOT_TOGGLE:
  390. oneshot_toggle();
  391. break;
  392. case QK_ONE_SHOT_ON:
  393. oneshot_enable();
  394. break;
  395. case QK_ONE_SHOT_OFF:
  396. oneshot_disable();
  397. break;
  398. #endif
  399. #ifdef ENABLE_COMPILE_KEYCODE
  400. case QK_MAKE: // Compiles the firmware, and adds the flash command based on keyboard bootloader
  401. {
  402. # ifdef NO_ACTION_ONESHOT
  403. const uint8_t temp_mod = mod_config(get_mods());
  404. # else
  405. const uint8_t temp_mod = mod_config(get_mods() | get_oneshot_mods());
  406. clear_oneshot_mods();
  407. # endif
  408. clear_mods();
  409. SEND_STRING_DELAY("qmk", TAP_CODE_DELAY);
  410. if (temp_mod & MOD_MASK_SHIFT) { // if shift is held, flash rather than compile
  411. SEND_STRING_DELAY(" flash ", TAP_CODE_DELAY);
  412. } else {
  413. SEND_STRING_DELAY(" compile ", TAP_CODE_DELAY);
  414. }
  415. # if defined(CONVERTER_ENABLED)
  416. SEND_STRING_DELAY("-kb " QMK_KEYBOARD " -km " QMK_KEYMAP " -e CONVERT_TO=" CONVERTER_TARGET SS_TAP(X_ENTER), TAP_CODE_DELAY);
  417. # else
  418. SEND_STRING_DELAY("-kb " QMK_KEYBOARD " -km " QMK_KEYMAP SS_TAP(X_ENTER), TAP_CODE_DELAY);
  419. # endif
  420. if (temp_mod & MOD_MASK_SHIFT && temp_mod & MOD_MASK_CTRL) {
  421. reset_keyboard();
  422. }
  423. }
  424. #endif
  425. }
  426. }
  427. return process_action_kb(record);
  428. }
  429. void set_single_default_layer(uint8_t default_layer) {
  430. #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
  431. PLAY_SONG(default_layer_songs[default_layer]);
  432. #endif
  433. default_layer_set((layer_state_t)1 << default_layer);
  434. }
  435. void set_single_persistent_default_layer(uint8_t default_layer) {
  436. eeconfig_update_default_layer((layer_state_t)1 << default_layer);
  437. set_single_default_layer(default_layer);
  438. }
  439. //------------------------------------------------------------------------------
  440. // Override these functions in your keymap file to play different tunes on
  441. // different events such as startup and bootloader jump
  442. __attribute__((weak)) bool shutdown_user(bool jump_to_bootloader) {
  443. return true;
  444. }
  445. __attribute__((weak)) bool shutdown_kb(bool jump_to_bootloader) {
  446. if (!shutdown_user(jump_to_bootloader)) {
  447. return false;
  448. }
  449. return true;
  450. }
  451. void suspend_power_down_quantum(void) {
  452. suspend_power_down_kb();
  453. #ifndef NO_SUSPEND_POWER_DOWN
  454. // Turn off backlight
  455. # ifdef BACKLIGHT_ENABLE
  456. backlight_level_noeeprom(0);
  457. # endif
  458. # ifdef LED_MATRIX_ENABLE
  459. led_matrix_task();
  460. # endif
  461. # ifdef RGB_MATRIX_ENABLE
  462. rgb_matrix_task();
  463. # endif
  464. // Turn off LED indicators
  465. led_suspend();
  466. // Turn off audio
  467. # ifdef AUDIO_ENABLE
  468. stop_all_notes();
  469. # endif
  470. // Turn off underglow
  471. # if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
  472. rgblight_suspend();
  473. # endif
  474. # if defined(LED_MATRIX_ENABLE)
  475. led_matrix_set_suspend_state(true);
  476. # endif
  477. # if defined(RGB_MATRIX_ENABLE)
  478. rgb_matrix_set_suspend_state(true);
  479. # endif
  480. # ifdef OLED_ENABLE
  481. oled_off();
  482. # endif
  483. # ifdef ST7565_ENABLE
  484. st7565_off();
  485. # endif
  486. # if defined(POINTING_DEVICE_ENABLE)
  487. // run to ensure scanning occurs while suspended
  488. pointing_device_task();
  489. # endif
  490. #endif
  491. }
  492. __attribute__((weak)) void suspend_wakeup_init_quantum(void) {
  493. // Turn on backlight
  494. #ifdef BACKLIGHT_ENABLE
  495. backlight_init();
  496. #endif
  497. // Restore LED indicators
  498. led_wakeup();
  499. // Wake up underglow
  500. #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
  501. rgblight_wakeup();
  502. #endif
  503. #if defined(LED_MATRIX_ENABLE)
  504. led_matrix_set_suspend_state(false);
  505. #endif
  506. #if defined(RGB_MATRIX_ENABLE)
  507. rgb_matrix_set_suspend_state(false);
  508. #endif
  509. suspend_wakeup_init_kb();
  510. }
  511. /** \brief converts unsigned integers into char arrays
  512. *
  513. * Takes an unsigned integer and converts that value into an equivalent char array
  514. * A padding character may be specified, ' ' for leading spaces, '0' for leading zeros.
  515. */
  516. const char *get_numeric_str(char *buf, size_t buf_len, uint32_t curr_num, char curr_pad) {
  517. buf[buf_len - 1] = '\0';
  518. for (size_t i = 0; i < buf_len - 1; ++i) {
  519. char c = '0' + curr_num % 10;
  520. buf[buf_len - 2 - i] = (c == '0' && i == 0) ? '0' : (curr_num > 0 ? c : curr_pad);
  521. curr_num /= 10;
  522. }
  523. return buf;
  524. }
  525. /** \brief converts uint8_t into char array
  526. *
  527. * Takes an uint8_t, and uses an internal static buffer to render that value into a char array
  528. * A padding character may be specified, ' ' for leading spaces, '0' for leading zeros.
  529. *
  530. * NOTE: Subsequent invocations will reuse the same static buffer and overwrite the previous
  531. * contents. Use the result immediately, instead of caching it.
  532. */
  533. const char *get_u8_str(uint8_t curr_num, char curr_pad) {
  534. static char buf[4] = {0};
  535. static uint8_t last_num = 0xFF;
  536. static char last_pad = '\0';
  537. if (last_num == curr_num && last_pad == curr_pad) {
  538. return buf;
  539. }
  540. last_num = curr_num;
  541. last_pad = curr_pad;
  542. return get_numeric_str(buf, sizeof(buf), curr_num, curr_pad);
  543. }
  544. /** \brief converts uint16_t into char array
  545. *
  546. * Takes an uint16_t, and uses an internal static buffer to render that value into a char array
  547. * A padding character may be specified, ' ' for leading spaces, '0' for leading zeros.
  548. *
  549. * NOTE: Subsequent invocations will reuse the same static buffer and overwrite the previous
  550. * contents. Use the result immediately, instead of caching it.
  551. */
  552. const char *get_u16_str(uint16_t curr_num, char curr_pad) {
  553. static char buf[6] = {0};
  554. static uint16_t last_num = 0xFF;
  555. static char last_pad = '\0';
  556. if (last_num == curr_num && last_pad == curr_pad) {
  557. return buf;
  558. }
  559. last_num = curr_num;
  560. last_pad = curr_pad;
  561. return get_numeric_str(buf, sizeof(buf), curr_num, curr_pad);
  562. }
  563. #if defined(SECURE_ENABLE)
  564. void secure_hook_quantum(secure_status_t secure_status) {
  565. // If keys are being held when this is triggered, they may not be released properly
  566. // this can result in stuck keys, mods and layers. To prevent that, manually
  567. // clear these, when it is triggered.
  568. if (secure_status == SECURE_PENDING) {
  569. clear_keyboard();
  570. layer_clear();
  571. }
  572. # if defined(XAP_ENABLE)
  573. xap_broadcast_secure_status(secure_status);
  574. # endif
  575. }
  576. #endif