quantum.c 31 KB

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