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