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_midi(keycode, record) &&
120 process_music(keycode, record) &&
122 #ifdef TAP_DANCE_ENABLE
123 process_tap_dance(keycode, record) &&
125 #ifndef DISABLE_LEADER
126 process_leader(keycode, record) &&
128 #ifndef DISABLE_CHORDING
129 process_chording(keycode, record) &&
131 #ifdef UNICODE_ENABLE
132 process_unicode(keycode, record) &&
135 process_ucis(keycode, record) &&
137 #ifdef UNICODEMAP_ENABLE
138 process_unicode_map(keycode, record) &&
144 // Shift / paren setup
148 if (record->event.pressed) {
154 if (record->event.pressed) {
155 print("\nDEBUG: enabled.\n");
160 #ifdef RGBLIGHT_ENABLE
162 if (record->event.pressed) {
168 if (record->event.pressed) {
174 if (record->event.pressed) {
175 rgblight_increase_hue();
180 if (record->event.pressed) {
181 rgblight_decrease_hue();
186 if (record->event.pressed) {
187 rgblight_increase_sat();
192 if (record->event.pressed) {
193 rgblight_decrease_sat();
198 if (record->event.pressed) {
199 rgblight_increase_val();
204 if (record->event.pressed) {
205 rgblight_decrease_val();
210 case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
211 if (record->event.pressed) {
212 // MAGIC actions (BOOTMAGIC without the boot)
213 if (!eeconfig_is_enabled()) {
217 keymap_config.raw = eeconfig_read_keymap();
220 case MAGIC_SWAP_CONTROL_CAPSLOCK:
221 keymap_config.swap_control_capslock = true;
223 case MAGIC_CAPSLOCK_TO_CONTROL:
224 keymap_config.capslock_to_control = true;
226 case MAGIC_SWAP_LALT_LGUI:
227 keymap_config.swap_lalt_lgui = true;
229 case MAGIC_SWAP_RALT_RGUI:
230 keymap_config.swap_ralt_rgui = true;
233 keymap_config.no_gui = true;
235 case MAGIC_SWAP_GRAVE_ESC:
236 keymap_config.swap_grave_esc = true;
238 case MAGIC_SWAP_BACKSLASH_BACKSPACE:
239 keymap_config.swap_backslash_backspace = true;
241 case MAGIC_HOST_NKRO:
242 keymap_config.nkro = true;
244 case MAGIC_SWAP_ALT_GUI:
245 keymap_config.swap_lalt_lgui = true;
246 keymap_config.swap_ralt_rgui = true;
248 case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
249 keymap_config.swap_control_capslock = false;
251 case MAGIC_UNCAPSLOCK_TO_CONTROL:
252 keymap_config.capslock_to_control = false;
254 case MAGIC_UNSWAP_LALT_LGUI:
255 keymap_config.swap_lalt_lgui = false;
257 case MAGIC_UNSWAP_RALT_RGUI:
258 keymap_config.swap_ralt_rgui = false;
261 keymap_config.no_gui = false;
263 case MAGIC_UNSWAP_GRAVE_ESC:
264 keymap_config.swap_grave_esc = false;
266 case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
267 keymap_config.swap_backslash_backspace = false;
269 case MAGIC_UNHOST_NKRO:
270 keymap_config.nkro = false;
272 case MAGIC_UNSWAP_ALT_GUI:
273 keymap_config.swap_lalt_lgui = false;
274 keymap_config.swap_ralt_rgui = false;
276 case MAGIC_TOGGLE_NKRO:
277 keymap_config.nkro = !keymap_config.nkro;
282 eeconfig_update_keymap(keymap_config.raw);
283 clear_keyboard(); // clear to prevent stuck keys
289 if (record->event.pressed) {
290 shift_interrupted[0] = false;
291 scs_timer = timer_read ();
292 register_mods(MOD_BIT(KC_LSFT));
295 #ifdef DISABLE_SPACE_CADET_ROLLOVER
296 if (get_mods() & MOD_BIT(KC_RSFT)) {
297 shift_interrupted[0] = true;
298 shift_interrupted[1] = true;
301 if (!shift_interrupted[0] && timer_elapsed(scs_timer) < TAPPING_TERM) {
302 register_code(LSPO_KEY);
303 unregister_code(LSPO_KEY);
305 unregister_mods(MOD_BIT(KC_LSFT));
312 if (record->event.pressed) {
313 shift_interrupted[1] = false;
314 scs_timer = timer_read ();
315 register_mods(MOD_BIT(KC_RSFT));
318 #ifdef DISABLE_SPACE_CADET_ROLLOVER
319 if (get_mods() & MOD_BIT(KC_LSFT)) {
320 shift_interrupted[0] = true;
321 shift_interrupted[1] = true;
324 if (!shift_interrupted[1] && timer_elapsed(scs_timer) < TAPPING_TERM) {
325 register_code(RSPC_KEY);
326 unregister_code(RSPC_KEY);
328 unregister_mods(MOD_BIT(KC_RSFT));
334 shift_interrupted[0] = true;
335 shift_interrupted[1] = true;
340 return process_action_kb(record);
343 const bool ascii_to_qwerty_shift_lut[0x80] PROGMEM = {
344 0, 0, 0, 0, 0, 0, 0, 0,
345 0, 0, 0, 0, 0, 0, 0, 0,
346 0, 0, 0, 0, 0, 0, 0, 0,
347 0, 0, 0, 0, 0, 0, 0, 0,
348 0, 1, 1, 1, 1, 1, 1, 0,
349 1, 1, 1, 1, 0, 0, 0, 0,
350 0, 0, 0, 0, 0, 0, 0, 0,
351 0, 0, 1, 0, 1, 0, 1, 1,
352 1, 1, 1, 1, 1, 1, 1, 1,
353 1, 1, 1, 1, 1, 1, 1, 1,
354 1, 1, 1, 1, 1, 1, 1, 1,
355 1, 1, 1, 0, 0, 0, 1, 1,
356 0, 0, 0, 0, 0, 0, 0, 0,
357 0, 0, 0, 0, 0, 0, 0, 0,
358 0, 0, 0, 0, 0, 0, 0, 0,
359 0, 0, 0, 1, 1, 1, 1, 0
362 const uint8_t ascii_to_qwerty_keycode_lut[0x80] PROGMEM = {
363 0, 0, 0, 0, 0, 0, 0, 0,
364 KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
365 0, 0, 0, 0, 0, 0, 0, 0,
366 0, 0, 0, KC_ESC, 0, 0, 0, 0,
367 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
368 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
369 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
370 KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
371 KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
372 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
373 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
374 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
375 KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
376 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
377 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
378 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
381 /* for users whose OSes are set to Colemak */
383 #include "keymap_colemak.h"
385 const bool ascii_to_colemak_shift_lut[0x80] PROGMEM = {
386 0, 0, 0, 0, 0, 0, 0, 0,
387 0, 0, 0, 0, 0, 0, 0, 0,
388 0, 0, 0, 0, 0, 0, 0, 0,
389 0, 0, 0, 0, 0, 0, 0, 0,
390 0, 1, 1, 1, 1, 1, 1, 0,
391 1, 1, 1, 1, 0, 0, 0, 0,
392 0, 0, 0, 0, 0, 0, 0, 0,
393 0, 0, 1, 0, 1, 0, 1, 1,
394 1, 1, 1, 1, 1, 1, 1, 1,
395 1, 1, 1, 1, 1, 1, 1, 1,
396 1, 1, 1, 1, 1, 1, 1, 1,
397 1, 1, 1, 0, 0, 0, 1, 1,
398 0, 0, 0, 0, 0, 0, 0, 0,
399 0, 0, 0, 0, 0, 0, 0, 0,
400 0, 0, 0, 0, 0, 0, 0, 0,
401 0, 0, 0, 1, 1, 1, 1, 0
404 const uint8_t ascii_to_colemak_keycode_lut[0x80] PROGMEM = {
405 0, 0, 0, 0, 0, 0, 0, 0,
406 KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
407 0, 0, 0, 0, 0, 0, 0, 0,
408 0, 0, 0, KC_ESC, 0, 0, 0, 0,
409 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
410 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
411 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
412 KC_8, KC_9, CM_SCLN, CM_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
413 KC_2, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
414 CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
415 CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
416 CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
417 KC_GRV, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
418 CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
419 CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
420 CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
425 void send_string(const char *str) {
428 uint8_t ascii_code = pgm_read_byte(str);
429 if (!ascii_code) break;
430 keycode = pgm_read_byte(&ascii_to_qwerty_keycode_lut[ascii_code]);
431 if (pgm_read_byte(&ascii_to_qwerty_shift_lut[ascii_code])) {
432 register_code(KC_LSFT);
433 register_code(keycode);
434 unregister_code(keycode);
435 unregister_code(KC_LSFT);
438 register_code(keycode);
439 unregister_code(keycode);
445 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
446 if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
453 void tap_random_base64(void) {
454 #if defined(__AVR_ATmega32U4__)
455 uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
457 uint8_t key = rand() % 64;
461 register_code(KC_LSFT);
462 register_code(key + KC_A);
463 unregister_code(key + KC_A);
464 unregister_code(KC_LSFT);
467 register_code(key - 26 + KC_A);
468 unregister_code(key - 26 + KC_A);
472 unregister_code(KC_0);
475 register_code(key - 53 + KC_1);
476 unregister_code(key - 53 + KC_1);
479 register_code(KC_LSFT);
480 register_code(KC_EQL);
481 unregister_code(KC_EQL);
482 unregister_code(KC_LSFT);
485 register_code(KC_SLSH);
486 unregister_code(KC_SLSH);
491 void matrix_init_quantum() {
492 #ifdef BACKLIGHT_ENABLE
493 backlight_init_ports();
498 void matrix_scan_quantum() {
503 #ifdef TAP_DANCE_ENABLE
504 matrix_scan_tap_dance();
509 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
511 static const uint8_t backlight_pin = BACKLIGHT_PIN;
513 #if BACKLIGHT_PIN == B7
514 # define COM1x1 COM1C1
516 #elif BACKLIGHT_PIN == B6
517 # define COM1x1 COM1B1
519 #elif BACKLIGHT_PIN == B5
520 # define COM1x1 COM1A1
523 # error "Backlight pin not supported - use B5, B6, or B7"
526 __attribute__ ((weak))
527 void backlight_init_ports(void)
530 // Setup backlight pin as output and output low.
532 _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
534 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
536 // Use full 16-bit resolution.
539 // I could write a wall of text here to explain... but TL;DW
540 // Go read the ATmega32u4 datasheet.
541 // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
543 // Pin PB7 = OCR1C (Timer 1, Channel C)
544 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
545 // (i.e. start high, go low when counter matches.)
546 // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
547 // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
549 TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
550 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
553 #ifdef BACKLIGHT_BREATHING
554 breathing_defaults();
558 __attribute__ ((weak))
559 void backlight_set(uint8_t level)
561 // Prevent backlight blink on lowest level
563 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
566 // Turn off PWM control on backlight pin, revert to output low.
567 TCCR1A &= ~(_BV(COM1x1));
569 } else if ( level == BACKLIGHT_LEVELS ) {
570 // Turn on PWM control of backlight pin
571 TCCR1A |= _BV(COM1x1);
572 // Set the brightness
575 // Turn on PWM control of backlight pin
576 TCCR1A |= _BV(COM1x1);
577 // Set the brightness
578 OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
581 #ifdef BACKLIGHT_BREATHING
582 breathing_intensity_default();
587 #ifdef BACKLIGHT_BREATHING
589 #define BREATHING_NO_HALT 0
590 #define BREATHING_HALT_OFF 1
591 #define BREATHING_HALT_ON 2
593 static uint8_t breath_intensity;
594 static uint8_t breath_speed;
595 static uint16_t breathing_index;
596 static uint8_t breathing_halt;
598 void breathing_enable(void)
600 if (get_backlight_level() == 0)
606 // Set breathing_index to be at the midpoint (brightest point)
607 breathing_index = 0x20 << breath_speed;
610 breathing_halt = BREATHING_NO_HALT;
612 // Enable breathing interrupt
613 TIMSK1 |= _BV(OCIE1A);
616 void breathing_pulse(void)
618 if (get_backlight_level() == 0)
624 // Set breathing_index to be at the midpoint + 1 (brightest point)
625 breathing_index = 0x21 << breath_speed;
628 breathing_halt = BREATHING_HALT_ON;
630 // Enable breathing interrupt
631 TIMSK1 |= _BV(OCIE1A);
634 void breathing_disable(void)
636 // Disable breathing interrupt
637 TIMSK1 &= ~_BV(OCIE1A);
638 backlight_set(get_backlight_level());
641 void breathing_self_disable(void)
643 if (get_backlight_level() == 0)
645 breathing_halt = BREATHING_HALT_OFF;
649 breathing_halt = BREATHING_HALT_ON;
652 //backlight_set(get_backlight_level());
655 void breathing_toggle(void)
659 if (get_backlight_level() == 0)
665 // Set breathing_index to be at the midpoint + 1 (brightest point)
666 breathing_index = 0x21 << breath_speed;
669 breathing_halt = BREATHING_NO_HALT;
672 // Toggle breathing interrupt
673 TIMSK1 ^= _BV(OCIE1A);
675 // Restore backlight level
678 backlight_set(get_backlight_level());
682 bool is_breathing(void)
684 return (TIMSK1 && _BV(OCIE1A));
687 void breathing_intensity_default(void)
689 //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
690 breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
693 void breathing_intensity_set(uint8_t value)
695 breath_intensity = value;
698 void breathing_speed_default(void)
703 void breathing_speed_set(uint8_t value)
705 bool is_breathing_now = is_breathing();
706 uint8_t old_breath_speed = breath_speed;
708 if (is_breathing_now)
710 // Disable breathing interrupt
711 TIMSK1 &= ~_BV(OCIE1A);
714 breath_speed = value;
716 if (is_breathing_now)
718 // Adjust index to account for new speed
719 breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
721 // Enable breathing interrupt
722 TIMSK1 |= _BV(OCIE1A);
727 void breathing_speed_inc(uint8_t value)
729 if ((uint16_t)(breath_speed - value) > 10 )
731 breathing_speed_set(0);
735 breathing_speed_set(breath_speed - value);
739 void breathing_speed_dec(uint8_t value)
741 if ((uint16_t)(breath_speed + value) > 10 )
743 breathing_speed_set(10);
747 breathing_speed_set(breath_speed + value);
751 void breathing_defaults(void)
753 breathing_intensity_default();
754 breathing_speed_default();
755 breathing_halt = BREATHING_NO_HALT;
758 /* Breathing Sleep LED brighness(PWM On period) table
759 * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
761 * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
762 * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
764 static const uint8_t breathing_table[64] PROGMEM = {
765 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
766 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
767 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
768 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
771 ISR(TIMER1_COMPA_vect)
773 // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
776 uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
778 if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
780 // Disable breathing interrupt
781 TIMSK1 &= ~_BV(OCIE1A);
784 OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
794 __attribute__ ((weak))
795 void backlight_init_ports(void)
800 __attribute__ ((weak))
801 void backlight_set(uint8_t level)
810 __attribute__ ((weak))
811 void led_set_user(uint8_t usb_led) {
815 __attribute__ ((weak))
816 void led_set_kb(uint8_t usb_led) {
817 led_set_user(usb_led);
820 __attribute__ ((weak))
821 void led_init_ports(void)
826 __attribute__ ((weak))
827 void led_set(uint8_t usb_led)
832 // // Using PE6 Caps Lock LED
833 // if (usb_led & (1<<USB_LED_CAPS_LOCK))
850 //------------------------------------------------------------------------------
851 // Override these functions in your keymap file to play different tunes on
852 // different events such as startup and bootloader jump
854 __attribute__ ((weak))
855 void startup_user() {}
857 __attribute__ ((weak))
858 void shutdown_user() {}
860 //------------------------------------------------------------------------------