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