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