diff --git a/common.mk b/common.mk index 0930406d..e5a64933 100644 --- a/common.mk +++ b/common.mk @@ -61,6 +61,9 @@ endif ifdef SOFTPWM_LED_ENABLE SRC += $(COMMON_DIR)/softpwm_led.c OPT_DEFS += -DSOFTPWM_LED_ENABLE +ifdef FADING_LED_ENABLE + OPT_DEFS += -DFADING_LED_ENABLE +endif ifdef BREATHING_LED_ENABLE OPT_DEFS += -DBREATHING_LED_ENABLE endif diff --git a/common/action.c b/common/action.c index 4ef5d791..7d045aa1 100644 --- a/common/action.c +++ b/common/action.c @@ -43,6 +43,8 @@ void action_exec(keyevent_t event) keyrecord_t record = { .event = event }; + action_keyevent(event); + #ifndef NO_ACTION_TAPPING action_tapping_process(record); #else @@ -557,3 +559,8 @@ void debug_action(action_t action) } dprintf("[%X:%02X]", action.kind.param>>8, action.kind.param&0xff); } + +__attribute__ ((weak)) +void action_keyevent(keyevent_t event) +{ +} diff --git a/common/action.h b/common/action.h index 077711c2..99b2f2c5 100644 --- a/common/action.h +++ b/common/action.h @@ -55,6 +55,9 @@ const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt); /* user defined special function */ void action_function(keyrecord_t *record, uint8_t id, uint8_t opt); +/* keyevent callback */ +void action_keyevent(keyevent_t event); + /* Utilities for actions. */ void process_action(keyrecord_t *record); void register_code(uint8_t code); diff --git a/common/softpwm_led.c b/common/softpwm_led.c index d21c0357..b8751fb5 100644 --- a/common/softpwm_led.c +++ b/common/softpwm_led.c @@ -98,11 +98,97 @@ void softpwm_led_set_all(uint8_t val) } } +void softpwm_led_increase(uint8_t index, uint8_t offset) +{ + if (softpwm_led_ocr_buff[index] > 0xFF - offset) { + softpwm_led_ocr_buff[index] = 0xFF; + } + else { + softpwm_led_ocr_buff[index] += offset; + } +} + +void softpwm_led_increase_all(uint8_t offset) +{ + for (uint8_t i = 0; i < LED_COUNT; i++) { + softpwm_led_increase(i, offset); + } +} + +void softpwm_led_decrease(uint8_t index, uint8_t offset) +{ + if (softpwm_led_ocr_buff[index] < offset) { + softpwm_led_ocr_buff[index] = 0; + } + else { + softpwm_led_ocr_buff[index] -= offset; + } +} + +void softpwm_led_decrease_all(uint8_t offset) +{ + for (uint8_t i = 0; i < LED_COUNT; i++) { + softpwm_led_decrease(i, offset); + } +} + inline uint8_t softpwm_led_get_state(void) { return softpwm_led_state; } +#ifdef FADING_LED_ENABLE + +static led_pack_t fading_led_state = 0; +static led_pack_t fading_led_direction = 0; +static uint8_t fading_led_duration = 0; + +void fading_led_enable(uint8_t index) +{ + LED_BIT_SET(fading_led_state, index); +} + +void fading_led_enable_all(void) +{ + for (uint8_t i = 0; i < LED_COUNT; i++) { + LED_BIT_SET(fading_led_state, i); + } +} + +void fading_led_disable(uint8_t index) +{ + LED_BIT_CLEAR(fading_led_state, index); +} + +void fading_led_disable_all(void) +{ + fading_led_state = 0; +} + +void fading_led_toggle(uint8_t index) +{ + LED_BIT_XOR(fading_led_state, index); +} + +void fading_led_toggle_all(void) +{ + for (uint8_t i = 0; i < LED_COUNT; i++) { + LED_BIT_XOR(fading_led_state, i); + } +} + +void fading_led_set_direction(uint8_t dir) +{ + fading_led_direction = dir; +} + +void fading_led_set_duration(uint8_t dur) +{ + fading_led_duration = dur; +} + +#endif + #ifdef BREATHING_LED_ENABLE /* Breathing LED brighness(PWM On period) table @@ -111,12 +197,12 @@ inline uint8_t softpwm_led_get_state(void) * Table[floor((exp(sin(x/256*2*pi+3/2*pi))-1/e)*(256/(e-1/e))), {x,0,255,1}] * (0..255).each {|x| print ((exp(sin(x/256.0*2*PI+3.0/2*PI))-1/E)*(256/(E-1/E))).to_i, ', ' } */ -static const uint8_t breathing_table[256] PROGMEM = { -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 9, 10, 11, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 29, 30, 32, 34, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 56, 58, 61, 63, 66, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, 102, 105, 108, 112, 116, 119, 123, 126, 130, 134, 138, 142, 145, 149, 153, 157, 161, 165, 169, 173, 176, 180, 184, 188, 192, 195, 199, 203, 206, 210, 213, 216, 219, 223, 226, 228, 231, 234, 236, 239, 241, 243, 245, 247, 248, 250, 251, 252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 254, 253, 252, 251, 250, 248, 247, 245, 243, 241, 239, 236, 234, 231, 228, 226, 223, 219, 216, 213, 210, 206, 203, 199, 195, 192, 188, 184, 180, 176, 173, 169, 165, 161, 157, 153, 149, 145, 142, 138, 134, 130, 126, 123, 119, 116, 112, 108, 105, 102, 98, 95, 92, 89, 86, 83, 80, 77, 74, 71, 68, 66, 63, 61, 58, 56, 53, 51, 49, 47, 45, 43, 41, 39, 37, 35, 34, 32, 30, 29, 27, 26, 25, 23, 22, 21, 19, 18, 17, 16, 15, 14, 13, 12, 11, 11, 10, 9, 8, 8, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 +static const uint8_t breathing_table[128] PROGMEM = { +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 9, 10, 11, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 29, 30, 32, 34, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 56, 58, 61, 63, 66, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, 102, 105, 108, 112, 116, 119, 123, 126, 130, 134, 138, 142, 145, 149, 153, 157, 161, 165, 169, 173, 176, 180, 184, 188, 192, 195, 199, 203, 206, 210, 213, 216, 219, 223, 226, 228, 231, 234, 236, 239, 241, 243, 245, 247, 248, 250, 251, 252, 253, 254, 255, 255, 255 }; -static led_state_t breathing_led_state = 0; -static uint8_t breathing_led_duration[LED_COUNT] = {0}; +static led_pack_t breathing_led_state = 0; +static uint8_t breathing_led_duration = 0; void breathing_led_enable(uint8_t index) { @@ -152,17 +238,9 @@ void breathing_led_toggle_all(void) } } -void breathing_led_set_duration(uint8_t index, uint8_t dur) +void breathing_led_set_duration(uint8_t dur) { - breathing_led_duration[index] = dur; - //dprintf("breathing led set duration: %u\n", breathing_led_duration); -} - -void breathing_led_set_duration_all(uint8_t dur) -{ - for (uint8_t i = 0; i < LED_COUNT; i++) { - breathing_led_duration[i] = dur; - } + breathing_led_duration = dur; } #endif @@ -189,19 +267,59 @@ ISR(TIMER1_COMPA_vect) } } +#ifdef FADING_LED_ENABLE + static uint8_t fading_led_counter = 0; + static uint8_t fading_led_step = 0; + if (fading_led_state) { + if (++fading_led_counter > SOFTPWM_LED_FREQ) { + fading_led_counter = 0; + if (++fading_led_step > fading_led_duration) { + fading_led_step = 0; + for (uint8_t i = 0; i < LED_COUNT; i++) { + if (fading_led_state & LED_BIT(i)) { + if (fading_led_direction) { + softpwm_led_decrease(i, 1); + } + else { + softpwm_led_increase(i, 1); + } + } + } + } + } + } +#endif + #ifdef BREATHING_LED_ENABLE - static uint8_t count = 0; - static uint8_t index[LED_COUNT] = {0}; - static uint8_t step[LED_COUNT] = {0}; + static uint8_t breathing_led_counter = 0; + static uint8_t breathing_led_step = 0; + static uint8_t breathing_led_index = 0; + static uint8_t breathing_led_direction = 0; if (breathing_led_state) { - if (++count > SOFTPWM_LED_FREQ) { - count = 0; - for (uint8_t i = 0; i < LED_COUNT; i++) { - if (breathing_led_state & LED_BIT(i)) { - if (++step[i] > breathing_led_duration[i]) { - step[i] = 0; - softpwm_led_ocr_buff[i] = pgm_read_byte(&breathing_table[index[i]]); - index[i]++; + if (++breathing_led_counter > SOFTPWM_LED_FREQ) { + breathing_led_counter = 0; + if (++breathing_led_step > breathing_led_duration) { + breathing_led_step = 0; + uint8_t value = pgm_read_byte(&breathing_table[breathing_led_index]); + for (uint8_t i = 0; i < LED_COUNT; i++) { + if (breathing_led_state & LED_BIT(i)) { + softpwm_led_ocr_buff[i] = value; + } + } + if (breathing_led_direction) { + if (breathing_led_index == 0) { + breathing_led_direction = 0; + } + else { + breathing_led_index--; + } + } + else { + if (breathing_led_index == 0x7F) { + breathing_led_direction = 1; + } + else { + breathing_led_index++; } } } diff --git a/common/softpwm_led.h b/common/softpwm_led.h index ad95b577..0386d2a8 100644 --- a/common/softpwm_led.h +++ b/common/softpwm_led.h @@ -4,7 +4,6 @@ #include "stdint.h" #include "led.h" -typedef led_pack_t led_state_t; #ifdef SOFTPWM_LED_ENABLE @@ -15,11 +14,40 @@ void softpwm_led_disable(void); void softpwm_led_toggle(void); void softpwm_led_set(uint8_t index, uint8_t val); void softpwm_led_set_all(uint8_t val); +void softpwm_led_increase(uint8_t index, uint8_t offset); +void softpwm_led_increase_all(uint8_t offset); +void softpwm_led_decrease(uint8_t index, uint8_t offset); +void softpwm_led_decrease_all(uint8_t offset); void softpwm_led_on(uint8_t index); void softpwm_led_off(uint8_t index); uint8_t softpwm_led_get_state(void); void softpwm_led_state_change(uint8_t state); +enum { + FADING_LED_FADE_IN = 0, + FADING_LED_FADE_OUT +}; +#ifdef FADING_LED_ENABLE +void fading_led_enable(uint8_t index); +void fading_led_enable_all(void); +void fading_led_disable(uint8_t index); +void fading_led_disable_all(void); +void fading_led_toggle(uint8_t index); +void fading_led_toggle_all(void); +void fading_led_set_direction(uint8_t dir); +void fading_led_set_duration(uint8_t dur); +#else +#define fading_led_enable() +#define fading_led_enable_all() +#define fading_led_disable() +#define fading_led_disable_all() +#define fading_led_toggle() +#define fading_led_toggle_all() +#define fading_led_set_direction() +#define fading_led_set_direction_all() +#define fading_led_set_duration() +#endif + #ifdef BREATHING_LED_ENABLE #define breathing_led_init() void breathing_led_enable(uint8_t index); @@ -28,8 +56,7 @@ void breathing_led_disable(uint8_t index); void breathing_led_disable_all(void); void breathing_led_toggle(uint8_t index); void breathing_led_toggle_all(void); -void breathing_led_set_duration(uint8_t index, uint8_t dur); -void breathing_led_set_duration_all(uint8_t dur); +void breathing_led_set_duration(uint8_t dur); #else #define breathing_led_init() #define breathing_led_enable() @@ -51,6 +78,10 @@ void breathing_led_set_duration_all(uint8_t dur); #define softpwm_led_toggle() #define softpwm_led_set() #define softpwm_led_set_all() +#define softpwm_led_increase() +#define softpwm_led_increase_all() +#define softpwm_led_decrease() +#define softpwm_led_decrease_all() #define softpwm_led_on() #define softpwm_led_off() #define softpwm_led_get_state() diff --git a/keyboard/gh60/Makefile b/keyboard/gh60/Makefile index 822490ae..5d00526d 100644 --- a/keyboard/gh60/Makefile +++ b/keyboard/gh60/Makefile @@ -140,6 +140,7 @@ BACKLIGHT_ENABLE = yes # Enable keyboard backlight functionality KEYMAP_IN_EEPROM_ENABLE = yes # Read keymap from eeprom #KEYMAP_SECTION_ENABLE = yes # Fixed address keymap for keymap editor SOFTPWM_LED_ENABLE = yes # Enable SoftPWM to drive backlight +FADING_LED_ENABLE = yes # Enable fading backlight BREATHING_LED_ENABLE = yes # Enable breathing backlight LEDMAP_ENABLE = yes # Enable LED mapping LEDMAP_IN_EEPROM_ENABLE = yes # Read LED mapping from eeprom diff --git a/keyboard/gh60/backlight.c b/keyboard/gh60/backlight.c index 009d6afa..62421277 100644 --- a/keyboard/gh60/backlight.c +++ b/keyboard/gh60/backlight.c @@ -24,9 +24,12 @@ along with this program. If not, see . #else #include "breathing_led.h" #endif +#include "action.h" #ifdef BACKLIGHT_ENABLE +static uint8_t backlight_mode; + void backlight_enable(void); void backlight_disable(void); inline void backlight_set_raw(uint8_t raw); @@ -101,6 +104,7 @@ void backlight_disable(void) void backlight_set(uint8_t level) { + backlight_mode = level; #ifdef BREATHING_LED_ENABLE switch (level) { case 1: @@ -108,6 +112,7 @@ void backlight_set(uint8_t level) case 3: backlight_enable(); #ifdef SOFTPWM_LED_ENABLE + fading_led_disable_all(); breathing_led_disable_all(); #else breathing_led_disable(); @@ -120,15 +125,31 @@ void backlight_set(uint8_t level) backlight_enable(); #ifdef SOFTPWM_LED_ENABLE breathing_led_enable_all(); - breathing_led_set_duration_all(6 - level); + fading_led_disable_all(); + breathing_led_set_duration(6 - level); #else breathing_led_enable(); breathing_led_set_duration(6 - level); #endif break; + case 7: + backlight_enable(); + fading_led_enable_all(); + breathing_led_disable_all(); + fading_led_set_direction(FADING_LED_FADE_IN); + fading_led_set_duration(3); + break; + case 8: + backlight_enable(); + fading_led_enable_all(); + breathing_led_disable_all(); + fading_led_set_direction(FADING_LED_FADE_OUT); + fading_led_set_duration(3); + break; case 0: default: #ifdef SOFTPWM_LED_ENABLE + fading_led_disable_all(); breathing_led_disable_all(); #else breathing_led_disable(); @@ -204,6 +225,20 @@ void softpwm_led_off(uint8_t index) #endif #endif +void action_keyevent(keyevent_t event) +{ + if (backlight_mode == 7) { + if (event.pressed) { + softpwm_led_decrease_all(32); + } + } + if (backlight_mode == 8) { + if (event.pressed) { + softpwm_led_increase_all(32); + } + } +} + #ifndef SOFTPWM_LED_ENABLE #if defined(GH60_REV_CHN) diff --git a/keyboard/gh60/config.h b/keyboard/gh60/config.h index 2881b8fa..1efaef55 100644 --- a/keyboard/gh60/config.h +++ b/keyboard/gh60/config.h @@ -43,7 +43,7 @@ along with this program. If not, see . /* number of backlight levels */ #ifdef BREATHING_LED_ENABLE -#define BACKLIGHT_LEVELS 6 +#define BACKLIGHT_LEVELS 8 #else #define BACKLIGHT_LEVELS 3 #endif diff --git a/keyboard/gh60/ledmap.c b/keyboard/gh60/ledmap.c index 50cf8630..2d3334f5 100644 --- a/keyboard/gh60/ledmap.c +++ b/keyboard/gh60/ledmap.c @@ -8,7 +8,7 @@ static const uint8_t ledmaps[LED_COUNT] PROGMEM = { #if defined(GH60_REV_CHN) [0] = LEDMAP_CAPS_LOCK | LEDMAP_BACKLIGHT, // CapsLock - PB2 - [1] = LEDMAP_LAYER(1) | LEDMAP_BACKLIGHT, // PWM - PB6 + [1] = LEDMAP_LAYER(1), // PWM - PB6 #else [0] = LEDMAP_CAPS_LOCK | LEDMAP_BACKLIGHT, // CapsLock - PB2 [1] = LEDMAP_BACKLIGHT, // Esc - GPIO1 - PF6