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