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