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