]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/quantum.c
Fix #3566 use an hardware timer for software PWM stability (#3615)
[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 "quantum.h"
18
19 #if !defined(RGBLIGHT_ENABLE) && !defined(RGB_MATRIX_ENABLE)
20         #include "rgb.h"
21 #endif
22
23 #ifdef PROTOCOL_LUFA
24 #include "outputselect.h"
25 #endif
26
27 #ifndef TAPPING_TERM
28 #define TAPPING_TERM 200
29 #endif
30
31 #ifndef BREATHING_PERIOD
32 #define BREATHING_PERIOD 6
33 #endif
34
35 #include "backlight.h"
36 extern backlight_config_t backlight_config;
37
38 #ifdef FAUXCLICKY_ENABLE
39 #include "fauxclicky.h"
40 #endif
41
42 #ifdef API_ENABLE
43 #include "api.h"
44 #endif
45
46 #ifdef MIDI_ENABLE
47 #include "process_midi.h"
48 #endif
49
50 #ifdef VELOCIKEY_ENABLE
51 #include "velocikey.h"
52 #endif
53
54 #ifdef HAPTIC_ENABLE
55     #include "haptic.h"
56 #endif
57
58 #ifdef ENCODER_ENABLE
59 #include "encoder.h"
60 #endif
61
62 #ifdef AUDIO_ENABLE
63   #ifndef GOODBYE_SONG
64     #define GOODBYE_SONG SONG(GOODBYE_SOUND)
65   #endif
66   #ifndef AG_NORM_SONG
67     #define AG_NORM_SONG SONG(AG_NORM_SOUND)
68   #endif
69   #ifndef AG_SWAP_SONG
70     #define AG_SWAP_SONG SONG(AG_SWAP_SOUND)
71   #endif
72   float goodbye_song[][2] = GOODBYE_SONG;
73   float ag_norm_song[][2] = AG_NORM_SONG;
74   float ag_swap_song[][2] = AG_SWAP_SONG;
75   #ifdef DEFAULT_LAYER_SONGS
76     float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
77   #endif
78 #endif
79
80 static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
81   switch (code) {
82   case QK_MODS ... QK_MODS_MAX:
83     break;
84   default:
85     return;
86   }
87
88   if (code & QK_LCTL)
89     f(KC_LCTL);
90   if (code & QK_LSFT)
91     f(KC_LSFT);
92   if (code & QK_LALT)
93     f(KC_LALT);
94   if (code & QK_LGUI)
95     f(KC_LGUI);
96
97   if (code < QK_RMODS_MIN) return;
98
99   if (code & QK_RCTL)
100     f(KC_RCTL);
101   if (code & QK_RSFT)
102     f(KC_RSFT);
103   if (code & QK_RALT)
104     f(KC_RALT);
105   if (code & QK_RGUI)
106     f(KC_RGUI);
107 }
108
109 static inline void qk_register_weak_mods(uint8_t kc) {
110     add_weak_mods(MOD_BIT(kc));
111     send_keyboard_report();
112 }
113
114 static inline void qk_unregister_weak_mods(uint8_t kc) {
115     del_weak_mods(MOD_BIT(kc));
116     send_keyboard_report();
117 }
118
119 static inline void qk_register_mods(uint8_t kc) {
120     add_weak_mods(MOD_BIT(kc));
121     send_keyboard_report();
122 }
123
124 static inline void qk_unregister_mods(uint8_t kc) {
125     del_weak_mods(MOD_BIT(kc));
126     send_keyboard_report();
127 }
128
129 void register_code16 (uint16_t code) {
130   if (IS_MOD(code) || code == KC_NO) {
131       do_code16 (code, qk_register_mods);
132   } else {
133       do_code16 (code, qk_register_weak_mods);
134   }
135   register_code (code);
136 }
137
138 void unregister_code16 (uint16_t code) {
139   unregister_code (code);
140   if (IS_MOD(code) || code == KC_NO) {
141       do_code16 (code, qk_unregister_mods);
142   } else {
143       do_code16 (code, qk_unregister_weak_mods);
144   }
145 }
146
147 void tap_code16(uint16_t code) {
148   register_code16(code);
149   #if TAP_CODE_DELAY > 0
150     wait_ms(TAP_CODE_DELAY);
151   #endif
152   unregister_code16(code);
153 }
154
155 __attribute__ ((weak))
156 bool process_action_kb(keyrecord_t *record) {
157   return true;
158 }
159
160 __attribute__ ((weak))
161 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
162   return process_record_user(keycode, record);
163 }
164
165 __attribute__ ((weak))
166 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
167   return true;
168 }
169
170 void reset_keyboard(void) {
171   clear_keyboard();
172 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
173   process_midi_all_notes_off();
174 #endif
175 #ifdef AUDIO_ENABLE
176   #ifndef NO_MUSIC_MODE
177     music_all_notes_off();
178   #endif
179   uint16_t timer_start = timer_read();
180   PLAY_SONG(goodbye_song);
181   shutdown_user();
182   while(timer_elapsed(timer_start) < 250)
183     wait_ms(1);
184   stop_all_notes();
185 #else
186   shutdown_user();
187   wait_ms(250);
188 #endif
189 #ifdef HAPTIC_ENABLE
190   haptic_shutdown();
191 #endif
192 // this is also done later in bootloader.c - not sure if it's neccesary here
193 #ifdef BOOTLOADER_CATERINA
194   *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
195 #endif
196   bootloader_jump();
197 }
198
199 // Shift / paren setup
200
201 #ifndef LSPO_KEY
202   #define LSPO_KEY KC_9
203 #endif
204 #ifndef RSPC_KEY
205   #define RSPC_KEY KC_0
206 #endif
207
208 #ifndef LSPO_MOD
209   #define LSPO_MOD KC_LSFT
210 #endif
211 #ifndef RSPC_MOD
212   #define RSPC_MOD KC_RSFT
213 #endif
214
215 // Shift / Enter setup
216 #ifndef SFTENT_KEY
217   #define SFTENT_KEY KC_ENT
218 #endif
219
220 static bool shift_interrupted[2] = {0, 0};
221 static uint16_t scs_timer[2] = {0, 0};
222
223 /* true if the last press of GRAVE_ESC was shifted (i.e. GUI or SHIFT were pressed), false otherwise.
224  * Used to ensure that the correct keycode is released if the key is released.
225  */
226 static bool grave_esc_was_shifted = false;
227
228 /* Convert record into usable keycode via the contained event. */
229 uint16_t get_record_keycode(keyrecord_t *record) {
230   return get_event_keycode(record->event);
231 }
232
233
234 /* Convert event into usable keycode. Checks the layer cache to ensure that it
235  * retains the correct keycode after a layer change, if the key is still pressed.
236  */
237 uint16_t get_event_keycode(keyevent_t event) {
238
239   #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
240     /* TODO: Use store_or_get_action() or a similar function. */
241     if (!disable_action_cache) {
242       uint8_t layer;
243
244       if (event.pressed) {
245         layer = layer_switch_get_layer(event.key);
246         update_source_layers_cache(event.key, layer);
247       } else {
248         layer = read_source_layers_cache(event.key);
249       }
250       return keymap_key_to_keycode(layer, event.key);
251     } else
252   #endif
253     return keymap_key_to_keycode(layer_switch_get_layer(event.key), event.key);
254 }
255
256 /* Main keycode processing function. Hands off handling to other functions,
257  * then processes internal Quantum keycodes, then processes ACTIONs.
258  */
259 bool process_record_quantum(keyrecord_t *record) {
260     uint16_t keycode = get_record_keycode(record);
261
262     // This is how you use actions here
263     // if (keycode == KC_LEAD) {
264     //   action_t action;
265     //   action.code = ACTION_DEFAULT_LAYER_SET(0);
266     //   process_action(record, action);
267     //   return false;
268     // }
269
270   #ifdef VELOCIKEY_ENABLE
271     if (velocikey_enabled() && record->event.pressed) { velocikey_accelerate(); }
272   #endif
273
274   #ifdef TAP_DANCE_ENABLE
275     preprocess_tap_dance(keycode, record);
276   #endif
277
278   #if defined(OLED_DRIVER_ENABLE) && !defined(OLED_DISABLE_TIMEOUT)
279     // Wake up oled if user is using those fabulous keys!
280     if (record->event.pressed)
281       oled_on();
282   #endif
283
284   if (!(
285   #if defined(KEY_LOCK_ENABLE)
286     // Must run first to be able to mask key_up events.
287     process_key_lock(&keycode, record) &&
288   #endif
289   #if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
290     process_clicky(keycode, record) &&
291   #endif //AUDIO_CLICKY
292   #ifdef HAPTIC_ENABLE
293     process_haptic(keycode, record) &&
294   #endif //HAPTIC_ENABLE
295   #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_KEYREACTIVE_ENABLED)
296     process_rgb_matrix(keycode, record) &&
297   #endif
298     process_record_kb(keycode, record) &&
299   #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
300     process_midi(keycode, record) &&
301   #endif
302   #ifdef AUDIO_ENABLE
303     process_audio(keycode, record) &&
304   #endif
305   #ifdef STENO_ENABLE
306     process_steno(keycode, record) &&
307   #endif
308   #if (defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)
309     process_music(keycode, record) &&
310   #endif
311   #ifdef TAP_DANCE_ENABLE
312     process_tap_dance(keycode, record) &&
313   #endif
314   #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
315     process_unicode_common(keycode, record) &&
316   #endif
317   #ifdef LEADER_ENABLE
318     process_leader(keycode, record) &&
319   #endif
320   #ifdef COMBO_ENABLE
321     process_combo(keycode, record) &&
322   #endif
323   #ifdef PRINTING_ENABLE
324     process_printer(keycode, record) &&
325   #endif
326   #ifdef AUTO_SHIFT_ENABLE
327     process_auto_shift(keycode, record) &&
328   #endif
329   #ifdef TERMINAL_ENABLE
330     process_terminal(keycode, record) &&
331   #endif
332       true)) {
333     return false;
334   }
335
336   // Shift / paren setup
337
338   switch(keycode) {
339     case RESET:
340       if (record->event.pressed) {
341         reset_keyboard();
342       }
343     return false;
344     case DEBUG:
345       if (record->event.pressed) {
346           debug_enable = true;
347           print("DEBUG: enabled.\n");
348       }
349     return false;
350     case EEPROM_RESET:
351       if (record->event.pressed) {
352           eeconfig_init();
353       }
354     return false;
355   #ifdef FAUXCLICKY_ENABLE
356   case FC_TOG:
357     if (record->event.pressed) {
358       FAUXCLICKY_TOGGLE;
359     }
360     return false;
361   case FC_ON:
362     if (record->event.pressed) {
363       FAUXCLICKY_ON;
364     }
365     return false;
366   case FC_OFF:
367     if (record->event.pressed) {
368       FAUXCLICKY_OFF;
369     }
370     return false;
371   #endif
372   #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
373   case RGB_TOG:
374     // Split keyboards need to trigger on key-up for edge-case issue
375     #ifndef SPLIT_KEYBOARD
376     if (record->event.pressed) {
377     #else
378     if (!record->event.pressed) {
379     #endif
380       rgblight_toggle();
381     }
382     return false;
383   case RGB_MODE_FORWARD:
384     if (record->event.pressed) {
385       uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT));
386       if(shifted) {
387         rgblight_step_reverse();
388       }
389       else {
390         rgblight_step();
391       }
392     }
393     return false;
394   case RGB_MODE_REVERSE:
395     if (record->event.pressed) {
396       uint8_t shifted = get_mods() & (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT));
397       if(shifted) {
398         rgblight_step();
399       }
400       else {
401         rgblight_step_reverse();
402       }
403     }
404     return false;
405   case RGB_HUI:
406     // Split keyboards need to trigger on key-up for edge-case issue
407     #ifndef SPLIT_KEYBOARD
408     if (record->event.pressed) {
409     #else
410     if (!record->event.pressed) {
411     #endif
412       rgblight_increase_hue();
413     }
414     return false;
415   case RGB_HUD:
416     // Split keyboards need to trigger on key-up for edge-case issue
417     #ifndef SPLIT_KEYBOARD
418     if (record->event.pressed) {
419     #else
420     if (!record->event.pressed) {
421     #endif
422       rgblight_decrease_hue();
423     }
424     return false;
425   case RGB_SAI:
426     // Split keyboards need to trigger on key-up for edge-case issue
427     #ifndef SPLIT_KEYBOARD
428     if (record->event.pressed) {
429     #else
430     if (!record->event.pressed) {
431     #endif
432       rgblight_increase_sat();
433     }
434     return false;
435   case RGB_SAD:
436     // Split keyboards need to trigger on key-up for edge-case issue
437     #ifndef SPLIT_KEYBOARD
438     if (record->event.pressed) {
439     #else
440     if (!record->event.pressed) {
441     #endif
442       rgblight_decrease_sat();
443     }
444     return false;
445   case RGB_VAI:
446     // Split keyboards need to trigger on key-up for edge-case issue
447     #ifndef SPLIT_KEYBOARD
448     if (record->event.pressed) {
449     #else
450     if (!record->event.pressed) {
451     #endif
452       rgblight_increase_val();
453     }
454     return false;
455   case RGB_VAD:
456     // Split keyboards need to trigger on key-up for edge-case issue
457     #ifndef SPLIT_KEYBOARD
458     if (record->event.pressed) {
459     #else
460     if (!record->event.pressed) {
461     #endif
462       rgblight_decrease_val();
463     }
464     return false;
465   case RGB_SPI:
466     if (record->event.pressed) {
467       rgblight_increase_speed();
468     }
469     return false;
470   case RGB_SPD:
471     if (record->event.pressed) {
472       rgblight_decrease_speed();
473     }
474     return false;
475   case RGB_MODE_PLAIN:
476     if (record->event.pressed) {
477       rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
478     }
479     return false;
480   case RGB_MODE_BREATHE:
481   #ifdef RGBLIGHT_EFFECT_BREATHING
482     if (record->event.pressed) {
483       if ((RGBLIGHT_MODE_BREATHING <= rgblight_get_mode()) &&
484           (rgblight_get_mode() < RGBLIGHT_MODE_BREATHING_end)) {
485         rgblight_step();
486       } else {
487         rgblight_mode(RGBLIGHT_MODE_BREATHING);
488       }
489     }
490   #endif
491     return false;
492   case RGB_MODE_RAINBOW:
493   #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
494     if (record->event.pressed) {
495       if ((RGBLIGHT_MODE_RAINBOW_MOOD <= rgblight_get_mode()) &&
496           (rgblight_get_mode() < RGBLIGHT_MODE_RAINBOW_MOOD_end)) {
497         rgblight_step();
498       } else {
499         rgblight_mode(RGBLIGHT_MODE_RAINBOW_MOOD);
500       }
501     }
502   #endif
503     return false;
504   case RGB_MODE_SWIRL:
505   #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
506     if (record->event.pressed) {
507       if ((RGBLIGHT_MODE_RAINBOW_SWIRL <= rgblight_get_mode()) &&
508           (rgblight_get_mode() < RGBLIGHT_MODE_RAINBOW_SWIRL_end)) {
509         rgblight_step();
510       } else {
511         rgblight_mode(RGBLIGHT_MODE_RAINBOW_SWIRL);
512       }
513     }
514   #endif
515     return false;
516   case RGB_MODE_SNAKE:
517   #ifdef RGBLIGHT_EFFECT_SNAKE
518     if (record->event.pressed) {
519       if ((RGBLIGHT_MODE_SNAKE <= rgblight_get_mode()) &&
520           (rgblight_get_mode() < RGBLIGHT_MODE_SNAKE_end)) {
521         rgblight_step();
522       } else {
523         rgblight_mode(RGBLIGHT_MODE_SNAKE);
524       }
525     }
526   #endif
527     return false;
528   case RGB_MODE_KNIGHT:
529   #ifdef RGBLIGHT_EFFECT_KNIGHT
530     if (record->event.pressed) {
531       if ((RGBLIGHT_MODE_KNIGHT <= rgblight_get_mode()) &&
532           (rgblight_get_mode() < RGBLIGHT_MODE_KNIGHT_end)) {
533         rgblight_step();
534       } else {
535         rgblight_mode(RGBLIGHT_MODE_KNIGHT);
536       }
537     }
538   #endif
539     return false;
540   case RGB_MODE_XMAS:
541   #ifdef RGBLIGHT_EFFECT_CHRISTMAS
542     if (record->event.pressed) {
543       rgblight_mode(RGBLIGHT_MODE_CHRISTMAS);
544     }
545   #endif
546     return false;
547   case RGB_MODE_GRADIENT:
548   #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
549     if (record->event.pressed) {
550       if ((RGBLIGHT_MODE_STATIC_GRADIENT <= rgblight_get_mode()) &&
551           (rgblight_get_mode() < RGBLIGHT_MODE_STATIC_GRADIENT_end)) {
552         rgblight_step();
553       } else {
554         rgblight_mode(RGBLIGHT_MODE_STATIC_GRADIENT);
555       }
556     }
557   #endif
558     return false;
559   case RGB_MODE_RGBTEST:
560   #ifdef RGBLIGHT_EFFECT_RGB_TEST
561     if (record->event.pressed) {
562       rgblight_mode(RGBLIGHT_MODE_RGB_TEST);
563     }
564   #endif
565     return false;
566   #endif // defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
567   #ifdef VELOCIKEY_ENABLE
568     case VLK_TOG:
569       if (record->event.pressed) {
570         velocikey_toggle();
571       }
572       return false;
573   #endif
574   #ifdef PROTOCOL_LUFA
575     case OUT_AUTO:
576       if (record->event.pressed) {
577         set_output(OUTPUT_AUTO);
578       }
579       return false;
580     case OUT_USB:
581       if (record->event.pressed) {
582         set_output(OUTPUT_USB);
583       }
584       return false;
585     #ifdef BLUETOOTH_ENABLE
586     case OUT_BT:
587       if (record->event.pressed) {
588         set_output(OUTPUT_BLUETOOTH);
589       }
590       return false;
591     #endif
592     #endif
593     case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
594       if (record->event.pressed) {
595         // MAGIC actions (BOOTMAGIC without the boot)
596         if (!eeconfig_is_enabled()) {
597             eeconfig_init();
598         }
599         /* keymap config */
600         keymap_config.raw = eeconfig_read_keymap();
601         switch (keycode)
602         {
603           case MAGIC_SWAP_CONTROL_CAPSLOCK:
604             keymap_config.swap_control_capslock = true;
605             break;
606           case MAGIC_CAPSLOCK_TO_CONTROL:
607             keymap_config.capslock_to_control = true;
608             break;
609           case MAGIC_SWAP_LALT_LGUI:
610             keymap_config.swap_lalt_lgui = true;
611             break;
612           case MAGIC_SWAP_RALT_RGUI:
613             keymap_config.swap_ralt_rgui = true;
614             break;
615           case MAGIC_NO_GUI:
616             keymap_config.no_gui = true;
617             break;
618           case MAGIC_SWAP_GRAVE_ESC:
619             keymap_config.swap_grave_esc = true;
620             break;
621           case MAGIC_SWAP_BACKSLASH_BACKSPACE:
622             keymap_config.swap_backslash_backspace = true;
623             break;
624           case MAGIC_HOST_NKRO:
625             keymap_config.nkro = true;
626             break;
627           case MAGIC_SWAP_ALT_GUI:
628             keymap_config.swap_lalt_lgui = true;
629             keymap_config.swap_ralt_rgui = true;
630             #ifdef AUDIO_ENABLE
631               PLAY_SONG(ag_swap_song);
632             #endif
633             break;
634           case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
635             keymap_config.swap_control_capslock = false;
636             break;
637           case MAGIC_UNCAPSLOCK_TO_CONTROL:
638             keymap_config.capslock_to_control = false;
639             break;
640           case MAGIC_UNSWAP_LALT_LGUI:
641             keymap_config.swap_lalt_lgui = false;
642             break;
643           case MAGIC_UNSWAP_RALT_RGUI:
644             keymap_config.swap_ralt_rgui = false;
645             break;
646           case MAGIC_UNNO_GUI:
647             keymap_config.no_gui = false;
648             break;
649           case MAGIC_UNSWAP_GRAVE_ESC:
650             keymap_config.swap_grave_esc = false;
651             break;
652           case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
653             keymap_config.swap_backslash_backspace = false;
654             break;
655           case MAGIC_UNHOST_NKRO:
656             keymap_config.nkro = false;
657             break;
658           case MAGIC_UNSWAP_ALT_GUI:
659             keymap_config.swap_lalt_lgui = false;
660             keymap_config.swap_ralt_rgui = false;
661             #ifdef AUDIO_ENABLE
662               PLAY_SONG(ag_norm_song);
663             #endif
664             break;
665           case MAGIC_TOGGLE_ALT_GUI:
666             keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
667             keymap_config.swap_ralt_rgui = !keymap_config.swap_ralt_rgui;
668             #ifdef AUDIO_ENABLE
669               if (keymap_config.swap_ralt_rgui) {
670                 PLAY_SONG(ag_swap_song);
671               } else {
672                 PLAY_SONG(ag_norm_song);
673               }
674             #endif
675             break;
676           case MAGIC_TOGGLE_NKRO:
677             keymap_config.nkro = !keymap_config.nkro;
678             break;
679           default:
680             break;
681         }
682         eeconfig_update_keymap(keymap_config.raw);
683         clear_keyboard(); // clear to prevent stuck keys
684
685         return false;
686       }
687       break;
688     case KC_LSPO: {
689       if (record->event.pressed) {
690         shift_interrupted[0] = false;
691         scs_timer[0] = timer_read ();
692         register_mods(MOD_BIT(KC_LSFT));
693       }
694       else {
695         #ifdef DISABLE_SPACE_CADET_ROLLOVER
696           if (get_mods() & MOD_BIT(RSPC_MOD)) {
697             shift_interrupted[0] = true;
698             shift_interrupted[1] = true;
699           }
700         #endif
701         if (!shift_interrupted[0] && timer_elapsed(scs_timer[0]) < TAPPING_TERM) {
702           #ifdef DISABLE_SPACE_CADET_MODIFIER
703             unregister_mods(MOD_BIT(KC_LSFT));
704           #else
705             if( LSPO_MOD != KC_LSFT ){
706               unregister_mods(MOD_BIT(KC_LSFT));
707               register_mods(MOD_BIT(LSPO_MOD));
708             }
709           #endif
710           register_code(LSPO_KEY);
711           unregister_code(LSPO_KEY);
712           #ifndef DISABLE_SPACE_CADET_MODIFIER
713             if( LSPO_MOD != KC_LSFT ){
714               unregister_mods(MOD_BIT(LSPO_MOD));
715             }
716           #endif
717         }
718         unregister_mods(MOD_BIT(KC_LSFT));
719       }
720       return false;
721     }
722
723     case KC_RSPC: {
724       if (record->event.pressed) {
725         shift_interrupted[1] = false;
726         scs_timer[1] = timer_read ();
727         register_mods(MOD_BIT(KC_RSFT));
728       }
729       else {
730         #ifdef DISABLE_SPACE_CADET_ROLLOVER
731           if (get_mods() & MOD_BIT(LSPO_MOD)) {
732             shift_interrupted[0] = true;
733             shift_interrupted[1] = true;
734           }
735         #endif
736         if (!shift_interrupted[1] && timer_elapsed(scs_timer[1]) < TAPPING_TERM) {
737           #ifdef DISABLE_SPACE_CADET_MODIFIER
738             unregister_mods(MOD_BIT(KC_RSFT));
739           #else
740             if( RSPC_MOD != KC_RSFT ){
741               unregister_mods(MOD_BIT(KC_RSFT));
742               register_mods(MOD_BIT(RSPC_MOD));
743             }
744           #endif
745           register_code(RSPC_KEY);
746           unregister_code(RSPC_KEY);
747           #ifndef DISABLE_SPACE_CADET_MODIFIER
748             if ( RSPC_MOD != KC_RSFT ){
749               unregister_mods(MOD_BIT(RSPC_MOD));
750             }
751           #endif
752         }
753         unregister_mods(MOD_BIT(KC_RSFT));
754       }
755       return false;
756     }
757
758     case KC_SFTENT: {
759       if (record->event.pressed) {
760         shift_interrupted[1] = false;
761         scs_timer[1] = timer_read ();
762         register_mods(MOD_BIT(KC_RSFT));
763       }
764       else if (!shift_interrupted[1] && timer_elapsed(scs_timer[1]) < TAPPING_TERM) {
765         unregister_mods(MOD_BIT(KC_RSFT));
766         register_code(SFTENT_KEY);
767         unregister_code(SFTENT_KEY);
768       }
769       else {
770         unregister_mods(MOD_BIT(KC_RSFT));
771       }
772       return false;
773     }
774
775     case GRAVE_ESC: {
776       uint8_t shifted = get_mods() & ((MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)
777                                       |MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)));
778
779 #ifdef GRAVE_ESC_ALT_OVERRIDE
780       // if ALT is pressed, ESC is always sent
781       // this is handy for the cmd+opt+esc shortcut on macOS, among other things.
782       if (get_mods() & (MOD_BIT(KC_LALT) | MOD_BIT(KC_RALT))) {
783         shifted = 0;
784       }
785 #endif
786
787 #ifdef GRAVE_ESC_CTRL_OVERRIDE
788       // if CTRL is pressed, ESC is always sent
789       // this is handy for the ctrl+shift+esc shortcut on windows, among other things.
790       if (get_mods() & (MOD_BIT(KC_LCTL) | MOD_BIT(KC_RCTL))) {
791         shifted = 0;
792       }
793 #endif
794
795 #ifdef GRAVE_ESC_GUI_OVERRIDE
796       // if GUI is pressed, ESC is always sent
797       if (get_mods() & (MOD_BIT(KC_LGUI) | MOD_BIT(KC_RGUI))) {
798         shifted = 0;
799       }
800 #endif
801
802 #ifdef GRAVE_ESC_SHIFT_OVERRIDE
803       // if SHIFT is pressed, ESC is always sent
804       if (get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) {
805         shifted = 0;
806       }
807 #endif
808
809       if (record->event.pressed) {
810         grave_esc_was_shifted = shifted;
811         add_key(shifted ? KC_GRAVE : KC_ESCAPE);
812       }
813       else {
814         del_key(grave_esc_was_shifted ? KC_GRAVE : KC_ESCAPE);
815       }
816
817       send_keyboard_report();
818       return false;
819     }
820
821 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_BREATHING)
822     case BL_BRTG: {
823       if (record->event.pressed)
824         breathing_toggle();
825       return false;
826     }
827 #endif
828
829     default: {
830       shift_interrupted[0] = true;
831       shift_interrupted[1] = true;
832       break;
833     }
834   }
835
836   return process_action_kb(record);
837 }
838
839 __attribute__ ((weak))
840 const bool ascii_to_shift_lut[0x80] PROGMEM = {
841     0, 0, 0, 0, 0, 0, 0, 0,
842     0, 0, 0, 0, 0, 0, 0, 0,
843     0, 0, 0, 0, 0, 0, 0, 0,
844     0, 0, 0, 0, 0, 0, 0, 0,
845     0, 1, 1, 1, 1, 1, 1, 0,
846     1, 1, 1, 1, 0, 0, 0, 0,
847     0, 0, 0, 0, 0, 0, 0, 0,
848     0, 0, 1, 0, 1, 0, 1, 1,
849     1, 1, 1, 1, 1, 1, 1, 1,
850     1, 1, 1, 1, 1, 1, 1, 1,
851     1, 1, 1, 1, 1, 1, 1, 1,
852     1, 1, 1, 0, 0, 0, 1, 1,
853     0, 0, 0, 0, 0, 0, 0, 0,
854     0, 0, 0, 0, 0, 0, 0, 0,
855     0, 0, 0, 0, 0, 0, 0, 0,
856     0, 0, 0, 1, 1, 1, 1, 0
857 };
858
859 __attribute__ ((weak))
860 const bool ascii_to_altgr_lut[0x80] PROGMEM = {
861     0, 0, 0, 0, 0, 0, 0, 0,
862     0, 0, 0, 0, 0, 0, 0, 0,
863     0, 0, 0, 0, 0, 0, 0, 0,
864     0, 0, 0, 0, 0, 0, 0, 0,
865     0, 0, 0, 0, 0, 0, 0, 0,
866     0, 0, 0, 0, 0, 0, 0, 0,
867     0, 0, 0, 0, 0, 0, 0, 0,
868     0, 0, 0, 0, 0, 0, 0, 0,
869     0, 0, 0, 0, 0, 0, 0, 0,
870     0, 0, 0, 0, 0, 0, 0, 0,
871     0, 0, 0, 0, 0, 0, 0, 0,
872     0, 0, 0, 0, 0, 0, 0, 0,
873     0, 0, 0, 0, 0, 0, 0, 0,
874     0, 0, 0, 0, 0, 0, 0, 0,
875     0, 0, 0, 0, 0, 0, 0, 0,
876     0, 0, 0, 0, 0, 0, 0, 0
877 };
878
879 __attribute__ ((weak))
880 const uint8_t ascii_to_keycode_lut[0x80] PROGMEM = {
881     0, 0, 0, 0, 0, 0, 0, 0,
882     KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
883     0, 0, 0, 0, 0, 0, 0, 0,
884     0, 0, 0, KC_ESC, 0, 0, 0, 0,
885     KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
886     KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
887     KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
888     KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
889     KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
890     KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
891     KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
892     KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
893     KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
894     KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
895     KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
896     KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
897 };
898
899 void send_string(const char *str) {
900   send_string_with_delay(str, 0);
901 }
902
903 void send_string_P(const char *str) {
904   send_string_with_delay_P(str, 0);
905 }
906
907 void send_string_with_delay(const char *str, uint8_t interval) {
908     while (1) {
909         char ascii_code = *str;
910         if (!ascii_code) break;
911         if (ascii_code == SS_TAP_CODE) {
912           // tap
913           uint8_t keycode = *(++str);
914           register_code(keycode);
915           unregister_code(keycode);
916         } else if (ascii_code == SS_DOWN_CODE) {
917           // down
918           uint8_t keycode = *(++str);
919           register_code(keycode);
920         } else if (ascii_code == SS_UP_CODE) {
921           // up
922           uint8_t keycode = *(++str);
923           unregister_code(keycode);
924         } else {
925           send_char(ascii_code);
926         }
927         ++str;
928         // interval
929         { uint8_t ms = interval; while (ms--) wait_ms(1); }
930     }
931 }
932
933 void send_string_with_delay_P(const char *str, uint8_t interval) {
934     while (1) {
935         char ascii_code = pgm_read_byte(str);
936         if (!ascii_code) break;
937         if (ascii_code == SS_TAP_CODE) {
938           // tap
939           uint8_t keycode = pgm_read_byte(++str);
940           register_code(keycode);
941           unregister_code(keycode);
942         } else if (ascii_code == SS_DOWN_CODE) {
943           // down
944           uint8_t keycode = pgm_read_byte(++str);
945           register_code(keycode);
946         } else if (ascii_code == SS_UP_CODE) {
947           // up
948           uint8_t keycode = pgm_read_byte(++str);
949           unregister_code(keycode);
950         } else {
951           send_char(ascii_code);
952         }
953         ++str;
954         // interval
955         { uint8_t ms = interval; while (ms--) wait_ms(1); }
956     }
957 }
958
959 void send_char(char ascii_code) {
960   uint8_t keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
961   bool is_shifted = pgm_read_byte(&ascii_to_shift_lut[(uint8_t)ascii_code]);
962   bool is_altgred = pgm_read_byte(&ascii_to_altgr_lut[(uint8_t)ascii_code]);
963
964   if (is_shifted) {
965     register_code(KC_LSFT);
966   }
967   if (is_altgred) {
968     register_code(KC_RALT);
969   }
970   tap_code(keycode);
971   if (is_altgred) {
972     unregister_code(KC_RALT);
973   }
974   if (is_shifted) {
975     unregister_code(KC_LSFT);
976   }
977 }
978
979 void set_single_persistent_default_layer(uint8_t default_layer) {
980   #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
981     PLAY_SONG(default_layer_songs[default_layer]);
982   #endif
983   eeconfig_update_default_layer(1U<<default_layer);
984   default_layer_set(1U<<default_layer);
985 }
986
987 uint32_t update_tri_layer_state(uint32_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3) {
988   uint32_t mask12 = (1UL << layer1) | (1UL << layer2);
989   uint32_t mask3 = 1UL << layer3;
990   return (state & mask12) == mask12 ? (state | mask3) : (state & ~mask3);
991 }
992
993 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
994   layer_state_set(update_tri_layer_state(layer_state, layer1, layer2, layer3));
995 }
996
997 void tap_random_base64(void) {
998   #if defined(__AVR_ATmega32U4__)
999     uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
1000   #else
1001     uint8_t key = rand() % 64;
1002   #endif
1003   switch (key) {
1004     case 0 ... 25:
1005       register_code(KC_LSFT);
1006       register_code(key + KC_A);
1007       unregister_code(key + KC_A);
1008       unregister_code(KC_LSFT);
1009       break;
1010     case 26 ... 51:
1011       register_code(key - 26 + KC_A);
1012       unregister_code(key - 26 + KC_A);
1013       break;
1014     case 52:
1015       register_code(KC_0);
1016       unregister_code(KC_0);
1017       break;
1018     case 53 ... 61:
1019       register_code(key - 53 + KC_1);
1020       unregister_code(key - 53 + KC_1);
1021       break;
1022     case 62:
1023       register_code(KC_LSFT);
1024       register_code(KC_EQL);
1025       unregister_code(KC_EQL);
1026       unregister_code(KC_LSFT);
1027       break;
1028     case 63:
1029       register_code(KC_SLSH);
1030       unregister_code(KC_SLSH);
1031       break;
1032   }
1033 }
1034
1035 __attribute__((weak))
1036 void bootmagic_lite(void) {
1037   // The lite version of TMK's bootmagic based on Wilba.
1038   // 100% less potential for accidentally making the
1039   // keyboard do stupid things.
1040
1041   // We need multiple scans because debouncing can't be turned off.
1042   matrix_scan();
1043   #if defined(DEBOUNCING_DELAY) && DEBOUNCING_DELAY > 0
1044     wait_ms(DEBOUNCING_DELAY * 2);
1045   #elif defined(DEBOUNCE) && DEBOUNCE > 0
1046     wait_ms(DEBOUNCE * 2);
1047   #else
1048     wait_ms(30);
1049   #endif
1050   matrix_scan();
1051
1052   // If the Esc and space bar are held down on power up,
1053   // reset the EEPROM valid state and jump to bootloader.
1054   // Assumes Esc is at [0,0].
1055   // This isn't very generalized, but we need something that doesn't
1056   // rely on user's keymaps in firmware or EEPROM.
1057   if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
1058     eeconfig_disable();
1059     // Jump to bootloader.
1060     bootloader_jump();
1061   }
1062 }
1063
1064 void matrix_init_quantum() {
1065   #ifdef BOOTMAGIC_LITE
1066     bootmagic_lite();
1067   #endif
1068   if (!eeconfig_is_enabled()) {
1069     eeconfig_init();
1070   }
1071   #ifdef BACKLIGHT_ENABLE
1072     #ifdef LED_MATRIX_ENABLE
1073         led_matrix_init();
1074     #else
1075         backlight_init_ports();
1076     #endif
1077   #endif
1078   #ifdef AUDIO_ENABLE
1079     audio_init();
1080   #endif
1081   #ifdef RGB_MATRIX_ENABLE
1082     rgb_matrix_init();
1083   #endif
1084   #ifdef ENCODER_ENABLE
1085     encoder_init();
1086   #endif
1087   #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
1088     unicode_input_mode_init();
1089   #endif
1090   #ifdef HAPTIC_ENABLE
1091     haptic_init();
1092   #endif
1093   #ifdef OUTPUT_AUTO_ENABLE
1094     set_output(OUTPUT_AUTO);
1095   #endif
1096   #ifdef OLED_DRIVER_ENABLE
1097     oled_init(OLED_ROTATION_0);
1098   #endif
1099   matrix_init_kb();
1100 }
1101
1102 void matrix_scan_quantum() {
1103   #if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE)
1104     matrix_scan_music();
1105   #endif
1106
1107   #ifdef TAP_DANCE_ENABLE
1108     matrix_scan_tap_dance();
1109   #endif
1110
1111   #ifdef COMBO_ENABLE
1112     matrix_scan_combo();
1113   #endif
1114
1115   #if defined(BACKLIGHT_ENABLE)
1116     #if defined(LED_MATRIX_ENABLE)
1117         led_matrix_task();
1118     #elif defined(BACKLIGHT_PIN)
1119         backlight_task();
1120     #endif
1121   #endif
1122
1123   #ifdef RGB_MATRIX_ENABLE
1124     rgb_matrix_task();
1125   #endif
1126
1127   #ifdef ENCODER_ENABLE
1128     encoder_read();
1129   #endif
1130
1131   #ifdef HAPTIC_ENABLE
1132     haptic_task();
1133   #endif
1134
1135   #ifdef OLED_DRIVER_ENABLE
1136     oled_task();
1137   #endif
1138
1139   matrix_scan_kb();
1140 }
1141 #if defined(BACKLIGHT_ENABLE) && (defined(BACKLIGHT_PIN) || defined(BACKLIGHT_PINS))
1142
1143 // The logic is a bit complex, we support 3 setups:
1144 // 1. hardware PWM when backlight is wired to a PWM pin
1145 // depending on this pin, we use a different output compare unit
1146 // 2. software PWM with hardware timers, but the used timer depends
1147 // on the audio setup (audio wins other backlight)
1148 // 3. full software PWM
1149
1150 #if BACKLIGHT_PIN == B7
1151 #  define HARDWARE_PWM
1152 #  define TCCRxA TCCR1A
1153 #  define TCCRxB TCCR1B
1154 #  define COMxx1 COM1C1
1155 #  define OCRxx  OCR1C
1156 #  define ICRx   ICR1
1157 #elif BACKLIGHT_PIN == B6
1158 #  define HARDWARE_PWM
1159 #  define TCCRxA TCCR1A
1160 #  define TCCRxB TCCR1B
1161 #  define COMxx1 COM1B1
1162 #  define OCRxx  OCR1B
1163 #  define ICRx   ICR1
1164 #elif BACKLIGHT_PIN == B5
1165 #  define HARDWARE_PWM
1166 #  define TCCRxA TCCR1A
1167 #  define TCCRxB TCCR1B
1168 #  define COMxx1 COM1A1
1169 #  define OCRxx  OCR1A
1170 #  define ICRx   ICR1
1171 #elif BACKLIGHT_PIN == C6
1172 #  define HARDWARE_PWM
1173 #  define TCCRxA TCCR3A
1174 #  define TCCRxB TCCR3B
1175 #  define COMxx1 COM1A1
1176 #  define OCRxx  OCR3A
1177 #  define ICRx   ICR3
1178 #elif defined(__AVR_ATmega32A__) && BACKLIGHT_PIN == D4
1179 #  define TCCRxA TCCR1A
1180 #  define TCCRxB TCCR1B
1181 #  define COMxx1 COM1B1
1182 #  define OCRxx  OCR1B
1183 #  define ICRx   ICR1
1184 #  define TIMSK1 TIMSK
1185 #else
1186 #  if !defined(BACKLIGHT_CUSTOM_DRIVER)
1187 #    if !defined(B5_AUDIO) && !defined(B6_AUDIO) && !defined(B7_AUDIO)
1188      // timer 1 is not used by audio , backlight can use it
1189 #pragma message "Using hardware timer 1 with software PWM"
1190 #      define HARDWARE_PWM
1191 #      define BACKLIGHT_PWM_TIMER
1192 #      define TCCRxA TCCR1A
1193 #      define TCCRxB TCCR1B
1194 #      define OCRxx  OCR1A
1195 #      define OCRxAH OCR1AH
1196 #      define OCRxAL OCR1AL
1197 #      define TIMERx_COMPA_vect TIMER1_COMPA_vect
1198 #      define TIMERx_OVF_vect TIMER1_OVF_vect
1199 #      define OCIExA OCIE1A
1200 #      define TOIEx  TOIE1
1201 #      define ICRx   ICR1
1202 #      ifndef TIMSK
1203 #        define TIMSK TIMSK1
1204 #      endif
1205 #    elif !defined(C6_AUDIO) && !defined(C5_AUDIO) && !defined(C4_AUDIO)
1206 #pragma message "Using hardware timer 3 with software PWM"
1207 // timer 3 is not used by audio, backlight can use it
1208 #      define HARDWARE_PWM
1209 #      define BACKLIGHT_PWM_TIMER
1210 #      define TCCRxA TCCR3A
1211 #      define TCCRxB TCCR3B
1212 #      define OCRxx OCR3A
1213 #      define OCRxAH OCR3AH
1214 #      define OCRxAL OCR3AL
1215 #      define TIMERx_COMPA_vect TIMER3_COMPA_vect
1216 #      define TIMERx_OVF_vect TIMER3_OVF_vect
1217 #      define OCIExA OCIE3A
1218 #      define TOIEx  TOIE3
1219 #      define ICRx   ICR1
1220 #      ifndef TIMSK
1221 #        define TIMSK TIMSK3
1222 #      endif
1223 #    else
1224 #pragma message "Audio in use - using pure software PWM"
1225 #define NO_HARDWARE_PWM
1226 #    endif
1227 #  else
1228 #pragma message "Custom driver defined - using pure software PWM"
1229 #define NO_HARDWARE_PWM
1230 #  endif
1231 #endif
1232
1233 #ifndef BACKLIGHT_ON_STATE
1234 #define BACKLIGHT_ON_STATE 0
1235 #endif
1236
1237 void backlight_on(uint8_t backlight_pin) {
1238 #if BACKLIGHT_ON_STATE == 0
1239   writePinLow(backlight_pin);
1240 #else
1241   writePinHigh(backlight_pin);
1242 #endif
1243 }
1244
1245 void backlight_off(uint8_t backlight_pin) {
1246 #if BACKLIGHT_ON_STATE == 0
1247   writePinHigh(backlight_pin);
1248 #else
1249   writePinLow(backlight_pin);
1250 #endif
1251 }
1252
1253
1254 #if defined(NO_HARDWARE_PWM) || defined(BACKLIGHT_PWM_TIMER)  // pwm through software
1255
1256 // we support multiple backlight pins
1257 #ifndef BACKLIGHT_LED_COUNT
1258 #define BACKLIGHT_LED_COUNT 1
1259 #endif
1260
1261 #if BACKLIGHT_LED_COUNT == 1
1262 #define BACKLIGHT_PIN_INIT { BACKLIGHT_PIN }
1263 #else
1264 #define BACKLIGHT_PIN_INIT BACKLIGHT_PINS
1265 #endif
1266
1267 #define FOR_EACH_LED(x)                             \
1268   for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) \
1269   {                                                 \
1270     uint8_t backlight_pin = backlight_pins[i];      \
1271     { \
1272       x                         \
1273     }                                             \
1274   }
1275
1276 static const uint8_t backlight_pins[BACKLIGHT_LED_COUNT] = BACKLIGHT_PIN_INIT;
1277
1278 #else // full hardware PWM
1279
1280 // we support only one backlight pin
1281 static const uint8_t backlight_pin = BACKLIGHT_PIN;
1282 #define FOR_EACH_LED(x) x
1283
1284 #endif
1285
1286 #ifdef NO_HARDWARE_PWM
1287 __attribute__((weak))
1288 void backlight_init_ports(void)
1289 {
1290   // Setup backlight pin as output and output to on state.
1291   FOR_EACH_LED(
1292     setPinOutput(backlight_pin);
1293     backlight_on(backlight_pin);
1294   )
1295 }
1296
1297 __attribute__ ((weak))
1298 void backlight_set(uint8_t level) {}
1299
1300 uint8_t backlight_tick = 0;
1301
1302 #ifndef BACKLIGHT_CUSTOM_DRIVER
1303 void backlight_task(void) {
1304   if ((0xFFFF >> ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
1305     FOR_EACH_LED(
1306       backlight_on(backlight_pin);
1307     )
1308   }
1309   else {
1310     FOR_EACH_LED(
1311       backlight_off(backlight_pin);
1312     )
1313   }
1314   backlight_tick = (backlight_tick + 1) % 16;
1315 }
1316 #endif
1317
1318 #ifdef BACKLIGHT_BREATHING
1319   #ifndef BACKLIGHT_CUSTOM_DRIVER
1320   #error "Backlight breathing only available with hardware PWM. Please disable."
1321   #endif
1322 #endif
1323
1324 #else // hardware pwm through timer
1325
1326 #ifdef BACKLIGHT_PWM_TIMER
1327
1328 // The idea of software PWM assisted by hardware timers is the following
1329 // we use the hardware timer in fast PWM mode like for hardware PWM, but
1330 // instead of letting the Output Match Comparator control the led pin
1331 // (which is not possible since the backlight is not wired to PWM pins on the
1332 // CPU), we do the LED on/off by oursleves.
1333 // The timer is setup to count up to 0xFFFF, and we set the Output Compare
1334 // register to the current 16bits backlight level (after CIE correction). 
1335 // This means the CPU will trigger a compare match interrupt when the counter 
1336 // reaches the backlight level, where we turn off the LEDs, 
1337 // but also an overflow interrupt when the counter rolls back to 0, 
1338 // in which we're going to turn on the LEDs.
1339 // The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz.
1340
1341 // Triggered when the counter reaches the OCRx value
1342 ISR(TIMERx_COMPA_vect) {
1343   FOR_EACH_LED(
1344     backlight_off(backlight_pin);
1345   )
1346 }
1347
1348 // Triggered when the counter reaches the TOP value
1349 // this one triggers at F_CPU/65536 =~ 244 Hz 
1350 ISR(TIMERx_OVF_vect) {
1351 #ifdef BACKLIGHT_BREATHING
1352   breathing_task();
1353 #endif
1354   // for very small values of OCRxx (or backlight level)
1355   // we can't guarantee this whole code won't execute
1356   // at the same time as the compare match interrupt
1357   // which means that we might turn on the leds while
1358   // trying to turn them off, leading to flickering
1359   // artifacts (especially while breathing, because breathing_task
1360   // takes many computation cycles).
1361   // so better not turn them on while the counter TOP is very low.
1362   if (OCRxx > 256) {
1363     FOR_EACH_LED(
1364       backlight_on(backlight_pin);
1365     )
1366   }
1367 }
1368
1369 #endif
1370
1371 #define TIMER_TOP 0xFFFFU
1372
1373 // See http://jared.geek.nz/2013/feb/linear-led-pwm
1374 static uint16_t cie_lightness(uint16_t v) {
1375   if (v <= 5243) // if below 8% of max
1376     return v / 9; // same as dividing by 900%
1377   else {
1378     uint32_t y = (((uint32_t) v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
1379     // to get a useful result with integer division, we shift left in the expression above
1380     // and revert what we've done again after squaring.
1381     y = y * y * y >> 8;
1382     if (y > 0xFFFFUL) // prevent overflow
1383       return 0xFFFFU;
1384     else
1385       return (uint16_t) y;
1386   }
1387 }
1388
1389 // range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val.
1390 static inline void set_pwm(uint16_t val) {
1391         OCRxx = val;
1392 }
1393
1394 #ifndef BACKLIGHT_CUSTOM_DRIVER
1395 __attribute__ ((weak))
1396 void backlight_set(uint8_t level) {
1397   if (level > BACKLIGHT_LEVELS)
1398     level = BACKLIGHT_LEVELS;
1399
1400   if (level == 0) {
1401     #ifdef BACKLIGHT_PWM_TIMER
1402       if (OCRxx) {
1403         TIMSK &= ~(_BV(OCIExA));
1404         TIMSK &= ~(_BV(TOIEx));
1405         FOR_EACH_LED(
1406           backlight_off(backlight_pin);
1407         )
1408       }
1409     #else
1410     // Turn off PWM control on backlight pin
1411     TCCRxA &= ~(_BV(COMxx1));
1412     #endif
1413   } else {
1414     #ifdef BACKLIGHT_PWM_TIMER
1415       if (!OCRxx) {
1416         TIMSK |= _BV(OCIExA);
1417         TIMSK |= _BV(TOIEx);
1418       }
1419     #else
1420     // Turn on PWM control of backlight pin
1421     TCCRxA |= _BV(COMxx1);
1422     #endif
1423   }
1424   // Set the brightness
1425   set_pwm(cie_lightness(TIMER_TOP * (uint32_t)level / BACKLIGHT_LEVELS));
1426 }
1427
1428 void backlight_task(void) {}
1429 #endif  // BACKLIGHT_CUSTOM_DRIVER
1430
1431 #ifdef BACKLIGHT_BREATHING
1432
1433 #define BREATHING_NO_HALT  0
1434 #define BREATHING_HALT_OFF 1
1435 #define BREATHING_HALT_ON  2
1436 #define BREATHING_STEPS 128
1437
1438 static uint8_t breathing_period = BREATHING_PERIOD;
1439 static uint8_t breathing_halt = BREATHING_NO_HALT;
1440 static uint16_t breathing_counter = 0;
1441
1442 #ifdef BACKLIGHT_PWM_TIMER
1443 static bool breathing = false;
1444
1445 bool is_breathing(void) {
1446   return breathing;
1447 }
1448
1449 #define breathing_interrupt_enable() do { breathing = true; } while (0)
1450 #define breathing_interrupt_disable() do { breathing = false; } while (0)
1451 #else
1452
1453 bool is_breathing(void) {
1454     return !!(TIMSK1 & _BV(TOIE1));
1455 }
1456
1457 #define breathing_interrupt_enable() do {TIMSK1 |= _BV(TOIE1);} while (0)
1458 #define breathing_interrupt_disable() do {TIMSK1 &= ~_BV(TOIE1);} while (0)
1459 #endif
1460
1461 #define breathing_min() do {breathing_counter = 0;} while (0)
1462 #define breathing_max() do {breathing_counter = breathing_period * 244 / 2;} while (0)
1463
1464 void breathing_enable(void)
1465 {
1466   breathing_counter = 0;
1467   breathing_halt = BREATHING_NO_HALT;
1468   breathing_interrupt_enable();
1469 }
1470
1471 void breathing_pulse(void)
1472 {
1473     if (get_backlight_level() == 0)
1474       breathing_min();
1475     else
1476       breathing_max();
1477     breathing_halt = BREATHING_HALT_ON;
1478     breathing_interrupt_enable();
1479 }
1480
1481 void breathing_disable(void)
1482 {
1483     breathing_interrupt_disable();
1484     // Restore backlight level
1485     backlight_set(get_backlight_level());
1486 }
1487
1488 void breathing_self_disable(void)
1489 {
1490   if (get_backlight_level() == 0)
1491     breathing_halt = BREATHING_HALT_OFF;
1492   else
1493     breathing_halt = BREATHING_HALT_ON;
1494 }
1495
1496 void breathing_toggle(void) {
1497   if (is_breathing())
1498     breathing_disable();
1499   else
1500     breathing_enable();
1501 }
1502
1503 void breathing_period_set(uint8_t value)
1504 {
1505   if (!value)
1506     value = 1;
1507   breathing_period = value;
1508 }
1509
1510 void breathing_period_default(void) {
1511   breathing_period_set(BREATHING_PERIOD);
1512 }
1513
1514 void breathing_period_inc(void)
1515 {
1516   breathing_period_set(breathing_period+1);
1517 }
1518
1519 void breathing_period_dec(void)
1520 {
1521   breathing_period_set(breathing_period-1);
1522 }
1523
1524 /* To generate breathing curve in python:
1525  * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
1526  */
1527 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};
1528
1529 // Use this before the cie_lightness function.
1530 static inline uint16_t scale_backlight(uint16_t v) {
1531   return v / BACKLIGHT_LEVELS * get_backlight_level();
1532 }
1533
1534 #ifdef BACKLIGHT_PWM_TIMER
1535 void breathing_task(void)
1536 #else
1537 /* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run
1538  * about 244 times per second.
1539  */
1540 ISR(TIMER1_OVF_vect)
1541 #endif
1542 {
1543   uint16_t interval = (uint16_t) breathing_period * 244 / BREATHING_STEPS;
1544   // resetting after one period to prevent ugly reset at overflow.
1545   breathing_counter = (breathing_counter + 1) % (breathing_period * 244);
1546   uint8_t index = breathing_counter / interval % BREATHING_STEPS;
1547
1548   if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) ||
1549       ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1)))
1550   {
1551       breathing_interrupt_disable();
1552   }
1553
1554   set_pwm(cie_lightness(scale_backlight((uint16_t) pgm_read_byte(&breathing_table[index]) * 0x0101U)));
1555 }
1556
1557 #endif // BACKLIGHT_BREATHING
1558
1559 __attribute__ ((weak))
1560 void backlight_init_ports(void)
1561 {
1562   // Setup backlight pin as output and output to on state.
1563   FOR_EACH_LED(
1564     setPinOutput(backlight_pin);
1565     backlight_on(backlight_pin);
1566   )
1567
1568   // I could write a wall of text here to explain... but TL;DW
1569   // Go read the ATmega32u4 datasheet.
1570   // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
1571
1572 #ifdef BACKLIGHT_PWM_TIMER
1573   // TimerX setup, Fast PWM mode count to TOP set in ICRx
1574   TCCRxA = _BV(WGM11); // = 0b00000010;
1575   // clock select clk/1
1576   TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
1577 #else // hardware PWM
1578   // Pin PB7 = OCR1C (Timer 1, Channel C)
1579   // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
1580   // (i.e. start high, go low when counter matches.)
1581   // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
1582   // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
1583
1584   /*
1585   14.8.3:
1586   "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 [..]."
1587   "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)."
1588   */
1589   TCCRxA = _BV(COMxx1) | _BV(WGM11);            // = 0b00001010;
1590   TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
1591 #endif
1592   // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0.
1593   ICRx = TIMER_TOP;
1594
1595   backlight_init();
1596   #ifdef BACKLIGHT_BREATHING
1597     breathing_enable();
1598   #endif
1599 }
1600
1601 #endif // hardware backlight
1602
1603 #else // no backlight
1604
1605 __attribute__ ((weak))
1606 void backlight_init_ports(void) {}
1607
1608 __attribute__ ((weak))
1609 void backlight_set(uint8_t level) {}
1610
1611 #endif // backlight
1612
1613 #ifdef HD44780_ENABLED
1614 #include "hd44780.h"
1615 #endif
1616
1617
1618 // Functions for spitting out values
1619 //
1620
1621 void send_dword(uint32_t number) { // this might not actually work
1622     uint16_t word = (number >> 16);
1623     send_word(word);
1624     send_word(number & 0xFFFFUL);
1625 }
1626
1627 void send_word(uint16_t number) {
1628     uint8_t byte = number >> 8;
1629     send_byte(byte);
1630     send_byte(number & 0xFF);
1631 }
1632
1633 void send_byte(uint8_t number) {
1634     uint8_t nibble = number >> 4;
1635     send_nibble(nibble);
1636     send_nibble(number & 0xF);
1637 }
1638
1639 void send_nibble(uint8_t number) {
1640     switch (number) {
1641         case 0:
1642             register_code(KC_0);
1643             unregister_code(KC_0);
1644             break;
1645         case 1 ... 9:
1646             register_code(KC_1 + (number - 1));
1647             unregister_code(KC_1 + (number - 1));
1648             break;
1649         case 0xA ... 0xF:
1650             register_code(KC_A + (number - 0xA));
1651             unregister_code(KC_A + (number - 0xA));
1652             break;
1653     }
1654 }
1655
1656
1657 __attribute__((weak))
1658 uint16_t hex_to_keycode(uint8_t hex)
1659 {
1660   hex = hex & 0xF;
1661   if (hex == 0x0) {
1662     return KC_0;
1663   } else if (hex < 0xA) {
1664     return KC_1 + (hex - 0x1);
1665   } else {
1666     return KC_A + (hex - 0xA);
1667   }
1668 }
1669
1670 void api_send_unicode(uint32_t unicode) {
1671 #ifdef API_ENABLE
1672     uint8_t chunk[4];
1673     dword_to_bytes(unicode, chunk);
1674     MT_SEND_DATA(DT_UNICODE, chunk, 5);
1675 #endif
1676 }
1677
1678 __attribute__ ((weak))
1679 void led_set_user(uint8_t usb_led) {
1680
1681 }
1682
1683 __attribute__ ((weak))
1684 void led_set_kb(uint8_t usb_led) {
1685     led_set_user(usb_led);
1686 }
1687
1688 __attribute__ ((weak))
1689 void led_init_ports(void)
1690 {
1691
1692 }
1693
1694 __attribute__ ((weak))
1695 void led_set(uint8_t usb_led)
1696 {
1697
1698   // Example LED Code
1699   //
1700     // // Using PE6 Caps Lock LED
1701     // if (usb_led & (1<<USB_LED_CAPS_LOCK))
1702     // {
1703     //     // Output high.
1704     //     DDRE |= (1<<6);
1705     //     PORTE |= (1<<6);
1706     // }
1707     // else
1708     // {
1709     //     // Output low.
1710     //     DDRE &= ~(1<<6);
1711     //     PORTE &= ~(1<<6);
1712     // }
1713
1714 #if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
1715   // Use backlight as Caps Lock indicator
1716   uint8_t bl_toggle_lvl = 0;
1717
1718   if (IS_LED_ON(usb_led, USB_LED_CAPS_LOCK) && !backlight_config.enable) {
1719     // Turning Caps Lock ON and backlight is disabled in config
1720     // Toggling backlight to the brightest level
1721     bl_toggle_lvl = BACKLIGHT_LEVELS;
1722   } else if (IS_LED_OFF(usb_led, USB_LED_CAPS_LOCK) && backlight_config.enable) {
1723     // Turning Caps Lock OFF and backlight is enabled in config
1724     // Toggling backlight and restoring config level
1725     bl_toggle_lvl = backlight_config.level;
1726   }
1727
1728   // Set level without modify backlight_config to keep ability to restore state
1729   backlight_set(bl_toggle_lvl);
1730 #endif
1731
1732   led_set_kb(usb_led);
1733 }
1734
1735
1736 //------------------------------------------------------------------------------
1737 // Override these functions in your keymap file to play different tunes on
1738 // different events such as startup and bootloader jump
1739
1740 __attribute__ ((weak))
1741 void startup_user() {}
1742
1743 __attribute__ ((weak))
1744 void shutdown_user() {}
1745
1746 //------------------------------------------------------------------------------