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) &&
135 #ifdef UNICODEMAP_ENABLE
136 process_unicode_map(keycode, record) &&
142 // Shift / paren setup
146 if (record->event.pressed) {
152 if (record->event.pressed) {
153 print("\nDEBUG: enabled.\n");
158 #ifdef RGBLIGHT_ENABLE
160 if (record->event.pressed) {
166 if (record->event.pressed) {
172 if (record->event.pressed) {
173 rgblight_increase_hue();
178 if (record->event.pressed) {
179 rgblight_decrease_hue();
184 if (record->event.pressed) {
185 rgblight_increase_sat();
190 if (record->event.pressed) {
191 rgblight_decrease_sat();
196 if (record->event.pressed) {
197 rgblight_increase_val();
202 if (record->event.pressed) {
203 rgblight_decrease_val();
208 case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
209 if (record->event.pressed) {
210 // MAGIC actions (BOOTMAGIC without the boot)
211 if (!eeconfig_is_enabled()) {
215 keymap_config.raw = eeconfig_read_keymap();
218 case MAGIC_SWAP_CONTROL_CAPSLOCK:
219 keymap_config.swap_control_capslock = true;
221 case MAGIC_CAPSLOCK_TO_CONTROL:
222 keymap_config.capslock_to_control = true;
224 case MAGIC_SWAP_LALT_LGUI:
225 keymap_config.swap_lalt_lgui = true;
227 case MAGIC_SWAP_RALT_RGUI:
228 keymap_config.swap_ralt_rgui = true;
231 keymap_config.no_gui = true;
233 case MAGIC_SWAP_GRAVE_ESC:
234 keymap_config.swap_grave_esc = true;
236 case MAGIC_SWAP_BACKSLASH_BACKSPACE:
237 keymap_config.swap_backslash_backspace = true;
239 case MAGIC_HOST_NKRO:
240 keymap_config.nkro = true;
242 case MAGIC_SWAP_ALT_GUI:
243 keymap_config.swap_lalt_lgui = true;
244 keymap_config.swap_ralt_rgui = true;
246 case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
247 keymap_config.swap_control_capslock = false;
249 case MAGIC_UNCAPSLOCK_TO_CONTROL:
250 keymap_config.capslock_to_control = false;
252 case MAGIC_UNSWAP_LALT_LGUI:
253 keymap_config.swap_lalt_lgui = false;
255 case MAGIC_UNSWAP_RALT_RGUI:
256 keymap_config.swap_ralt_rgui = false;
259 keymap_config.no_gui = false;
261 case MAGIC_UNSWAP_GRAVE_ESC:
262 keymap_config.swap_grave_esc = false;
264 case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
265 keymap_config.swap_backslash_backspace = false;
267 case MAGIC_UNHOST_NKRO:
268 keymap_config.nkro = false;
270 case MAGIC_UNSWAP_ALT_GUI:
271 keymap_config.swap_lalt_lgui = false;
272 keymap_config.swap_ralt_rgui = false;
274 case MAGIC_TOGGLE_NKRO:
275 keymap_config.nkro = !keymap_config.nkro;
280 eeconfig_update_keymap(keymap_config.raw);
281 clear_keyboard(); // clear to prevent stuck keys
287 if (record->event.pressed) {
288 shift_interrupted[0] = false;
289 register_mods(MOD_BIT(KC_LSFT));
292 #ifdef DISABLE_SPACE_CADET_ROLLOVER
293 if (get_mods() & MOD_BIT(KC_RSFT)) {
294 shift_interrupted[0] = true;
295 shift_interrupted[1] = true;
298 if (!shift_interrupted[0]) {
299 register_code(LSPO_KEY);
300 unregister_code(LSPO_KEY);
302 unregister_mods(MOD_BIT(KC_LSFT));
309 if (record->event.pressed) {
310 shift_interrupted[1] = false;
311 register_mods(MOD_BIT(KC_RSFT));
314 #ifdef DISABLE_SPACE_CADET_ROLLOVER
315 if (get_mods() & MOD_BIT(KC_LSFT)) {
316 shift_interrupted[0] = true;
317 shift_interrupted[1] = true;
320 if (!shift_interrupted[1]) {
321 register_code(RSPC_KEY);
322 unregister_code(RSPC_KEY);
324 unregister_mods(MOD_BIT(KC_RSFT));
330 shift_interrupted[0] = true;
331 shift_interrupted[1] = true;
336 return process_action_kb(record);
339 const bool ascii_to_qwerty_shift_lut[0x80] PROGMEM = {
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, 0, 0, 0, 0, 0, 0, 0,
344 0, 1, 1, 1, 1, 1, 1, 0,
345 1, 1, 1, 1, 0, 0, 0, 0,
346 0, 0, 0, 0, 0, 0, 0, 0,
347 0, 0, 1, 0, 1, 0, 1, 1,
348 1, 1, 1, 1, 1, 1, 1, 1,
349 1, 1, 1, 1, 1, 1, 1, 1,
350 1, 1, 1, 1, 1, 1, 1, 1,
351 1, 1, 1, 0, 0, 0, 1, 1,
352 0, 0, 0, 0, 0, 0, 0, 0,
353 0, 0, 0, 0, 0, 0, 0, 0,
354 0, 0, 0, 0, 0, 0, 0, 0,
355 0, 0, 0, 1, 1, 1, 1, 0
358 const uint8_t ascii_to_qwerty_keycode_lut[0x80] PROGMEM = {
359 0, 0, 0, 0, 0, 0, 0, 0,
360 KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
361 0, 0, 0, 0, 0, 0, 0, 0,
362 0, 0, 0, KC_ESC, 0, 0, 0, 0,
363 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
364 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
365 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
366 KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
367 KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
368 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
369 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_6, KC_MINS,
371 KC_GRV, 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_GRV, KC_DEL
377 /* for users whose OSes are set to Colemak */
379 #include "keymap_colemak.h"
381 const bool ascii_to_colemak_shift_lut[0x80] PROGMEM = {
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, 0, 0, 0, 0, 0, 0, 0,
386 0, 1, 1, 1, 1, 1, 1, 0,
387 1, 1, 1, 1, 0, 0, 0, 0,
388 0, 0, 0, 0, 0, 0, 0, 0,
389 0, 0, 1, 0, 1, 0, 1, 1,
390 1, 1, 1, 1, 1, 1, 1, 1,
391 1, 1, 1, 1, 1, 1, 1, 1,
392 1, 1, 1, 1, 1, 1, 1, 1,
393 1, 1, 1, 0, 0, 0, 1, 1,
394 0, 0, 0, 0, 0, 0, 0, 0,
395 0, 0, 0, 0, 0, 0, 0, 0,
396 0, 0, 0, 0, 0, 0, 0, 0,
397 0, 0, 0, 1, 1, 1, 1, 0
400 const uint8_t ascii_to_colemak_keycode_lut[0x80] PROGMEM = {
401 0, 0, 0, 0, 0, 0, 0, 0,
402 KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
403 0, 0, 0, 0, 0, 0, 0, 0,
404 0, 0, 0, KC_ESC, 0, 0, 0, 0,
405 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
406 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
407 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
408 KC_8, KC_9, CM_SCLN, CM_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
409 KC_2, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
410 CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
411 CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
412 CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
413 KC_GRV, 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_GRV, KC_DEL
421 void send_string(const char *str) {
424 uint8_t ascii_code = pgm_read_byte(str);
425 if (!ascii_code) break;
426 keycode = pgm_read_byte(&ascii_to_qwerty_keycode_lut[ascii_code]);
427 if (pgm_read_byte(&ascii_to_qwerty_shift_lut[ascii_code])) {
428 register_code(KC_LSFT);
429 register_code(keycode);
430 unregister_code(keycode);
431 unregister_code(KC_LSFT);
434 register_code(keycode);
435 unregister_code(keycode);
441 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
442 if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
449 void tap_random_base64(void) {
450 #if defined(__AVR_ATmega32U4__)
451 uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
453 uint8_t key = rand() % 64;
457 register_code(KC_LSFT);
458 register_code(key + KC_A);
459 unregister_code(key + KC_A);
460 unregister_code(KC_LSFT);
463 register_code(key - 26 + KC_A);
464 unregister_code(key - 26 + KC_A);
468 unregister_code(KC_0);
471 register_code(key - 53 + KC_1);
472 unregister_code(key - 53 + KC_1);
475 register_code(KC_LSFT);
476 register_code(KC_EQL);
477 unregister_code(KC_EQL);
478 unregister_code(KC_LSFT);
481 register_code(KC_SLSH);
482 unregister_code(KC_SLSH);
487 void matrix_init_quantum() {
488 #ifdef BACKLIGHT_ENABLE
489 backlight_init_ports();
494 void matrix_scan_quantum() {
499 #ifdef TAP_DANCE_ENABLE
500 matrix_scan_tap_dance();
505 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
507 static const uint8_t backlight_pin = BACKLIGHT_PIN;
509 #if BACKLIGHT_PIN == B7
510 # define COM1x1 COM1C1
512 #elif BACKLIGHT_PIN == B6
513 # define COM1x1 COM1B1
515 #elif BACKLIGHT_PIN == B5
516 # define COM1x1 COM1A1
519 # error "Backlight pin not supported - use B5, B6, or B7"
522 __attribute__ ((weak))
523 void backlight_init_ports(void)
526 // Setup backlight pin as output and output low.
528 _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
530 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
532 // Use full 16-bit resolution.
535 // I could write a wall of text here to explain... but TL;DW
536 // Go read the ATmega32u4 datasheet.
537 // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
539 // Pin PB7 = OCR1C (Timer 1, Channel C)
540 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
541 // (i.e. start high, go low when counter matches.)
542 // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
543 // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
545 TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
546 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
549 #ifdef BACKLIGHT_BREATHING
550 breathing_defaults();
554 __attribute__ ((weak))
555 void backlight_set(uint8_t level)
557 // Prevent backlight blink on lowest level
559 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
562 // Turn off PWM control on backlight pin, revert to output low.
563 TCCR1A &= ~(_BV(COM1x1));
565 } else if ( level == BACKLIGHT_LEVELS ) {
566 // Turn on PWM control of backlight pin
567 TCCR1A |= _BV(COM1x1);
568 // Set the brightness
571 // Turn on PWM control of backlight pin
572 TCCR1A |= _BV(COM1x1);
573 // Set the brightness
574 OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
577 #ifdef BACKLIGHT_BREATHING
578 breathing_intensity_default();
583 #ifdef BACKLIGHT_BREATHING
585 #define BREATHING_NO_HALT 0
586 #define BREATHING_HALT_OFF 1
587 #define BREATHING_HALT_ON 2
589 static uint8_t breath_intensity;
590 static uint8_t breath_speed;
591 static uint16_t breathing_index;
592 static uint8_t breathing_halt;
594 void breathing_enable(void)
596 if (get_backlight_level() == 0)
602 // Set breathing_index to be at the midpoint (brightest point)
603 breathing_index = 0x20 << breath_speed;
606 breathing_halt = BREATHING_NO_HALT;
608 // Enable breathing interrupt
609 TIMSK1 |= _BV(OCIE1A);
612 void breathing_pulse(void)
614 if (get_backlight_level() == 0)
620 // Set breathing_index to be at the midpoint + 1 (brightest point)
621 breathing_index = 0x21 << breath_speed;
624 breathing_halt = BREATHING_HALT_ON;
626 // Enable breathing interrupt
627 TIMSK1 |= _BV(OCIE1A);
630 void breathing_disable(void)
632 // Disable breathing interrupt
633 TIMSK1 &= ~_BV(OCIE1A);
634 backlight_set(get_backlight_level());
637 void breathing_self_disable(void)
639 if (get_backlight_level() == 0)
641 breathing_halt = BREATHING_HALT_OFF;
645 breathing_halt = BREATHING_HALT_ON;
648 //backlight_set(get_backlight_level());
651 void breathing_toggle(void)
655 if (get_backlight_level() == 0)
661 // Set breathing_index to be at the midpoint + 1 (brightest point)
662 breathing_index = 0x21 << breath_speed;
665 breathing_halt = BREATHING_NO_HALT;
668 // Toggle breathing interrupt
669 TIMSK1 ^= _BV(OCIE1A);
671 // Restore backlight level
674 backlight_set(get_backlight_level());
678 bool is_breathing(void)
680 return (TIMSK1 && _BV(OCIE1A));
683 void breathing_intensity_default(void)
685 //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
686 breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
689 void breathing_intensity_set(uint8_t value)
691 breath_intensity = value;
694 void breathing_speed_default(void)
699 void breathing_speed_set(uint8_t value)
701 bool is_breathing_now = is_breathing();
702 uint8_t old_breath_speed = breath_speed;
704 if (is_breathing_now)
706 // Disable breathing interrupt
707 TIMSK1 &= ~_BV(OCIE1A);
710 breath_speed = value;
712 if (is_breathing_now)
714 // Adjust index to account for new speed
715 breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
717 // Enable breathing interrupt
718 TIMSK1 |= _BV(OCIE1A);
723 void breathing_speed_inc(uint8_t value)
725 if ((uint16_t)(breath_speed - value) > 10 )
727 breathing_speed_set(0);
731 breathing_speed_set(breath_speed - value);
735 void breathing_speed_dec(uint8_t value)
737 if ((uint16_t)(breath_speed + value) > 10 )
739 breathing_speed_set(10);
743 breathing_speed_set(breath_speed + value);
747 void breathing_defaults(void)
749 breathing_intensity_default();
750 breathing_speed_default();
751 breathing_halt = BREATHING_NO_HALT;
754 /* Breathing Sleep LED brighness(PWM On period) table
755 * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
757 * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
758 * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
760 static const uint8_t breathing_table[64] PROGMEM = {
761 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
762 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
763 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
764 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
767 ISR(TIMER1_COMPA_vect)
769 // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
772 uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
774 if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
776 // Disable breathing interrupt
777 TIMSK1 &= ~_BV(OCIE1A);
780 OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
790 __attribute__ ((weak))
791 void backlight_init_ports(void)
796 __attribute__ ((weak))
797 void backlight_set(uint8_t level)
806 __attribute__ ((weak))
807 void led_set_user(uint8_t usb_led) {
811 __attribute__ ((weak))
812 void led_set_kb(uint8_t usb_led) {
813 led_set_user(usb_led);
816 __attribute__ ((weak))
817 void led_init_ports(void)
822 __attribute__ ((weak))
823 void led_set(uint8_t usb_led)
828 // // Using PE6 Caps Lock LED
829 // if (usb_led & (1<<USB_LED_CAPS_LOCK))
846 //------------------------------------------------------------------------------
847 // Override these functions in your keymap file to play different tunes on
848 // different events such as startup and bootloader jump
850 __attribute__ ((weak))
851 void startup_user() {}
853 __attribute__ ((weak))
854 void shutdown_user() {}
856 //------------------------------------------------------------------------------