]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/quantum.c
Added Auto Shift, tap key = normal, hold key = shifted state.
[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 #ifdef PROTOCOL_LUFA
19 #include "outputselect.h"
20 #endif
21
22 #ifndef TAPPING_TERM
23 #define TAPPING_TERM 200
24 #endif
25
26 #include "backlight.h"
27 extern backlight_config_t backlight_config;
28
29 #ifdef FAUXCLICKY_ENABLE
30 #include "fauxclicky.h"
31 #endif
32
33 #ifdef AUDIO_ENABLE
34   #ifndef GOODBYE_SONG
35     #define GOODBYE_SONG SONG(GOODBYE_SOUND)
36   #endif
37   #ifndef AG_NORM_SONG
38     #define AG_NORM_SONG SONG(AG_NORM_SOUND)
39   #endif
40   #ifndef AG_SWAP_SONG
41     #define AG_SWAP_SONG SONG(AG_SWAP_SOUND)
42   #endif
43   float goodbye_song[][2] = GOODBYE_SONG;
44   float ag_norm_song[][2] = AG_NORM_SONG;
45   float ag_swap_song[][2] = AG_SWAP_SONG;
46   #ifdef DEFAULT_LAYER_SONGS
47     float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
48   #endif
49 #endif
50
51 static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
52   switch (code) {
53   case QK_MODS ... QK_MODS_MAX:
54     break;
55   default:
56     return;
57   }
58
59   if (code & QK_LCTL)
60     f(KC_LCTL);
61   if (code & QK_LSFT)
62     f(KC_LSFT);
63   if (code & QK_LALT)
64     f(KC_LALT);
65   if (code & QK_LGUI)
66     f(KC_LGUI);
67
68   if (code < QK_RMODS_MIN) return;
69
70   if (code & QK_RCTL)
71     f(KC_RCTL);
72   if (code & QK_RSFT)
73     f(KC_RSFT);
74   if (code & QK_RALT)
75     f(KC_RALT);
76   if (code & QK_RGUI)
77     f(KC_RGUI);
78 }
79
80 static inline void qk_register_weak_mods(uint8_t kc) {
81     add_weak_mods(MOD_BIT(kc));
82     send_keyboard_report();
83 }
84
85 static inline void qk_unregister_weak_mods(uint8_t kc) {
86     del_weak_mods(MOD_BIT(kc));
87     send_keyboard_report();
88 }
89
90 static inline void qk_register_mods(uint8_t kc) {
91     add_weak_mods(MOD_BIT(kc));
92     send_keyboard_report();
93 }
94
95 static inline void qk_unregister_mods(uint8_t kc) {
96     del_weak_mods(MOD_BIT(kc));
97     send_keyboard_report();
98 }
99
100 void register_code16 (uint16_t code) {
101   if (IS_MOD(code) || code == KC_NO) {
102       do_code16 (code, qk_register_mods);
103   } else {
104       do_code16 (code, qk_register_weak_mods);
105   }
106   register_code (code);
107 }
108
109 void unregister_code16 (uint16_t code) {
110   unregister_code (code);
111   if (IS_MOD(code) || code == KC_NO) {
112       do_code16 (code, qk_unregister_mods);
113   } else {
114       do_code16 (code, qk_unregister_weak_mods);
115   }
116 }
117
118 __attribute__ ((weak))
119 bool process_action_kb(keyrecord_t *record) {
120   return true;
121 }
122
123 __attribute__ ((weak))
124 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
125   return process_record_user(keycode, record);
126 }
127
128 __attribute__ ((weak))
129 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
130   return true;
131 }
132
133 void reset_keyboard(void) {
134   clear_keyboard();
135 #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_ENABLE_BASIC))
136   music_all_notes_off();
137   uint16_t timer_start = timer_read();
138   PLAY_SONG(goodbye_song);
139   shutdown_user();
140   while(timer_elapsed(timer_start) < 250) 
141     wait_ms(1);
142   stop_all_notes();
143 #else
144   wait_ms(250);
145 #endif
146 #ifdef CATERINA_BOOTLOADER
147   *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
148 #endif
149   bootloader_jump();
150 }
151
152 // Shift / paren setup
153
154 #ifndef LSPO_KEY
155   #define LSPO_KEY KC_9
156 #endif
157 #ifndef RSPC_KEY
158   #define RSPC_KEY KC_0
159 #endif
160
161 static bool shift_interrupted[2] = {0, 0};
162 static uint16_t scs_timer[2] = {0, 0};
163
164 /* true if the last press of GRAVE_ESC was shifted (i.e. GUI or SHIFT were pressed), false otherwise.
165  * Used to ensure that the correct keycode is released if the key is released.
166  */
167 static bool grave_esc_was_shifted = false;
168
169 bool process_record_quantum(keyrecord_t *record) {
170
171   /* This gets the keycode from the key pressed */
172   keypos_t key = record->event.key;
173   uint16_t keycode;
174
175   #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
176     /* TODO: Use store_or_get_action() or a similar function. */
177     if (!disable_action_cache) {
178       uint8_t layer;
179
180       if (record->event.pressed) {
181         layer = layer_switch_get_layer(key);
182         update_source_layers_cache(key, layer);
183       } else {
184         layer = read_source_layers_cache(key);
185       }
186       keycode = keymap_key_to_keycode(layer, key);
187     } else
188   #endif
189     keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
190
191     // This is how you use actions here
192     // if (keycode == KC_LEAD) {
193     //   action_t action;
194     //   action.code = ACTION_DEFAULT_LAYER_SET(0);
195     //   process_action(record, action);
196     //   return false;
197     // }
198
199   if (!(
200   #if defined(KEY_LOCK_ENABLE)
201     // Must run first to be able to mask key_up events.
202     process_key_lock(&keycode, record) &&
203   #endif
204     process_record_kb(keycode, record) &&
205   #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
206     process_midi(keycode, record) &&
207   #endif
208   #ifdef AUDIO_ENABLE
209     process_audio(keycode, record) &&
210   #endif
211   #ifdef STENO_ENABLE
212     process_steno(keycode, record) &&
213   #endif
214   #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
215     process_music(keycode, record) &&
216   #endif
217   #ifdef TAP_DANCE_ENABLE
218     process_tap_dance(keycode, record) &&
219   #endif
220   #ifndef DISABLE_LEADER
221     process_leader(keycode, record) &&
222   #endif
223   #ifndef DISABLE_CHORDING
224     process_chording(keycode, record) &&
225   #endif
226   #ifdef COMBO_ENABLE
227     process_combo(keycode, record) &&
228   #endif
229   #ifdef UNICODE_ENABLE
230     process_unicode(keycode, record) &&
231   #endif
232   #ifdef UCIS_ENABLE
233     process_ucis(keycode, record) &&
234   #endif
235   #ifdef PRINTING_ENABLE
236     process_printer(keycode, record) &&
237   #endif
238   #ifdef AUTO_SHIFT_ENABLE
239     process_auto_shift(keycode, record) &&
240   #endif
241   #ifdef UNICODEMAP_ENABLE
242     process_unicode_map(keycode, record) &&
243   #endif
244   #ifdef TERMINAL_ENABLE
245     process_terminal(keycode, record) &&
246   #endif
247       true)) {
248     return false;
249   }
250
251   // Shift / paren setup
252
253   switch(keycode) {
254     case RESET:
255       if (record->event.pressed) {
256         reset_keyboard();
257       }
258     return false;
259     case DEBUG:
260       if (record->event.pressed) {
261           debug_enable = true;
262           print("DEBUG: enabled.\n");
263       }
264     return false;
265   #ifdef FAUXCLICKY_ENABLE
266   case FC_TOG:
267     if (record->event.pressed) {
268       FAUXCLICKY_TOGGLE;
269     }
270     return false;
271   case FC_ON:
272     if (record->event.pressed) {
273       FAUXCLICKY_ON;
274     }
275     return false;
276   case FC_OFF:
277     if (record->event.pressed) {
278       FAUXCLICKY_OFF;
279     }
280     return false;
281   #endif
282   #ifdef RGBLIGHT_ENABLE
283   case RGB_TOG:
284     if (record->event.pressed) {
285       rgblight_toggle();
286     }
287     return false;
288   case RGB_MOD:
289     if (record->event.pressed) {
290       rgblight_step();
291     }
292     return false;
293   case RGB_HUI:
294     if (record->event.pressed) {
295       rgblight_increase_hue();
296     }
297     return false;
298   case RGB_HUD:
299     if (record->event.pressed) {
300       rgblight_decrease_hue();
301     }
302     return false;
303   case RGB_SAI:
304     if (record->event.pressed) {
305       rgblight_increase_sat();
306     }
307     return false;
308   case RGB_SAD:
309     if (record->event.pressed) {
310       rgblight_decrease_sat();
311     }
312     return false;
313   case RGB_VAI:
314     if (record->event.pressed) {
315       rgblight_increase_val();
316     }
317     return false;
318   case RGB_VAD:
319     if (record->event.pressed) {
320       rgblight_decrease_val();
321     }
322     return false;
323   case RGB_MODE_PLAIN:
324     if (record->event.pressed) {
325       rgblight_mode(1);
326     }
327     return false;
328   case RGB_MODE_BREATHE:
329     if (record->event.pressed) {
330       if ((2 <= rgblight_get_mode()) && (rgblight_get_mode() < 5)) {
331         rgblight_step();
332       } else {
333         rgblight_mode(2);
334       }
335     }
336     return false;
337   case RGB_MODE_RAINBOW:
338     if (record->event.pressed) {
339       if ((6 <= rgblight_get_mode()) && (rgblight_get_mode() < 8)) {
340         rgblight_step();
341       } else {
342         rgblight_mode(6);
343       }
344     }
345     return false;
346   case RGB_MODE_SWIRL:
347     if (record->event.pressed) {
348       if ((9 <= rgblight_get_mode()) && (rgblight_get_mode() < 14)) {
349         rgblight_step();
350       } else {
351         rgblight_mode(9);
352       }
353     }
354     return false;
355   case RGB_MODE_SNAKE:
356     if (record->event.pressed) {
357       if ((15 <= rgblight_get_mode()) && (rgblight_get_mode() < 20)) {
358         rgblight_step();
359       } else {
360         rgblight_mode(15);
361       }
362     }
363     return false;
364   case RGB_MODE_KNIGHT:
365     if (record->event.pressed) {
366       if ((21 <= rgblight_get_mode()) && (rgblight_get_mode() < 23)) {
367         rgblight_step();
368       } else {
369         rgblight_mode(21);
370       }
371     }
372     return false;
373   case RGB_MODE_XMAS:
374     if (record->event.pressed) {
375       rgblight_mode(24);
376     }
377     return false;
378   case RGB_MODE_GRADIENT:
379     if (record->event.pressed) {
380       if ((25 <= rgblight_get_mode()) && (rgblight_get_mode() < 34)) {
381         rgblight_step();
382       } else {
383         rgblight_mode(25);
384       }
385     }
386     return false;
387   #endif
388     #ifdef PROTOCOL_LUFA
389     case OUT_AUTO:
390       if (record->event.pressed) {
391         set_output(OUTPUT_AUTO);
392       }
393       return false;
394     case OUT_USB:
395       if (record->event.pressed) {
396         set_output(OUTPUT_USB);
397       }
398       return false;
399     #ifdef BLUETOOTH_ENABLE
400     case OUT_BT:
401       if (record->event.pressed) {
402         set_output(OUTPUT_BLUETOOTH);
403       }
404       return false;
405     #endif
406     #endif
407     case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
408       if (record->event.pressed) {
409         // MAGIC actions (BOOTMAGIC without the boot)
410         if (!eeconfig_is_enabled()) {
411             eeconfig_init();
412         }
413         /* keymap config */
414         keymap_config.raw = eeconfig_read_keymap();
415         switch (keycode)
416         {
417           case MAGIC_SWAP_CONTROL_CAPSLOCK:
418             keymap_config.swap_control_capslock = true;
419             break;
420           case MAGIC_CAPSLOCK_TO_CONTROL:
421             keymap_config.capslock_to_control = true;
422             break;
423           case MAGIC_SWAP_LALT_LGUI:
424             keymap_config.swap_lalt_lgui = true;
425             break;
426           case MAGIC_SWAP_RALT_RGUI:
427             keymap_config.swap_ralt_rgui = true;
428             break;
429           case MAGIC_NO_GUI:
430             keymap_config.no_gui = true;
431             break;
432           case MAGIC_SWAP_GRAVE_ESC:
433             keymap_config.swap_grave_esc = true;
434             break;
435           case MAGIC_SWAP_BACKSLASH_BACKSPACE:
436             keymap_config.swap_backslash_backspace = true;
437             break;
438           case MAGIC_HOST_NKRO:
439             keymap_config.nkro = true;
440             break;
441           case MAGIC_SWAP_ALT_GUI:
442             keymap_config.swap_lalt_lgui = true;
443             keymap_config.swap_ralt_rgui = true;
444             #ifdef AUDIO_ENABLE
445               PLAY_SONG(ag_swap_song);
446             #endif
447             break;
448           case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
449             keymap_config.swap_control_capslock = false;
450             break;
451           case MAGIC_UNCAPSLOCK_TO_CONTROL:
452             keymap_config.capslock_to_control = false;
453             break;
454           case MAGIC_UNSWAP_LALT_LGUI:
455             keymap_config.swap_lalt_lgui = false;
456             break;
457           case MAGIC_UNSWAP_RALT_RGUI:
458             keymap_config.swap_ralt_rgui = false;
459             break;
460           case MAGIC_UNNO_GUI:
461             keymap_config.no_gui = false;
462             break;
463           case MAGIC_UNSWAP_GRAVE_ESC:
464             keymap_config.swap_grave_esc = false;
465             break;
466           case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
467             keymap_config.swap_backslash_backspace = false;
468             break;
469           case MAGIC_UNHOST_NKRO:
470             keymap_config.nkro = false;
471             break;
472           case MAGIC_UNSWAP_ALT_GUI:
473             keymap_config.swap_lalt_lgui = false;
474             keymap_config.swap_ralt_rgui = false;
475             #ifdef AUDIO_ENABLE
476               PLAY_SONG(ag_norm_song);
477             #endif
478             break;
479           case MAGIC_TOGGLE_NKRO:
480             keymap_config.nkro = !keymap_config.nkro;
481             break;
482           default:
483             break;
484         }
485         eeconfig_update_keymap(keymap_config.raw);
486         clear_keyboard(); // clear to prevent stuck keys
487
488         return false;
489       }
490       break;
491     case KC_LSPO: {
492       if (record->event.pressed) {
493         shift_interrupted[0] = false;
494         scs_timer[0] = timer_read ();
495         register_mods(MOD_BIT(KC_LSFT));
496       }
497       else {
498         #ifdef DISABLE_SPACE_CADET_ROLLOVER
499           if (get_mods() & MOD_BIT(KC_RSFT)) {
500             shift_interrupted[0] = true;
501             shift_interrupted[1] = true;
502           }
503         #endif
504         if (!shift_interrupted[0] && timer_elapsed(scs_timer[0]) < TAPPING_TERM) {
505           register_code(LSPO_KEY);
506           unregister_code(LSPO_KEY);
507         }
508         unregister_mods(MOD_BIT(KC_LSFT));
509       }
510       return false;
511     }
512
513     case KC_RSPC: {
514       if (record->event.pressed) {
515         shift_interrupted[1] = false;
516         scs_timer[1] = timer_read ();
517         register_mods(MOD_BIT(KC_RSFT));
518       }
519       else {
520         #ifdef DISABLE_SPACE_CADET_ROLLOVER
521           if (get_mods() & MOD_BIT(KC_LSFT)) {
522             shift_interrupted[0] = true;
523             shift_interrupted[1] = true;
524           }
525         #endif
526         if (!shift_interrupted[1] && timer_elapsed(scs_timer[1]) < TAPPING_TERM) {
527           register_code(RSPC_KEY);
528           unregister_code(RSPC_KEY);
529         }
530         unregister_mods(MOD_BIT(KC_RSFT));
531       }
532       return false;
533     }
534     case GRAVE_ESC: {
535       uint8_t shifted = get_mods() & ((MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)
536                                       |MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)));
537
538 #ifdef GRAVE_ESC_CTRL_OVERRIDE
539       // if CTRL is pressed, ESC is always read as ESC, even if SHIFT or GUI is pressed.
540       // this is handy for the ctrl+shift+esc shortcut on windows, among other things.
541       if (get_mods() & (MOD_BIT(KC_LCTL) | MOD_BIT(KC_RCTL)))
542         shifted = 0;
543 #endif
544
545       if (record->event.pressed) {
546         grave_esc_was_shifted = shifted;
547         add_key(shifted ? KC_GRAVE : KC_ESCAPE);
548       }
549       else {
550         del_key(grave_esc_was_shifted ? KC_GRAVE : KC_ESCAPE);
551       }
552
553       send_keyboard_report();
554     }
555     default: {
556       shift_interrupted[0] = true;
557       shift_interrupted[1] = true;
558       break;
559     }
560   }
561
562   return process_action_kb(record);
563 }
564
565 __attribute__ ((weak))
566 const bool ascii_to_shift_lut[0x80] PROGMEM = {
567     0, 0, 0, 0, 0, 0, 0, 0,
568     0, 0, 0, 0, 0, 0, 0, 0,
569     0, 0, 0, 0, 0, 0, 0, 0,
570     0, 0, 0, 0, 0, 0, 0, 0,
571     0, 1, 1, 1, 1, 1, 1, 0,
572     1, 1, 1, 1, 0, 0, 0, 0,
573     0, 0, 0, 0, 0, 0, 0, 0,
574     0, 0, 1, 0, 1, 0, 1, 1,
575     1, 1, 1, 1, 1, 1, 1, 1,
576     1, 1, 1, 1, 1, 1, 1, 1,
577     1, 1, 1, 1, 1, 1, 1, 1,
578     1, 1, 1, 0, 0, 0, 1, 1,
579     0, 0, 0, 0, 0, 0, 0, 0,
580     0, 0, 0, 0, 0, 0, 0, 0,
581     0, 0, 0, 0, 0, 0, 0, 0,
582     0, 0, 0, 1, 1, 1, 1, 0
583 };
584
585 __attribute__ ((weak))
586 const uint8_t ascii_to_keycode_lut[0x80] PROGMEM = {
587     0, 0, 0, 0, 0, 0, 0, 0,
588     KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
589     0, 0, 0, 0, 0, 0, 0, 0,
590     0, 0, 0, KC_ESC, 0, 0, 0, 0,
591     KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
592     KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
593     KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
594     KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
595     KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
596     KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
597     KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
598     KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
599     KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
600     KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
601     KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
602     KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
603 };
604
605 void send_string(const char *str) {
606   send_string_with_delay(str, 0);
607 }
608
609 void send_string_P(const char *str) {
610   send_string_with_delay_P(str, 0);
611 }
612
613 void send_string_with_delay(const char *str, uint8_t interval) {
614     while (1) {
615         char ascii_code = *str;
616         if (!ascii_code) break;
617         if (ascii_code == 1) {
618           // tap
619           uint8_t keycode = *(++str);
620           register_code(keycode);
621           unregister_code(keycode);
622         } else if (ascii_code == 2) {
623           // down
624           uint8_t keycode = *(++str);
625           register_code(keycode);
626         } else if (ascii_code == 3) {
627           // up
628           uint8_t keycode = *(++str);
629           unregister_code(keycode);
630         } else {
631           send_char(ascii_code);
632         }
633         ++str;
634         // interval
635         { uint8_t ms = interval; while (ms--) wait_ms(1); }
636     }
637 }
638
639 void send_string_with_delay_P(const char *str, uint8_t interval) {
640     while (1) {
641         char ascii_code = pgm_read_byte(str);
642         if (!ascii_code) break;
643         if (ascii_code == 1) {
644           // tap
645           uint8_t keycode = pgm_read_byte(++str);
646           register_code(keycode);
647           unregister_code(keycode);
648         } else if (ascii_code == 2) {
649           // down
650           uint8_t keycode = pgm_read_byte(++str);
651           register_code(keycode);
652         } else if (ascii_code == 3) {
653           // up
654           uint8_t keycode = pgm_read_byte(++str);
655           unregister_code(keycode);
656         } else {
657           send_char(ascii_code);
658         }
659         ++str;
660         // interval
661         { uint8_t ms = interval; while (ms--) wait_ms(1); }
662     }
663 }
664
665 void send_char(char ascii_code) {
666   uint8_t keycode;
667   keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
668   if (pgm_read_byte(&ascii_to_shift_lut[(uint8_t)ascii_code])) {
669       register_code(KC_LSFT);
670       register_code(keycode);
671       unregister_code(keycode);
672       unregister_code(KC_LSFT);
673   } else {
674       register_code(keycode);
675       unregister_code(keycode);
676   }
677 }
678
679 void set_single_persistent_default_layer(uint8_t default_layer) {
680   #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
681     PLAY_SONG(default_layer_songs[default_layer]);
682   #endif
683   eeconfig_update_default_layer(1U<<default_layer);
684   default_layer_set(1U<<default_layer);
685 }
686
687 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
688   if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
689     layer_on(layer3);
690   } else {
691     layer_off(layer3);
692   }
693 }
694
695 void tap_random_base64(void) {
696   #if defined(__AVR_ATmega32U4__)
697     uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
698   #else
699     uint8_t key = rand() % 64;
700   #endif
701   switch (key) {
702     case 0 ... 25:
703       register_code(KC_LSFT);
704       register_code(key + KC_A);
705       unregister_code(key + KC_A);
706       unregister_code(KC_LSFT);
707       break;
708     case 26 ... 51:
709       register_code(key - 26 + KC_A);
710       unregister_code(key - 26 + KC_A);
711       break;
712     case 52:
713       register_code(KC_0);
714       unregister_code(KC_0);
715       break;
716     case 53 ... 61:
717       register_code(key - 53 + KC_1);
718       unregister_code(key - 53 + KC_1);
719       break;
720     case 62:
721       register_code(KC_LSFT);
722       register_code(KC_EQL);
723       unregister_code(KC_EQL);
724       unregister_code(KC_LSFT);
725       break;
726     case 63:
727       register_code(KC_SLSH);
728       unregister_code(KC_SLSH);
729       break;
730   }
731 }
732
733 void matrix_init_quantum() {
734   #ifdef BACKLIGHT_ENABLE
735     backlight_init_ports();
736   #endif
737   #ifdef AUDIO_ENABLE
738     audio_init();
739   #endif
740   matrix_init_kb();
741 }
742
743 void matrix_scan_quantum() {
744   #ifdef AUDIO_ENABLE
745     matrix_scan_music();
746   #endif
747
748   #ifdef TAP_DANCE_ENABLE
749     matrix_scan_tap_dance();
750   #endif
751
752   #ifdef COMBO_ENABLE
753     matrix_scan_combo();
754   #endif
755
756   #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
757     backlight_task();
758   #endif
759
760   matrix_scan_kb();
761 }
762
763 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
764
765 static const uint8_t backlight_pin = BACKLIGHT_PIN;
766
767 #if BACKLIGHT_PIN == B7
768 #  define COM1x1 COM1C1
769 #  define OCR1x  OCR1C
770 #elif BACKLIGHT_PIN == B6
771 #  define COM1x1 COM1B1
772 #  define OCR1x  OCR1B
773 #elif BACKLIGHT_PIN == B5
774 #  define COM1x1 COM1A1
775 #  define OCR1x  OCR1A
776 #else
777 #  define NO_BACKLIGHT_CLOCK
778 #endif
779
780 #ifndef BACKLIGHT_ON_STATE
781 #define BACKLIGHT_ON_STATE 0
782 #endif
783
784 __attribute__ ((weak))
785 void backlight_init_ports(void)
786 {
787
788   // Setup backlight pin as output and output to on state.
789   // DDRx |= n
790   _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
791   #if BACKLIGHT_ON_STATE == 0
792     // PORTx &= ~n
793     _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
794   #else
795     // PORTx |= n
796     _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
797   #endif
798
799   #ifndef NO_BACKLIGHT_CLOCK
800     // Use full 16-bit resolution.
801     ICR1 = 0xFFFF;
802
803     // I could write a wall of text here to explain... but TL;DW
804     // Go read the ATmega32u4 datasheet.
805     // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
806
807     // Pin PB7 = OCR1C (Timer 1, Channel C)
808     // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
809     // (i.e. start high, go low when counter matches.)
810     // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
811     // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
812
813     TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
814     TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
815   #endif
816
817   backlight_init();
818   #ifdef BACKLIGHT_BREATHING
819     breathing_defaults();
820   #endif
821 }
822
823 __attribute__ ((weak))
824 void backlight_set(uint8_t level)
825 {
826   // Prevent backlight blink on lowest level
827   // #if BACKLIGHT_ON_STATE == 0
828   //   // PORTx &= ~n
829   //   _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
830   // #else
831   //   // PORTx |= n
832   //   _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
833   // #endif
834
835   if ( level == 0 ) {
836     #ifndef NO_BACKLIGHT_CLOCK
837       // Turn off PWM control on backlight pin, revert to output low.
838       TCCR1A &= ~(_BV(COM1x1));
839       OCR1x = 0x0;
840     #else
841       // #if BACKLIGHT_ON_STATE == 0
842       //   // PORTx |= n
843       //   _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
844       // #else
845       //   // PORTx &= ~n
846       //   _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
847       // #endif
848     #endif
849   }
850   #ifndef NO_BACKLIGHT_CLOCK
851     else if ( level == BACKLIGHT_LEVELS ) {
852       // Turn on PWM control of backlight pin
853       TCCR1A |= _BV(COM1x1);
854       // Set the brightness
855       OCR1x = 0xFFFF;
856     }
857     else {
858       // Turn on PWM control of backlight pin
859       TCCR1A |= _BV(COM1x1);
860       // Set the brightness
861       OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
862     }
863   #endif
864
865   #ifdef BACKLIGHT_BREATHING
866     breathing_intensity_default();
867   #endif
868 }
869
870 uint8_t backlight_tick = 0;
871
872 void backlight_task(void) {
873   #ifdef NO_BACKLIGHT_CLOCK
874   if ((0xFFFF >> ((BACKLIGHT_LEVELS - backlight_config.level) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
875     #if BACKLIGHT_ON_STATE == 0
876       // PORTx &= ~n
877       _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
878     #else
879       // PORTx |= n
880       _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
881     #endif
882   } else {
883     #if BACKLIGHT_ON_STATE == 0
884       // PORTx |= n
885       _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
886     #else
887       // PORTx &= ~n
888       _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
889     #endif
890   }
891   backlight_tick = (backlight_tick + 1) % 16;
892   #endif
893 }
894
895 #ifdef BACKLIGHT_BREATHING
896
897 #define BREATHING_NO_HALT  0
898 #define BREATHING_HALT_OFF 1
899 #define BREATHING_HALT_ON  2
900
901 static uint8_t breath_intensity;
902 static uint8_t breath_speed;
903 static uint16_t breathing_index;
904 static uint8_t breathing_halt;
905
906 void breathing_enable(void)
907 {
908     if (get_backlight_level() == 0)
909     {
910         breathing_index = 0;
911     }
912     else
913     {
914         // Set breathing_index to be at the midpoint (brightest point)
915         breathing_index = 0x20 << breath_speed;
916     }
917
918     breathing_halt = BREATHING_NO_HALT;
919
920     // Enable breathing interrupt
921     TIMSK1 |= _BV(OCIE1A);
922 }
923
924 void breathing_pulse(void)
925 {
926     if (get_backlight_level() == 0)
927     {
928         breathing_index = 0;
929     }
930     else
931     {
932         // Set breathing_index to be at the midpoint + 1 (brightest point)
933         breathing_index = 0x21 << breath_speed;
934     }
935
936     breathing_halt = BREATHING_HALT_ON;
937
938     // Enable breathing interrupt
939     TIMSK1 |= _BV(OCIE1A);
940 }
941
942 void breathing_disable(void)
943 {
944     // Disable breathing interrupt
945     TIMSK1 &= ~_BV(OCIE1A);
946     backlight_set(get_backlight_level());
947 }
948
949 void breathing_self_disable(void)
950 {
951     if (get_backlight_level() == 0)
952     {
953         breathing_halt = BREATHING_HALT_OFF;
954     }
955     else
956     {
957         breathing_halt = BREATHING_HALT_ON;
958     }
959
960     //backlight_set(get_backlight_level());
961 }
962
963 void breathing_toggle(void)
964 {
965     if (!is_breathing())
966     {
967         if (get_backlight_level() == 0)
968         {
969             breathing_index = 0;
970         }
971         else
972         {
973             // Set breathing_index to be at the midpoint + 1 (brightest point)
974             breathing_index = 0x21 << breath_speed;
975         }
976
977         breathing_halt = BREATHING_NO_HALT;
978     }
979
980     // Toggle breathing interrupt
981     TIMSK1 ^= _BV(OCIE1A);
982
983     // Restore backlight level
984     if (!is_breathing())
985     {
986         backlight_set(get_backlight_level());
987     }
988 }
989
990 bool is_breathing(void)
991 {
992     return (TIMSK1 && _BV(OCIE1A));
993 }
994
995 void breathing_intensity_default(void)
996 {
997     //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
998     breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
999 }
1000
1001 void breathing_intensity_set(uint8_t value)
1002 {
1003     breath_intensity = value;
1004 }
1005
1006 void breathing_speed_default(void)
1007 {
1008     breath_speed = 4;
1009 }
1010
1011 void breathing_speed_set(uint8_t value)
1012 {
1013     bool is_breathing_now = is_breathing();
1014     uint8_t old_breath_speed = breath_speed;
1015
1016     if (is_breathing_now)
1017     {
1018         // Disable breathing interrupt
1019         TIMSK1 &= ~_BV(OCIE1A);
1020     }
1021
1022     breath_speed = value;
1023
1024     if (is_breathing_now)
1025     {
1026         // Adjust index to account for new speed
1027         breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
1028
1029         // Enable breathing interrupt
1030         TIMSK1 |= _BV(OCIE1A);
1031     }
1032
1033 }
1034
1035 void breathing_speed_inc(uint8_t value)
1036 {
1037     if ((uint16_t)(breath_speed - value) > 10 )
1038     {
1039         breathing_speed_set(0);
1040     }
1041     else
1042     {
1043         breathing_speed_set(breath_speed - value);
1044     }
1045 }
1046
1047 void breathing_speed_dec(uint8_t value)
1048 {
1049     if ((uint16_t)(breath_speed + value) > 10 )
1050     {
1051         breathing_speed_set(10);
1052     }
1053     else
1054     {
1055         breathing_speed_set(breath_speed + value);
1056     }
1057 }
1058
1059 void breathing_defaults(void)
1060 {
1061     breathing_intensity_default();
1062     breathing_speed_default();
1063     breathing_halt = BREATHING_NO_HALT;
1064 }
1065
1066 /* Breathing Sleep LED brighness(PWM On period) table
1067  * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
1068  *
1069  * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
1070  * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
1071  */
1072 static const uint8_t breathing_table[64] PROGMEM = {
1073   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   1,   2,   4,   6,  10,
1074  15,  23,  32,  44,  58,  74,  93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
1075 255, 252, 245, 233, 218, 199, 179, 157, 135, 113,  93,  74,  58,  44,  32,  23,
1076  15,  10,   6,   4,   2,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
1077 };
1078
1079 ISR(TIMER1_COMPA_vect)
1080 {
1081     // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
1082
1083
1084     uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
1085
1086     if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
1087     {
1088         // Disable breathing interrupt
1089         TIMSK1 &= ~_BV(OCIE1A);
1090     }
1091
1092     OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
1093
1094 }
1095
1096
1097
1098 #endif // breathing
1099
1100 #else // backlight
1101
1102 __attribute__ ((weak))
1103 void backlight_init_ports(void)
1104 {
1105
1106 }
1107
1108 __attribute__ ((weak))
1109 void backlight_set(uint8_t level)
1110 {
1111
1112 }
1113
1114 #endif // backlight
1115
1116
1117 // Functions for spitting out values
1118 //
1119
1120 void send_dword(uint32_t number) { // this might not actually work
1121     uint16_t word = (number >> 16);
1122     send_word(word);
1123     send_word(number & 0xFFFFUL);
1124 }
1125
1126 void send_word(uint16_t number) {
1127     uint8_t byte = number >> 8;
1128     send_byte(byte);
1129     send_byte(number & 0xFF);
1130 }
1131
1132 void send_byte(uint8_t number) {
1133     uint8_t nibble = number >> 4;
1134     send_nibble(nibble);
1135     send_nibble(number & 0xF);
1136 }
1137
1138 void send_nibble(uint8_t number) {
1139     switch (number) {
1140         case 0:
1141             register_code(KC_0);
1142             unregister_code(KC_0);
1143             break;
1144         case 1 ... 9:
1145             register_code(KC_1 + (number - 1));
1146             unregister_code(KC_1 + (number - 1));
1147             break;
1148         case 0xA ... 0xF:
1149             register_code(KC_A + (number - 0xA));
1150             unregister_code(KC_A + (number - 0xA));
1151             break;
1152     }
1153 }
1154
1155
1156 __attribute__((weak))
1157 uint16_t hex_to_keycode(uint8_t hex)
1158 {
1159   if (hex == 0x0) {
1160     return KC_0;
1161   } else if (hex < 0xA) {
1162     return KC_1 + (hex - 0x1);
1163   } else {
1164     return KC_A + (hex - 0xA);
1165   }
1166 }
1167
1168 void api_send_unicode(uint32_t unicode) {
1169 #ifdef API_ENABLE
1170     uint8_t chunk[4];
1171     dword_to_bytes(unicode, chunk);
1172     MT_SEND_DATA(DT_UNICODE, chunk, 5);
1173 #endif
1174 }
1175
1176 __attribute__ ((weak))
1177 void led_set_user(uint8_t usb_led) {
1178
1179 }
1180
1181 __attribute__ ((weak))
1182 void led_set_kb(uint8_t usb_led) {
1183     led_set_user(usb_led);
1184 }
1185
1186 __attribute__ ((weak))
1187 void led_init_ports(void)
1188 {
1189
1190 }
1191
1192 __attribute__ ((weak))
1193 void led_set(uint8_t usb_led)
1194 {
1195
1196   // Example LED Code
1197   //
1198     // // Using PE6 Caps Lock LED
1199     // if (usb_led & (1<<USB_LED_CAPS_LOCK))
1200     // {
1201     //     // Output high.
1202     //     DDRE |= (1<<6);
1203     //     PORTE |= (1<<6);
1204     // }
1205     // else
1206     // {
1207     //     // Output low.
1208     //     DDRE &= ~(1<<6);
1209     //     PORTE &= ~(1<<6);
1210     // }
1211
1212   led_set_kb(usb_led);
1213 }
1214
1215
1216 //------------------------------------------------------------------------------
1217 // Override these functions in your keymap file to play different tunes on
1218 // different events such as startup and bootloader jump
1219
1220 __attribute__ ((weak))
1221 void startup_user() {}
1222
1223 __attribute__ ((weak))
1224 void shutdown_user() {}
1225
1226 //------------------------------------------------------------------------------