4 bool process_action_kb(keyrecord_t *record) {
9 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
10 return process_record_user(keycode, record);
13 __attribute__ ((weak))
14 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
18 // Shift / paren setup
27 static bool shift_interrupted[2] = {0, 0};
29 bool process_record_quantum(keyrecord_t *record) {
31 /* This gets the keycode from the key pressed */
32 keypos_t key = record->event.key;
35 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
38 if (record->event.pressed) {
39 layer = layer_switch_get_layer(key);
40 update_source_layers_cache(key, layer);
42 layer = read_source_layers_cache(key);
44 keycode = keymap_key_to_keycode(layer, key);
46 keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
49 // This is how you use actions here
50 // if (keycode == KC_LEAD) {
52 // action.code = ACTION_DEFAULT_LAYER_SET(0);
53 // process_action(record, action);
58 process_record_kb(keycode, record) &&
60 process_midi(keycode, record) &&
63 process_music(keycode, record) &&
65 #ifdef TAP_DANCE_ENABLE
66 process_tap_dance(keycode, record) &&
68 #ifndef DISABLE_LEADER
69 process_leader(keycode, record) &&
71 #ifndef DISABLE_CHORDING
72 process_chording(keycode, record) &&
75 process_unicode(keycode, record) &&
81 // Shift / paren setup
85 if (record->event.pressed) {
93 *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
100 if (record->event.pressed) {
101 print("\nDEBUG: enabled.\n");
106 case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_UNSWAP_ALT_GUI:
107 if (record->event.pressed) {
108 // MAGIC actions (BOOTMAGIC without the boot)
109 if (!eeconfig_is_enabled()) {
113 keymap_config.raw = eeconfig_read_keymap();
114 if (keycode == MAGIC_SWAP_CONTROL_CAPSLOCK) {
115 keymap_config.swap_control_capslock = 1;
116 } else if (keycode == MAGIC_CAPSLOCK_TO_CONTROL) {
117 keymap_config.capslock_to_control = 1;
118 } else if (keycode == MAGIC_SWAP_LALT_LGUI) {
119 keymap_config.swap_lalt_lgui = 1;
120 } else if (keycode == MAGIC_SWAP_RALT_RGUI) {
121 keymap_config.swap_ralt_rgui = 1;
122 } else if (keycode == MAGIC_NO_GUI) {
123 keymap_config.no_gui = 1;
124 } else if (keycode == MAGIC_SWAP_GRAVE_ESC) {
125 keymap_config.swap_grave_esc = 1;
126 } else if (keycode == MAGIC_SWAP_BACKSLASH_BACKSPACE) {
127 keymap_config.swap_backslash_backspace = 1;
128 } else if (keycode == MAGIC_HOST_NKRO) {
129 keymap_config.nkro = 1;
130 } else if (keycode == MAGIC_SWAP_ALT_GUI) {
131 keymap_config.swap_lalt_lgui = 1;
132 keymap_config.swap_ralt_rgui = 1;
135 else if (keycode == MAGIC_UNSWAP_CONTROL_CAPSLOCK) {
136 keymap_config.swap_control_capslock = 0;
137 } else if (keycode == MAGIC_UNCAPSLOCK_TO_CONTROL) {
138 keymap_config.capslock_to_control = 0;
139 } else if (keycode == MAGIC_UNSWAP_LALT_LGUI) {
140 keymap_config.swap_lalt_lgui = 0;
141 } else if (keycode == MAGIC_UNSWAP_RALT_RGUI) {
142 keymap_config.swap_ralt_rgui = 0;
143 } else if (keycode == MAGIC_UNNO_GUI) {
144 keymap_config.no_gui = 0;
145 } else if (keycode == MAGIC_UNSWAP_GRAVE_ESC) {
146 keymap_config.swap_grave_esc = 0;
147 } else if (keycode == MAGIC_UNSWAP_BACKSLASH_BACKSPACE) {
148 keymap_config.swap_backslash_backspace = 0;
149 } else if (keycode == MAGIC_UNHOST_NKRO) {
150 keymap_config.nkro = 0;
151 } else if (keycode == MAGIC_UNSWAP_ALT_GUI) {
152 keymap_config.swap_lalt_lgui = 0;
153 keymap_config.swap_ralt_rgui = 0;
155 eeconfig_update_keymap(keymap_config.raw);
160 if (record->event.pressed) {
161 shift_interrupted[0] = false;
162 register_mods(MOD_BIT(KC_LSFT));
165 if (!shift_interrupted[0]) {
166 register_code(LSPO_KEY);
167 unregister_code(LSPO_KEY);
169 unregister_mods(MOD_BIT(KC_LSFT));
176 if (record->event.pressed) {
177 shift_interrupted[1] = false;
178 register_mods(MOD_BIT(KC_RSFT));
181 if (!shift_interrupted[1]) {
182 register_code(RSPC_KEY);
183 unregister_code(RSPC_KEY);
185 unregister_mods(MOD_BIT(KC_RSFT));
191 shift_interrupted[0] = true;
192 shift_interrupted[1] = true;
197 return process_action_kb(record);
200 const bool ascii_to_qwerty_shift_lut[0x80] PROGMEM = {
201 0, 0, 0, 0, 0, 0, 0, 0,
202 0, 0, 0, 0, 0, 0, 0, 0,
203 0, 0, 0, 0, 0, 0, 0, 0,
204 0, 0, 0, 0, 0, 0, 0, 0,
205 0, 1, 1, 1, 1, 1, 1, 0,
206 1, 1, 1, 1, 0, 0, 0, 0,
207 0, 0, 0, 0, 0, 0, 0, 0,
208 0, 0, 1, 0, 1, 0, 1, 1,
209 1, 1, 1, 1, 1, 1, 1, 1,
210 1, 1, 1, 1, 1, 1, 1, 1,
211 1, 1, 1, 1, 1, 1, 1, 1,
212 1, 1, 1, 0, 0, 0, 1, 1,
213 0, 0, 0, 0, 0, 0, 0, 0,
214 0, 0, 0, 0, 0, 0, 0, 0,
215 0, 0, 0, 0, 0, 0, 0, 0,
216 0, 0, 0, 1, 1, 1, 1, 0
219 const uint8_t ascii_to_qwerty_keycode_lut[0x80] PROGMEM = {
220 0, 0, 0, 0, 0, 0, 0, 0,
221 KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
222 0, 0, 0, 0, 0, 0, 0, 0,
223 0, 0, 0, KC_ESC, 0, 0, 0, 0,
224 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
225 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
226 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
227 KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
228 KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
229 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
230 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
231 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
232 KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
233 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
234 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
235 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
238 /* for users whose OSes are set to Colemak */
240 #include "keymap_colemak.h"
242 const bool ascii_to_colemak_shift_lut[0x80] PROGMEM = {
243 0, 0, 0, 0, 0, 0, 0, 0,
244 0, 0, 0, 0, 0, 0, 0, 0,
245 0, 0, 0, 0, 0, 0, 0, 0,
246 0, 0, 0, 0, 0, 0, 0, 0,
247 0, 1, 1, 1, 1, 1, 1, 0,
248 1, 1, 1, 1, 0, 0, 0, 0,
249 0, 0, 0, 0, 0, 0, 0, 0,
250 0, 0, 1, 0, 1, 0, 1, 1,
251 1, 1, 1, 1, 1, 1, 1, 1,
252 1, 1, 1, 1, 1, 1, 1, 1,
253 1, 1, 1, 1, 1, 1, 1, 1,
254 1, 1, 1, 0, 0, 0, 1, 1,
255 0, 0, 0, 0, 0, 0, 0, 0,
256 0, 0, 0, 0, 0, 0, 0, 0,
257 0, 0, 0, 0, 0, 0, 0, 0,
258 0, 0, 0, 1, 1, 1, 1, 0
261 const uint8_t ascii_to_colemak_keycode_lut[0x80] PROGMEM = {
262 0, 0, 0, 0, 0, 0, 0, 0,
263 KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
264 0, 0, 0, 0, 0, 0, 0, 0,
265 0, 0, 0, KC_ESC, 0, 0, 0, 0,
266 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
267 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
268 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
269 KC_8, KC_9, CM_SCLN, CM_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
270 KC_2, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
271 CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
272 CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
273 CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
274 KC_GRV, CM_A, CM_B, CM_C, CM_D, CM_E, CM_F, CM_G,
275 CM_H, CM_I, CM_J, CM_K, CM_L, CM_M, CM_N, CM_O,
276 CM_P, CM_Q, CM_R, CM_S, CM_T, CM_U, CM_V, CM_W,
277 CM_X, CM_Y, CM_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
282 void send_string(const char *str) {
285 uint8_t ascii_code = pgm_read_byte(str);
286 if (!ascii_code) break;
287 keycode = pgm_read_byte(&ascii_to_qwerty_keycode_lut[ascii_code]);
288 if (pgm_read_byte(&ascii_to_qwerty_shift_lut[ascii_code])) {
289 register_code(KC_LSFT);
290 register_code(keycode);
291 unregister_code(keycode);
292 unregister_code(KC_LSFT);
295 register_code(keycode);
296 unregister_code(keycode);
302 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
303 if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
310 void matrix_init_quantum() {
311 #ifdef BACKLIGHT_ENABLE
312 backlight_init_ports();
317 void matrix_scan_quantum() {
322 #ifdef TAP_DANCE_ENABLE
323 matrix_scan_tap_dance();
328 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
330 static const uint8_t backlight_pin = BACKLIGHT_PIN;
332 #if BACKLIGHT_PIN == B7
333 # define COM1x1 COM1C1
335 #elif BACKLIGHT_PIN == B6
336 # define COM1x1 COM1B1
338 #elif BACKLIGHT_PIN == B5
339 # define COM1x1 COM1A1
342 # error "Backlight pin not supported - use B5, B6, or B7"
345 __attribute__ ((weak))
346 void backlight_init_ports(void)
349 // Setup backlight pin as output and output low.
351 _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
353 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
355 // Use full 16-bit resolution.
358 // I could write a wall of text here to explain... but TL;DW
359 // Go read the ATmega32u4 datasheet.
360 // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
362 // Pin PB7 = OCR1C (Timer 1, Channel C)
363 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
364 // (i.e. start high, go low when counter matches.)
365 // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
366 // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
368 TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
369 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
372 #ifdef BACKLIGHT_BREATHING
373 breathing_defaults();
377 __attribute__ ((weak))
378 void backlight_set(uint8_t level)
380 // Prevent backlight blink on lowest level
382 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
385 // Turn off PWM control on backlight pin, revert to output low.
386 TCCR1A &= ~(_BV(COM1x1));
388 } else if ( level == BACKLIGHT_LEVELS ) {
389 // Turn on PWM control of backlight pin
390 TCCR1A |= _BV(COM1x1);
391 // Set the brightness
394 // Turn on PWM control of backlight pin
395 TCCR1A |= _BV(COM1x1);
396 // Set the brightness
397 OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
400 #ifdef BACKLIGHT_BREATHING
401 breathing_intensity_default();
406 #ifdef BACKLIGHT_BREATHING
408 #define BREATHING_NO_HALT 0
409 #define BREATHING_HALT_OFF 1
410 #define BREATHING_HALT_ON 2
412 static uint8_t breath_intensity;
413 static uint8_t breath_speed;
414 static uint16_t breathing_index;
415 static uint8_t breathing_halt;
417 void breathing_enable(void)
419 if (get_backlight_level() == 0)
425 // Set breathing_index to be at the midpoint (brightest point)
426 breathing_index = 0x20 << breath_speed;
429 breathing_halt = BREATHING_NO_HALT;
431 // Enable breathing interrupt
432 TIMSK1 |= _BV(OCIE1A);
435 void breathing_pulse(void)
437 if (get_backlight_level() == 0)
443 // Set breathing_index to be at the midpoint + 1 (brightest point)
444 breathing_index = 0x21 << breath_speed;
447 breathing_halt = BREATHING_HALT_ON;
449 // Enable breathing interrupt
450 TIMSK1 |= _BV(OCIE1A);
453 void breathing_disable(void)
455 // Disable breathing interrupt
456 TIMSK1 &= ~_BV(OCIE1A);
457 backlight_set(get_backlight_level());
460 void breathing_self_disable(void)
462 if (get_backlight_level() == 0)
464 breathing_halt = BREATHING_HALT_OFF;
468 breathing_halt = BREATHING_HALT_ON;
471 //backlight_set(get_backlight_level());
474 void breathing_toggle(void)
478 if (get_backlight_level() == 0)
484 // Set breathing_index to be at the midpoint + 1 (brightest point)
485 breathing_index = 0x21 << breath_speed;
488 breathing_halt = BREATHING_NO_HALT;
491 // Toggle breathing interrupt
492 TIMSK1 ^= _BV(OCIE1A);
494 // Restore backlight level
497 backlight_set(get_backlight_level());
501 bool is_breathing(void)
503 return (TIMSK1 && _BV(OCIE1A));
506 void breathing_intensity_default(void)
508 //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
509 breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
512 void breathing_intensity_set(uint8_t value)
514 breath_intensity = value;
517 void breathing_speed_default(void)
522 void breathing_speed_set(uint8_t value)
524 bool is_breathing_now = is_breathing();
525 uint8_t old_breath_speed = breath_speed;
527 if (is_breathing_now)
529 // Disable breathing interrupt
530 TIMSK1 &= ~_BV(OCIE1A);
533 breath_speed = value;
535 if (is_breathing_now)
537 // Adjust index to account for new speed
538 breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
540 // Enable breathing interrupt
541 TIMSK1 |= _BV(OCIE1A);
546 void breathing_speed_inc(uint8_t value)
548 if ((uint16_t)(breath_speed - value) > 10 )
550 breathing_speed_set(0);
554 breathing_speed_set(breath_speed - value);
558 void breathing_speed_dec(uint8_t value)
560 if ((uint16_t)(breath_speed + value) > 10 )
562 breathing_speed_set(10);
566 breathing_speed_set(breath_speed + value);
570 void breathing_defaults(void)
572 breathing_intensity_default();
573 breathing_speed_default();
574 breathing_halt = BREATHING_NO_HALT;
577 /* Breathing Sleep LED brighness(PWM On period) table
578 * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
580 * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
581 * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
583 static const uint8_t breathing_table[64] PROGMEM = {
584 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
585 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
586 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
587 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
590 ISR(TIMER1_COMPA_vect)
592 // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
595 uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
597 if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
599 // Disable breathing interrupt
600 TIMSK1 &= ~_BV(OCIE1A);
603 OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
613 __attribute__ ((weak))
614 void backlight_init_ports(void)
619 __attribute__ ((weak))
620 void backlight_set(uint8_t level)
629 __attribute__ ((weak))
630 void led_set_user(uint8_t usb_led) {
634 __attribute__ ((weak))
635 void led_set_kb(uint8_t usb_led) {
636 led_set_user(usb_led);
639 __attribute__ ((weak))
640 void led_init_ports(void)
645 __attribute__ ((weak))
646 void led_set(uint8_t usb_led)
651 // // Using PE6 Caps Lock LED
652 // if (usb_led & (1<<USB_LED_CAPS_LOCK))
669 //------------------------------------------------------------------------------
670 // Override these functions in your keymap file to play different tunes on
671 // different events such as startup and bootloader jump
673 __attribute__ ((weak))
674 void startup_user() {}
676 __attribute__ ((weak))
677 void shutdown_user() {}
679 //------------------------------------------------------------------------------