1 /* Copyright 2016-2017 Jack Humbert
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.
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.
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/>.
19 #if !defined(RGBLIGHT_ENABLE) && !defined(RGB_MATRIX_ENABLE)
24 #include "outputselect.h"
27 #ifndef BREATHING_PERIOD
28 #define BREATHING_PERIOD 6
31 #include "backlight.h"
32 extern backlight_config_t backlight_config;
34 #ifdef FAUXCLICKY_ENABLE
35 #include "fauxclicky.h"
43 #include "process_midi.h"
46 #ifdef VELOCIKEY_ENABLE
47 #include "velocikey.h"
60 #define GOODBYE_SONG SONG(GOODBYE_SOUND)
63 #define AG_NORM_SONG SONG(AG_NORM_SOUND)
66 #define AG_SWAP_SONG SONG(AG_SWAP_SOUND)
69 #define CG_NORM_SONG SONG(AG_NORM_SOUND)
72 #define CG_SWAP_SONG SONG(AG_SWAP_SOUND)
74 float goodbye_song[][2] = GOODBYE_SONG;
75 float ag_norm_song[][2] = AG_NORM_SONG;
76 float ag_swap_song[][2] = AG_SWAP_SONG;
77 float cg_norm_song[][2] = CG_NORM_SONG;
78 float cg_swap_song[][2] = CG_SWAP_SONG;
79 #ifdef DEFAULT_LAYER_SONGS
80 float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
84 static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
86 case QK_MODS ... QK_MODS_MAX:
101 if (code < QK_RMODS_MIN) return;
113 static inline void qk_register_weak_mods(uint8_t kc) {
114 add_weak_mods(MOD_BIT(kc));
115 send_keyboard_report();
118 static inline void qk_unregister_weak_mods(uint8_t kc) {
119 del_weak_mods(MOD_BIT(kc));
120 send_keyboard_report();
123 static inline void qk_register_mods(uint8_t kc) {
124 add_weak_mods(MOD_BIT(kc));
125 send_keyboard_report();
128 static inline void qk_unregister_mods(uint8_t kc) {
129 del_weak_mods(MOD_BIT(kc));
130 send_keyboard_report();
133 void register_code16 (uint16_t code) {
134 if (IS_MOD(code) || code == KC_NO) {
135 do_code16 (code, qk_register_mods);
137 do_code16 (code, qk_register_weak_mods);
139 register_code (code);
142 void unregister_code16 (uint16_t code) {
143 unregister_code (code);
144 if (IS_MOD(code) || code == KC_NO) {
145 do_code16 (code, qk_unregister_mods);
147 do_code16 (code, qk_unregister_weak_mods);
151 void tap_code16(uint16_t code) {
152 register_code16(code);
153 #if TAP_CODE_DELAY > 0
154 wait_ms(TAP_CODE_DELAY);
156 unregister_code16(code);
159 __attribute__ ((weak))
160 bool process_action_kb(keyrecord_t *record) {
164 __attribute__ ((weak))
165 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
166 return process_record_user(keycode, record);
169 __attribute__ ((weak))
170 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
174 void reset_keyboard(void) {
176 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
177 process_midi_all_notes_off();
180 #ifndef NO_MUSIC_MODE
181 music_all_notes_off();
183 uint16_t timer_start = timer_read();
184 PLAY_SONG(goodbye_song);
186 while(timer_elapsed(timer_start) < 250)
196 // this is also done later in bootloader.c - not sure if it's neccesary here
197 #ifdef BOOTLOADER_CATERINA
198 *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
203 /* true if the last press of GRAVE_ESC was shifted (i.e. GUI or SHIFT were pressed), false otherwise.
204 * Used to ensure that the correct keycode is released if the key is released.
206 static bool grave_esc_was_shifted = false;
208 /* Convert record into usable keycode via the contained event. */
209 uint16_t get_record_keycode(keyrecord_t *record) {
210 return get_event_keycode(record->event);
214 /* Convert event into usable keycode. Checks the layer cache to ensure that it
215 * retains the correct keycode after a layer change, if the key is still pressed.
217 uint16_t get_event_keycode(keyevent_t event) {
219 #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
220 /* TODO: Use store_or_get_action() or a similar function. */
221 if (!disable_action_cache) {
225 layer = layer_switch_get_layer(event.key);
226 update_source_layers_cache(event.key, layer);
228 layer = read_source_layers_cache(event.key);
230 return keymap_key_to_keycode(layer, event.key);
233 return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key);
236 /* Main keycode processing function. Hands off handling to other functions,
237 * then processes internal Quantum keycodes, then processes ACTIONs.
239 bool process_record_quantum(keyrecord_t *record) {
240 uint16_t keycode = get_record_keycode(record);
242 // This is how you use actions here
243 // if (keycode == KC_LEAD) {
245 // action.code = ACTION_DEFAULT_LAYER_SET(0);
246 // process_action(record, action);
250 #ifdef VELOCIKEY_ENABLE
251 if (velocikey_enabled() && record->event.pressed) { velocikey_accelerate(); }
254 #ifdef TAP_DANCE_ENABLE
255 preprocess_tap_dance(keycode, record);
259 #if defined(KEY_LOCK_ENABLE)
260 // Must run first to be able to mask key_up events.
261 process_key_lock(&keycode, record) &&
263 #if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
264 process_clicky(keycode, record) &&
265 #endif //AUDIO_CLICKY
267 process_haptic(keycode, record) &&
268 #endif //HAPTIC_ENABLE
269 #if defined(RGB_MATRIX_ENABLE)
270 process_rgb_matrix(keycode, record) &&
272 process_record_kb(keycode, record) &&
273 #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
274 process_midi(keycode, record) &&
277 process_audio(keycode, record) &&
280 process_steno(keycode, record) &&
282 #if (defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)
283 process_music(keycode, record) &&
285 #ifdef TAP_DANCE_ENABLE
286 process_tap_dance(keycode, record) &&
288 #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
289 process_unicode_common(keycode, record) &&
292 process_leader(keycode, record) &&
295 process_combo(keycode, record) &&
297 #ifdef PRINTING_ENABLE
298 process_printer(keycode, record) &&
300 #ifdef AUTO_SHIFT_ENABLE
301 process_auto_shift(keycode, record) &&
303 #ifdef TERMINAL_ENABLE
304 process_terminal(keycode, record) &&
306 #ifdef SPACE_CADET_ENABLE
307 process_space_cadet(keycode, record) &&
313 // Shift / paren setup
317 if (record->event.pressed) {
322 if (record->event.pressed) {
325 print("DEBUG: enabled.\n");
327 print("DEBUG: disabled.\n");
332 if (record->event.pressed) {
336 #ifdef FAUXCLICKY_ENABLE
338 if (record->event.pressed) {
343 if (record->event.pressed) {
348 if (record->event.pressed) {
353 #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
355 // Split keyboards need to trigger on key-up for edge-case issue
356 #ifndef SPLIT_KEYBOARD
357 if (record->event.pressed) {
359 if (!record->event.pressed) {
364 case RGB_MODE_FORWARD:
365 if (record->event.pressed) {
366 uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT));
368 rgblight_step_reverse();
375 case RGB_MODE_REVERSE:
376 if (record->event.pressed) {
377 uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT));
382 rgblight_step_reverse();
387 // Split keyboards need to trigger on key-up for edge-case issue
388 #ifndef SPLIT_KEYBOARD
389 if (record->event.pressed) {
391 if (!record->event.pressed) {
393 rgblight_increase_hue();
397 // Split keyboards need to trigger on key-up for edge-case issue
398 #ifndef SPLIT_KEYBOARD
399 if (record->event.pressed) {
401 if (!record->event.pressed) {
403 rgblight_decrease_hue();
407 // Split keyboards need to trigger on key-up for edge-case issue
408 #ifndef SPLIT_KEYBOARD
409 if (record->event.pressed) {
411 if (!record->event.pressed) {
413 rgblight_increase_sat();
417 // Split keyboards need to trigger on key-up for edge-case issue
418 #ifndef SPLIT_KEYBOARD
419 if (record->event.pressed) {
421 if (!record->event.pressed) {
423 rgblight_decrease_sat();
427 // Split keyboards need to trigger on key-up for edge-case issue
428 #ifndef SPLIT_KEYBOARD
429 if (record->event.pressed) {
431 if (!record->event.pressed) {
433 rgblight_increase_val();
437 // Split keyboards need to trigger on key-up for edge-case issue
438 #ifndef SPLIT_KEYBOARD
439 if (record->event.pressed) {
441 if (!record->event.pressed) {
443 rgblight_decrease_val();
447 if (record->event.pressed) {
448 rgblight_increase_speed();
452 if (record->event.pressed) {
453 rgblight_decrease_speed();
457 if (record->event.pressed) {
458 rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
461 case RGB_MODE_BREATHE:
462 #ifdef RGBLIGHT_EFFECT_BREATHING
463 if (record->event.pressed) {
464 if ((RGBLIGHT_MODE_BREATHING <= rgblight_get_mode()) &&
465 (rgblight_get_mode() < RGBLIGHT_MODE_BREATHING_end)) {
468 rgblight_mode(RGBLIGHT_MODE_BREATHING);
473 case RGB_MODE_RAINBOW:
474 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
475 if (record->event.pressed) {
476 if ((RGBLIGHT_MODE_RAINBOW_MOOD <= rgblight_get_mode()) &&
477 (rgblight_get_mode() < RGBLIGHT_MODE_RAINBOW_MOOD_end)) {
480 rgblight_mode(RGBLIGHT_MODE_RAINBOW_MOOD);
486 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
487 if (record->event.pressed) {
488 if ((RGBLIGHT_MODE_RAINBOW_SWIRL <= rgblight_get_mode()) &&
489 (rgblight_get_mode() < RGBLIGHT_MODE_RAINBOW_SWIRL_end)) {
492 rgblight_mode(RGBLIGHT_MODE_RAINBOW_SWIRL);
498 #ifdef RGBLIGHT_EFFECT_SNAKE
499 if (record->event.pressed) {
500 if ((RGBLIGHT_MODE_SNAKE <= rgblight_get_mode()) &&
501 (rgblight_get_mode() < RGBLIGHT_MODE_SNAKE_end)) {
504 rgblight_mode(RGBLIGHT_MODE_SNAKE);
509 case RGB_MODE_KNIGHT:
510 #ifdef RGBLIGHT_EFFECT_KNIGHT
511 if (record->event.pressed) {
512 if ((RGBLIGHT_MODE_KNIGHT <= rgblight_get_mode()) &&
513 (rgblight_get_mode() < RGBLIGHT_MODE_KNIGHT_end)) {
516 rgblight_mode(RGBLIGHT_MODE_KNIGHT);
522 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
523 if (record->event.pressed) {
524 rgblight_mode(RGBLIGHT_MODE_CHRISTMAS);
528 case RGB_MODE_GRADIENT:
529 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
530 if (record->event.pressed) {
531 if ((RGBLIGHT_MODE_STATIC_GRADIENT <= rgblight_get_mode()) &&
532 (rgblight_get_mode() < RGBLIGHT_MODE_STATIC_GRADIENT_end)) {
535 rgblight_mode(RGBLIGHT_MODE_STATIC_GRADIENT);
540 case RGB_MODE_RGBTEST:
541 #ifdef RGBLIGHT_EFFECT_RGB_TEST
542 if (record->event.pressed) {
543 rgblight_mode(RGBLIGHT_MODE_RGB_TEST);
547 #endif // defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
548 #ifdef VELOCIKEY_ENABLE
550 if (record->event.pressed) {
557 if (record->event.pressed) {
558 set_output(OUTPUT_AUTO);
562 if (record->event.pressed) {
563 set_output(OUTPUT_USB);
566 #ifdef BLUETOOTH_ENABLE
568 if (record->event.pressed) {
569 set_output(OUTPUT_BLUETOOTH);
574 case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_ALT_GUI:
575 case MAGIC_SWAP_LCTL_LGUI ... MAGIC_TOGGLE_CTL_GUI:
576 if (record->event.pressed) {
577 // MAGIC actions (BOOTMAGIC without the boot)
578 if (!eeconfig_is_enabled()) {
582 keymap_config.raw = eeconfig_read_keymap();
585 case MAGIC_SWAP_CONTROL_CAPSLOCK:
586 keymap_config.swap_control_capslock = true;
588 case MAGIC_CAPSLOCK_TO_CONTROL:
589 keymap_config.capslock_to_control = true;
591 case MAGIC_SWAP_LALT_LGUI:
592 keymap_config.swap_lalt_lgui = true;
594 case MAGIC_SWAP_RALT_RGUI:
595 keymap_config.swap_ralt_rgui = true;
597 case MAGIC_SWAP_LCTL_LGUI:
598 keymap_config.swap_lctl_lgui = true;
600 case MAGIC_SWAP_RCTL_RGUI:
601 keymap_config.swap_rctl_rgui = true;
604 keymap_config.no_gui = true;
606 case MAGIC_SWAP_GRAVE_ESC:
607 keymap_config.swap_grave_esc = true;
609 case MAGIC_SWAP_BACKSLASH_BACKSPACE:
610 keymap_config.swap_backslash_backspace = true;
612 case MAGIC_HOST_NKRO:
613 keymap_config.nkro = true;
615 case MAGIC_SWAP_ALT_GUI:
616 keymap_config.swap_lalt_lgui = true;
617 keymap_config.swap_ralt_rgui = true;
619 PLAY_SONG(ag_swap_song);
622 case MAGIC_SWAP_CTL_GUI:
623 keymap_config.swap_lctl_lgui = true;
624 keymap_config.swap_rctl_rgui = true;
626 PLAY_SONG(cg_swap_song);
629 case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
630 keymap_config.swap_control_capslock = false;
632 case MAGIC_UNCAPSLOCK_TO_CONTROL:
633 keymap_config.capslock_to_control = false;
635 case MAGIC_UNSWAP_LALT_LGUI:
636 keymap_config.swap_lalt_lgui = false;
638 case MAGIC_UNSWAP_RALT_RGUI:
639 keymap_config.swap_ralt_rgui = false;
641 case MAGIC_UNSWAP_LCTL_LGUI:
642 keymap_config.swap_lctl_lgui = false;
644 case MAGIC_UNSWAP_RCTL_RGUI:
645 keymap_config.swap_rctl_rgui = false;
648 keymap_config.no_gui = false;
650 case MAGIC_UNSWAP_GRAVE_ESC:
651 keymap_config.swap_grave_esc = false;
653 case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
654 keymap_config.swap_backslash_backspace = false;
656 case MAGIC_UNHOST_NKRO:
657 keymap_config.nkro = false;
659 case MAGIC_UNSWAP_ALT_GUI:
660 keymap_config.swap_lalt_lgui = false;
661 keymap_config.swap_ralt_rgui = false;
663 PLAY_SONG(ag_norm_song);
666 case MAGIC_UNSWAP_CTL_GUI:
667 keymap_config.swap_lctl_lgui = false;
668 keymap_config.swap_rctl_rgui = false;
670 PLAY_SONG(cg_norm_song);
673 case MAGIC_TOGGLE_ALT_GUI:
674 keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
675 keymap_config.swap_ralt_rgui = !keymap_config.swap_ralt_rgui;
677 if (keymap_config.swap_ralt_rgui) {
678 PLAY_SONG(ag_swap_song);
680 PLAY_SONG(ag_norm_song);
684 case MAGIC_TOGGLE_CTL_GUI:
685 keymap_config.swap_lctl_lgui = !keymap_config.swap_lctl_lgui;
686 keymap_config.swap_rctl_rgui = !keymap_config.swap_rctl_rgui;
688 if (keymap_config.swap_rctl_rgui) {
689 PLAY_SONG(cg_swap_song);
691 PLAY_SONG(cg_norm_song);
695 case MAGIC_TOGGLE_NKRO:
696 keymap_config.nkro = !keymap_config.nkro;
701 eeconfig_update_keymap(keymap_config.raw);
702 clear_keyboard(); // clear to prevent stuck keys
709 uint8_t shifted = get_mods() & ((MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)
710 |MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)));
712 #ifdef GRAVE_ESC_ALT_OVERRIDE
713 // if ALT is pressed, ESC is always sent
714 // this is handy for the cmd+opt+esc shortcut on macOS, among other things.
715 if (get_mods() & (MOD_BIT(KC_LALT) | MOD_BIT(KC_RALT))) {
720 #ifdef GRAVE_ESC_CTRL_OVERRIDE
721 // if CTRL is pressed, ESC is always sent
722 // this is handy for the ctrl+shift+esc shortcut on windows, among other things.
723 if (get_mods() & (MOD_BIT(KC_LCTL) | MOD_BIT(KC_RCTL))) {
728 #ifdef GRAVE_ESC_GUI_OVERRIDE
729 // if GUI is pressed, ESC is always sent
730 if (get_mods() & (MOD_BIT(KC_LGUI) | MOD_BIT(KC_RGUI))) {
735 #ifdef GRAVE_ESC_SHIFT_OVERRIDE
736 // if SHIFT is pressed, ESC is always sent
737 if (get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) {
742 if (record->event.pressed) {
743 grave_esc_was_shifted = shifted;
744 add_key(shifted ? KC_GRAVE : KC_ESCAPE);
747 del_key(grave_esc_was_shifted ? KC_GRAVE : KC_ESCAPE);
750 send_keyboard_report();
754 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_BREATHING)
756 if (record->event.pressed) {
757 backlight_toggle_breathing();
764 return process_action_kb(record);
767 __attribute__ ((weak))
768 const bool ascii_to_shift_lut[128] PROGMEM = {
769 0, 0, 0, 0, 0, 0, 0, 0,
770 0, 0, 0, 0, 0, 0, 0, 0,
771 0, 0, 0, 0, 0, 0, 0, 0,
772 0, 0, 0, 0, 0, 0, 0, 0,
774 0, 1, 1, 1, 1, 1, 1, 0,
775 1, 1, 1, 1, 0, 0, 0, 0,
776 0, 0, 0, 0, 0, 0, 0, 0,
777 0, 0, 1, 0, 1, 0, 1, 1,
778 1, 1, 1, 1, 1, 1, 1, 1,
779 1, 1, 1, 1, 1, 1, 1, 1,
780 1, 1, 1, 1, 1, 1, 1, 1,
781 1, 1, 1, 0, 0, 0, 1, 1,
782 0, 0, 0, 0, 0, 0, 0, 0,
783 0, 0, 0, 0, 0, 0, 0, 0,
784 0, 0, 0, 0, 0, 0, 0, 0,
785 0, 0, 0, 1, 1, 1, 1, 0
788 __attribute__ ((weak))
789 const bool ascii_to_altgr_lut[128] PROGMEM = {
790 0, 0, 0, 0, 0, 0, 0, 0,
791 0, 0, 0, 0, 0, 0, 0, 0,
792 0, 0, 0, 0, 0, 0, 0, 0,
793 0, 0, 0, 0, 0, 0, 0, 0,
795 0, 0, 0, 0, 0, 0, 0, 0,
796 0, 0, 0, 0, 0, 0, 0, 0,
797 0, 0, 0, 0, 0, 0, 0, 0,
798 0, 0, 0, 0, 0, 0, 0, 0,
799 0, 0, 0, 0, 0, 0, 0, 0,
800 0, 0, 0, 0, 0, 0, 0, 0,
801 0, 0, 0, 0, 0, 0, 0, 0,
802 0, 0, 0, 0, 0, 0, 0, 0,
803 0, 0, 0, 0, 0, 0, 0, 0,
804 0, 0, 0, 0, 0, 0, 0, 0,
805 0, 0, 0, 0, 0, 0, 0, 0,
806 0, 0, 0, 0, 0, 0, 0, 0
809 __attribute__ ((weak))
810 const uint8_t ascii_to_keycode_lut[128] PROGMEM = {
811 // NUL SOH STX ETX EOT ENQ ACK BEL
812 XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
813 // BS TAB LF VT FF CR SO SI
814 KC_BSPC, KC_TAB, KC_ENT, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
815 // DLE DC1 DC2 DC3 DC4 NAK SYN ETB
816 XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
817 // CAN EM SUB ESC FS GS RS US
818 XXXXXXX, XXXXXXX, XXXXXXX, KC_ESC, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
821 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
823 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
825 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
827 KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
829 KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
831 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
833 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
835 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
837 KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
839 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
841 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
843 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
846 void send_string(const char *str) {
847 send_string_with_delay(str, 0);
850 void send_string_P(const char *str) {
851 send_string_with_delay_P(str, 0);
854 void send_string_with_delay(const char *str, uint8_t interval) {
856 char ascii_code = *str;
857 if (!ascii_code) break;
858 if (ascii_code == SS_TAP_CODE) {
860 uint8_t keycode = *(++str);
861 register_code(keycode);
862 unregister_code(keycode);
863 } else if (ascii_code == SS_DOWN_CODE) {
865 uint8_t keycode = *(++str);
866 register_code(keycode);
867 } else if (ascii_code == SS_UP_CODE) {
869 uint8_t keycode = *(++str);
870 unregister_code(keycode);
872 send_char(ascii_code);
876 { uint8_t ms = interval; while (ms--) wait_ms(1); }
880 void send_string_with_delay_P(const char *str, uint8_t interval) {
882 char ascii_code = pgm_read_byte(str);
883 if (!ascii_code) break;
884 if (ascii_code == SS_TAP_CODE) {
886 uint8_t keycode = pgm_read_byte(++str);
887 register_code(keycode);
888 unregister_code(keycode);
889 } else if (ascii_code == SS_DOWN_CODE) {
891 uint8_t keycode = pgm_read_byte(++str);
892 register_code(keycode);
893 } else if (ascii_code == SS_UP_CODE) {
895 uint8_t keycode = pgm_read_byte(++str);
896 unregister_code(keycode);
898 send_char(ascii_code);
902 { uint8_t ms = interval; while (ms--) wait_ms(1); }
906 void send_char(char ascii_code) {
907 uint8_t keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
908 bool is_shifted = pgm_read_byte(&ascii_to_shift_lut[(uint8_t)ascii_code]);
909 bool is_altgred = pgm_read_byte(&ascii_to_altgr_lut[(uint8_t)ascii_code]);
912 register_code(KC_LSFT);
915 register_code(KC_RALT);
919 unregister_code(KC_RALT);
922 unregister_code(KC_LSFT);
926 void set_single_persistent_default_layer(uint8_t default_layer) {
927 #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
928 PLAY_SONG(default_layer_songs[default_layer]);
930 eeconfig_update_default_layer(1U<<default_layer);
931 default_layer_set(1U<<default_layer);
934 uint32_t update_tri_layer_state(uint32_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3) {
935 uint32_t mask12 = (1UL << layer1) | (1UL << layer2);
936 uint32_t mask3 = 1UL << layer3;
937 return (state & mask12) == mask12 ? (state | mask3) : (state & ~mask3);
940 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
941 layer_state_set(update_tri_layer_state(layer_state, layer1, layer2, layer3));
944 void tap_random_base64(void) {
945 #if defined(__AVR_ATmega32U4__)
946 uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
948 uint8_t key = rand() % 64;
952 register_code(KC_LSFT);
953 register_code(key + KC_A);
954 unregister_code(key + KC_A);
955 unregister_code(KC_LSFT);
958 register_code(key - 26 + KC_A);
959 unregister_code(key - 26 + KC_A);
963 unregister_code(KC_0);
966 register_code(key - 53 + KC_1);
967 unregister_code(key - 53 + KC_1);
970 register_code(KC_LSFT);
971 register_code(KC_EQL);
972 unregister_code(KC_EQL);
973 unregister_code(KC_LSFT);
976 register_code(KC_SLSH);
977 unregister_code(KC_SLSH);
982 __attribute__((weak))
983 void bootmagic_lite(void) {
984 // The lite version of TMK's bootmagic based on Wilba.
985 // 100% less potential for accidentally making the
986 // keyboard do stupid things.
988 // We need multiple scans because debouncing can't be turned off.
990 #if defined(DEBOUNCING_DELAY) && DEBOUNCING_DELAY > 0
991 wait_ms(DEBOUNCING_DELAY * 2);
992 #elif defined(DEBOUNCE) && DEBOUNCE > 0
993 wait_ms(DEBOUNCE * 2);
999 // If the Esc and space bar are held down on power up,
1000 // reset the EEPROM valid state and jump to bootloader.
1001 // Assumes Esc is at [0,0].
1002 // This isn't very generalized, but we need something that doesn't
1003 // rely on user's keymaps in firmware or EEPROM.
1004 if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
1006 // Jump to bootloader.
1011 void matrix_init_quantum() {
1012 #ifdef BOOTMAGIC_LITE
1015 if (!eeconfig_is_enabled()) {
1018 #ifdef BACKLIGHT_ENABLE
1019 #ifdef LED_MATRIX_ENABLE
1022 backlight_init_ports();
1028 #ifdef RGB_MATRIX_ENABLE
1031 #ifdef ENCODER_ENABLE
1034 #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
1035 unicode_input_mode_init();
1037 #ifdef HAPTIC_ENABLE
1040 #ifdef OUTPUT_AUTO_ENABLE
1041 set_output(OUTPUT_AUTO);
1046 void matrix_scan_quantum() {
1047 #if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE)
1048 matrix_scan_music();
1051 #ifdef TAP_DANCE_ENABLE
1052 matrix_scan_tap_dance();
1056 matrix_scan_combo();
1059 #if defined(BACKLIGHT_ENABLE)
1060 #if defined(LED_MATRIX_ENABLE)
1062 #elif defined(BACKLIGHT_PIN)
1067 #ifdef RGB_MATRIX_ENABLE
1071 #ifdef ENCODER_ENABLE
1075 #ifdef HAPTIC_ENABLE
1081 #if defined(BACKLIGHT_ENABLE) && (defined(BACKLIGHT_PIN) || defined(BACKLIGHT_PINS))
1083 // This logic is a bit complex, we support 3 setups:
1085 // 1. Hardware PWM when backlight is wired to a PWM pin.
1086 // Depending on this pin, we use a different output compare unit.
1087 // 2. Software PWM with hardware timers, but the used timer
1088 // depends on the Audio setup (Audio wins over Backlight).
1089 // 3. Full software PWM, driven by the matrix scan, if both timers are used by Audio.
1091 #if (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) \
1092 || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) \
1093 || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) \
1094 && (BACKLIGHT_PIN == B5 || BACKLIGHT_PIN == B6 || BACKLIGHT_PIN == B7)
1095 #define HARDWARE_PWM
1097 #define TCCRxA TCCR1A
1098 #define TCCRxB TCCR1B
1099 #define TIMERx_OVF_vect TIMER1_OVF_vect
1100 #define TIMSKx TIMSK1
1103 #if BACKLIGHT_PIN == B5
1104 #define COMxx1 COM1A1
1106 #elif BACKLIGHT_PIN == B6
1107 #define COMxx1 COM1B1
1109 #elif BACKLIGHT_PIN == B7
1110 #define COMxx1 COM1C1
1113 #elif (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) \
1114 || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) \
1115 || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) \
1116 && (BACKLIGHT_PIN == C4 || BACKLIGHT_PIN == C5 || BACKLIGHT_PIN == C6)
1117 #define HARDWARE_PWM
1119 #define TCCRxA TCCR3A
1120 #define TCCRxB TCCR3B
1121 #define TIMERx_OVF_vect TIMER3_OVF_vect
1122 #define TIMSKx TIMSK3
1125 #if BACKLIGHT_PIN == C4
1126 #if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
1127 #error This MCU has no C4 pin!
1129 #define COMxx1 COM3C1
1132 #elif BACKLIGHT_PIN == C5
1133 #if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
1134 #error This MCU has no C5 pin!
1136 #define COMxx1 COM3B1
1139 #elif BACKLIGHT_PIN == C6
1140 #define COMxx1 COM3A1
1143 #elif (defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__)) \
1144 && (BACKLIGHT_PIN == B7 || BACKLIGHT_PIN == C5 || BACKLIGHT_PIN == C6)
1145 #define HARDWARE_PWM
1147 #define TCCRxA TCCR1A
1148 #define TCCRxB TCCR1B
1149 #define TIMERx_OVF_vect TIMER1_OVF_vect
1150 #define TIMSKx TIMSK1
1153 #if BACKLIGHT_PIN == B7
1154 #define COMxx1 COM1C1
1156 #elif BACKLIGHT_PIN == C5
1157 #define COMxx1 COM1B1
1159 #elif BACKLIGHT_PIN == C6
1160 #define COMxx1 COM1A1
1163 #elif defined(__AVR_ATmega32A__) \
1164 && (BACKLIGHT_PIN == D4 || BACKLIGHT_PIN == D5)
1165 #define HARDWARE_PWM
1167 #define TCCRxA TCCR1A
1168 #define TCCRxB TCCR1B
1169 #define TIMERx_OVF_vect TIMER1_OVF_vect
1170 #define TIMSKx TIMSK
1173 #if BACKLIGHT_PIN == D4
1174 #define COMxx1 COM1B1
1176 #elif BACKLIGHT_PIN == D5
1177 #define COMxx1 COM1A1
1181 #if !defined(BACKLIGHT_CUSTOM_DRIVER)
1182 #if !defined(B5_AUDIO) && !defined(B6_AUDIO) && !defined(B7_AUDIO)
1183 // Timer 1 is not in use by Audio feature, Backlight can use it
1184 #pragma message "Using hardware timer 1 with software PWM"
1185 #define HARDWARE_PWM
1186 #define BACKLIGHT_PWM_TIMER
1188 #define TCCRxA TCCR1A
1189 #define TCCRxB TCCR1B
1190 #define TIMERx_COMPA_vect TIMER1_COMPA_vect
1191 #define TIMERx_OVF_vect TIMER1_OVF_vect
1192 #if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register
1193 #define TIMSKx TIMSK
1195 #define TIMSKx TIMSK1
1199 #define OCIExA OCIE1A
1201 #elif !defined(C6_AUDIO) && !defined(C5_AUDIO) && !defined(C4_AUDIO)
1202 #pragma message "Using hardware timer 3 with software PWM"
1203 // Timer 3 is not in use by Audio feature, Backlight can use it
1204 #define HARDWARE_PWM
1205 #define BACKLIGHT_PWM_TIMER
1207 #define TCCRxA TCCR3A
1208 #define TCCRxB TCCR3B
1209 #define TIMERx_COMPA_vect TIMER3_COMPA_vect
1210 #define TIMERx_OVF_vect TIMER3_OVF_vect
1211 #define TIMSKx TIMSK3
1214 #define OCIExA OCIE3A
1217 #pragma message "Audio in use - using pure software PWM"
1218 #define NO_HARDWARE_PWM
1221 #pragma message "Custom driver defined - using pure software PWM"
1222 #define NO_HARDWARE_PWM
1226 #ifndef BACKLIGHT_ON_STATE
1227 #define BACKLIGHT_ON_STATE 0
1230 void backlight_on(uint8_t backlight_pin) {
1231 #if BACKLIGHT_ON_STATE == 0
1232 writePinLow(backlight_pin);
1234 writePinHigh(backlight_pin);
1238 void backlight_off(uint8_t backlight_pin) {
1239 #if BACKLIGHT_ON_STATE == 0
1240 writePinHigh(backlight_pin);
1242 writePinLow(backlight_pin);
1247 #if defined(NO_HARDWARE_PWM) || defined(BACKLIGHT_PWM_TIMER) // pwm through software
1249 // we support multiple backlight pins
1250 #ifndef BACKLIGHT_LED_COUNT
1251 #define BACKLIGHT_LED_COUNT 1
1254 #if BACKLIGHT_LED_COUNT == 1
1255 #define BACKLIGHT_PIN_INIT { BACKLIGHT_PIN }
1257 #define BACKLIGHT_PIN_INIT BACKLIGHT_PINS
1260 #define FOR_EACH_LED(x) \
1261 for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) \
1263 uint8_t backlight_pin = backlight_pins[i]; \
1269 static const uint8_t backlight_pins[BACKLIGHT_LED_COUNT] = BACKLIGHT_PIN_INIT;
1271 #else // full hardware PWM
1273 // we support only one backlight pin
1274 static const uint8_t backlight_pin = BACKLIGHT_PIN;
1275 #define FOR_EACH_LED(x) x
1279 #ifdef NO_HARDWARE_PWM
1280 __attribute__((weak))
1281 void backlight_init_ports(void)
1283 // Setup backlight pin as output and output to on state.
1285 setPinOutput(backlight_pin);
1286 backlight_on(backlight_pin);
1289 #ifdef BACKLIGHT_BREATHING
1290 if (is_backlight_breathing()) {
1296 __attribute__ ((weak))
1297 void backlight_set(uint8_t level) {}
1299 uint8_t backlight_tick = 0;
1301 #ifndef BACKLIGHT_CUSTOM_DRIVER
1302 void backlight_task(void) {
1303 if ((0xFFFF >> ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
1305 backlight_on(backlight_pin);
1310 backlight_off(backlight_pin);
1313 backlight_tick = (backlight_tick + 1) % 16;
1317 #ifdef BACKLIGHT_BREATHING
1318 #ifndef BACKLIGHT_CUSTOM_DRIVER
1319 #error "Backlight breathing only available with hardware PWM. Please disable."
1323 #else // hardware pwm through timer
1325 #ifdef BACKLIGHT_PWM_TIMER
1327 // The idea of software PWM assisted by hardware timers is the following
1328 // we use the hardware timer in fast PWM mode like for hardware PWM, but
1329 // instead of letting the Output Match Comparator control the led pin
1330 // (which is not possible since the backlight is not wired to PWM pins on the
1331 // CPU), we do the LED on/off by oursleves.
1332 // The timer is setup to count up to 0xFFFF, and we set the Output Compare
1333 // register to the current 16bits backlight level (after CIE correction).
1334 // This means the CPU will trigger a compare match interrupt when the counter
1335 // reaches the backlight level, where we turn off the LEDs,
1336 // but also an overflow interrupt when the counter rolls back to 0,
1337 // in which we're going to turn on the LEDs.
1338 // The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz.
1340 // Triggered when the counter reaches the OCRx value
1341 ISR(TIMERx_COMPA_vect) {
1343 backlight_off(backlight_pin);
1347 // Triggered when the counter reaches the TOP value
1348 // this one triggers at F_CPU/65536 =~ 244 Hz
1349 ISR(TIMERx_OVF_vect) {
1350 #ifdef BACKLIGHT_BREATHING
1351 if(is_breathing()) {
1355 // for very small values of OCRxx (or backlight level)
1356 // we can't guarantee this whole code won't execute
1357 // at the same time as the compare match interrupt
1358 // which means that we might turn on the leds while
1359 // trying to turn them off, leading to flickering
1360 // artifacts (especially while breathing, because breathing_task
1361 // takes many computation cycles).
1362 // so better not turn them on while the counter TOP is very low.
1365 backlight_on(backlight_pin);
1372 #define TIMER_TOP 0xFFFFU
1374 // See http://jared.geek.nz/2013/feb/linear-led-pwm
1375 static uint16_t cie_lightness(uint16_t v) {
1376 if (v <= 5243) // if below 8% of max
1377 return v / 9; // same as dividing by 900%
1379 uint32_t y = (((uint32_t) v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
1380 // to get a useful result with integer division, we shift left in the expression above
1381 // and revert what we've done again after squaring.
1383 if (y > 0xFFFFUL) // prevent overflow
1386 return (uint16_t) y;
1390 // range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val.
1391 static inline void set_pwm(uint16_t val) {
1395 #ifndef BACKLIGHT_CUSTOM_DRIVER
1396 __attribute__ ((weak))
1397 void backlight_set(uint8_t level) {
1398 if (level > BACKLIGHT_LEVELS)
1399 level = BACKLIGHT_LEVELS;
1402 #ifdef BACKLIGHT_PWM_TIMER
1404 TIMSKx &= ~(_BV(OCIExA));
1405 TIMSKx &= ~(_BV(TOIEx));
1407 backlight_off(backlight_pin);
1411 // Turn off PWM control on backlight pin
1412 TCCRxA &= ~(_BV(COMxx1));
1415 #ifdef BACKLIGHT_PWM_TIMER
1417 TIMSKx |= _BV(OCIExA);
1418 TIMSKx |= _BV(TOIEx);
1421 // Turn on PWM control of backlight pin
1422 TCCRxA |= _BV(COMxx1);
1425 // Set the brightness
1426 set_pwm(cie_lightness(TIMER_TOP * (uint32_t)level / BACKLIGHT_LEVELS));
1429 void backlight_task(void) {}
1430 #endif // BACKLIGHT_CUSTOM_DRIVER
1432 #ifdef BACKLIGHT_BREATHING
1434 #define BREATHING_NO_HALT 0
1435 #define BREATHING_HALT_OFF 1
1436 #define BREATHING_HALT_ON 2
1437 #define BREATHING_STEPS 128
1439 static uint8_t breathing_period = BREATHING_PERIOD;
1440 static uint8_t breathing_halt = BREATHING_NO_HALT;
1441 static uint16_t breathing_counter = 0;
1443 #ifdef BACKLIGHT_PWM_TIMER
1444 static bool breathing = false;
1446 bool is_breathing(void) {
1450 #define breathing_interrupt_enable() do { breathing = true; } while (0)
1451 #define breathing_interrupt_disable() do { breathing = false; } while (0)
1454 bool is_breathing(void) {
1455 return !!(TIMSKx & _BV(TOIEx));
1458 #define breathing_interrupt_enable() do {TIMSKx |= _BV(TOIEx);} while (0)
1459 #define breathing_interrupt_disable() do {TIMSKx &= ~_BV(TOIEx);} while (0)
1462 #define breathing_min() do {breathing_counter = 0;} while (0)
1463 #define breathing_max() do {breathing_counter = breathing_period * 244 / 2;} while (0)
1465 void breathing_enable(void)
1467 breathing_counter = 0;
1468 breathing_halt = BREATHING_NO_HALT;
1469 breathing_interrupt_enable();
1472 void breathing_pulse(void)
1474 if (get_backlight_level() == 0)
1478 breathing_halt = BREATHING_HALT_ON;
1479 breathing_interrupt_enable();
1482 void breathing_disable(void)
1484 breathing_interrupt_disable();
1485 // Restore backlight level
1486 backlight_set(get_backlight_level());
1489 void breathing_self_disable(void)
1491 if (get_backlight_level() == 0)
1492 breathing_halt = BREATHING_HALT_OFF;
1494 breathing_halt = BREATHING_HALT_ON;
1497 void breathing_toggle(void) {
1499 breathing_disable();
1504 void breathing_period_set(uint8_t value)
1508 breathing_period = value;
1511 void breathing_period_default(void) {
1512 breathing_period_set(BREATHING_PERIOD);
1515 void breathing_period_inc(void)
1517 breathing_period_set(breathing_period+1);
1520 void breathing_period_dec(void)
1522 breathing_period_set(breathing_period-1);
1525 /* To generate breathing curve in python:
1526 * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
1528 static const uint8_t breathing_table[BREATHING_STEPS] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 17, 20, 24, 28, 32, 36, 41, 46, 51, 57, 63, 70, 76, 83, 91, 98, 106, 113, 121, 129, 138, 146, 154, 162, 170, 178, 185, 193, 200, 207, 213, 220, 225, 231, 235, 240, 244, 247, 250, 252, 253, 254, 255, 254, 253, 252, 250, 247, 244, 240, 235, 231, 225, 220, 213, 207, 200, 193, 185, 178, 170, 162, 154, 146, 138, 129, 121, 113, 106, 98, 91, 83, 76, 70, 63, 57, 51, 46, 41, 36, 32, 28, 24, 20, 17, 15, 12, 10, 8, 6, 5, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1530 // Use this before the cie_lightness function.
1531 static inline uint16_t scale_backlight(uint16_t v) {
1532 return v / BACKLIGHT_LEVELS * get_backlight_level();
1535 #ifdef BACKLIGHT_PWM_TIMER
1536 void breathing_task(void)
1538 /* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run
1539 * about 244 times per second.
1541 ISR(TIMERx_OVF_vect)
1544 uint16_t interval = (uint16_t) breathing_period * 244 / BREATHING_STEPS;
1545 // resetting after one period to prevent ugly reset at overflow.
1546 breathing_counter = (breathing_counter + 1) % (breathing_period * 244);
1547 uint8_t index = breathing_counter / interval % BREATHING_STEPS;
1549 if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) ||
1550 ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1)))
1552 breathing_interrupt_disable();
1555 set_pwm(cie_lightness(scale_backlight((uint16_t) pgm_read_byte(&breathing_table[index]) * 0x0101U)));
1558 #endif // BACKLIGHT_BREATHING
1560 __attribute__ ((weak))
1561 void backlight_init_ports(void)
1563 // Setup backlight pin as output and output to on state.
1565 setPinOutput(backlight_pin);
1566 backlight_on(backlight_pin);
1569 // I could write a wall of text here to explain... but TL;DW
1570 // Go read the ATmega32u4 datasheet.
1571 // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
1573 #ifdef BACKLIGHT_PWM_TIMER
1574 // TimerX setup, Fast PWM mode count to TOP set in ICRx
1575 TCCRxA = _BV(WGM11); // = 0b00000010;
1576 // clock select clk/1
1577 TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
1578 #else // hardware PWM
1579 // Pin PB7 = OCR1C (Timer 1, Channel C)
1580 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
1581 // (i.e. start high, go low when counter matches.)
1582 // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
1583 // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
1587 "In fast PWM mode, the compare units allow generation of PWM waveforms on the OCnx pins. Setting the COMnx1:0 bits to two will produce a non-inverted PWM [..]."
1588 "In fast PWM mode the counter is incremented until the counter value matches either one of the fixed values 0x00FF, 0x01FF, or 0x03FF (WGMn3:0 = 5, 6, or 7), the value in ICRn (WGMn3:0 = 14), or the value in OCRnA (WGMn3:0 = 15)."
1590 TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010;
1591 TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
1593 // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0.
1597 #ifdef BACKLIGHT_BREATHING
1598 if (is_backlight_breathing()) {
1604 #endif // hardware backlight
1606 #else // no backlight
1608 __attribute__ ((weak))
1609 void backlight_init_ports(void) {}
1611 __attribute__ ((weak))
1612 void backlight_set(uint8_t level) {}
1616 #ifdef HD44780_ENABLED
1617 #include "hd44780.h"
1621 // Functions for spitting out values
1624 void send_dword(uint32_t number) { // this might not actually work
1625 uint16_t word = (number >> 16);
1627 send_word(number & 0xFFFFUL);
1630 void send_word(uint16_t number) {
1631 uint8_t byte = number >> 8;
1633 send_byte(number & 0xFF);
1636 void send_byte(uint8_t number) {
1637 uint8_t nibble = number >> 4;
1638 send_nibble(nibble);
1639 send_nibble(number & 0xF);
1642 void send_nibble(uint8_t number) {
1645 register_code(KC_0);
1646 unregister_code(KC_0);
1649 register_code(KC_1 + (number - 1));
1650 unregister_code(KC_1 + (number - 1));
1653 register_code(KC_A + (number - 0xA));
1654 unregister_code(KC_A + (number - 0xA));
1660 __attribute__((weak))
1661 uint16_t hex_to_keycode(uint8_t hex)
1666 } else if (hex < 0xA) {
1667 return KC_1 + (hex - 0x1);
1669 return KC_A + (hex - 0xA);
1673 void api_send_unicode(uint32_t unicode) {
1676 dword_to_bytes(unicode, chunk);
1677 MT_SEND_DATA(DT_UNICODE, chunk, 5);
1681 __attribute__ ((weak))
1682 void led_set_user(uint8_t usb_led) {
1686 __attribute__ ((weak))
1687 void led_set_kb(uint8_t usb_led) {
1688 led_set_user(usb_led);
1691 __attribute__ ((weak))
1692 void led_init_ports(void)
1697 __attribute__ ((weak))
1698 void led_set(uint8_t usb_led)
1700 #if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
1701 // Use backlight as Caps Lock indicator
1702 uint8_t bl_toggle_lvl = 0;
1704 if (IS_LED_ON(usb_led, USB_LED_CAPS_LOCK) && !backlight_config.enable) {
1705 // Turning Caps Lock ON and backlight is disabled in config
1706 // Toggling backlight to the brightest level
1707 bl_toggle_lvl = BACKLIGHT_LEVELS;
1708 } else if (IS_LED_OFF(usb_led, USB_LED_CAPS_LOCK) && backlight_config.enable) {
1709 // Turning Caps Lock OFF and backlight is enabled in config
1710 // Toggling backlight and restoring config level
1711 bl_toggle_lvl = backlight_config.level;
1714 // Set level without modify backlight_config to keep ability to restore state
1715 backlight_set(bl_toggle_lvl);
1718 led_set_kb(usb_led);
1722 //------------------------------------------------------------------------------
1723 // Override these functions in your keymap file to play different tunes on
1724 // different events such as startup and bootloader jump
1726 __attribute__ ((weak))
1727 void startup_user() {}
1729 __attribute__ ((weak))
1730 void shutdown_user() {}
1732 //------------------------------------------------------------------------------