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