1 /* Copyright 2016-2017 Jack Humbert
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.
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.
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/>.
21 # include "outputselect.h"
24 #ifdef BACKLIGHT_ENABLE
25 # include "backlight.h"
26 extern backlight_config_t backlight_config
;
29 #ifdef FAUXCLICKY_ENABLE
30 # include "fauxclicky.h"
38 # include "process_midi.h"
41 #ifdef VELOCIKEY_ENABLE
42 # include "velocikey.h"
55 # define GOODBYE_SONG SONG(GOODBYE_SOUND)
57 float goodbye_song
[][2] = GOODBYE_SONG
;
58 # ifdef DEFAULT_LAYER_SONGS
59 float default_layer_songs
[][16][2] = DEFAULT_LAYER_SONGS
;
61 # ifdef SENDSTRING_BELL
62 float bell_song
[][2] = SONG(TERMINAL_SOUND
);
66 static void do_code16(uint16_t code
, void (*f
)(uint8_t)) {
68 case QK_MODS
... QK_MODS_MAX
:
74 uint8_t mods_to_send
= 0;
76 if (code
& QK_RMODS_MIN
) { // Right mod flag is set
77 if (code
& QK_LCTL
) mods_to_send
|= MOD_BIT(KC_RCTL
);
78 if (code
& QK_LSFT
) mods_to_send
|= MOD_BIT(KC_RSFT
);
79 if (code
& QK_LALT
) mods_to_send
|= MOD_BIT(KC_RALT
);
80 if (code
& QK_LGUI
) mods_to_send
|= MOD_BIT(KC_RGUI
);
82 if (code
& QK_LCTL
) mods_to_send
|= MOD_BIT(KC_LCTL
);
83 if (code
& QK_LSFT
) mods_to_send
|= MOD_BIT(KC_LSFT
);
84 if (code
& QK_LALT
) mods_to_send
|= MOD_BIT(KC_LALT
);
85 if (code
& QK_LGUI
) mods_to_send
|= MOD_BIT(KC_LGUI
);
91 void register_code16(uint16_t code
) {
92 if (IS_MOD(code
) || code
== KC_NO
) {
93 do_code16(code
, register_mods
);
95 do_code16(code
, register_weak_mods
);
100 void unregister_code16(uint16_t code
) {
101 unregister_code(code
);
102 if (IS_MOD(code
) || code
== KC_NO
) {
103 do_code16(code
, unregister_mods
);
105 do_code16(code
, unregister_weak_mods
);
109 void tap_code16(uint16_t code
) {
110 register_code16(code
);
111 #if TAP_CODE_DELAY > 0
112 wait_ms(TAP_CODE_DELAY
);
114 unregister_code16(code
);
117 __attribute__((weak
)) bool process_action_kb(keyrecord_t
*record
) { return true; }
119 __attribute__((weak
)) bool process_record_kb(uint16_t keycode
, keyrecord_t
*record
) { return process_record_user(keycode
, record
); }
121 __attribute__((weak
)) bool process_record_user(uint16_t keycode
, keyrecord_t
*record
) { return true; }
123 void reset_keyboard(void) {
125 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
126 process_midi_all_notes_off();
129 # ifndef NO_MUSIC_MODE
130 music_all_notes_off();
132 uint16_t timer_start
= timer_read();
133 PLAY_SONG(goodbye_song
);
135 while (timer_elapsed(timer_start
) < 250) wait_ms(1);
144 // this is also done later in bootloader.c - not sure if it's neccesary here
145 #ifdef BOOTLOADER_CATERINA
146 *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
151 /* Convert record into usable keycode via the contained event. */
152 uint16_t get_record_keycode(keyrecord_t
*record
) { return get_event_keycode(record
->event
); }
154 /* Convert event into usable keycode. Checks the layer cache to ensure that it
155 * retains the correct keycode after a layer change, if the key is still pressed.
157 uint16_t get_event_keycode(keyevent_t event
) {
158 #if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
159 /* TODO: Use store_or_get_action() or a similar function. */
160 if (!disable_action_cache
) {
164 layer
= layer_switch_get_layer(event
.key
);
165 update_source_layers_cache(event
.key
, layer
);
167 layer
= read_source_layers_cache(event
.key
);
169 return keymap_key_to_keycode(layer
, event
.key
);
172 return keymap_key_to_keycode(layer_switch_get_layer(event
.key
), event
.key
);
175 /* Main keycode processing function. Hands off handling to other functions,
176 * then processes internal Quantum keycodes, then processes ACTIONs.
178 bool process_record_quantum(keyrecord_t
*record
) {
179 uint16_t keycode
= get_record_keycode(record
);
181 // This is how you use actions here
182 // if (keycode == KC_LEAD) {
184 // action.code = ACTION_DEFAULT_LAYER_SET(0);
185 // process_action(record, action);
189 #ifdef VELOCIKEY_ENABLE
190 if (velocikey_enabled() && record
->event
.pressed
) {
191 velocikey_accelerate();
195 #ifdef TAP_DANCE_ENABLE
196 preprocess_tap_dance(keycode
, record
);
200 #if defined(KEY_LOCK_ENABLE)
201 // Must run first to be able to mask key_up events.
202 process_key_lock(&keycode
, record
) &&
204 #if defined(DYNAMIC_MACRO_ENABLE) && !defined(DYNAMIC_MACRO_USER_CALL)
205 // Must run asap to ensure all keypresses are recorded.
206 process_dynamic_macro(keycode
, record
) &&
208 #if defined(AUDIO_ENABLE) && defined(AUDIO_CLICKY)
209 process_clicky(keycode
, record
) &&
210 #endif // AUDIO_CLICKY
212 process_haptic(keycode
, record
) &&
213 #endif // HAPTIC_ENABLE
214 #if defined(RGB_MATRIX_ENABLE)
215 process_rgb_matrix(keycode
, record
) &&
217 #if defined(VIA_ENABLE)
218 process_record_via(keycode
, record
) &&
220 process_record_kb(keycode
, record
) &&
221 #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
222 process_midi(keycode
, record
) &&
225 process_audio(keycode
, record
) &&
227 #ifdef BACKLIGHT_ENABLE
228 process_backlight(keycode
, record
) &&
231 process_steno(keycode
, record
) &&
233 #if (defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))) && !defined(NO_MUSIC_MODE)
234 process_music(keycode
, record
) &&
236 #ifdef TAP_DANCE_ENABLE
237 process_tap_dance(keycode
, record
) &&
239 #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
240 process_unicode_common(keycode
, record
) &&
243 process_leader(keycode
, record
) &&
246 process_combo(keycode
, record
) &&
248 #ifdef PRINTING_ENABLE
249 process_printer(keycode
, record
) &&
251 #ifdef AUTO_SHIFT_ENABLE
252 process_auto_shift(keycode
, record
) &&
254 #ifdef TERMINAL_ENABLE
255 process_terminal(keycode
, record
) &&
257 #ifdef SPACE_CADET_ENABLE
258 process_space_cadet(keycode
, record
) &&
260 #ifdef MAGIC_KEYCODE_ENABLE
261 process_magic(keycode
, record
) &&
263 #ifdef GRAVE_ESC_ENABLE
264 process_grave_esc(keycode
, record
) &&
266 #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
267 process_rgb(keycode
, record
) &&
273 if (record
->event
.pressed
) {
282 print("DEBUG: enabled.\n");
284 print("DEBUG: disabled.\n");
291 #ifdef FAUXCLICKY_ENABLE
302 #ifdef VELOCIKEY_ENABLE
307 #ifdef BLUETOOTH_ENABLE
309 set_output(OUTPUT_AUTO
);
312 set_output(OUTPUT_USB
);
315 set_output(OUTPUT_BLUETOOTH
);
318 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_BREATHING)
320 backlight_toggle_breathing();
326 return process_action_kb(record
);
329 __attribute__((weak
)) const bool ascii_to_shift_lut
[128] PROGMEM
= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
331 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0};
333 __attribute__((weak
)) const bool ascii_to_altgr_lut
[128] PROGMEM
= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
335 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
338 __attribute__((weak
)) const uint8_t ascii_to_keycode_lut
[128] PROGMEM
= {// NUL SOH STX ETX EOT ENQ ACK BEL
339 XXXXXXX
, XXXXXXX
, XXXXXXX
, XXXXXXX
, XXXXXXX
, XXXXXXX
, XXXXXXX
, XXXXXXX
,
340 // BS TAB LF VT FF CR SO SI
341 KC_BSPC
, KC_TAB
, KC_ENT
, XXXXXXX
, XXXXXXX
, XXXXXXX
, XXXXXXX
, XXXXXXX
,
342 // DLE DC1 DC2 DC3 DC4 NAK SYN ETB
343 XXXXXXX
, XXXXXXX
, XXXXXXX
, XXXXXXX
, XXXXXXX
, XXXXXXX
, XXXXXXX
, XXXXXXX
,
344 // CAN EM SUB ESC FS GS RS US
345 XXXXXXX
, XXXXXXX
, XXXXXXX
, KC_ESC
, XXXXXXX
, XXXXXXX
, XXXXXXX
, XXXXXXX
,
348 KC_SPC
, KC_1
, KC_QUOT
, KC_3
, KC_4
, KC_5
, KC_7
, KC_QUOT
,
350 KC_9
, KC_0
, KC_8
, KC_EQL
, KC_COMM
, KC_MINS
, KC_DOT
, KC_SLSH
,
352 KC_0
, KC_1
, KC_2
, KC_3
, KC_4
, KC_5
, KC_6
, KC_7
,
354 KC_8
, KC_9
, KC_SCLN
, KC_SCLN
, KC_COMM
, KC_EQL
, KC_DOT
, KC_SLSH
,
356 KC_2
, KC_A
, KC_B
, KC_C
, KC_D
, KC_E
, KC_F
, KC_G
,
358 KC_H
, KC_I
, KC_J
, KC_K
, KC_L
, KC_M
, KC_N
, KC_O
,
360 KC_P
, KC_Q
, KC_R
, KC_S
, KC_T
, KC_U
, KC_V
, KC_W
,
362 KC_X
, KC_Y
, KC_Z
, KC_LBRC
, KC_BSLS
, KC_RBRC
, KC_6
, KC_MINS
,
364 KC_GRV
, KC_A
, KC_B
, KC_C
, KC_D
, KC_E
, KC_F
, KC_G
,
366 KC_H
, KC_I
, KC_J
, KC_K
, KC_L
, KC_M
, KC_N
, KC_O
,
368 KC_P
, KC_Q
, KC_R
, KC_S
, KC_T
, KC_U
, KC_V
, KC_W
,
370 KC_X
, KC_Y
, KC_Z
, KC_LBRC
, KC_BSLS
, KC_RBRC
, KC_GRV
, KC_DEL
};
373 void send_string(const char *str
) { send_string_with_delay(str
, 0); }
375 void send_string_P(const char *str
) { send_string_with_delay_P(str
, 0); }
377 void send_string_with_delay(const char *str
, uint8_t interval
) {
379 char ascii_code
= *str
;
380 if (!ascii_code
) break;
381 if (ascii_code
== SS_QMK_PREFIX
) {
382 ascii_code
= *(++str
);
383 if (ascii_code
== SS_TAP_CODE
) {
385 uint8_t keycode
= *(++str
);
386 register_code(keycode
);
387 unregister_code(keycode
);
388 } else if (ascii_code
== SS_DOWN_CODE
) {
390 uint8_t keycode
= *(++str
);
391 register_code(keycode
);
392 } else if (ascii_code
== SS_UP_CODE
) {
394 uint8_t keycode
= *(++str
);
395 unregister_code(keycode
);
396 } else if (ascii_code
== SS_DELAY_CODE
) {
399 uint8_t keycode
= *(++str
);
400 while (isdigit(keycode
)) {
405 while (ms
--) wait_ms(1);
408 send_char(ascii_code
);
413 uint8_t ms
= interval
;
414 while (ms
--) wait_ms(1);
419 void send_string_with_delay_P(const char *str
, uint8_t interval
) {
421 char ascii_code
= pgm_read_byte(str
);
422 if (!ascii_code
) break;
423 if (ascii_code
== SS_QMK_PREFIX
) {
424 ascii_code
= pgm_read_byte(++str
);
425 if (ascii_code
== SS_TAP_CODE
) {
427 uint8_t keycode
= pgm_read_byte(++str
);
428 register_code(keycode
);
429 unregister_code(keycode
);
430 } else if (ascii_code
== SS_DOWN_CODE
) {
432 uint8_t keycode
= pgm_read_byte(++str
);
433 register_code(keycode
);
434 } else if (ascii_code
== SS_UP_CODE
) {
436 uint8_t keycode
= pgm_read_byte(++str
);
437 unregister_code(keycode
);
438 } else if (ascii_code
== SS_DELAY_CODE
) {
441 uint8_t keycode
= pgm_read_byte(++str
);
442 while (isdigit(keycode
)) {
445 keycode
= pgm_read_byte(++str
);
447 while (ms
--) wait_ms(1);
450 send_char(ascii_code
);
455 uint8_t ms
= interval
;
456 while (ms
--) wait_ms(1);
461 void send_char(char ascii_code
) {
462 #if defined(AUDIO_ENABLE) && defined(SENDSTRING_BELL)
463 if (ascii_code
== '\a') { // BEL
464 PLAY_SONG(bell_song
);
469 uint8_t keycode
= pgm_read_byte(&ascii_to_keycode_lut
[(uint8_t)ascii_code
]);
470 bool is_shifted
= pgm_read_byte(&ascii_to_shift_lut
[(uint8_t)ascii_code
]);
471 bool is_altgred
= pgm_read_byte(&ascii_to_altgr_lut
[(uint8_t)ascii_code
]);
474 register_code(KC_LSFT
);
477 register_code(KC_RALT
);
481 unregister_code(KC_RALT
);
484 unregister_code(KC_LSFT
);
488 void set_single_persistent_default_layer(uint8_t default_layer
) {
489 #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
490 PLAY_SONG(default_layer_songs
[default_layer
]);
492 eeconfig_update_default_layer(1U << default_layer
);
493 default_layer_set(1U << default_layer
);
496 layer_state_t
update_tri_layer_state(layer_state_t state
, uint8_t layer1
, uint8_t layer2
, uint8_t layer3
) {
497 layer_state_t mask12
= (1UL << layer1
) | (1UL << layer2
);
498 layer_state_t mask3
= 1UL << layer3
;
499 return (state
& mask12
) == mask12
? (state
| mask3
) : (state
& ~mask3
);
502 void update_tri_layer(uint8_t layer1
, uint8_t layer2
, uint8_t layer3
) { layer_state_set(update_tri_layer_state(layer_state
, layer1
, layer2
, layer3
)); }
504 void tap_random_base64(void) {
505 #if defined(__AVR_ATmega32U4__)
506 uint8_t key
= (TCNT0
+ TCNT1
+ TCNT3
+ TCNT4
) % 64;
508 uint8_t key
= rand() % 64;
512 register_code(KC_LSFT
);
513 register_code(key
+ KC_A
);
514 unregister_code(key
+ KC_A
);
515 unregister_code(KC_LSFT
);
518 register_code(key
- 26 + KC_A
);
519 unregister_code(key
- 26 + KC_A
);
523 unregister_code(KC_0
);
526 register_code(key
- 53 + KC_1
);
527 unregister_code(key
- 53 + KC_1
);
530 register_code(KC_LSFT
);
531 register_code(KC_EQL
);
532 unregister_code(KC_EQL
);
533 unregister_code(KC_LSFT
);
536 register_code(KC_SLSH
);
537 unregister_code(KC_SLSH
);
542 __attribute__((weak
)) void bootmagic_lite(void) {
543 // The lite version of TMK's bootmagic based on Wilba.
544 // 100% less potential for accidentally making the
545 // keyboard do stupid things.
547 // We need multiple scans because debouncing can't be turned off.
549 #if defined(DEBOUNCE) && DEBOUNCE > 0
550 wait_ms(DEBOUNCE
* 2);
556 // If the Esc and space bar are held down on power up,
557 // reset the EEPROM valid state and jump to bootloader.
558 // Assumes Esc is at [0,0].
559 // This isn't very generalized, but we need something that doesn't
560 // rely on user's keymaps in firmware or EEPROM.
561 if (matrix_get_row(BOOTMAGIC_LITE_ROW
) & (1 << BOOTMAGIC_LITE_COLUMN
)) {
563 // Jump to bootloader.
568 void matrix_init_quantum() {
569 #ifdef BOOTMAGIC_LITE
572 if (!eeconfig_is_enabled()) {
575 #ifdef BACKLIGHT_ENABLE
576 # ifdef LED_MATRIX_ENABLE
579 backlight_init_ports();
585 #ifdef RGB_MATRIX_ENABLE
588 #ifdef ENCODER_ENABLE
591 #if defined(UNICODE_ENABLE) || defined(UNICODEMAP_ENABLE) || defined(UCIS_ENABLE)
592 unicode_input_mode_init();
597 #ifdef OUTPUT_AUTO_ENABLE
598 set_output(OUTPUT_AUTO
);
600 #ifdef DIP_SWITCH_ENABLE
607 void matrix_scan_quantum() {
608 #if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE)
612 #ifdef TAP_DANCE_ENABLE
613 matrix_scan_tap_dance();
620 #ifdef LED_MATRIX_ENABLE
624 #ifdef RGB_MATRIX_ENABLE
628 #ifdef ENCODER_ENABLE
636 #ifdef DIP_SWITCH_ENABLE
637 dip_switch_read(false);
643 #ifdef HD44780_ENABLED
644 # include "hd44780.h"
647 // Functions for spitting out values
650 void send_dword(uint32_t number
) { // this might not actually work
651 uint16_t word
= (number
>> 16);
653 send_word(number
& 0xFFFFUL
);
656 void send_word(uint16_t number
) {
657 uint8_t byte
= number
>> 8;
659 send_byte(number
& 0xFF);
662 void send_byte(uint8_t number
) {
663 uint8_t nibble
= number
>> 4;
665 send_nibble(number
& 0xF);
668 void send_nibble(uint8_t number
) {
672 unregister_code(KC_0
);
675 register_code(KC_1
+ (number
- 1));
676 unregister_code(KC_1
+ (number
- 1));
679 register_code(KC_A
+ (number
- 0xA));
680 unregister_code(KC_A
+ (number
- 0xA));
685 __attribute__((weak
)) uint16_t hex_to_keycode(uint8_t hex
) {
689 } else if (hex
< 0xA) {
690 return KC_1
+ (hex
- 0x1);
692 return KC_A
+ (hex
- 0xA);
696 void api_send_unicode(uint32_t unicode
) {
699 dword_to_bytes(unicode
, chunk
);
700 MT_SEND_DATA(DT_UNICODE
, chunk
, 5);
704 /** \brief Lock LED set callback - keymap/user level
706 * \deprecated Use led_update_user() instead.
708 __attribute__((weak
)) void led_set_user(uint8_t usb_led
) {}
710 /** \brief Lock LED set callback - keyboard level
712 * \deprecated Use led_update_kb() instead.
714 __attribute__((weak
)) void led_set_kb(uint8_t usb_led
) { led_set_user(usb_led
); }
716 /** \brief Lock LED update callback - keymap/user level
718 * \return True if led_update_kb() should run its own code, false otherwise.
720 __attribute__((weak
)) bool led_update_user(led_t led_state
) { return true; }
722 /** \brief Lock LED update callback - keyboard level
724 * \return Ignored for now.
726 __attribute__((weak
)) bool led_update_kb(led_t led_state
) { return led_update_user(led_state
); }
728 __attribute__((weak
)) void led_init_ports(void) {}
730 __attribute__((weak
)) void led_set(uint8_t usb_led
) {
731 #if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE)
732 // Use backlight as Caps Lock indicator
733 uint8_t bl_toggle_lvl
= 0;
735 if (IS_LED_ON(usb_led
, USB_LED_CAPS_LOCK
) && !backlight_config
.enable
) {
736 // Turning Caps Lock ON and backlight is disabled in config
737 // Toggling backlight to the brightest level
738 bl_toggle_lvl
= BACKLIGHT_LEVELS
;
739 } else if (IS_LED_OFF(usb_led
, USB_LED_CAPS_LOCK
) && backlight_config
.enable
) {
740 // Turning Caps Lock OFF and backlight is enabled in config
741 // Toggling backlight and restoring config level
742 bl_toggle_lvl
= backlight_config
.level
;
745 // Set level without modify backlight_config to keep ability to restore state
746 backlight_set(bl_toggle_lvl
);
750 led_update_kb((led_t
)usb_led
);
753 //------------------------------------------------------------------------------
754 // Override these functions in your keymap file to play different tunes on
755 // different events such as startup and bootloader jump
757 __attribute__((weak
)) void startup_user() {}
759 __attribute__((weak
)) void shutdown_user() {}
761 //------------------------------------------------------------------------------