1 /* Copyright 2016-2017 Jack Humbert
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "outputselect.h"
23 #define TAPPING_TERM 200
26 #include "backlight.h"
27 extern backlight_config_t backlight_config;
29 #ifdef FAUXCLICKY_ENABLE
30 #include "fauxclicky.h"
35 #define GOODBYE_SONG SONG(GOODBYE_SOUND)
38 #define AG_NORM_SONG SONG(AG_NORM_SOUND)
41 #define AG_SWAP_SONG SONG(AG_SWAP_SOUND)
43 float goodbye_song[][2] = GOODBYE_SONG;
44 float ag_norm_song[][2] = AG_NORM_SONG;
45 float ag_swap_song[][2] = AG_SWAP_SONG;
46 #ifdef DEFAULT_LAYER_SONGS
47 float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
51 static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
53 case QK_MODS ... QK_MODS_MAX:
68 if (code < QK_RMODS_MIN) return;
80 static inline void qk_register_weak_mods(uint8_t kc) {
81 add_weak_mods(MOD_BIT(kc));
82 send_keyboard_report();
85 static inline void qk_unregister_weak_mods(uint8_t kc) {
86 del_weak_mods(MOD_BIT(kc));
87 send_keyboard_report();
90 static inline void qk_register_mods(uint8_t kc) {
91 add_weak_mods(MOD_BIT(kc));
92 send_keyboard_report();
95 static inline void qk_unregister_mods(uint8_t kc) {
96 del_weak_mods(MOD_BIT(kc));
97 send_keyboard_report();
100 void register_code16 (uint16_t code) {
101 if (IS_MOD(code) || code == KC_NO) {
102 do_code16 (code, qk_register_mods);
104 do_code16 (code, qk_register_weak_mods);
106 register_code (code);
109 void unregister_code16 (uint16_t code) {
110 unregister_code (code);
111 if (IS_MOD(code) || code == KC_NO) {
112 do_code16 (code, qk_unregister_mods);
114 do_code16 (code, qk_unregister_weak_mods);
118 __attribute__ ((weak))
119 bool process_action_kb(keyrecord_t *record) {
123 __attribute__ ((weak))
124 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
125 return process_record_user(keycode, record);
128 __attribute__ ((weak))
129 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
133 void reset_keyboard(void) {
135 #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_ENABLE_BASIC))
136 music_all_notes_off();
137 uint16_t timer_start = timer_read();
138 PLAY_SONG(goodbye_song);
140 while(timer_elapsed(timer_start) < 250)
146 #ifdef CATERINA_BOOTLOADER
147 *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
152 // Shift / paren setup
155 #define LSPO_KEY KC_9
158 #define RSPC_KEY KC_0
161 static bool shift_interrupted[2] = {0, 0};
162 static uint16_t scs_timer[2] = {0, 0};
164 bool process_record_quantum(keyrecord_t *record) {
166 /* This gets the keycode from the key pressed */
167 keypos_t key = record->event.key;
170 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
171 /* TODO: Use store_or_get_action() or a similar function. */
172 if (!disable_action_cache) {
175 if (record->event.pressed) {
176 layer = layer_switch_get_layer(key);
177 update_source_layers_cache(key, layer);
179 layer = read_source_layers_cache(key);
181 keycode = keymap_key_to_keycode(layer, key);
184 keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
186 // This is how you use actions here
187 // if (keycode == KC_LEAD) {
189 // action.code = ACTION_DEFAULT_LAYER_SET(0);
190 // process_action(record, action);
195 #if defined(KEY_LOCK_ENABLE)
196 // Must run first to be able to mask key_up events.
197 process_key_lock(&keycode, record) &&
199 process_record_kb(keycode, record) &&
200 #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
201 process_midi(keycode, record) &&
204 process_audio(keycode, record) &&
207 process_steno(keycode, record) &&
209 #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
210 process_music(keycode, record) &&
212 #ifdef TAP_DANCE_ENABLE
213 process_tap_dance(keycode, record) &&
215 #ifndef DISABLE_LEADER
216 process_leader(keycode, record) &&
218 #ifndef DISABLE_CHORDING
219 process_chording(keycode, record) &&
222 process_combo(keycode, record) &&
224 #ifdef UNICODE_ENABLE
225 process_unicode(keycode, record) &&
228 process_ucis(keycode, record) &&
230 #ifdef PRINTING_ENABLE
231 process_printer(keycode, record) &&
233 #ifdef UNICODEMAP_ENABLE
234 process_unicode_map(keycode, record) &&
240 // Shift / paren setup
244 if (record->event.pressed) {
250 if (record->event.pressed) {
251 print("\nDEBUG: enabled.\n");
256 #ifdef FAUXCLICKY_ENABLE
258 if (record->event.pressed) {
264 if (record->event.pressed) {
270 if (record->event.pressed) {
276 #ifdef RGBLIGHT_ENABLE
278 if (record->event.pressed) {
284 if (record->event.pressed) {
290 if (record->event.pressed) {
291 rgblight_increase_hue();
296 if (record->event.pressed) {
297 rgblight_decrease_hue();
302 if (record->event.pressed) {
303 rgblight_increase_sat();
308 if (record->event.pressed) {
309 rgblight_decrease_sat();
314 if (record->event.pressed) {
315 rgblight_increase_val();
320 if (record->event.pressed) {
321 rgblight_decrease_val();
328 if (record->event.pressed) {
329 set_output(OUTPUT_AUTO);
334 if (record->event.pressed) {
335 set_output(OUTPUT_USB);
339 #ifdef BLUETOOTH_ENABLE
341 if (record->event.pressed) {
342 set_output(OUTPUT_BLUETOOTH);
348 case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
349 if (record->event.pressed) {
350 // MAGIC actions (BOOTMAGIC without the boot)
351 if (!eeconfig_is_enabled()) {
355 keymap_config.raw = eeconfig_read_keymap();
358 case MAGIC_SWAP_CONTROL_CAPSLOCK:
359 keymap_config.swap_control_capslock = true;
361 case MAGIC_CAPSLOCK_TO_CONTROL:
362 keymap_config.capslock_to_control = true;
364 case MAGIC_SWAP_LALT_LGUI:
365 keymap_config.swap_lalt_lgui = true;
367 case MAGIC_SWAP_RALT_RGUI:
368 keymap_config.swap_ralt_rgui = true;
371 keymap_config.no_gui = true;
373 case MAGIC_SWAP_GRAVE_ESC:
374 keymap_config.swap_grave_esc = true;
376 case MAGIC_SWAP_BACKSLASH_BACKSPACE:
377 keymap_config.swap_backslash_backspace = true;
379 case MAGIC_HOST_NKRO:
380 keymap_config.nkro = true;
382 case MAGIC_SWAP_ALT_GUI:
383 keymap_config.swap_lalt_lgui = true;
384 keymap_config.swap_ralt_rgui = true;
386 PLAY_SONG(ag_swap_song);
389 case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
390 keymap_config.swap_control_capslock = false;
392 case MAGIC_UNCAPSLOCK_TO_CONTROL:
393 keymap_config.capslock_to_control = false;
395 case MAGIC_UNSWAP_LALT_LGUI:
396 keymap_config.swap_lalt_lgui = false;
398 case MAGIC_UNSWAP_RALT_RGUI:
399 keymap_config.swap_ralt_rgui = false;
402 keymap_config.no_gui = false;
404 case MAGIC_UNSWAP_GRAVE_ESC:
405 keymap_config.swap_grave_esc = false;
407 case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
408 keymap_config.swap_backslash_backspace = false;
410 case MAGIC_UNHOST_NKRO:
411 keymap_config.nkro = false;
413 case MAGIC_UNSWAP_ALT_GUI:
414 keymap_config.swap_lalt_lgui = false;
415 keymap_config.swap_ralt_rgui = false;
417 PLAY_SONG(ag_norm_song);
420 case MAGIC_TOGGLE_NKRO:
421 keymap_config.nkro = !keymap_config.nkro;
426 eeconfig_update_keymap(keymap_config.raw);
427 clear_keyboard(); // clear to prevent stuck keys
433 if (record->event.pressed) {
434 shift_interrupted[0] = false;
435 scs_timer[0] = timer_read ();
436 register_mods(MOD_BIT(KC_LSFT));
439 #ifdef DISABLE_SPACE_CADET_ROLLOVER
440 if (get_mods() & MOD_BIT(KC_RSFT)) {
441 shift_interrupted[0] = true;
442 shift_interrupted[1] = true;
445 if (!shift_interrupted[0] && timer_elapsed(scs_timer[0]) < TAPPING_TERM) {
446 register_code(LSPO_KEY);
447 unregister_code(LSPO_KEY);
449 unregister_mods(MOD_BIT(KC_LSFT));
456 if (record->event.pressed) {
457 shift_interrupted[1] = false;
458 scs_timer[1] = timer_read ();
459 register_mods(MOD_BIT(KC_RSFT));
462 #ifdef DISABLE_SPACE_CADET_ROLLOVER
463 if (get_mods() & MOD_BIT(KC_LSFT)) {
464 shift_interrupted[0] = true;
465 shift_interrupted[1] = true;
468 if (!shift_interrupted[1] && timer_elapsed(scs_timer[1]) < TAPPING_TERM) {
469 register_code(RSPC_KEY);
470 unregister_code(RSPC_KEY);
472 unregister_mods(MOD_BIT(KC_RSFT));
478 void (*method)(uint8_t) = (record->event.pressed) ? &add_key : &del_key;
479 uint8_t shifted = get_mods() & ((MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)
480 |MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)));
482 #ifdef GRAVE_ESC_CTRL_OVERRIDE
483 if (get_mods() & (MOD_BIT(KC_LCTL) | MOD_BIT(KC_RCTL)))
487 method(shifted ? KC_GRAVE : KC_ESCAPE);
488 send_keyboard_report();
491 shift_interrupted[0] = true;
492 shift_interrupted[1] = true;
497 return process_action_kb(record);
500 __attribute__ ((weak))
501 const bool ascii_to_shift_lut[0x80] PROGMEM = {
502 0, 0, 0, 0, 0, 0, 0, 0,
503 0, 0, 0, 0, 0, 0, 0, 0,
504 0, 0, 0, 0, 0, 0, 0, 0,
505 0, 0, 0, 0, 0, 0, 0, 0,
506 0, 1, 1, 1, 1, 1, 1, 0,
507 1, 1, 1, 1, 0, 0, 0, 0,
508 0, 0, 0, 0, 0, 0, 0, 0,
509 0, 0, 1, 0, 1, 0, 1, 1,
510 1, 1, 1, 1, 1, 1, 1, 1,
511 1, 1, 1, 1, 1, 1, 1, 1,
512 1, 1, 1, 1, 1, 1, 1, 1,
513 1, 1, 1, 0, 0, 0, 1, 1,
514 0, 0, 0, 0, 0, 0, 0, 0,
515 0, 0, 0, 0, 0, 0, 0, 0,
516 0, 0, 0, 0, 0, 0, 0, 0,
517 0, 0, 0, 1, 1, 1, 1, 0
520 __attribute__ ((weak))
521 const uint8_t ascii_to_keycode_lut[0x80] PROGMEM = {
522 0, 0, 0, 0, 0, 0, 0, 0,
523 KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
524 0, 0, 0, 0, 0, 0, 0, 0,
525 0, 0, 0, KC_ESC, 0, 0, 0, 0,
526 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
527 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
528 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
529 KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
530 KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
531 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
532 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
533 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
534 KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
535 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
536 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
537 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
540 void send_string(const char *str) {
541 send_string_with_delay(str, 0);
544 void send_string_with_delay(const char *str, uint8_t interval) {
547 uint8_t ascii_code = pgm_read_byte(str);
548 if (!ascii_code) break;
549 keycode = pgm_read_byte(&ascii_to_keycode_lut[ascii_code]);
550 if (pgm_read_byte(&ascii_to_shift_lut[ascii_code])) {
551 register_code(KC_LSFT);
552 register_code(keycode);
553 unregister_code(keycode);
554 unregister_code(KC_LSFT);
557 register_code(keycode);
558 unregister_code(keycode);
562 { uint8_t ms = interval; while (ms--) wait_ms(1); }
566 void set_single_persistent_default_layer(uint8_t default_layer) {
567 #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
568 PLAY_SONG(default_layer_songs[default_layer]);
570 eeconfig_update_default_layer(1U<<default_layer);
571 default_layer_set(1U<<default_layer);
574 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
575 if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
582 void tap_random_base64(void) {
583 #if defined(__AVR_ATmega32U4__)
584 uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
586 uint8_t key = rand() % 64;
590 register_code(KC_LSFT);
591 register_code(key + KC_A);
592 unregister_code(key + KC_A);
593 unregister_code(KC_LSFT);
596 register_code(key - 26 + KC_A);
597 unregister_code(key - 26 + KC_A);
601 unregister_code(KC_0);
604 register_code(key - 53 + KC_1);
605 unregister_code(key - 53 + KC_1);
608 register_code(KC_LSFT);
609 register_code(KC_EQL);
610 unregister_code(KC_EQL);
611 unregister_code(KC_LSFT);
614 register_code(KC_SLSH);
615 unregister_code(KC_SLSH);
620 void matrix_init_quantum() {
621 #ifdef BACKLIGHT_ENABLE
622 backlight_init_ports();
630 void matrix_scan_quantum() {
635 #ifdef TAP_DANCE_ENABLE
636 matrix_scan_tap_dance();
643 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
650 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
652 static const uint8_t backlight_pin = BACKLIGHT_PIN;
654 #if BACKLIGHT_PIN == B7
655 # define COM1x1 COM1C1
657 #elif BACKLIGHT_PIN == B6
658 # define COM1x1 COM1B1
660 #elif BACKLIGHT_PIN == B5
661 # define COM1x1 COM1A1
664 # define NO_BACKLIGHT_CLOCK
667 #ifndef BACKLIGHT_ON_STATE
668 #define BACKLIGHT_ON_STATE 0
671 __attribute__ ((weak))
672 void backlight_init_ports(void)
675 // Setup backlight pin as output and output to on state.
677 _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
678 #if BACKLIGHT_ON_STATE == 0
680 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
683 _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
686 #ifndef NO_BACKLIGHT_CLOCK
687 // Use full 16-bit resolution.
690 // I could write a wall of text here to explain... but TL;DW
691 // Go read the ATmega32u4 datasheet.
692 // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
694 // Pin PB7 = OCR1C (Timer 1, Channel C)
695 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
696 // (i.e. start high, go low when counter matches.)
697 // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
698 // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
700 TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
701 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
705 #ifdef BACKLIGHT_BREATHING
706 breathing_defaults();
710 __attribute__ ((weak))
711 void backlight_set(uint8_t level)
713 // Prevent backlight blink on lowest level
714 // #if BACKLIGHT_ON_STATE == 0
716 // _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
719 // _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
723 #ifndef NO_BACKLIGHT_CLOCK
724 // Turn off PWM control on backlight pin, revert to output low.
725 TCCR1A &= ~(_BV(COM1x1));
728 // #if BACKLIGHT_ON_STATE == 0
730 // _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
733 // _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
737 #ifndef NO_BACKLIGHT_CLOCK
738 else if ( level == BACKLIGHT_LEVELS ) {
739 // Turn on PWM control of backlight pin
740 TCCR1A |= _BV(COM1x1);
741 // Set the brightness
745 // Turn on PWM control of backlight pin
746 TCCR1A |= _BV(COM1x1);
747 // Set the brightness
748 OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
752 #ifdef BACKLIGHT_BREATHING
753 breathing_intensity_default();
757 uint8_t backlight_tick = 0;
759 void backlight_task(void) {
760 #ifdef NO_BACKLIGHT_CLOCK
761 if ((0xFFFF >> ((BACKLIGHT_LEVELS - backlight_config.level) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
762 #if BACKLIGHT_ON_STATE == 0
764 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
767 _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
770 #if BACKLIGHT_ON_STATE == 0
772 _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
775 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
778 backlight_tick = (backlight_tick + 1) % 16;
782 #ifdef BACKLIGHT_BREATHING
784 #define BREATHING_NO_HALT 0
785 #define BREATHING_HALT_OFF 1
786 #define BREATHING_HALT_ON 2
788 static uint8_t breath_intensity;
789 static uint8_t breath_speed;
790 static uint16_t breathing_index;
791 static uint8_t breathing_halt;
793 void breathing_enable(void)
795 if (get_backlight_level() == 0)
801 // Set breathing_index to be at the midpoint (brightest point)
802 breathing_index = 0x20 << breath_speed;
805 breathing_halt = BREATHING_NO_HALT;
807 // Enable breathing interrupt
808 TIMSK1 |= _BV(OCIE1A);
811 void breathing_pulse(void)
813 if (get_backlight_level() == 0)
819 // Set breathing_index to be at the midpoint + 1 (brightest point)
820 breathing_index = 0x21 << breath_speed;
823 breathing_halt = BREATHING_HALT_ON;
825 // Enable breathing interrupt
826 TIMSK1 |= _BV(OCIE1A);
829 void breathing_disable(void)
831 // Disable breathing interrupt
832 TIMSK1 &= ~_BV(OCIE1A);
833 backlight_set(get_backlight_level());
836 void breathing_self_disable(void)
838 if (get_backlight_level() == 0)
840 breathing_halt = BREATHING_HALT_OFF;
844 breathing_halt = BREATHING_HALT_ON;
847 //backlight_set(get_backlight_level());
850 void breathing_toggle(void)
854 if (get_backlight_level() == 0)
860 // Set breathing_index to be at the midpoint + 1 (brightest point)
861 breathing_index = 0x21 << breath_speed;
864 breathing_halt = BREATHING_NO_HALT;
867 // Toggle breathing interrupt
868 TIMSK1 ^= _BV(OCIE1A);
870 // Restore backlight level
873 backlight_set(get_backlight_level());
877 bool is_breathing(void)
879 return (TIMSK1 && _BV(OCIE1A));
882 void breathing_intensity_default(void)
884 //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
885 breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
888 void breathing_intensity_set(uint8_t value)
890 breath_intensity = value;
893 void breathing_speed_default(void)
898 void breathing_speed_set(uint8_t value)
900 bool is_breathing_now = is_breathing();
901 uint8_t old_breath_speed = breath_speed;
903 if (is_breathing_now)
905 // Disable breathing interrupt
906 TIMSK1 &= ~_BV(OCIE1A);
909 breath_speed = value;
911 if (is_breathing_now)
913 // Adjust index to account for new speed
914 breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
916 // Enable breathing interrupt
917 TIMSK1 |= _BV(OCIE1A);
922 void breathing_speed_inc(uint8_t value)
924 if ((uint16_t)(breath_speed - value) > 10 )
926 breathing_speed_set(0);
930 breathing_speed_set(breath_speed - value);
934 void breathing_speed_dec(uint8_t value)
936 if ((uint16_t)(breath_speed + value) > 10 )
938 breathing_speed_set(10);
942 breathing_speed_set(breath_speed + value);
946 void breathing_defaults(void)
948 breathing_intensity_default();
949 breathing_speed_default();
950 breathing_halt = BREATHING_NO_HALT;
953 /* Breathing Sleep LED brighness(PWM On period) table
954 * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
956 * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
957 * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
959 static const uint8_t breathing_table[64] PROGMEM = {
960 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
961 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
962 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
963 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
966 ISR(TIMER1_COMPA_vect)
968 // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
971 uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
973 if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
975 // Disable breathing interrupt
976 TIMSK1 &= ~_BV(OCIE1A);
979 OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
989 __attribute__ ((weak))
990 void backlight_init_ports(void)
995 __attribute__ ((weak))
996 void backlight_set(uint8_t level)
1004 // Functions for spitting out values
1007 void send_dword(uint32_t number) { // this might not actually work
1008 uint16_t word = (number >> 16);
1010 send_word(number & 0xFFFFUL);
1013 void send_word(uint16_t number) {
1014 uint8_t byte = number >> 8;
1016 send_byte(number & 0xFF);
1019 void send_byte(uint8_t number) {
1020 uint8_t nibble = number >> 4;
1021 send_nibble(nibble);
1022 send_nibble(number & 0xF);
1025 void send_nibble(uint8_t number) {
1028 register_code(KC_0);
1029 unregister_code(KC_0);
1032 register_code(KC_1 + (number - 1));
1033 unregister_code(KC_1 + (number - 1));
1036 register_code(KC_A + (number - 0xA));
1037 unregister_code(KC_A + (number - 0xA));
1043 __attribute__((weak))
1044 uint16_t hex_to_keycode(uint8_t hex)
1048 } else if (hex < 0xA) {
1049 return KC_1 + (hex - 0x1);
1051 return KC_A + (hex - 0xA);
1055 void api_send_unicode(uint32_t unicode) {
1058 dword_to_bytes(unicode, chunk);
1059 MT_SEND_DATA(DT_UNICODE, chunk, 5);
1063 __attribute__ ((weak))
1064 void led_set_user(uint8_t usb_led) {
1068 __attribute__ ((weak))
1069 void led_set_kb(uint8_t usb_led) {
1070 led_set_user(usb_led);
1073 __attribute__ ((weak))
1074 void led_init_ports(void)
1079 __attribute__ ((weak))
1080 void led_set(uint8_t usb_led)
1085 // // Using PE6 Caps Lock LED
1086 // if (usb_led & (1<<USB_LED_CAPS_LOCK))
1096 // PORTE &= ~(1<<6);
1099 led_set_kb(usb_led);
1103 //------------------------------------------------------------------------------
1104 // Override these functions in your keymap file to play different tunes on
1105 // different events such as startup and bootloader jump
1107 __attribute__ ((weak))
1108 void startup_user() {}
1110 __attribute__ ((weak))
1111 void shutdown_user() {}
1113 //------------------------------------------------------------------------------