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