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