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