quantum.c 19 KB

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