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