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