3 static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
5 case QK_MODS ... QK_MODS_MAX:
31 extern bool keyboard_nkro;
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};
83 bool process_record_quantum(keyrecord_t *record) {
85 /* This gets the keycode from the key pressed */
86 keypos_t key = record->event.key;
89 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
90 /* TODO: Use store_or_get_action() or a similar function. */
91 if (!disable_action_cache) {
94 if (record->event.pressed) {
95 layer = layer_switch_get_layer(key);
96 update_source_layers_cache(key, layer);
98 layer = read_source_layers_cache(key);
100 keycode = keymap_key_to_keycode(layer, key);
103 keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
105 // This is how you use actions here
106 // if (keycode == KC_LEAD) {
108 // action.code = ACTION_DEFAULT_LAYER_SET(0);
109 // process_action(record, action);
114 process_record_kb(keycode, record) &&
116 process_midi(keycode, record) &&
119 process_music(keycode, record) &&
121 #ifdef TAP_DANCE_ENABLE
122 process_tap_dance(keycode, record) &&
124 #ifndef DISABLE_LEADER
125 process_leader(keycode, record) &&
127 #ifndef DISABLE_CHORDING
128 process_chording(keycode, record) &&
130 #ifdef UNICODE_ENABLE
131 process_unicode(keycode, record) &&
134 process_ucis(keycode, record) &&
140 // Shift / paren setup
144 if (record->event.pressed) {
150 if (record->event.pressed) {
151 print("\nDEBUG: enabled.\n");
156 #ifdef RGBLIGHT_ENABLE
158 if (record->event.pressed) {
164 if (record->event.pressed) {
170 if (record->event.pressed) {
171 rgblight_increase_hue();
176 if (record->event.pressed) {
177 rgblight_decrease_hue();
182 if (record->event.pressed) {
183 rgblight_increase_sat();
188 if (record->event.pressed) {
189 rgblight_decrease_sat();
194 if (record->event.pressed) {
195 rgblight_increase_val();
200 if (record->event.pressed) {
201 rgblight_decrease_val();
206 case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_UNSWAP_ALT_GUI:
207 if (record->event.pressed) {
208 // MAGIC actions (BOOTMAGIC without the boot)
209 if (!eeconfig_is_enabled()) {
213 keymap_config.raw = eeconfig_read_keymap();
214 if (keycode == MAGIC_SWAP_CONTROL_CAPSLOCK) {
215 keymap_config.swap_control_capslock = 1;
216 } else if (keycode == MAGIC_CAPSLOCK_TO_CONTROL) {
217 keymap_config.capslock_to_control = 1;
218 } else if (keycode == MAGIC_SWAP_LALT_LGUI) {
219 keymap_config.swap_lalt_lgui = 1;
220 } else if (keycode == MAGIC_SWAP_RALT_RGUI) {
221 keymap_config.swap_ralt_rgui = 1;
222 } else if (keycode == MAGIC_NO_GUI) {
223 keymap_config.no_gui = 1;
224 } else if (keycode == MAGIC_SWAP_GRAVE_ESC) {
225 keymap_config.swap_grave_esc = 1;
226 } else if (keycode == MAGIC_SWAP_BACKSLASH_BACKSPACE) {
227 keymap_config.swap_backslash_backspace = 1;
228 } else if (keycode == MAGIC_HOST_NKRO) {
229 keymap_config.nkro = 1;
232 clear_keyboard(); // clear to prevent stuck keys
233 keyboard_nkro = keymap_config.nkro;
235 } else if (keycode == MAGIC_SWAP_ALT_GUI) {
236 keymap_config.swap_lalt_lgui = 1;
237 keymap_config.swap_ralt_rgui = 1;
240 else if (keycode == MAGIC_UNSWAP_CONTROL_CAPSLOCK) {
241 keymap_config.swap_control_capslock = 0;
242 } else if (keycode == MAGIC_UNCAPSLOCK_TO_CONTROL) {
243 keymap_config.capslock_to_control = 0;
244 } else if (keycode == MAGIC_UNSWAP_LALT_LGUI) {
245 keymap_config.swap_lalt_lgui = 0;
246 } else if (keycode == MAGIC_UNSWAP_RALT_RGUI) {
247 keymap_config.swap_ralt_rgui = 0;
248 } else if (keycode == MAGIC_UNNO_GUI) {
249 keymap_config.no_gui = 0;
250 } else if (keycode == MAGIC_UNSWAP_GRAVE_ESC) {
251 keymap_config.swap_grave_esc = 0;
252 } else if (keycode == MAGIC_UNSWAP_BACKSLASH_BACKSPACE) {
253 keymap_config.swap_backslash_backspace = 0;
254 } else if (keycode == MAGIC_UNHOST_NKRO) {
255 keymap_config.nkro = 0;
257 clear_keyboard(); // clear to prevent stuck keys
258 keyboard_nkro = keymap_config.nkro;
260 } else if (keycode == MAGIC_UNSWAP_ALT_GUI) {
261 keymap_config.swap_lalt_lgui = 0;
262 keymap_config.swap_ralt_rgui = 0;
264 eeconfig_update_keymap(keymap_config.raw);
269 if (record->event.pressed) {
270 shift_interrupted[0] = false;
271 register_mods(MOD_BIT(KC_LSFT));
274 #ifdef DISABLE_SPACE_CADET_ROLLOVER
275 if (get_mods() & MOD_BIT(KC_RSFT)) {
276 shift_interrupted[0] = true;
277 shift_interrupted[1] = true;
280 if (!shift_interrupted[0]) {
281 register_code(LSPO_KEY);
282 unregister_code(LSPO_KEY);
284 unregister_mods(MOD_BIT(KC_LSFT));
291 if (record->event.pressed) {
292 shift_interrupted[1] = false;
293 register_mods(MOD_BIT(KC_RSFT));
296 #ifdef DISABLE_SPACE_CADET_ROLLOVER
297 if (get_mods() & MOD_BIT(KC_LSFT)) {
298 shift_interrupted[0] = true;
299 shift_interrupted[1] = true;
302 if (!shift_interrupted[1]) {
303 register_code(RSPC_KEY);
304 unregister_code(RSPC_KEY);
306 unregister_mods(MOD_BIT(KC_RSFT));
312 shift_interrupted[0] = true;
313 shift_interrupted[1] = true;
318 return process_action_kb(record);
321 const bool ascii_to_qwerty_shift_lut[0x80] PROGMEM = {
322 0, 0, 0, 0, 0, 0, 0, 0,
323 0, 0, 0, 0, 0, 0, 0, 0,
324 0, 0, 0, 0, 0, 0, 0, 0,
325 0, 0, 0, 0, 0, 0, 0, 0,
326 0, 1, 1, 1, 1, 1, 1, 0,
327 1, 1, 1, 1, 0, 0, 0, 0,
328 0, 0, 0, 0, 0, 0, 0, 0,
329 0, 0, 1, 0, 1, 0, 1, 1,
330 1, 1, 1, 1, 1, 1, 1, 1,
331 1, 1, 1, 1, 1, 1, 1, 1,
332 1, 1, 1, 1, 1, 1, 1, 1,
333 1, 1, 1, 0, 0, 0, 1, 1,
334 0, 0, 0, 0, 0, 0, 0, 0,
335 0, 0, 0, 0, 0, 0, 0, 0,
336 0, 0, 0, 0, 0, 0, 0, 0,
337 0, 0, 0, 1, 1, 1, 1, 0
340 const uint8_t ascii_to_qwerty_keycode_lut[0x80] PROGMEM = {
341 0, 0, 0, 0, 0, 0, 0, 0,
342 KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
343 0, 0, 0, 0, 0, 0, 0, 0,
344 0, 0, 0, KC_ESC, 0, 0, 0, 0,
345 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
346 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
347 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
348 KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
349 KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
350 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
351 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
352 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
353 KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
354 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
355 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
356 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
359 /* for users whose OSes are set to Colemak */
361 #include "keymap_colemak.h"
363 const bool ascii_to_colemak_shift_lut[0x80] PROGMEM = {
364 0, 0, 0, 0, 0, 0, 0, 0,
365 0, 0, 0, 0, 0, 0, 0, 0,
366 0, 0, 0, 0, 0, 0, 0, 0,
367 0, 0, 0, 0, 0, 0, 0, 0,
368 0, 1, 1, 1, 1, 1, 1, 0,
369 1, 1, 1, 1, 0, 0, 0, 0,
370 0, 0, 0, 0, 0, 0, 0, 0,
371 0, 0, 1, 0, 1, 0, 1, 1,
372 1, 1, 1, 1, 1, 1, 1, 1,
373 1, 1, 1, 1, 1, 1, 1, 1,
374 1, 1, 1, 1, 1, 1, 1, 1,
375 1, 1, 1, 0, 0, 0, 1, 1,
376 0, 0, 0, 0, 0, 0, 0, 0,
377 0, 0, 0, 0, 0, 0, 0, 0,
378 0, 0, 0, 0, 0, 0, 0, 0,
379 0, 0, 0, 1, 1, 1, 1, 0
382 const uint8_t ascii_to_colemak_keycode_lut[0x80] PROGMEM = {
383 0, 0, 0, 0, 0, 0, 0, 0,
384 KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
385 0, 0, 0, 0, 0, 0, 0, 0,
386 0, 0, 0, KC_ESC, 0, 0, 0, 0,
387 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
388 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
389 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
390 KC_8, KC_9, CM_SCLN, CM_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
391 KC_2, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
392 CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
393 CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
394 CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
395 KC_GRV, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
396 CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
397 CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
398 CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
403 void send_string(const char *str) {
406 uint8_t ascii_code = pgm_read_byte(str);
407 if (!ascii_code) break;
408 keycode = pgm_read_byte(&ascii_to_qwerty_keycode_lut[ascii_code]);
409 if (pgm_read_byte(&ascii_to_qwerty_shift_lut[ascii_code])) {
410 register_code(KC_LSFT);
411 register_code(keycode);
412 unregister_code(keycode);
413 unregister_code(KC_LSFT);
416 register_code(keycode);
417 unregister_code(keycode);
423 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
424 if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
431 void tap_random_base64(void) {
432 #if defined(__AVR_ATmega32U4__)
433 uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
435 uint8_t key = rand() % 64;
439 register_code(KC_LSFT);
440 register_code(key + KC_A);
441 unregister_code(key + KC_A);
442 unregister_code(KC_LSFT);
445 register_code(key - 26 + KC_A);
446 unregister_code(key - 26 + KC_A);
450 unregister_code(KC_0);
453 register_code(key - 53 + KC_1);
454 unregister_code(key - 53 + KC_1);
457 register_code(KC_LSFT);
458 register_code(KC_EQL);
459 unregister_code(KC_EQL);
460 unregister_code(KC_LSFT);
463 register_code(KC_SLSH);
464 unregister_code(KC_SLSH);
469 void matrix_init_quantum() {
470 #ifdef BACKLIGHT_ENABLE
471 backlight_init_ports();
476 void matrix_scan_quantum() {
481 #ifdef TAP_DANCE_ENABLE
482 matrix_scan_tap_dance();
487 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
489 static const uint8_t backlight_pin = BACKLIGHT_PIN;
491 #if BACKLIGHT_PIN == B7
492 # define COM1x1 COM1C1
494 #elif BACKLIGHT_PIN == B6
495 # define COM1x1 COM1B1
497 #elif BACKLIGHT_PIN == B5
498 # define COM1x1 COM1A1
501 # error "Backlight pin not supported - use B5, B6, or B7"
504 __attribute__ ((weak))
505 void backlight_init_ports(void)
508 // Setup backlight pin as output and output low.
510 _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
512 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
514 // Use full 16-bit resolution.
517 // I could write a wall of text here to explain... but TL;DW
518 // Go read the ATmega32u4 datasheet.
519 // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
521 // Pin PB7 = OCR1C (Timer 1, Channel C)
522 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
523 // (i.e. start high, go low when counter matches.)
524 // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
525 // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
527 TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
528 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
531 #ifdef BACKLIGHT_BREATHING
532 breathing_defaults();
536 __attribute__ ((weak))
537 void backlight_set(uint8_t level)
539 // Prevent backlight blink on lowest level
541 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
544 // Turn off PWM control on backlight pin, revert to output low.
545 TCCR1A &= ~(_BV(COM1x1));
547 } else if ( level == BACKLIGHT_LEVELS ) {
548 // Turn on PWM control of backlight pin
549 TCCR1A |= _BV(COM1x1);
550 // Set the brightness
553 // Turn on PWM control of backlight pin
554 TCCR1A |= _BV(COM1x1);
555 // Set the brightness
556 OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
559 #ifdef BACKLIGHT_BREATHING
560 breathing_intensity_default();
565 #ifdef BACKLIGHT_BREATHING
567 #define BREATHING_NO_HALT 0
568 #define BREATHING_HALT_OFF 1
569 #define BREATHING_HALT_ON 2
571 static uint8_t breath_intensity;
572 static uint8_t breath_speed;
573 static uint16_t breathing_index;
574 static uint8_t breathing_halt;
576 void breathing_enable(void)
578 if (get_backlight_level() == 0)
584 // Set breathing_index to be at the midpoint (brightest point)
585 breathing_index = 0x20 << breath_speed;
588 breathing_halt = BREATHING_NO_HALT;
590 // Enable breathing interrupt
591 TIMSK1 |= _BV(OCIE1A);
594 void breathing_pulse(void)
596 if (get_backlight_level() == 0)
602 // Set breathing_index to be at the midpoint + 1 (brightest point)
603 breathing_index = 0x21 << breath_speed;
606 breathing_halt = BREATHING_HALT_ON;
608 // Enable breathing interrupt
609 TIMSK1 |= _BV(OCIE1A);
612 void breathing_disable(void)
614 // Disable breathing interrupt
615 TIMSK1 &= ~_BV(OCIE1A);
616 backlight_set(get_backlight_level());
619 void breathing_self_disable(void)
621 if (get_backlight_level() == 0)
623 breathing_halt = BREATHING_HALT_OFF;
627 breathing_halt = BREATHING_HALT_ON;
630 //backlight_set(get_backlight_level());
633 void breathing_toggle(void)
637 if (get_backlight_level() == 0)
643 // Set breathing_index to be at the midpoint + 1 (brightest point)
644 breathing_index = 0x21 << breath_speed;
647 breathing_halt = BREATHING_NO_HALT;
650 // Toggle breathing interrupt
651 TIMSK1 ^= _BV(OCIE1A);
653 // Restore backlight level
656 backlight_set(get_backlight_level());
660 bool is_breathing(void)
662 return (TIMSK1 && _BV(OCIE1A));
665 void breathing_intensity_default(void)
667 //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
668 breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
671 void breathing_intensity_set(uint8_t value)
673 breath_intensity = value;
676 void breathing_speed_default(void)
681 void breathing_speed_set(uint8_t value)
683 bool is_breathing_now = is_breathing();
684 uint8_t old_breath_speed = breath_speed;
686 if (is_breathing_now)
688 // Disable breathing interrupt
689 TIMSK1 &= ~_BV(OCIE1A);
692 breath_speed = value;
694 if (is_breathing_now)
696 // Adjust index to account for new speed
697 breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
699 // Enable breathing interrupt
700 TIMSK1 |= _BV(OCIE1A);
705 void breathing_speed_inc(uint8_t value)
707 if ((uint16_t)(breath_speed - value) > 10 )
709 breathing_speed_set(0);
713 breathing_speed_set(breath_speed - value);
717 void breathing_speed_dec(uint8_t value)
719 if ((uint16_t)(breath_speed + value) > 10 )
721 breathing_speed_set(10);
725 breathing_speed_set(breath_speed + value);
729 void breathing_defaults(void)
731 breathing_intensity_default();
732 breathing_speed_default();
733 breathing_halt = BREATHING_NO_HALT;
736 /* Breathing Sleep LED brighness(PWM On period) table
737 * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
739 * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
740 * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
742 static const uint8_t breathing_table[64] PROGMEM = {
743 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
744 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
745 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
746 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
749 ISR(TIMER1_COMPA_vect)
751 // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
754 uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
756 if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
758 // Disable breathing interrupt
759 TIMSK1 &= ~_BV(OCIE1A);
762 OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
772 __attribute__ ((weak))
773 void backlight_init_ports(void)
778 __attribute__ ((weak))
779 void backlight_set(uint8_t level)
788 __attribute__ ((weak))
789 void led_set_user(uint8_t usb_led) {
793 __attribute__ ((weak))
794 void led_set_kb(uint8_t usb_led) {
795 led_set_user(usb_led);
798 __attribute__ ((weak))
799 void led_init_ports(void)
804 __attribute__ ((weak))
805 void led_set(uint8_t usb_led)
810 // // Using PE6 Caps Lock LED
811 // if (usb_led & (1<<USB_LED_CAPS_LOCK))
828 //------------------------------------------------------------------------------
829 // Override these functions in your keymap file to play different tunes on
830 // different events such as startup and bootloader jump
832 __attribute__ ((weak))
833 void startup_user() {}
835 __attribute__ ((weak))
836 void shutdown_user() {}
838 //------------------------------------------------------------------------------