3 static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
5 case QK_MODS ... QK_MODS_MAX:
30 void register_code16 (uint16_t code) {
31 do_code16 (code, register_code);
35 void unregister_code16 (uint16_t code) {
36 unregister_code (code);
37 do_code16 (code, unregister_code);
40 __attribute__ ((weak))
41 bool process_action_kb(keyrecord_t *record) {
45 __attribute__ ((weak))
46 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
47 return process_record_user(keycode, record);
50 __attribute__ ((weak))
51 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
55 void reset_keyboard(void) {
62 #ifdef CATERINA_BOOTLOADER
63 *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
68 // Shift / paren setup
77 static bool shift_interrupted[2] = {0, 0};
79 bool process_record_quantum(keyrecord_t *record) {
81 /* This gets the keycode from the key pressed */
82 keypos_t key = record->event.key;
85 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
86 /* TODO: Use store_or_get_action() or a similar function. */
87 if (!disable_action_cache) {
90 if (record->event.pressed) {
91 layer = layer_switch_get_layer(key);
92 update_source_layers_cache(key, layer);
94 layer = read_source_layers_cache(key);
96 keycode = keymap_key_to_keycode(layer, key);
99 keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
101 // This is how you use actions here
102 // if (keycode == KC_LEAD) {
104 // action.code = ACTION_DEFAULT_LAYER_SET(0);
105 // process_action(record, action);
110 process_record_kb(keycode, record) &&
112 process_midi(keycode, record) &&
115 process_music(keycode, record) &&
117 #ifdef TAP_DANCE_ENABLE
118 process_tap_dance(keycode, record) &&
120 #ifndef DISABLE_LEADER
121 process_leader(keycode, record) &&
123 #ifndef DISABLE_CHORDING
124 process_chording(keycode, record) &&
126 #ifdef UNICODE_ENABLE
127 process_unicode(keycode, record) &&
130 process_ucis(keycode, record) &&
132 #ifdef PRINTING_ENABLE
133 process_printer(keycode, record) &&
134 #ifdef UNICODEMAP_ENABLE
135 process_unicode_map(keycode, record) &&
141 // Shift / paren setup
145 if (record->event.pressed) {
151 if (record->event.pressed) {
152 print("\nDEBUG: enabled.\n");
157 #ifdef RGBLIGHT_ENABLE
159 if (record->event.pressed) {
165 if (record->event.pressed) {
171 if (record->event.pressed) {
172 rgblight_increase_hue();
177 if (record->event.pressed) {
178 rgblight_decrease_hue();
183 if (record->event.pressed) {
184 rgblight_increase_sat();
189 if (record->event.pressed) {
190 rgblight_decrease_sat();
195 if (record->event.pressed) {
196 rgblight_increase_val();
201 if (record->event.pressed) {
202 rgblight_decrease_val();
207 case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
208 if (record->event.pressed) {
209 // MAGIC actions (BOOTMAGIC without the boot)
210 if (!eeconfig_is_enabled()) {
214 keymap_config.raw = eeconfig_read_keymap();
217 case MAGIC_SWAP_CONTROL_CAPSLOCK:
218 keymap_config.swap_control_capslock = true;
220 case MAGIC_CAPSLOCK_TO_CONTROL:
221 keymap_config.capslock_to_control = true;
223 case MAGIC_SWAP_LALT_LGUI:
224 keymap_config.swap_lalt_lgui = true;
226 case MAGIC_SWAP_RALT_RGUI:
227 keymap_config.swap_ralt_rgui = true;
230 keymap_config.no_gui = true;
232 case MAGIC_SWAP_GRAVE_ESC:
233 keymap_config.swap_grave_esc = true;
235 case MAGIC_SWAP_BACKSLASH_BACKSPACE:
236 keymap_config.swap_backslash_backspace = true;
238 case MAGIC_HOST_NKRO:
239 keymap_config.nkro = true;
241 case MAGIC_SWAP_ALT_GUI:
242 keymap_config.swap_lalt_lgui = true;
243 keymap_config.swap_ralt_rgui = true;
245 case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
246 keymap_config.swap_control_capslock = false;
248 case MAGIC_UNCAPSLOCK_TO_CONTROL:
249 keymap_config.capslock_to_control = false;
251 case MAGIC_UNSWAP_LALT_LGUI:
252 keymap_config.swap_lalt_lgui = false;
254 case MAGIC_UNSWAP_RALT_RGUI:
255 keymap_config.swap_ralt_rgui = false;
258 keymap_config.no_gui = false;
260 case MAGIC_UNSWAP_GRAVE_ESC:
261 keymap_config.swap_grave_esc = false;
263 case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
264 keymap_config.swap_backslash_backspace = false;
266 case MAGIC_UNHOST_NKRO:
267 keymap_config.nkro = false;
269 case MAGIC_UNSWAP_ALT_GUI:
270 keymap_config.swap_lalt_lgui = false;
271 keymap_config.swap_ralt_rgui = false;
273 case MAGIC_TOGGLE_NKRO:
274 keymap_config.nkro = !keymap_config.nkro;
279 eeconfig_update_keymap(keymap_config.raw);
280 clear_keyboard(); // clear to prevent stuck keys
286 if (record->event.pressed) {
287 shift_interrupted[0] = false;
288 register_mods(MOD_BIT(KC_LSFT));
291 #ifdef DISABLE_SPACE_CADET_ROLLOVER
292 if (get_mods() & MOD_BIT(KC_RSFT)) {
293 shift_interrupted[0] = true;
294 shift_interrupted[1] = true;
297 if (!shift_interrupted[0]) {
298 register_code(LSPO_KEY);
299 unregister_code(LSPO_KEY);
301 unregister_mods(MOD_BIT(KC_LSFT));
308 if (record->event.pressed) {
309 shift_interrupted[1] = false;
310 register_mods(MOD_BIT(KC_RSFT));
313 #ifdef DISABLE_SPACE_CADET_ROLLOVER
314 if (get_mods() & MOD_BIT(KC_LSFT)) {
315 shift_interrupted[0] = true;
316 shift_interrupted[1] = true;
319 if (!shift_interrupted[1]) {
320 register_code(RSPC_KEY);
321 unregister_code(RSPC_KEY);
323 unregister_mods(MOD_BIT(KC_RSFT));
329 shift_interrupted[0] = true;
330 shift_interrupted[1] = true;
335 return process_action_kb(record);
338 const bool ascii_to_qwerty_shift_lut[0x80] PROGMEM = {
339 0, 0, 0, 0, 0, 0, 0, 0,
340 0, 0, 0, 0, 0, 0, 0, 0,
341 0, 0, 0, 0, 0, 0, 0, 0,
342 0, 0, 0, 0, 0, 0, 0, 0,
343 0, 1, 1, 1, 1, 1, 1, 0,
344 1, 1, 1, 1, 0, 0, 0, 0,
345 0, 0, 0, 0, 0, 0, 0, 0,
346 0, 0, 1, 0, 1, 0, 1, 1,
347 1, 1, 1, 1, 1, 1, 1, 1,
348 1, 1, 1, 1, 1, 1, 1, 1,
349 1, 1, 1, 1, 1, 1, 1, 1,
350 1, 1, 1, 0, 0, 0, 1, 1,
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, 0, 0, 1, 1, 1, 1, 0
357 const uint8_t ascii_to_qwerty_keycode_lut[0x80] PROGMEM = {
358 0, 0, 0, 0, 0, 0, 0, 0,
359 KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
360 0, 0, 0, 0, 0, 0, 0, 0,
361 0, 0, 0, KC_ESC, 0, 0, 0, 0,
362 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
363 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
364 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
365 KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
366 KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
367 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,
369 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
370 KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
371 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
372 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
373 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
376 /* for users whose OSes are set to Colemak */
378 #include "keymap_colemak.h"
380 const bool ascii_to_colemak_shift_lut[0x80] PROGMEM = {
381 0, 0, 0, 0, 0, 0, 0, 0,
382 0, 0, 0, 0, 0, 0, 0, 0,
383 0, 0, 0, 0, 0, 0, 0, 0,
384 0, 0, 0, 0, 0, 0, 0, 0,
385 0, 1, 1, 1, 1, 1, 1, 0,
386 1, 1, 1, 1, 0, 0, 0, 0,
387 0, 0, 0, 0, 0, 0, 0, 0,
388 0, 0, 1, 0, 1, 0, 1, 1,
389 1, 1, 1, 1, 1, 1, 1, 1,
390 1, 1, 1, 1, 1, 1, 1, 1,
391 1, 1, 1, 1, 1, 1, 1, 1,
392 1, 1, 1, 0, 0, 0, 1, 1,
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, 0, 0, 1, 1, 1, 1, 0
399 const uint8_t ascii_to_colemak_keycode_lut[0x80] PROGMEM = {
400 0, 0, 0, 0, 0, 0, 0, 0,
401 KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
402 0, 0, 0, 0, 0, 0, 0, 0,
403 0, 0, 0, KC_ESC, 0, 0, 0, 0,
404 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
405 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
406 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
407 KC_8, KC_9, CM_SCLN, CM_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
408 KC_2, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
409 CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
410 CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
411 CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
412 KC_GRV, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
413 CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
414 CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
415 CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
420 void send_string(const char *str) {
423 uint8_t ascii_code = pgm_read_byte(str);
424 if (!ascii_code) break;
425 keycode = pgm_read_byte(&ascii_to_qwerty_keycode_lut[ascii_code]);
426 if (pgm_read_byte(&ascii_to_qwerty_shift_lut[ascii_code])) {
427 register_code(KC_LSFT);
428 register_code(keycode);
429 unregister_code(keycode);
430 unregister_code(KC_LSFT);
433 register_code(keycode);
434 unregister_code(keycode);
440 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
441 if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
448 void tap_random_base64(void) {
449 #if defined(__AVR_ATmega32U4__)
450 uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
452 uint8_t key = rand() % 64;
456 register_code(KC_LSFT);
457 register_code(key + KC_A);
458 unregister_code(key + KC_A);
459 unregister_code(KC_LSFT);
462 register_code(key - 26 + KC_A);
463 unregister_code(key - 26 + KC_A);
467 unregister_code(KC_0);
470 register_code(key - 53 + KC_1);
471 unregister_code(key - 53 + KC_1);
474 register_code(KC_LSFT);
475 register_code(KC_EQL);
476 unregister_code(KC_EQL);
477 unregister_code(KC_LSFT);
480 register_code(KC_SLSH);
481 unregister_code(KC_SLSH);
486 void matrix_init_quantum() {
487 #ifdef BACKLIGHT_ENABLE
488 backlight_init_ports();
493 void matrix_scan_quantum() {
498 #ifdef TAP_DANCE_ENABLE
499 matrix_scan_tap_dance();
504 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
506 static const uint8_t backlight_pin = BACKLIGHT_PIN;
508 #if BACKLIGHT_PIN == B7
509 # define COM1x1 COM1C1
511 #elif BACKLIGHT_PIN == B6
512 # define COM1x1 COM1B1
514 #elif BACKLIGHT_PIN == B5
515 # define COM1x1 COM1A1
518 # error "Backlight pin not supported - use B5, B6, or B7"
521 __attribute__ ((weak))
522 void backlight_init_ports(void)
525 // Setup backlight pin as output and output low.
527 _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
529 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
531 // Use full 16-bit resolution.
534 // I could write a wall of text here to explain... but TL;DW
535 // Go read the ATmega32u4 datasheet.
536 // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
538 // Pin PB7 = OCR1C (Timer 1, Channel C)
539 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
540 // (i.e. start high, go low when counter matches.)
541 // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
542 // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
544 TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
545 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
548 #ifdef BACKLIGHT_BREATHING
549 breathing_defaults();
553 __attribute__ ((weak))
554 void backlight_set(uint8_t level)
556 // Prevent backlight blink on lowest level
558 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
561 // Turn off PWM control on backlight pin, revert to output low.
562 TCCR1A &= ~(_BV(COM1x1));
564 } else if ( level == BACKLIGHT_LEVELS ) {
565 // Turn on PWM control of backlight pin
566 TCCR1A |= _BV(COM1x1);
567 // Set the brightness
570 // Turn on PWM control of backlight pin
571 TCCR1A |= _BV(COM1x1);
572 // Set the brightness
573 OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
576 #ifdef BACKLIGHT_BREATHING
577 breathing_intensity_default();
582 #ifdef BACKLIGHT_BREATHING
584 #define BREATHING_NO_HALT 0
585 #define BREATHING_HALT_OFF 1
586 #define BREATHING_HALT_ON 2
588 static uint8_t breath_intensity;
589 static uint8_t breath_speed;
590 static uint16_t breathing_index;
591 static uint8_t breathing_halt;
593 void breathing_enable(void)
595 if (get_backlight_level() == 0)
601 // Set breathing_index to be at the midpoint (brightest point)
602 breathing_index = 0x20 << breath_speed;
605 breathing_halt = BREATHING_NO_HALT;
607 // Enable breathing interrupt
608 TIMSK1 |= _BV(OCIE1A);
611 void breathing_pulse(void)
613 if (get_backlight_level() == 0)
619 // Set breathing_index to be at the midpoint + 1 (brightest point)
620 breathing_index = 0x21 << breath_speed;
623 breathing_halt = BREATHING_HALT_ON;
625 // Enable breathing interrupt
626 TIMSK1 |= _BV(OCIE1A);
629 void breathing_disable(void)
631 // Disable breathing interrupt
632 TIMSK1 &= ~_BV(OCIE1A);
633 backlight_set(get_backlight_level());
636 void breathing_self_disable(void)
638 if (get_backlight_level() == 0)
640 breathing_halt = BREATHING_HALT_OFF;
644 breathing_halt = BREATHING_HALT_ON;
647 //backlight_set(get_backlight_level());
650 void breathing_toggle(void)
654 if (get_backlight_level() == 0)
660 // Set breathing_index to be at the midpoint + 1 (brightest point)
661 breathing_index = 0x21 << breath_speed;
664 breathing_halt = BREATHING_NO_HALT;
667 // Toggle breathing interrupt
668 TIMSK1 ^= _BV(OCIE1A);
670 // Restore backlight level
673 backlight_set(get_backlight_level());
677 bool is_breathing(void)
679 return (TIMSK1 && _BV(OCIE1A));
682 void breathing_intensity_default(void)
684 //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
685 breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
688 void breathing_intensity_set(uint8_t value)
690 breath_intensity = value;
693 void breathing_speed_default(void)
698 void breathing_speed_set(uint8_t value)
700 bool is_breathing_now = is_breathing();
701 uint8_t old_breath_speed = breath_speed;
703 if (is_breathing_now)
705 // Disable breathing interrupt
706 TIMSK1 &= ~_BV(OCIE1A);
709 breath_speed = value;
711 if (is_breathing_now)
713 // Adjust index to account for new speed
714 breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
716 // Enable breathing interrupt
717 TIMSK1 |= _BV(OCIE1A);
722 void breathing_speed_inc(uint8_t value)
724 if ((uint16_t)(breath_speed - value) > 10 )
726 breathing_speed_set(0);
730 breathing_speed_set(breath_speed - value);
734 void breathing_speed_dec(uint8_t value)
736 if ((uint16_t)(breath_speed + value) > 10 )
738 breathing_speed_set(10);
742 breathing_speed_set(breath_speed + value);
746 void breathing_defaults(void)
748 breathing_intensity_default();
749 breathing_speed_default();
750 breathing_halt = BREATHING_NO_HALT;
753 /* Breathing Sleep LED brighness(PWM On period) table
754 * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
756 * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
757 * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
759 static const uint8_t breathing_table[64] PROGMEM = {
760 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
761 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
762 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
763 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
766 ISR(TIMER1_COMPA_vect)
768 // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
771 uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
773 if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
775 // Disable breathing interrupt
776 TIMSK1 &= ~_BV(OCIE1A);
779 OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
789 __attribute__ ((weak))
790 void backlight_init_ports(void)
795 __attribute__ ((weak))
796 void backlight_set(uint8_t level)
805 __attribute__ ((weak))
806 void led_set_user(uint8_t usb_led) {
810 __attribute__ ((weak))
811 void led_set_kb(uint8_t usb_led) {
812 led_set_user(usb_led);
815 __attribute__ ((weak))
816 void led_init_ports(void)
821 __attribute__ ((weak))
822 void led_set(uint8_t usb_led)
827 // // Using PE6 Caps Lock LED
828 // if (usb_led & (1<<USB_LED_CAPS_LOCK))
845 //------------------------------------------------------------------------------
846 // Override these functions in your keymap file to play different tunes on
847 // different events such as startup and bootloader jump
849 __attribute__ ((weak))
850 void startup_user() {}
852 __attribute__ ((weak))
853 void shutdown_user() {}
855 //------------------------------------------------------------------------------