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