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