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