4 #define TAPPING_TERM 200
7 static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
9 case QK_MODS ... QK_MODS_MAX:
34 void register_code16 (uint16_t code) {
35 do_code16 (code, register_code);
39 void unregister_code16 (uint16_t code) {
40 unregister_code (code);
41 do_code16 (code, unregister_code);
44 __attribute__ ((weak))
45 bool process_action_kb(keyrecord_t *record) {
49 __attribute__ ((weak))
50 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
51 return process_record_user(keycode, record);
54 __attribute__ ((weak))
55 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
59 void reset_keyboard(void) {
66 #ifdef CATERINA_BOOTLOADER
67 *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
72 // Shift / paren setup
81 static bool shift_interrupted[2] = {0, 0};
82 static uint16_t scs_timer = 0;
84 bool process_record_quantum(keyrecord_t *record) {
86 /* This gets the keycode from the key pressed */
87 keypos_t key = record->event.key;
90 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
91 /* TODO: Use store_or_get_action() or a similar function. */
92 if (!disable_action_cache) {
95 if (record->event.pressed) {
96 layer = layer_switch_get_layer(key);
97 update_source_layers_cache(key, layer);
99 layer = read_source_layers_cache(key);
101 keycode = keymap_key_to_keycode(layer, key);
104 keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
106 // This is how you use actions here
107 // if (keycode == KC_LEAD) {
109 // action.code = ACTION_DEFAULT_LAYER_SET(0);
110 // process_action(record, action);
115 process_record_kb(keycode, record) &&
117 process_combo(keycode, record) &&
120 process_midi(keycode, record) &&
123 process_music(keycode, record) &&
125 #ifdef TAP_DANCE_ENABLE
126 process_tap_dance(keycode, record) &&
128 #ifndef DISABLE_LEADER
129 process_leader(keycode, record) &&
131 #ifndef DISABLE_CHORDING
132 process_chording(keycode, record) &&
134 #ifdef UNICODE_ENABLE
135 process_unicode(keycode, record) &&
138 process_ucis(keycode, record) &&
140 #ifdef PRINTING_ENABLE
141 process_printer(keycode, record) &&
143 #ifdef UNICODEMAP_ENABLE
144 process_unicode_map(keycode, record) &&
150 // Shift / paren setup
154 if (record->event.pressed) {
160 if (record->event.pressed) {
161 print("\nDEBUG: enabled.\n");
166 #ifdef RGBLIGHT_ENABLE
168 if (record->event.pressed) {
174 if (record->event.pressed) {
180 if (record->event.pressed) {
181 rgblight_increase_hue();
186 if (record->event.pressed) {
187 rgblight_decrease_hue();
192 if (record->event.pressed) {
193 rgblight_increase_sat();
198 if (record->event.pressed) {
199 rgblight_decrease_sat();
204 if (record->event.pressed) {
205 rgblight_increase_val();
210 if (record->event.pressed) {
211 rgblight_decrease_val();
216 case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
217 if (record->event.pressed) {
218 // MAGIC actions (BOOTMAGIC without the boot)
219 if (!eeconfig_is_enabled()) {
223 keymap_config.raw = eeconfig_read_keymap();
226 case MAGIC_SWAP_CONTROL_CAPSLOCK:
227 keymap_config.swap_control_capslock = true;
229 case MAGIC_CAPSLOCK_TO_CONTROL:
230 keymap_config.capslock_to_control = true;
232 case MAGIC_SWAP_LALT_LGUI:
233 keymap_config.swap_lalt_lgui = true;
235 case MAGIC_SWAP_RALT_RGUI:
236 keymap_config.swap_ralt_rgui = true;
239 keymap_config.no_gui = true;
241 case MAGIC_SWAP_GRAVE_ESC:
242 keymap_config.swap_grave_esc = true;
244 case MAGIC_SWAP_BACKSLASH_BACKSPACE:
245 keymap_config.swap_backslash_backspace = true;
247 case MAGIC_HOST_NKRO:
248 keymap_config.nkro = true;
250 case MAGIC_SWAP_ALT_GUI:
251 keymap_config.swap_lalt_lgui = true;
252 keymap_config.swap_ralt_rgui = true;
254 case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
255 keymap_config.swap_control_capslock = false;
257 case MAGIC_UNCAPSLOCK_TO_CONTROL:
258 keymap_config.capslock_to_control = false;
260 case MAGIC_UNSWAP_LALT_LGUI:
261 keymap_config.swap_lalt_lgui = false;
263 case MAGIC_UNSWAP_RALT_RGUI:
264 keymap_config.swap_ralt_rgui = false;
267 keymap_config.no_gui = false;
269 case MAGIC_UNSWAP_GRAVE_ESC:
270 keymap_config.swap_grave_esc = false;
272 case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
273 keymap_config.swap_backslash_backspace = false;
275 case MAGIC_UNHOST_NKRO:
276 keymap_config.nkro = false;
278 case MAGIC_UNSWAP_ALT_GUI:
279 keymap_config.swap_lalt_lgui = false;
280 keymap_config.swap_ralt_rgui = false;
282 case MAGIC_TOGGLE_NKRO:
283 keymap_config.nkro = !keymap_config.nkro;
288 eeconfig_update_keymap(keymap_config.raw);
289 clear_keyboard(); // clear to prevent stuck keys
295 if (record->event.pressed) {
296 shift_interrupted[0] = false;
297 scs_timer = timer_read ();
298 register_mods(MOD_BIT(KC_LSFT));
301 #ifdef DISABLE_SPACE_CADET_ROLLOVER
302 if (get_mods() & MOD_BIT(KC_RSFT)) {
303 shift_interrupted[0] = true;
304 shift_interrupted[1] = true;
307 if (!shift_interrupted[0] && timer_elapsed(scs_timer) < TAPPING_TERM) {
308 register_code(LSPO_KEY);
309 unregister_code(LSPO_KEY);
311 unregister_mods(MOD_BIT(KC_LSFT));
318 if (record->event.pressed) {
319 shift_interrupted[1] = false;
320 scs_timer = timer_read ();
321 register_mods(MOD_BIT(KC_RSFT));
324 #ifdef DISABLE_SPACE_CADET_ROLLOVER
325 if (get_mods() & MOD_BIT(KC_LSFT)) {
326 shift_interrupted[0] = true;
327 shift_interrupted[1] = true;
330 if (!shift_interrupted[1] && timer_elapsed(scs_timer) < TAPPING_TERM) {
331 register_code(RSPC_KEY);
332 unregister_code(RSPC_KEY);
334 unregister_mods(MOD_BIT(KC_RSFT));
340 shift_interrupted[0] = true;
341 shift_interrupted[1] = true;
346 return process_action_kb(record);
349 const bool ascii_to_qwerty_shift_lut[0x80] PROGMEM = {
350 0, 0, 0, 0, 0, 0, 0, 0,
351 0, 0, 0, 0, 0, 0, 0, 0,
352 0, 0, 0, 0, 0, 0, 0, 0,
353 0, 0, 0, 0, 0, 0, 0, 0,
354 0, 1, 1, 1, 1, 1, 1, 0,
355 1, 1, 1, 1, 0, 0, 0, 0,
356 0, 0, 0, 0, 0, 0, 0, 0,
357 0, 0, 1, 0, 1, 0, 1, 1,
358 1, 1, 1, 1, 1, 1, 1, 1,
359 1, 1, 1, 1, 1, 1, 1, 1,
360 1, 1, 1, 1, 1, 1, 1, 1,
361 1, 1, 1, 0, 0, 0, 1, 1,
362 0, 0, 0, 0, 0, 0, 0, 0,
363 0, 0, 0, 0, 0, 0, 0, 0,
364 0, 0, 0, 0, 0, 0, 0, 0,
365 0, 0, 0, 1, 1, 1, 1, 0
368 const uint8_t ascii_to_qwerty_keycode_lut[0x80] PROGMEM = {
369 0, 0, 0, 0, 0, 0, 0, 0,
370 KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
371 0, 0, 0, 0, 0, 0, 0, 0,
372 0, 0, 0, KC_ESC, 0, 0, 0, 0,
373 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
374 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
375 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
376 KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
377 KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
378 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
379 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
380 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
381 KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
382 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
383 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
384 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
387 /* for users whose OSes are set to Colemak */
389 #include "keymap_colemak.h"
391 const bool ascii_to_colemak_shift_lut[0x80] PROGMEM = {
392 0, 0, 0, 0, 0, 0, 0, 0,
393 0, 0, 0, 0, 0, 0, 0, 0,
394 0, 0, 0, 0, 0, 0, 0, 0,
395 0, 0, 0, 0, 0, 0, 0, 0,
396 0, 1, 1, 1, 1, 1, 1, 0,
397 1, 1, 1, 1, 0, 0, 0, 0,
398 0, 0, 0, 0, 0, 0, 0, 0,
399 0, 0, 1, 0, 1, 0, 1, 1,
400 1, 1, 1, 1, 1, 1, 1, 1,
401 1, 1, 1, 1, 1, 1, 1, 1,
402 1, 1, 1, 1, 1, 1, 1, 1,
403 1, 1, 1, 0, 0, 0, 1, 1,
404 0, 0, 0, 0, 0, 0, 0, 0,
405 0, 0, 0, 0, 0, 0, 0, 0,
406 0, 0, 0, 0, 0, 0, 0, 0,
407 0, 0, 0, 1, 1, 1, 1, 0
410 const uint8_t ascii_to_colemak_keycode_lut[0x80] PROGMEM = {
411 0, 0, 0, 0, 0, 0, 0, 0,
412 KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
413 0, 0, 0, 0, 0, 0, 0, 0,
414 0, 0, 0, KC_ESC, 0, 0, 0, 0,
415 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
416 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
417 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
418 KC_8, KC_9, CM_SCLN, CM_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
419 KC_2, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
420 CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
421 CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
422 CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
423 KC_GRV, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
424 CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
425 CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
426 CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
431 void send_string(const char *str) {
434 uint8_t ascii_code = pgm_read_byte(str);
435 if (!ascii_code) break;
436 keycode = pgm_read_byte(&ascii_to_qwerty_keycode_lut[ascii_code]);
437 if (pgm_read_byte(&ascii_to_qwerty_shift_lut[ascii_code])) {
438 register_code(KC_LSFT);
439 register_code(keycode);
440 unregister_code(keycode);
441 unregister_code(KC_LSFT);
444 register_code(keycode);
445 unregister_code(keycode);
451 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
452 if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
459 void tap_random_base64(void) {
460 #if defined(__AVR_ATmega32U4__)
461 uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
463 uint8_t key = rand() % 64;
467 register_code(KC_LSFT);
468 register_code(key + KC_A);
469 unregister_code(key + KC_A);
470 unregister_code(KC_LSFT);
473 register_code(key - 26 + KC_A);
474 unregister_code(key - 26 + KC_A);
478 unregister_code(KC_0);
481 register_code(key - 53 + KC_1);
482 unregister_code(key - 53 + KC_1);
485 register_code(KC_LSFT);
486 register_code(KC_EQL);
487 unregister_code(KC_EQL);
488 unregister_code(KC_LSFT);
491 register_code(KC_SLSH);
492 unregister_code(KC_SLSH);
497 void matrix_init_quantum() {
498 #ifdef BACKLIGHT_ENABLE
499 backlight_init_ports();
504 void matrix_scan_quantum() {
509 #ifdef TAP_DANCE_ENABLE
510 matrix_scan_tap_dance();
515 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
517 static const uint8_t backlight_pin = BACKLIGHT_PIN;
519 #if BACKLIGHT_PIN == B7
520 # define COM1x1 COM1C1
522 #elif BACKLIGHT_PIN == B6
523 # define COM1x1 COM1B1
525 #elif BACKLIGHT_PIN == B5
526 # define COM1x1 COM1A1
529 # error "Backlight pin not supported - use B5, B6, or B7"
532 __attribute__ ((weak))
533 void backlight_init_ports(void)
536 // Setup backlight pin as output and output low.
538 _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
540 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
542 // Use full 16-bit resolution.
545 // I could write a wall of text here to explain... but TL;DW
546 // Go read the ATmega32u4 datasheet.
547 // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
549 // Pin PB7 = OCR1C (Timer 1, Channel C)
550 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
551 // (i.e. start high, go low when counter matches.)
552 // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
553 // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
555 TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
556 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
559 #ifdef BACKLIGHT_BREATHING
560 breathing_defaults();
564 __attribute__ ((weak))
565 void backlight_set(uint8_t level)
567 // Prevent backlight blink on lowest level
569 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
572 // Turn off PWM control on backlight pin, revert to output low.
573 TCCR1A &= ~(_BV(COM1x1));
575 } else if ( level == BACKLIGHT_LEVELS ) {
576 // Turn on PWM control of backlight pin
577 TCCR1A |= _BV(COM1x1);
578 // Set the brightness
581 // Turn on PWM control of backlight pin
582 TCCR1A |= _BV(COM1x1);
583 // Set the brightness
584 OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
587 #ifdef BACKLIGHT_BREATHING
588 breathing_intensity_default();
593 #ifdef BACKLIGHT_BREATHING
595 #define BREATHING_NO_HALT 0
596 #define BREATHING_HALT_OFF 1
597 #define BREATHING_HALT_ON 2
599 static uint8_t breath_intensity;
600 static uint8_t breath_speed;
601 static uint16_t breathing_index;
602 static uint8_t breathing_halt;
604 void breathing_enable(void)
606 if (get_backlight_level() == 0)
612 // Set breathing_index to be at the midpoint (brightest point)
613 breathing_index = 0x20 << breath_speed;
616 breathing_halt = BREATHING_NO_HALT;
618 // Enable breathing interrupt
619 TIMSK1 |= _BV(OCIE1A);
622 void breathing_pulse(void)
624 if (get_backlight_level() == 0)
630 // Set breathing_index to be at the midpoint + 1 (brightest point)
631 breathing_index = 0x21 << breath_speed;
634 breathing_halt = BREATHING_HALT_ON;
636 // Enable breathing interrupt
637 TIMSK1 |= _BV(OCIE1A);
640 void breathing_disable(void)
642 // Disable breathing interrupt
643 TIMSK1 &= ~_BV(OCIE1A);
644 backlight_set(get_backlight_level());
647 void breathing_self_disable(void)
649 if (get_backlight_level() == 0)
651 breathing_halt = BREATHING_HALT_OFF;
655 breathing_halt = BREATHING_HALT_ON;
658 //backlight_set(get_backlight_level());
661 void breathing_toggle(void)
665 if (get_backlight_level() == 0)
671 // Set breathing_index to be at the midpoint + 1 (brightest point)
672 breathing_index = 0x21 << breath_speed;
675 breathing_halt = BREATHING_NO_HALT;
678 // Toggle breathing interrupt
679 TIMSK1 ^= _BV(OCIE1A);
681 // Restore backlight level
684 backlight_set(get_backlight_level());
688 bool is_breathing(void)
690 return (TIMSK1 && _BV(OCIE1A));
693 void breathing_intensity_default(void)
695 //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
696 breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
699 void breathing_intensity_set(uint8_t value)
701 breath_intensity = value;
704 void breathing_speed_default(void)
709 void breathing_speed_set(uint8_t value)
711 bool is_breathing_now = is_breathing();
712 uint8_t old_breath_speed = breath_speed;
714 if (is_breathing_now)
716 // Disable breathing interrupt
717 TIMSK1 &= ~_BV(OCIE1A);
720 breath_speed = value;
722 if (is_breathing_now)
724 // Adjust index to account for new speed
725 breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
727 // Enable breathing interrupt
728 TIMSK1 |= _BV(OCIE1A);
733 void breathing_speed_inc(uint8_t value)
735 if ((uint16_t)(breath_speed - value) > 10 )
737 breathing_speed_set(0);
741 breathing_speed_set(breath_speed - value);
745 void breathing_speed_dec(uint8_t value)
747 if ((uint16_t)(breath_speed + value) > 10 )
749 breathing_speed_set(10);
753 breathing_speed_set(breath_speed + value);
757 void breathing_defaults(void)
759 breathing_intensity_default();
760 breathing_speed_default();
761 breathing_halt = BREATHING_NO_HALT;
764 /* Breathing Sleep LED brighness(PWM On period) table
765 * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
767 * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
768 * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
770 static const uint8_t breathing_table[64] PROGMEM = {
771 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
772 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
773 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
774 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
777 ISR(TIMER1_COMPA_vect)
779 // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
782 uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
784 if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
786 // Disable breathing interrupt
787 TIMSK1 &= ~_BV(OCIE1A);
790 OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
800 __attribute__ ((weak))
801 void backlight_init_ports(void)
806 __attribute__ ((weak))
807 void backlight_set(uint8_t level)
815 // Functions for spitting out values
818 void send_dword(uint32_t number) { // this might not actually work
819 uint16_t word = (number >> 16);
821 send_word(number & 0xFFFFUL);
824 void send_word(uint16_t number) {
825 uint8_t byte = number >> 8;
827 send_byte(number & 0xFF);
830 void send_byte(uint8_t number) {
831 uint8_t nibble = number >> 4;
833 send_nibble(number & 0xF);
836 void send_nibble(uint8_t number) {
840 unregister_code(KC_0);
843 register_code(KC_1 + (number - 1));
844 unregister_code(KC_1 + (number - 1));
847 register_code(KC_A + (number - 0xA));
848 unregister_code(KC_A + (number - 0xA));
853 void api_send_unicode(uint32_t unicode) {
856 dword_to_bytes(unicode, chunk);
857 MT_SEND_DATA(DT_UNICODE, chunk, 5);
861 __attribute__ ((weak))
862 void led_set_user(uint8_t usb_led) {
866 __attribute__ ((weak))
867 void led_set_kb(uint8_t usb_led) {
868 led_set_user(usb_led);
871 __attribute__ ((weak))
872 void led_init_ports(void)
877 __attribute__ ((weak))
878 void led_set(uint8_t usb_led)
883 // // Using PE6 Caps Lock LED
884 // if (usb_led & (1<<USB_LED_CAPS_LOCK))
901 //------------------------------------------------------------------------------
902 // Override these functions in your keymap file to play different tunes on
903 // different events such as startup and bootloader jump
905 __attribute__ ((weak))
906 void startup_user() {}
908 __attribute__ ((weak))
909 void shutdown_user() {}
911 //------------------------------------------------------------------------------