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