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