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) &&
136 // Shift / paren setup
140 if (record->event.pressed) {
146 if (record->event.pressed) {
147 print("\nDEBUG: enabled.\n");
152 #ifdef RGBLIGHT_ENABLE
154 if (record->event.pressed) {
160 if (record->event.pressed) {
166 if (record->event.pressed) {
167 rgblight_increase_hue();
172 if (record->event.pressed) {
173 rgblight_decrease_hue();
178 if (record->event.pressed) {
179 rgblight_increase_sat();
184 if (record->event.pressed) {
185 rgblight_decrease_sat();
190 if (record->event.pressed) {
191 rgblight_increase_val();
196 if (record->event.pressed) {
197 rgblight_decrease_val();
202 case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_UNSWAP_ALT_GUI:
203 if (record->event.pressed) {
204 // MAGIC actions (BOOTMAGIC without the boot)
205 if (!eeconfig_is_enabled()) {
209 keymap_config.raw = eeconfig_read_keymap();
210 if (keycode == MAGIC_SWAP_CONTROL_CAPSLOCK) {
211 keymap_config.swap_control_capslock = 1;
212 } else if (keycode == MAGIC_CAPSLOCK_TO_CONTROL) {
213 keymap_config.capslock_to_control = 1;
214 } else if (keycode == MAGIC_SWAP_LALT_LGUI) {
215 keymap_config.swap_lalt_lgui = 1;
216 } else if (keycode == MAGIC_SWAP_RALT_RGUI) {
217 keymap_config.swap_ralt_rgui = 1;
218 } else if (keycode == MAGIC_NO_GUI) {
219 keymap_config.no_gui = 1;
220 } else if (keycode == MAGIC_SWAP_GRAVE_ESC) {
221 keymap_config.swap_grave_esc = 1;
222 } else if (keycode == MAGIC_SWAP_BACKSLASH_BACKSPACE) {
223 keymap_config.swap_backslash_backspace = 1;
224 } else if (keycode == MAGIC_HOST_NKRO) {
225 keymap_config.nkro = 1;
226 } else if (keycode == MAGIC_SWAP_ALT_GUI) {
227 keymap_config.swap_lalt_lgui = 1;
228 keymap_config.swap_ralt_rgui = 1;
231 else if (keycode == MAGIC_UNSWAP_CONTROL_CAPSLOCK) {
232 keymap_config.swap_control_capslock = 0;
233 } else if (keycode == MAGIC_UNCAPSLOCK_TO_CONTROL) {
234 keymap_config.capslock_to_control = 0;
235 } else if (keycode == MAGIC_UNSWAP_LALT_LGUI) {
236 keymap_config.swap_lalt_lgui = 0;
237 } else if (keycode == MAGIC_UNSWAP_RALT_RGUI) {
238 keymap_config.swap_ralt_rgui = 0;
239 } else if (keycode == MAGIC_UNNO_GUI) {
240 keymap_config.no_gui = 0;
241 } else if (keycode == MAGIC_UNSWAP_GRAVE_ESC) {
242 keymap_config.swap_grave_esc = 0;
243 } else if (keycode == MAGIC_UNSWAP_BACKSLASH_BACKSPACE) {
244 keymap_config.swap_backslash_backspace = 0;
245 } else if (keycode == MAGIC_UNHOST_NKRO) {
246 keymap_config.nkro = 0;
247 } else if (keycode == MAGIC_UNSWAP_ALT_GUI) {
248 keymap_config.swap_lalt_lgui = 0;
249 keymap_config.swap_ralt_rgui = 0;
251 eeconfig_update_keymap(keymap_config.raw);
256 if (record->event.pressed) {
257 shift_interrupted[0] = false;
258 register_mods(MOD_BIT(KC_LSFT));
261 #ifdef DISABLE_SPACE_CADET_ROLLOVER
262 if (get_mods() & MOD_BIT(KC_RSFT)) {
263 shift_interrupted[0] = true;
264 shift_interrupted[1] = true;
267 if (!shift_interrupted[0]) {
268 register_code(LSPO_KEY);
269 unregister_code(LSPO_KEY);
271 unregister_mods(MOD_BIT(KC_LSFT));
278 if (record->event.pressed) {
279 shift_interrupted[1] = false;
280 register_mods(MOD_BIT(KC_RSFT));
283 #ifdef DISABLE_SPACE_CADET_ROLLOVER
284 if (get_mods() & MOD_BIT(KC_LSFT)) {
285 shift_interrupted[0] = true;
286 shift_interrupted[1] = true;
289 if (!shift_interrupted[1]) {
290 register_code(RSPC_KEY);
291 unregister_code(RSPC_KEY);
293 unregister_mods(MOD_BIT(KC_RSFT));
299 shift_interrupted[0] = true;
300 shift_interrupted[1] = true;
305 return process_action_kb(record);
308 const bool ascii_to_qwerty_shift_lut[0x80] PROGMEM = {
309 0, 0, 0, 0, 0, 0, 0, 0,
310 0, 0, 0, 0, 0, 0, 0, 0,
311 0, 0, 0, 0, 0, 0, 0, 0,
312 0, 0, 0, 0, 0, 0, 0, 0,
313 0, 1, 1, 1, 1, 1, 1, 0,
314 1, 1, 1, 1, 0, 0, 0, 0,
315 0, 0, 0, 0, 0, 0, 0, 0,
316 0, 0, 1, 0, 1, 0, 1, 1,
317 1, 1, 1, 1, 1, 1, 1, 1,
318 1, 1, 1, 1, 1, 1, 1, 1,
319 1, 1, 1, 1, 1, 1, 1, 1,
320 1, 1, 1, 0, 0, 0, 1, 1,
321 0, 0, 0, 0, 0, 0, 0, 0,
322 0, 0, 0, 0, 0, 0, 0, 0,
323 0, 0, 0, 0, 0, 0, 0, 0,
324 0, 0, 0, 1, 1, 1, 1, 0
327 const uint8_t ascii_to_qwerty_keycode_lut[0x80] PROGMEM = {
328 0, 0, 0, 0, 0, 0, 0, 0,
329 KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
330 0, 0, 0, 0, 0, 0, 0, 0,
331 0, 0, 0, KC_ESC, 0, 0, 0, 0,
332 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
333 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
334 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
335 KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
336 KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
337 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
338 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
339 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
340 KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
341 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
342 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
343 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
346 /* for users whose OSes are set to Colemak */
348 #include "keymap_colemak.h"
350 const bool ascii_to_colemak_shift_lut[0x80] PROGMEM = {
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, 0, 0, 0, 0, 0,
355 0, 1, 1, 1, 1, 1, 1, 0,
356 1, 1, 1, 1, 0, 0, 0, 0,
357 0, 0, 0, 0, 0, 0, 0, 0,
358 0, 0, 1, 0, 1, 0, 1, 1,
359 1, 1, 1, 1, 1, 1, 1, 1,
360 1, 1, 1, 1, 1, 1, 1, 1,
361 1, 1, 1, 1, 1, 1, 1, 1,
362 1, 1, 1, 0, 0, 0, 1, 1,
363 0, 0, 0, 0, 0, 0, 0, 0,
364 0, 0, 0, 0, 0, 0, 0, 0,
365 0, 0, 0, 0, 0, 0, 0, 0,
366 0, 0, 0, 1, 1, 1, 1, 0
369 const uint8_t ascii_to_colemak_keycode_lut[0x80] PROGMEM = {
370 0, 0, 0, 0, 0, 0, 0, 0,
371 KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
372 0, 0, 0, 0, 0, 0, 0, 0,
373 0, 0, 0, KC_ESC, 0, 0, 0, 0,
374 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
375 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
376 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
377 KC_8, KC_9, CM_SCLN, CM_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
378 KC_2, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
379 CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
380 CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
381 CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
382 KC_GRV, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
383 CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
384 CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
385 CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
390 void send_string(const char *str) {
393 uint8_t ascii_code = pgm_read_byte(str);
394 if (!ascii_code) break;
395 keycode = pgm_read_byte(&ascii_to_qwerty_keycode_lut[ascii_code]);
396 if (pgm_read_byte(&ascii_to_qwerty_shift_lut[ascii_code])) {
397 register_code(KC_LSFT);
398 register_code(keycode);
399 unregister_code(keycode);
400 unregister_code(KC_LSFT);
403 register_code(keycode);
404 unregister_code(keycode);
410 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
411 if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
418 void tap_random_base64(void) {
419 #if defined(__AVR_ATmega32U4__)
420 uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
422 uint8_t key = rand() % 64;
426 register_code(KC_LSFT);
427 register_code(key + KC_A);
428 unregister_code(key + KC_A);
429 unregister_code(KC_LSFT);
432 register_code(key - 26 + KC_A);
433 unregister_code(key - 26 + KC_A);
437 unregister_code(KC_0);
440 register_code(key - 53 + KC_1);
441 unregister_code(key - 53 + KC_1);
444 register_code(KC_LSFT);
445 register_code(KC_EQL);
446 unregister_code(KC_EQL);
447 unregister_code(KC_LSFT);
450 register_code(KC_SLSH);
451 unregister_code(KC_SLSH);
456 void matrix_init_quantum() {
457 #ifdef BACKLIGHT_ENABLE
458 backlight_init_ports();
463 void matrix_scan_quantum() {
468 #ifdef TAP_DANCE_ENABLE
469 matrix_scan_tap_dance();
474 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
476 static const uint8_t backlight_pin = BACKLIGHT_PIN;
478 #if BACKLIGHT_PIN == B7
479 # define COM1x1 COM1C1
481 #elif BACKLIGHT_PIN == B6
482 # define COM1x1 COM1B1
484 #elif BACKLIGHT_PIN == B5
485 # define COM1x1 COM1A1
488 # error "Backlight pin not supported - use B5, B6, or B7"
491 __attribute__ ((weak))
492 void backlight_init_ports(void)
495 // Setup backlight pin as output and output low.
497 _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
499 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
501 // Use full 16-bit resolution.
504 // I could write a wall of text here to explain... but TL;DW
505 // Go read the ATmega32u4 datasheet.
506 // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
508 // Pin PB7 = OCR1C (Timer 1, Channel C)
509 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
510 // (i.e. start high, go low when counter matches.)
511 // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
512 // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
514 TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
515 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
518 #ifdef BACKLIGHT_BREATHING
519 breathing_defaults();
523 __attribute__ ((weak))
524 void backlight_set(uint8_t level)
526 // Prevent backlight blink on lowest level
528 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
531 // Turn off PWM control on backlight pin, revert to output low.
532 TCCR1A &= ~(_BV(COM1x1));
534 } else if ( level == BACKLIGHT_LEVELS ) {
535 // Turn on PWM control of backlight pin
536 TCCR1A |= _BV(COM1x1);
537 // Set the brightness
540 // Turn on PWM control of backlight pin
541 TCCR1A |= _BV(COM1x1);
542 // Set the brightness
543 OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
546 #ifdef BACKLIGHT_BREATHING
547 breathing_intensity_default();
552 #ifdef BACKLIGHT_BREATHING
554 #define BREATHING_NO_HALT 0
555 #define BREATHING_HALT_OFF 1
556 #define BREATHING_HALT_ON 2
558 static uint8_t breath_intensity;
559 static uint8_t breath_speed;
560 static uint16_t breathing_index;
561 static uint8_t breathing_halt;
563 void breathing_enable(void)
565 if (get_backlight_level() == 0)
571 // Set breathing_index to be at the midpoint (brightest point)
572 breathing_index = 0x20 << breath_speed;
575 breathing_halt = BREATHING_NO_HALT;
577 // Enable breathing interrupt
578 TIMSK1 |= _BV(OCIE1A);
581 void breathing_pulse(void)
583 if (get_backlight_level() == 0)
589 // Set breathing_index to be at the midpoint + 1 (brightest point)
590 breathing_index = 0x21 << breath_speed;
593 breathing_halt = BREATHING_HALT_ON;
595 // Enable breathing interrupt
596 TIMSK1 |= _BV(OCIE1A);
599 void breathing_disable(void)
601 // Disable breathing interrupt
602 TIMSK1 &= ~_BV(OCIE1A);
603 backlight_set(get_backlight_level());
606 void breathing_self_disable(void)
608 if (get_backlight_level() == 0)
610 breathing_halt = BREATHING_HALT_OFF;
614 breathing_halt = BREATHING_HALT_ON;
617 //backlight_set(get_backlight_level());
620 void breathing_toggle(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_NO_HALT;
637 // Toggle breathing interrupt
638 TIMSK1 ^= _BV(OCIE1A);
640 // Restore backlight level
643 backlight_set(get_backlight_level());
647 bool is_breathing(void)
649 return (TIMSK1 && _BV(OCIE1A));
652 void breathing_intensity_default(void)
654 //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
655 breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
658 void breathing_intensity_set(uint8_t value)
660 breath_intensity = value;
663 void breathing_speed_default(void)
668 void breathing_speed_set(uint8_t value)
670 bool is_breathing_now = is_breathing();
671 uint8_t old_breath_speed = breath_speed;
673 if (is_breathing_now)
675 // Disable breathing interrupt
676 TIMSK1 &= ~_BV(OCIE1A);
679 breath_speed = value;
681 if (is_breathing_now)
683 // Adjust index to account for new speed
684 breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
686 // Enable breathing interrupt
687 TIMSK1 |= _BV(OCIE1A);
692 void breathing_speed_inc(uint8_t value)
694 if ((uint16_t)(breath_speed - value) > 10 )
696 breathing_speed_set(0);
700 breathing_speed_set(breath_speed - value);
704 void breathing_speed_dec(uint8_t value)
706 if ((uint16_t)(breath_speed + value) > 10 )
708 breathing_speed_set(10);
712 breathing_speed_set(breath_speed + value);
716 void breathing_defaults(void)
718 breathing_intensity_default();
719 breathing_speed_default();
720 breathing_halt = BREATHING_NO_HALT;
723 /* Breathing Sleep LED brighness(PWM On period) table
724 * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
726 * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
727 * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
729 static const uint8_t breathing_table[64] PROGMEM = {
730 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
731 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
732 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
733 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
736 ISR(TIMER1_COMPA_vect)
738 // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
741 uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
743 if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
745 // Disable breathing interrupt
746 TIMSK1 &= ~_BV(OCIE1A);
749 OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
759 __attribute__ ((weak))
760 void backlight_init_ports(void)
765 __attribute__ ((weak))
766 void backlight_set(uint8_t level)
775 __attribute__ ((weak))
776 void led_set_user(uint8_t usb_led) {
780 __attribute__ ((weak))
781 void led_set_kb(uint8_t usb_led) {
782 led_set_user(usb_led);
785 __attribute__ ((weak))
786 void led_init_ports(void)
791 __attribute__ ((weak))
792 void led_set(uint8_t usb_led)
797 // // Using PE6 Caps Lock LED
798 // if (usb_led & (1<<USB_LED_CAPS_LOCK))
815 //------------------------------------------------------------------------------
816 // Override these functions in your keymap file to play different tunes on
817 // different events such as startup and bootloader jump
819 __attribute__ ((weak))
820 void startup_user() {}
822 __attribute__ ((weak))
823 void shutdown_user() {}
825 //------------------------------------------------------------------------------