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 #ifndef DEFAULT_LAYER_SONGS
44 #define DEFAULT_LAYER_SONGS { }
46 float goodbye_song[][2] = GOODBYE_SONG;
47 float ag_norm_song[][2] = AG_NORM_SONG;
48 float ag_swap_song[][2] = AG_SWAP_SONG;
49 float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
52 static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
54 case QK_MODS ... QK_MODS_MAX:
69 if (code < QK_RMODS_MIN) return;
81 static inline void qk_register_weak_mods(uint8_t kc) {
82 add_weak_mods(MOD_BIT(kc));
83 send_keyboard_report();
86 static inline void qk_unregister_weak_mods(uint8_t kc) {
87 del_weak_mods(MOD_BIT(kc));
88 send_keyboard_report();
91 static inline void qk_register_mods(uint8_t kc) {
92 add_weak_mods(MOD_BIT(kc));
93 send_keyboard_report();
96 static inline void qk_unregister_mods(uint8_t kc) {
97 del_weak_mods(MOD_BIT(kc));
98 send_keyboard_report();
101 void register_code16 (uint16_t code) {
102 if (IS_MOD(code) || code == KC_NO) {
103 do_code16 (code, qk_register_mods);
105 do_code16 (code, qk_register_weak_mods);
107 register_code (code);
110 void unregister_code16 (uint16_t code) {
111 unregister_code (code);
112 if (IS_MOD(code) || code == KC_NO) {
113 do_code16 (code, qk_unregister_mods);
115 do_code16 (code, qk_unregister_weak_mods);
119 __attribute__ ((weak))
120 bool process_action_kb(keyrecord_t *record) {
124 __attribute__ ((weak))
125 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
126 return process_record_user(keycode, record);
129 __attribute__ ((weak))
130 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
134 void reset_keyboard(void) {
136 #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_ENABLE_BASIC))
137 music_all_notes_off();
138 uint16_t timer_start = timer_read();
139 PLAY_SONG(goodbye_song);
141 while(timer_elapsed(timer_start) < 250)
147 #ifdef CATERINA_BOOTLOADER
148 *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
153 // Shift / paren setup
156 #define LSPO_KEY KC_9
159 #define RSPC_KEY KC_0
162 static bool shift_interrupted[2] = {0, 0};
163 static uint16_t scs_timer[2] = {0, 0};
165 bool process_record_quantum(keyrecord_t *record) {
167 /* This gets the keycode from the key pressed */
168 keypos_t key = record->event.key;
171 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
172 /* TODO: Use store_or_get_action() or a similar function. */
173 if (!disable_action_cache) {
176 if (record->event.pressed) {
177 layer = layer_switch_get_layer(key);
178 update_source_layers_cache(key, layer);
180 layer = read_source_layers_cache(key);
182 keycode = keymap_key_to_keycode(layer, key);
185 keycode = keymap_key_to_keycode(layer_switch_get_layer(key), key);
187 // This is how you use actions here
188 // if (keycode == KC_LEAD) {
190 // action.code = ACTION_DEFAULT_LAYER_SET(0);
191 // process_action(record, action);
196 process_record_kb(keycode, record) &&
197 #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
198 process_midi(keycode, record) &&
201 process_audio(keycode, record) &&
204 process_steno(keycode, record) &&
206 #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
207 process_music(keycode, record) &&
209 #ifdef TAP_DANCE_ENABLE
210 process_tap_dance(keycode, record) &&
212 #ifndef DISABLE_LEADER
213 process_leader(keycode, record) &&
215 #ifndef DISABLE_CHORDING
216 process_chording(keycode, record) &&
219 process_combo(keycode, record) &&
221 #ifdef UNICODE_ENABLE
222 process_unicode(keycode, record) &&
225 process_ucis(keycode, record) &&
227 #ifdef PRINTING_ENABLE
228 process_printer(keycode, record) &&
230 #ifdef UNICODEMAP_ENABLE
231 process_unicode_map(keycode, record) &&
237 // Shift / paren setup
241 if (record->event.pressed) {
247 if (record->event.pressed) {
248 print("\nDEBUG: enabled.\n");
253 #ifdef FAUXCLICKY_ENABLE
255 if (record->event.pressed) {
261 if (record->event.pressed) {
267 if (record->event.pressed) {
273 #ifdef RGBLIGHT_ENABLE
275 if (record->event.pressed) {
281 if (record->event.pressed) {
287 if (record->event.pressed) {
288 rgblight_increase_hue();
293 if (record->event.pressed) {
294 rgblight_decrease_hue();
299 if (record->event.pressed) {
300 rgblight_increase_sat();
305 if (record->event.pressed) {
306 rgblight_decrease_sat();
311 if (record->event.pressed) {
312 rgblight_increase_val();
317 if (record->event.pressed) {
318 rgblight_decrease_val();
325 if (record->event.pressed) {
326 set_output(OUTPUT_AUTO);
331 if (record->event.pressed) {
332 set_output(OUTPUT_USB);
336 #ifdef BLUETOOTH_ENABLE
338 if (record->event.pressed) {
339 set_output(OUTPUT_BLUETOOTH);
345 case MAGIC_SWAP_CONTROL_CAPSLOCK ... MAGIC_TOGGLE_NKRO:
346 if (record->event.pressed) {
347 // MAGIC actions (BOOTMAGIC without the boot)
348 if (!eeconfig_is_enabled()) {
352 keymap_config.raw = eeconfig_read_keymap();
355 case MAGIC_SWAP_CONTROL_CAPSLOCK:
356 keymap_config.swap_control_capslock = true;
358 case MAGIC_CAPSLOCK_TO_CONTROL:
359 keymap_config.capslock_to_control = true;
361 case MAGIC_SWAP_LALT_LGUI:
362 keymap_config.swap_lalt_lgui = true;
364 case MAGIC_SWAP_RALT_RGUI:
365 keymap_config.swap_ralt_rgui = true;
368 keymap_config.no_gui = true;
370 case MAGIC_SWAP_GRAVE_ESC:
371 keymap_config.swap_grave_esc = true;
373 case MAGIC_SWAP_BACKSLASH_BACKSPACE:
374 keymap_config.swap_backslash_backspace = true;
376 case MAGIC_HOST_NKRO:
377 keymap_config.nkro = true;
379 case MAGIC_SWAP_ALT_GUI:
380 keymap_config.swap_lalt_lgui = true;
381 keymap_config.swap_ralt_rgui = true;
383 PLAY_SONG(ag_swap_song);
386 case MAGIC_UNSWAP_CONTROL_CAPSLOCK:
387 keymap_config.swap_control_capslock = false;
389 case MAGIC_UNCAPSLOCK_TO_CONTROL:
390 keymap_config.capslock_to_control = false;
392 case MAGIC_UNSWAP_LALT_LGUI:
393 keymap_config.swap_lalt_lgui = false;
395 case MAGIC_UNSWAP_RALT_RGUI:
396 keymap_config.swap_ralt_rgui = false;
399 keymap_config.no_gui = false;
401 case MAGIC_UNSWAP_GRAVE_ESC:
402 keymap_config.swap_grave_esc = false;
404 case MAGIC_UNSWAP_BACKSLASH_BACKSPACE:
405 keymap_config.swap_backslash_backspace = false;
407 case MAGIC_UNHOST_NKRO:
408 keymap_config.nkro = false;
410 case MAGIC_UNSWAP_ALT_GUI:
411 keymap_config.swap_lalt_lgui = false;
412 keymap_config.swap_ralt_rgui = false;
414 PLAY_SONG(ag_norm_song);
417 case MAGIC_TOGGLE_NKRO:
418 keymap_config.nkro = !keymap_config.nkro;
423 eeconfig_update_keymap(keymap_config.raw);
424 clear_keyboard(); // clear to prevent stuck keys
430 if (record->event.pressed) {
431 shift_interrupted[0] = false;
432 scs_timer[0] = timer_read ();
433 register_mods(MOD_BIT(KC_LSFT));
436 #ifdef DISABLE_SPACE_CADET_ROLLOVER
437 if (get_mods() & MOD_BIT(KC_RSFT)) {
438 shift_interrupted[0] = true;
439 shift_interrupted[1] = true;
442 if (!shift_interrupted[0] && timer_elapsed(scs_timer[0]) < TAPPING_TERM) {
443 register_code(LSPO_KEY);
444 unregister_code(LSPO_KEY);
446 unregister_mods(MOD_BIT(KC_LSFT));
453 if (record->event.pressed) {
454 shift_interrupted[1] = false;
455 scs_timer[1] = timer_read ();
456 register_mods(MOD_BIT(KC_RSFT));
459 #ifdef DISABLE_SPACE_CADET_ROLLOVER
460 if (get_mods() & MOD_BIT(KC_LSFT)) {
461 shift_interrupted[0] = true;
462 shift_interrupted[1] = true;
465 if (!shift_interrupted[1] && timer_elapsed(scs_timer[1]) < TAPPING_TERM) {
466 register_code(RSPC_KEY);
467 unregister_code(RSPC_KEY);
469 unregister_mods(MOD_BIT(KC_RSFT));
475 void (*method)(uint8_t) = (record->event.pressed) ? &add_key : &del_key;
476 uint8_t shifted = get_mods() & ((MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)
477 |MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)));
479 #ifdef GRAVE_ESC_CTRL_OVERRIDE
480 if (get_mods() & (MOD_BIT(KC_LCTL) | MOD_BIT(KC_RCTL)))
484 method(shifted ? KC_GRAVE : KC_ESCAPE);
485 send_keyboard_report();
488 shift_interrupted[0] = true;
489 shift_interrupted[1] = true;
494 return process_action_kb(record);
497 __attribute__ ((weak))
498 const bool ascii_to_shift_lut[0x80] PROGMEM = {
499 0, 0, 0, 0, 0, 0, 0, 0,
500 0, 0, 0, 0, 0, 0, 0, 0,
501 0, 0, 0, 0, 0, 0, 0, 0,
502 0, 0, 0, 0, 0, 0, 0, 0,
503 0, 1, 1, 1, 1, 1, 1, 0,
504 1, 1, 1, 1, 0, 0, 0, 0,
505 0, 0, 0, 0, 0, 0, 0, 0,
506 0, 0, 1, 0, 1, 0, 1, 1,
507 1, 1, 1, 1, 1, 1, 1, 1,
508 1, 1, 1, 1, 1, 1, 1, 1,
509 1, 1, 1, 1, 1, 1, 1, 1,
510 1, 1, 1, 0, 0, 0, 1, 1,
511 0, 0, 0, 0, 0, 0, 0, 0,
512 0, 0, 0, 0, 0, 0, 0, 0,
513 0, 0, 0, 0, 0, 0, 0, 0,
514 0, 0, 0, 1, 1, 1, 1, 0
517 __attribute__ ((weak))
518 const uint8_t ascii_to_keycode_lut[0x80] PROGMEM = {
519 0, 0, 0, 0, 0, 0, 0, 0,
520 KC_BSPC, KC_TAB, KC_ENT, 0, 0, 0, 0, 0,
521 0, 0, 0, 0, 0, 0, 0, 0,
522 0, 0, 0, KC_ESC, 0, 0, 0, 0,
523 KC_SPC, KC_1, KC_QUOT, KC_3, KC_4, KC_5, KC_7, KC_QUOT,
524 KC_9, KC_0, KC_8, KC_EQL, KC_COMM, KC_MINS, KC_DOT, KC_SLSH,
525 KC_0, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7,
526 KC_8, KC_9, KC_SCLN, KC_SCLN, KC_COMM, KC_EQL, KC_DOT, KC_SLSH,
527 KC_2, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
528 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
529 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
530 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_6, KC_MINS,
531 KC_GRV, KC_A, KC_B, KC_C, KC_D, KC_E, KC_F, KC_G,
532 KC_H, KC_I, KC_J, KC_K, KC_L, KC_M, KC_N, KC_O,
533 KC_P, KC_Q, KC_R, KC_S, KC_T, KC_U, KC_V, KC_W,
534 KC_X, KC_Y, KC_Z, KC_LBRC, KC_BSLS, KC_RBRC, KC_GRV, KC_DEL
537 void send_string(const char *str) {
538 send_string_with_delay(str, 0);
541 void send_string_with_delay(const char *str, uint8_t interval) {
544 uint8_t ascii_code = pgm_read_byte(str);
545 if (!ascii_code) break;
546 keycode = pgm_read_byte(&ascii_to_keycode_lut[ascii_code]);
547 if (pgm_read_byte(&ascii_to_shift_lut[ascii_code])) {
548 register_code(KC_LSFT);
549 register_code(keycode);
550 unregister_code(keycode);
551 unregister_code(KC_LSFT);
554 register_code(keycode);
555 unregister_code(keycode);
559 { uint8_t ms = interval; while (ms--) wait_ms(1); }
563 void set_single_persistent_default_layer(uint8_t default_layer) {
565 PLAY_SONG(default_layer_songs[default_layer]);
567 eeconfig_update_default_layer(1U<<default_layer);
568 default_layer_set(1U<<default_layer);
571 void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
572 if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
579 void tap_random_base64(void) {
580 #if defined(__AVR_ATmega32U4__)
581 uint8_t key = (TCNT0 + TCNT1 + TCNT3 + TCNT4) % 64;
583 uint8_t key = rand() % 64;
587 register_code(KC_LSFT);
588 register_code(key + KC_A);
589 unregister_code(key + KC_A);
590 unregister_code(KC_LSFT);
593 register_code(key - 26 + KC_A);
594 unregister_code(key - 26 + KC_A);
598 unregister_code(KC_0);
601 register_code(key - 53 + KC_1);
602 unregister_code(key - 53 + KC_1);
605 register_code(KC_LSFT);
606 register_code(KC_EQL);
607 unregister_code(KC_EQL);
608 unregister_code(KC_LSFT);
611 register_code(KC_SLSH);
612 unregister_code(KC_SLSH);
617 void matrix_init_quantum() {
618 #ifdef BACKLIGHT_ENABLE
619 backlight_init_ports();
627 void matrix_scan_quantum() {
632 #ifdef TAP_DANCE_ENABLE
633 matrix_scan_tap_dance();
640 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
647 #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)
649 static const uint8_t backlight_pin = BACKLIGHT_PIN;
651 #if BACKLIGHT_PIN == B7
652 # define COM1x1 COM1C1
654 #elif BACKLIGHT_PIN == B6
655 # define COM1x1 COM1B1
657 #elif BACKLIGHT_PIN == B5
658 # define COM1x1 COM1A1
661 # define NO_BACKLIGHT_CLOCK
664 #ifndef BACKLIGHT_ON_STATE
665 #define BACKLIGHT_ON_STATE 0
668 __attribute__ ((weak))
669 void backlight_init_ports(void)
672 // Setup backlight pin as output and output to on state.
674 _SFR_IO8((backlight_pin >> 4) + 1) |= _BV(backlight_pin & 0xF);
675 #if BACKLIGHT_ON_STATE == 0
677 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
680 _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
683 #ifndef NO_BACKLIGHT_CLOCK
684 // Use full 16-bit resolution.
687 // I could write a wall of text here to explain... but TL;DW
688 // Go read the ATmega32u4 datasheet.
689 // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
691 // Pin PB7 = OCR1C (Timer 1, Channel C)
692 // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
693 // (i.e. start high, go low when counter matches.)
694 // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
695 // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
697 TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
698 TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
702 #ifdef BACKLIGHT_BREATHING
703 breathing_defaults();
707 __attribute__ ((weak))
708 void backlight_set(uint8_t level)
710 // Prevent backlight blink on lowest level
711 // #if BACKLIGHT_ON_STATE == 0
713 // _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
716 // _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
720 #ifndef NO_BACKLIGHT_CLOCK
721 // Turn off PWM control on backlight pin, revert to output low.
722 TCCR1A &= ~(_BV(COM1x1));
725 // #if BACKLIGHT_ON_STATE == 0
727 // _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
730 // _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
734 #ifndef NO_BACKLIGHT_CLOCK
735 else if ( level == BACKLIGHT_LEVELS ) {
736 // Turn on PWM control of backlight pin
737 TCCR1A |= _BV(COM1x1);
738 // Set the brightness
742 // Turn on PWM control of backlight pin
743 TCCR1A |= _BV(COM1x1);
744 // Set the brightness
745 OCR1x = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
749 #ifdef BACKLIGHT_BREATHING
750 breathing_intensity_default();
754 uint8_t backlight_tick = 0;
756 void backlight_task(void) {
757 #ifdef NO_BACKLIGHT_CLOCK
758 if ((0xFFFF >> ((BACKLIGHT_LEVELS - backlight_config.level) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
759 #if BACKLIGHT_ON_STATE == 0
761 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
764 _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
767 #if BACKLIGHT_ON_STATE == 0
769 _SFR_IO8((backlight_pin >> 4) + 2) |= _BV(backlight_pin & 0xF);
772 _SFR_IO8((backlight_pin >> 4) + 2) &= ~_BV(backlight_pin & 0xF);
775 backlight_tick = (backlight_tick + 1) % 16;
779 #ifdef BACKLIGHT_BREATHING
781 #define BREATHING_NO_HALT 0
782 #define BREATHING_HALT_OFF 1
783 #define BREATHING_HALT_ON 2
785 static uint8_t breath_intensity;
786 static uint8_t breath_speed;
787 static uint16_t breathing_index;
788 static uint8_t breathing_halt;
790 void breathing_enable(void)
792 if (get_backlight_level() == 0)
798 // Set breathing_index to be at the midpoint (brightest point)
799 breathing_index = 0x20 << breath_speed;
802 breathing_halt = BREATHING_NO_HALT;
804 // Enable breathing interrupt
805 TIMSK1 |= _BV(OCIE1A);
808 void breathing_pulse(void)
810 if (get_backlight_level() == 0)
816 // Set breathing_index to be at the midpoint + 1 (brightest point)
817 breathing_index = 0x21 << breath_speed;
820 breathing_halt = BREATHING_HALT_ON;
822 // Enable breathing interrupt
823 TIMSK1 |= _BV(OCIE1A);
826 void breathing_disable(void)
828 // Disable breathing interrupt
829 TIMSK1 &= ~_BV(OCIE1A);
830 backlight_set(get_backlight_level());
833 void breathing_self_disable(void)
835 if (get_backlight_level() == 0)
837 breathing_halt = BREATHING_HALT_OFF;
841 breathing_halt = BREATHING_HALT_ON;
844 //backlight_set(get_backlight_level());
847 void breathing_toggle(void)
851 if (get_backlight_level() == 0)
857 // Set breathing_index to be at the midpoint + 1 (brightest point)
858 breathing_index = 0x21 << breath_speed;
861 breathing_halt = BREATHING_NO_HALT;
864 // Toggle breathing interrupt
865 TIMSK1 ^= _BV(OCIE1A);
867 // Restore backlight level
870 backlight_set(get_backlight_level());
874 bool is_breathing(void)
876 return (TIMSK1 && _BV(OCIE1A));
879 void breathing_intensity_default(void)
881 //breath_intensity = (uint8_t)((uint16_t)100 * (uint16_t)get_backlight_level() / (uint16_t)BACKLIGHT_LEVELS);
882 breath_intensity = ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2));
885 void breathing_intensity_set(uint8_t value)
887 breath_intensity = value;
890 void breathing_speed_default(void)
895 void breathing_speed_set(uint8_t value)
897 bool is_breathing_now = is_breathing();
898 uint8_t old_breath_speed = breath_speed;
900 if (is_breathing_now)
902 // Disable breathing interrupt
903 TIMSK1 &= ~_BV(OCIE1A);
906 breath_speed = value;
908 if (is_breathing_now)
910 // Adjust index to account for new speed
911 breathing_index = (( (uint8_t)( (breathing_index) >> old_breath_speed ) ) & 0x3F) << breath_speed;
913 // Enable breathing interrupt
914 TIMSK1 |= _BV(OCIE1A);
919 void breathing_speed_inc(uint8_t value)
921 if ((uint16_t)(breath_speed - value) > 10 )
923 breathing_speed_set(0);
927 breathing_speed_set(breath_speed - value);
931 void breathing_speed_dec(uint8_t value)
933 if ((uint16_t)(breath_speed + value) > 10 )
935 breathing_speed_set(10);
939 breathing_speed_set(breath_speed + value);
943 void breathing_defaults(void)
945 breathing_intensity_default();
946 breathing_speed_default();
947 breathing_halt = BREATHING_NO_HALT;
950 /* Breathing Sleep LED brighness(PWM On period) table
951 * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
953 * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
954 * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
956 static const uint8_t breathing_table[64] PROGMEM = {
957 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
958 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
959 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
960 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
963 ISR(TIMER1_COMPA_vect)
965 // OCR1x = (pgm_read_byte(&breathing_table[ ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F ] )) * breath_intensity;
968 uint8_t local_index = ( (uint8_t)( (breathing_index++) >> breath_speed ) ) & 0x3F;
970 if (((breathing_halt == BREATHING_HALT_ON) && (local_index == 0x20)) || ((breathing_halt == BREATHING_HALT_OFF) && (local_index == 0x3F)))
972 // Disable breathing interrupt
973 TIMSK1 &= ~_BV(OCIE1A);
976 OCR1x = (uint16_t)(((uint16_t)pgm_read_byte(&breathing_table[local_index]) * 257)) >> breath_intensity;
986 __attribute__ ((weak))
987 void backlight_init_ports(void)
992 __attribute__ ((weak))
993 void backlight_set(uint8_t level)
1001 // Functions for spitting out values
1004 void send_dword(uint32_t number) { // this might not actually work
1005 uint16_t word = (number >> 16);
1007 send_word(number & 0xFFFFUL);
1010 void send_word(uint16_t number) {
1011 uint8_t byte = number >> 8;
1013 send_byte(number & 0xFF);
1016 void send_byte(uint8_t number) {
1017 uint8_t nibble = number >> 4;
1018 send_nibble(nibble);
1019 send_nibble(number & 0xF);
1022 void send_nibble(uint8_t number) {
1025 register_code(KC_0);
1026 unregister_code(KC_0);
1029 register_code(KC_1 + (number - 1));
1030 unregister_code(KC_1 + (number - 1));
1033 register_code(KC_A + (number - 0xA));
1034 unregister_code(KC_A + (number - 0xA));
1040 __attribute__((weak))
1041 uint16_t hex_to_keycode(uint8_t hex)
1045 } else if (hex < 0xA) {
1046 return KC_1 + (hex - 0x1);
1048 return KC_A + (hex - 0xA);
1052 void api_send_unicode(uint32_t unicode) {
1055 dword_to_bytes(unicode, chunk);
1056 MT_SEND_DATA(DT_UNICODE, chunk, 5);
1060 __attribute__ ((weak))
1061 void led_set_user(uint8_t usb_led) {
1065 __attribute__ ((weak))
1066 void led_set_kb(uint8_t usb_led) {
1067 led_set_user(usb_led);
1070 __attribute__ ((weak))
1071 void led_init_ports(void)
1076 __attribute__ ((weak))
1077 void led_set(uint8_t usb_led)
1082 // // Using PE6 Caps Lock LED
1083 // if (usb_led & (1<<USB_LED_CAPS_LOCK))
1093 // PORTE &= ~(1<<6);
1096 led_set_kb(usb_led);
1100 //------------------------------------------------------------------------------
1101 // Override these functions in your keymap file to play different tunes on
1102 // different events such as startup and bootloader jump
1104 __attribute__ ((weak))
1105 void startup_user() {}
1107 __attribute__ ((weak))
1108 void shutdown_user() {}
1110 //------------------------------------------------------------------------------