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