]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/quantum.c
Fixed NO_SUSPEND_POWER_DOWN handling
[qmk_firmware.git] / quantum / quantum.c
1 #include "quantum.h"
2
3 #ifndef TAPPING_TERM
4 #define TAPPING_TERM 200
5 #endif
6
7 static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
8   switch (code) {
9   case QK_MODS ... QK_MODS_MAX:
10     break;
11   default:
12     return;
13   }
14
15   if (code & QK_LCTL)
16     f(KC_LCTL);
17   if (code & QK_LSFT)
18     f(KC_LSFT);
19   if (code & QK_LALT)
20     f(KC_LALT);
21   if (code & QK_LGUI)
22     f(KC_LGUI);
23
24   if (code & QK_RCTL)
25     f(KC_RCTL);
26   if (code & QK_RSFT)
27     f(KC_RSFT);
28   if (code & QK_RALT)
29     f(KC_RALT);
30   if (code & QK_RGUI)
31     f(KC_RGUI);
32 }
33
34 void register_code16 (uint16_t code) {
35   do_code16 (code, register_code);
36   register_code (code);
37 }
38
39 void unregister_code16 (uint16_t code) {
40   unregister_code (code);
41   do_code16 (code, unregister_code);
42 }
43
44 __attribute__ ((weak))
45 bool process_action_kb(keyrecord_t *record) {
46   return true;
47 }
48
49 __attribute__ ((weak))
50 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
51   return process_record_user(keycode, record);
52 }
53
54 __attribute__ ((weak))
55 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
56   return true;
57 }
58
59 void reset_keyboard(void) {
60   clear_keyboard();
61 #ifdef AUDIO_ENABLE
62   stop_all_notes();
63   shutdown_user();
64 #endif
65   wait_ms(250);
66 #ifdef CATERINA_BOOTLOADER
67   *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
68 #endif
69   bootloader_jump();
70 }
71
72 // Shift / paren setup
73
74 #ifndef LSPO_KEY
75   #define LSPO_KEY KC_9
76 #endif
77 #ifndef RSPC_KEY
78   #define RSPC_KEY KC_0
79 #endif
80
81 static bool shift_interrupted[2] = {0, 0};
82 static uint16_t scs_timer = 0;
83
84 bool process_record_quantum(keyrecord_t *record) {
85
86   /* This gets the keycode from the key pressed */
87   keypos_t key = record->event.key;
88   uint16_t keycode;
89
90   #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
91     /* TODO: Use store_or_get_action() or a similar function. */
92     if (!disable_action_cache) {
93       uint8_t layer;
94
95       if (record->event.pressed) {
96         layer = layer_switch_get_layer(key);
97         update_source_layers_cache(key, layer);
98       } else {
99         layer = read_source_layers_cache(key);
100       }
101       keycode = keymap_key_to_keycode(layer, key);
102     } else
103   #endif
104     keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
105
106     // This is how you use actions here
107     // if (keycode == KC_LEAD) {
108     //   action_t action;
109     //   action.code = ACTION_DEFAULT_LAYER_SET(0);
110     //   process_action(record, action);
111     //   return false;
112     // }
113
114   if (!(
115     process_record_kb(keycode, record) &&
116   #ifdef MIDI_ENABLE
117     process_midi(keycode, record) &&
118   #endif
119   #ifdef AUDIO_ENABLE
120     process_music(keycode, record) &&
121   #endif
122   #ifdef TAP_DANCE_ENABLE
123     process_tap_dance(keycode, record) &&
124   #endif
125   #ifndef DISABLE_LEADER
126     process_leader(keycode, record) &&
127   #endif
128   #ifndef DISABLE_CHORDING
129     process_chording(keycode, record) &&
130   #endif
131   #ifdef UNICODE_ENABLE
132     process_unicode(keycode, record) &&
133   #endif
134   #ifdef UCIS_ENABLE
135     process_ucis(keycode, record) &&
136   #endif
137   #ifdef UNICODEMAP_ENABLE
138     process_unicode_map(keycode, record) &&
139   #endif
140       true)) {
141     return false;
142   }
143
144   // Shift / paren setup
145
146   switch(keycode) {
147     case RESET:
148       if (record->event.pressed) {
149         reset_keyboard();
150       }
151           return false;
152       break;
153     case DEBUG:
154       if (record->event.pressed) {
155           print("\nDEBUG: enabled.\n");
156           debug_enable = true;
157       }
158           return false;
159       break;
160         #ifdef RGBLIGHT_ENABLE
161         case RGB_TOG:
162                 if (record->event.pressed) {
163                         rgblight_toggle();
164       }
165           return false;
166       break;
167         case RGB_MOD:
168                 if (record->event.pressed) {
169                         rgblight_step();
170       }
171           return false;
172       break;
173         case RGB_HUI:
174                 if (record->event.pressed) {
175                         rgblight_increase_hue();
176       }
177           return false;
178       break;
179         case RGB_HUD:
180                 if (record->event.pressed) {
181                         rgblight_decrease_hue();
182       }
183           return false;
184       break;
185         case RGB_SAI:
186                 if (record->event.pressed) {
187                         rgblight_increase_sat();
188       }
189           return false;
190       break;
191         case RGB_SAD:
192                 if (record->event.pressed) {
193                         rgblight_decrease_sat();
194       }
195           return false;
196       break;
197         case RGB_VAI:
198                 if (record->event.pressed) {
199                         rgblight_increase_val();
200       }
201           return false;
202       break;
203         case RGB_VAD:
204                 if (record->event.pressed) {
205                         rgblight_decrease_val();
206       }
207           return false;
208       break;
209         #endif
210     case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
211       if (record->event.pressed) {
212         // MAGIC actions (BOOTMAGIC without the boot)
213         if (!eeconfig_is_enabled()) {
214             eeconfig_init();
215         }
216         /* keymap config */
217         keymap_config.raw = eeconfig_read_keymap();
218         switch (keycode)
219         {
220           case MAGIC_SWAP_CONTROL_CAPSLOCK:
221             keymap_config.swap_control_capslock = true;
222             break;
223           case MAGIC_CAPSLOCK_TO_CONTROL:
224             keymap_config.capslock_to_control = true;
225             break;
226           case MAGIC_SWAP_LALT_LGUI:
227             keymap_config.swap_lalt_lgui = true;
228             break;
229           case MAGIC_SWAP_RALT_RGUI:
230             keymap_config.swap_ralt_rgui = true;
231             break;
232           case MAGIC_NO_GUI:
233             keymap_config.no_gui = true;
234             break;
235           case MAGIC_SWAP_GRAVE_ESC:
236             keymap_config.swap_grave_esc = true;
237             break;
238           case MAGIC_SWAP_BACKSLASH_BACKSPACE:
239             keymap_config.swap_backslash_backspace = true;
240             break;
241           case MAGIC_HOST_NKRO:
242             keymap_config.nkro = true;
243             break;
244           case MAGIC_SWAP_ALT_GUI:
245             keymap_config.swap_lalt_lgui = true;
246             keymap_config.swap_ralt_rgui = true;
247             break;
248           case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
249             keymap_config.swap_control_capslock = false;
250             break;
251           case MAGIC_UNCAPSLOCK_TO_CONTROL:
252             keymap_config.capslock_to_control = false;
253             break;
254           case MAGIC_UNSWAP_LALT_LGUI:
255             keymap_config.swap_lalt_lgui = false;
256             break;
257           case MAGIC_UNSWAP_RALT_RGUI:
258             keymap_config.swap_ralt_rgui = false;
259             break;
260           case MAGIC_UNNO_GUI:
261             keymap_config.no_gui = false;
262             break;
263           case MAGIC_UNSWAP_GRAVE_ESC:
264             keymap_config.swap_grave_esc = false;
265             break;
266           case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
267             keymap_config.swap_backslash_backspace = false;
268             break;
269           case MAGIC_UNHOST_NKRO:
270             keymap_config.nkro = false;
271             break;
272           case MAGIC_UNSWAP_ALT_GUI:
273             keymap_config.swap_lalt_lgui = false;
274             keymap_config.swap_ralt_rgui = false;
275             break;
276           case MAGIC_TOGGLE_NKRO:
277             keymap_config.nkro = !keymap_config.nkro;
278             break;
279           default:
280             break;
281         }
282         eeconfig_update_keymap(keymap_config.raw);
283         clear_keyboard(); // clear to prevent stuck keys
284
285         return false;
286       }
287       break;
288     case KC_LSPO: {
289       if (record->event.pressed) {
290         shift_interrupted[0] = false;
291         scs_timer = timer_read ();
292         register_mods(MOD_BIT(KC_LSFT));
293       }
294       else {
295         #ifdef DISABLE_SPACE_CADET_ROLLOVER
296           if (get_mods() & MOD_BIT(KC_RSFT)) {
297             shift_interrupted[0] = true;
298             shift_interrupted[1] = true;
299           }
300         #endif
301         if (!shift_interrupted[0] && timer_elapsed(scs_timer) < TAPPING_TERM) {
302           register_code(LSPO_KEY);
303           unregister_code(LSPO_KEY);
304         }
305         unregister_mods(MOD_BIT(KC_LSFT));
306       }
307       return false;
308       // break;
309     }
310
311     case KC_RSPC: {
312       if (record->event.pressed) {
313         shift_interrupted[1] = false;
314         scs_timer = timer_read ();
315         register_mods(MOD_BIT(KC_RSFT));
316       }
317       else {
318         #ifdef DISABLE_SPACE_CADET_ROLLOVER
319           if (get_mods() & MOD_BIT(KC_LSFT)) {
320             shift_interrupted[0] = true;
321             shift_interrupted[1] = true;
322           }
323         #endif
324         if (!shift_interrupted[1] && timer_elapsed(scs_timer) < TAPPING_TERM) {
325           register_code(RSPC_KEY);
326           unregister_code(RSPC_KEY);
327         }
328         unregister_mods(MOD_BIT(KC_RSFT));
329       }
330       return false;
331       // break;
332     }
333     default: {
334       shift_interrupted[0] = true;
335       shift_interrupted[1] = true;
336       break;
337     }
338   }
339
340   return process_action_kb(record);
341 }
342
343 const bool ascii_to_qwerty_shift_lut[0x80] PROGMEM = {
344     0, 0, 0, 0, 0, 0, 0, 0,
345     0, 0, 0, 0, 0, 0, 0, 0,
346     0, 0, 0, 0, 0, 0, 0, 0,
347     0, 0, 0, 0, 0, 0, 0, 0,
348     0, 1, 1, 1, 1, 1, 1, 0,
349     1, 1, 1, 1, 0, 0, 0, 0,
350     0, 0, 0, 0, 0, 0, 0, 0,
351     0, 0, 1, 0, 1, 0, 1, 1,
352     1, 1, 1, 1, 1, 1, 1, 1,
353     1, 1, 1, 1, 1, 1, 1, 1,
354     1, 1, 1, 1, 1, 1, 1, 1,
355     1, 1, 1, 0, 0, 0, 1, 1,
356     0, 0, 0, 0, 0, 0, 0, 0,
357     0, 0, 0, 0, 0, 0, 0, 0,
358     0, 0, 0, 0, 0, 0, 0, 0,
359     0, 0, 0, 1, 1, 1, 1, 0
360 };
361
362 const uint8_t ascii_to_qwerty_keycode_lut[0x80] PROGMEM = {
363     0, 0, 0, 0, 0, 0, 0, 0,
364     KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
365     0, 0, 0, 0, 0, 0, 0, 0,
366     0, 0, 0, KC_ESC, 0, 0, 0, 0,
367     KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
368     KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
369     KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
370     KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
371     KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
372     KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
373     KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
374     KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
375     KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
376     KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
377     KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
378     KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
379 };
380
381 /* for users whose OSes are set to Colemak */
382 #if 0
383 #include "keymap_colemak.h"
384
385 const bool ascii_to_colemak_shift_lut[0x80] PROGMEM = {
386     0, 0, 0, 0, 0, 0, 0, 0,
387     0, 0, 0, 0, 0, 0, 0, 0,
388     0, 0, 0, 0, 0, 0, 0, 0,
389     0, 0, 0, 0, 0, 0, 0, 0,
390     0, 1, 1, 1, 1, 1, 1, 0,
391     1, 1, 1, 1, 0, 0, 0, 0,
392     0, 0, 0, 0, 0, 0, 0, 0,
393     0, 0, 1, 0, 1, 0, 1, 1,
394     1, 1, 1, 1, 1, 1, 1, 1,
395     1, 1, 1, 1, 1, 1, 1, 1,
396     1, 1, 1, 1, 1, 1, 1, 1,
397     1, 1, 1, 0, 0, 0, 1, 1,
398     0, 0, 0, 0, 0, 0, 0, 0,
399     0, 0, 0, 0, 0, 0, 0, 0,
400     0, 0, 0, 0, 0, 0, 0, 0,
401     0, 0, 0, 1, 1, 1, 1, 0
402 };
403
404 const uint8_t ascii_to_colemak_keycode_lut[0x80] PROGMEM = {
405     0, 0, 0, 0, 0, 0, 0, 0,
406     KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
407     0, 0, 0, 0, 0, 0, 0, 0,
408     0, 0, 0, KC_ESC, 0, 0, 0, 0,
409     KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
410     KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
411     KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
412     KC_8, KC_9, CM_SCLN, CM_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
413     KC_2, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
414     CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
415     CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
416     CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
417     KC_GRV, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
418     CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
419     CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
420     CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
421 };
422
423 #endif
424
425 void send_string(const char *str) {
426     while (1) {
427         uint8_t keycode;
428         uint8_t ascii_code = pgm_read_byte(str);
429         if (!ascii_code) break;
430         keycode = pgm_read_byte(&ascii_to_qwerty_keycode_lut[ascii_code]);
431         if (pgm_read_byte(&ascii_to_qwerty_shift_lut[ascii_code])) {
432             register_code(KC_LSFT);
433             register_code(keycode);
434             unregister_code(keycode);
435             unregister_code(KC_LSFT);
436         }
437         else {
438             register_code(keycode);
439             unregister_code(keycode);
440         }
441         ++str;
442     }
443 }
444
445 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
446   if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
447     layer_on(layer3);
448   } else {
449     layer_off(layer3);
450   }
451 }
452
453 void tap_random_base64(void) {
454   #if defined(__AVR_ATmega32U4__)
455     uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
456   #else
457     uint8_t key = rand() % 64;
458   #endif
459   switch (key) {
460     case 0 ... 25:
461       register_code(KC_LSFT);
462       register_code(key + KC_A);
463       unregister_code(key + KC_A);
464       unregister_code(KC_LSFT);
465       break;
466     case 26 ... 51:
467       register_code(key - 26 + KC_A);
468       unregister_code(key - 26 + KC_A);
469       break;
470     case 52:
471       register_code(KC_0);
472       unregister_code(KC_0);
473       break;
474     case 53 ... 61:
475       register_code(key - 53 + KC_1);
476       unregister_code(key - 53 + KC_1);
477       break;
478     case 62:
479       register_code(KC_LSFT);
480       register_code(KC_EQL);
481       unregister_code(KC_EQL);
482       unregister_code(KC_LSFT);
483       break;
484     case 63:
485       register_code(KC_SLSH);
486       unregister_code(KC_SLSH);
487       break;
488   }
489 }
490
491 void matrix_init_quantum() {
492   #ifdef BACKLIGHT_ENABLE
493     backlight_init_ports();
494   #endif
495   matrix_init_kb();
496 }
497
498 void matrix_scan_quantum() {
499   #ifdef AUDIO_ENABLE
500     matrix_scan_music();
501   #endif
502
503   #ifdef TAP_DANCE_ENABLE
504     matrix_scan_tap_dance();
505   #endif
506   matrix_scan_kb();
507 }
508
509 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
510
511 static const uint8_t backlight_pin = BACKLIGHT_PIN;
512
513 #if BACKLIGHT_PIN == B7
514 #  define COM1x1 COM1C1
515 #  define OCR1x  OCR1C
516 #elif BACKLIGHT_PIN == B6
517 #  define COM1x1 COM1B1
518 #  define OCR1x  OCR1B
519 #elif BACKLIGHT_PIN == B5
520 #  define COM1x1 COM1A1
521 #  define OCR1x  OCR1A
522 #else
523 #  error "Backlight pin not supported - use B5, B6, or B7"
524 #endif
525
526 __attribute__ ((weak))
527 void backlight_init_ports(void)
528 {
529
530   // Setup backlight pin as output and output low.
531   // DDRx |= n
532   _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
533   // PORTx &= ~n
534   _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
535
536   // Use full 16-bit resolution.
537   ICR1 = 0xFFFF;
538
539   // I could write a wall of text here to explain... but TL;DW
540   // Go read the ATmega32u4 datasheet.
541   // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
542
543   // Pin PB7 = OCR1C (Timer 1, Channel C)
544   // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
545   // (i.e. start high, go low when counter matches.)
546   // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
547   // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
548
549   TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
550   TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
551
552   backlight_init();
553   #ifdef BACKLIGHT_BREATHING
554     breathing_defaults();
555   #endif
556 }
557
558 __attribute__ ((weak))
559 void backlight_set(uint8_t level)
560 {
561   // Prevent backlight blink on lowest level
562   // PORTx &= ~n
563   _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
564
565   if ( level == 0 ) {
566     // Turn off PWM control on backlight pin, revert to output low.
567     TCCR1A &= ~(_BV(COM1x1));
568     OCR1x = 0x0;
569   } else if ( level == BACKLIGHT_LEVELS ) {
570     // Turn on PWM control of backlight pin
571     TCCR1A |= _BV(COM1x1);
572     // Set the brightness
573     OCR1x = 0xFFFF;
574   } else {
575     // Turn on PWM control of backlight pin
576     TCCR1A |= _BV(COM1x1);
577     // Set the brightness
578     OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
579   }
580
581   #ifdef BACKLIGHT_BREATHING
582     breathing_intensity_default();
583   #endif
584 }
585
586
587 #ifdef BACKLIGHT_BREATHING
588
589 #define BREATHING_NO_HALT  0
590 #define BREATHING_HALT_OFF 1
591 #define BREATHING_HALT_ON  2
592
593 static uint8_t breath_intensity;
594 static uint8_t breath_speed;
595 static uint16_t breathing_index;
596 static uint8_t breathing_halt;
597
598 void breathing_enable(void)
599 {
600     if (get_backlight_level() == 0)
601     {
602         breathing_index = 0;
603     }
604     else
605     {
606         // Set breathing_index to be at the midpoint (brightest point)
607         breathing_index = 0x20 << breath_speed;
608     }
609
610     breathing_halt = BREATHING_NO_HALT;
611
612     // Enable breathing interrupt
613     TIMSK1 |= _BV(OCIE1A);
614 }
615
616 void breathing_pulse(void)
617 {
618     if (get_backlight_level() == 0)
619     {
620         breathing_index = 0;
621     }
622     else
623     {
624         // Set breathing_index to be at the midpoint + 1 (brightest point)
625         breathing_index = 0x21 << breath_speed;
626     }
627
628     breathing_halt = BREATHING_HALT_ON;
629
630     // Enable breathing interrupt
631     TIMSK1 |= _BV(OCIE1A);
632 }
633
634 void breathing_disable(void)
635 {
636     // Disable breathing interrupt
637     TIMSK1 &= ~_BV(OCIE1A);
638     backlight_set(get_backlight_level());
639 }
640
641 void breathing_self_disable(void)
642 {
643     if (get_backlight_level() == 0)
644     {
645         breathing_halt = BREATHING_HALT_OFF;
646     }
647     else
648     {
649         breathing_halt = BREATHING_HALT_ON;
650     }
651
652     //backlight_set(get_backlight_level());
653 }
654
655 void breathing_toggle(void)
656 {
657     if (!is_breathing())
658     {
659         if (get_backlight_level() == 0)
660         {
661             breathing_index = 0;
662         }
663         else
664         {
665             // Set breathing_index to be at the midpoint + 1 (brightest point)
666             breathing_index = 0x21 << breath_speed;
667         }
668
669         breathing_halt = BREATHING_NO_HALT;
670     }
671
672     // Toggle breathing interrupt
673     TIMSK1 ^= _BV(OCIE1A);
674
675     // Restore backlight level
676     if (!is_breathing())
677     {
678         backlight_set(get_backlight_level());
679     }
680 }
681
682 bool is_breathing(void)
683 {
684     return (TIMSK1 && _BV(OCIE1A));
685 }
686
687 void breathing_intensity_default(void)
688 {
689     //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
690     breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
691 }
692
693 void breathing_intensity_set(uint8_t value)
694 {
695     breath_intensity = value;
696 }
697
698 void breathing_speed_default(void)
699 {
700     breath_speed = 4;
701 }
702
703 void breathing_speed_set(uint8_t value)
704 {
705     bool is_breathing_now = is_breathing();
706     uint8_t old_breath_speed = breath_speed;
707
708     if (is_breathing_now)
709     {
710         // Disable breathing interrupt
711         TIMSK1 &= ~_BV(OCIE1A);
712     }
713
714     breath_speed = value;
715
716     if (is_breathing_now)
717     {
718         // Adjust index to account for new speed
719         breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
720
721         // Enable breathing interrupt
722         TIMSK1 |= _BV(OCIE1A);
723     }
724
725 }
726
727 void breathing_speed_inc(uint8_t value)
728 {
729     if ((uint16_t)(breath_speed - value) > 10 )
730     {
731         breathing_speed_set(0);
732     }
733     else
734     {
735         breathing_speed_set(breath_speed - value);
736     }
737 }
738
739 void breathing_speed_dec(uint8_t value)
740 {
741     if ((uint16_t)(breath_speed + value) > 10 )
742     {
743         breathing_speed_set(10);
744     }
745     else
746     {
747         breathing_speed_set(breath_speed + value);
748     }
749 }
750
751 void breathing_defaults(void)
752 {
753     breathing_intensity_default();
754     breathing_speed_default();
755     breathing_halt = BREATHING_NO_HALT;
756 }
757
758 /* Breathing Sleep LED brighness(PWM On period) table
759  * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
760  *
761  * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
762  * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
763  */
764 static const uint8_t breathing_table[64] PROGMEM = {
765   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   1,   2,   4,   6,  10,
766  15,  23,  32,  44,  58,  74,  93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
767 255, 252, 245, 233, 218, 199, 179, 157, 135, 113,  93,  74,  58,  44,  32,  23,
768  15,  10,   6,   4,   2,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
769 };
770
771 ISR(TIMER1_COMPA_vect)
772 {
773     // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
774
775
776     uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
777
778     if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
779     {
780         // Disable breathing interrupt
781         TIMSK1 &= ~_BV(OCIE1A);
782     }
783
784     OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
785
786 }
787
788
789
790 #endif // breathing
791
792 #else // backlight
793
794 __attribute__ ((weak))
795 void backlight_init_ports(void)
796 {
797
798 }
799
800 __attribute__ ((weak))
801 void backlight_set(uint8_t level)
802 {
803
804 }
805
806 #endif // backlight
807
808
809
810 __attribute__ ((weak))
811 void led_set_user(uint8_t usb_led) {
812
813 }
814
815 __attribute__ ((weak))
816 void led_set_kb(uint8_t usb_led) {
817     led_set_user(usb_led);
818 }
819
820 __attribute__ ((weak))
821 void led_init_ports(void)
822 {
823
824 }
825
826 __attribute__ ((weak))
827 void led_set(uint8_t usb_led)
828 {
829
830   // Example LED Code
831   //
832     // // Using PE6 Caps Lock LED
833     // if (usb_led & (1<<USB_LED_CAPS_LOCK))
834     // {
835     //     // Output high.
836     //     DDRE |= (1<<6);
837     //     PORTE |= (1<<6);
838     // }
839     // else
840     // {
841     //     // Output low.
842     //     DDRE &= ~(1<<6);
843     //     PORTE &= ~(1<<6);
844     // }
845
846   led_set_kb(usb_led);
847 }
848
849
850 //------------------------------------------------------------------------------
851 // Override these functions in your keymap file to play different tunes on
852 // different events such as startup and bootloader jump
853
854 __attribute__ ((weak))
855 void startup_user() {}
856
857 __attribute__ ((weak))
858 void shutdown_user() {}
859
860 //------------------------------------------------------------------------------