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