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