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