From 844b64ff19cfeaa76833eade64cb8a1afdc1d231 Mon Sep 17 00:00:00 2001 From: Kai Ryu Date: Mon, 25 Aug 2014 14:03:23 +0900 Subject: [PATCH] Change implement from breathing led to fading led --- common/action.c | 7 +- common/action.h | 4 +- common/softpwm_led.c | 238 +++++++++++++++++++++++++------------------ common/softpwm_led.h | 52 ++++++---- 4 files changed, 181 insertions(+), 120 deletions(-) diff --git a/common/action.c b/common/action.c index de79ee0b..7d045aa1 100644 --- a/common/action.c +++ b/common/action.c @@ -43,7 +43,7 @@ void action_exec(keyevent_t event) keyrecord_t record = { .event = event }; - key_event(event); + action_keyevent(event); #ifndef NO_ACTION_TAPPING action_tapping_process(record); @@ -559,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 5cac78d5..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); @@ -66,7 +69,6 @@ void clear_keyboard(void); void clear_keyboard_but_mods(void); void layer_switch(uint8_t new_layer); bool is_tap_key(key_t key); -void key_event(keyevent_t event); /* debug */ void debug_event(keyevent_t event); diff --git a/common/softpwm_led.c b/common/softpwm_led.c index fe689bcb..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 @@ -116,11 +202,7 @@ static const uint8_t breathing_table[128] PROGMEM = { }; static led_pack_t breathing_led_state = 0; -static led_pack_t breathing_led_direction = 0; -static led_pack_t breathing_led_rebound_low = 0; -static led_pack_t breathing_led_rebound_high = 0; -static uint8_t breathing_led_index[LED_COUNT] = {0}; -static uint8_t breathing_led_duration[LED_COUNT] = {0}; +static uint8_t breathing_led_duration = 0; void breathing_led_enable(uint8_t index) { @@ -156,87 +238,9 @@ void breathing_led_toggle_all(void) } } -void breathing_led_increase(uint8_t index, uint8_t offset) +void breathing_led_set_duration(uint8_t dur) { - if (breathing_led_index[index] + offset > 0x7F) { - breathing_led_index[index] = 0x7F; - if (breathing_led_rebound_high & LED_BIT(index)) { - LED_BIT_SET(breathing_led_direction, index); - } - } - else { - breathing_led_index[index] += offset; - } -} - -void breathing_led_decrease(uint8_t index, uint8_t offset) -{ - if (breathing_led_index[index] < offset) { - breathing_led_index[index] = 0; - if (breathing_led_rebound_low & LED_BIT(index)) { - LED_BIT_CLEAR(breathing_led_direction, index); - } - } - else { - breathing_led_index[index] -= offset; - } -} - -void breathing_led_set_mode(uint8_t index, uint8_t mode) -{ - switch (mode) { - case BREATHING_LED_UP: - breathing_led_index[index] = 0; - LED_BIT_CLEAR(breathing_led_direction, index); - LED_BIT_CLEAR(breathing_led_rebound_low, index); - LED_BIT_CLEAR(breathing_led_rebound_high, index); - break; - case BREATHING_LED_DOWN: - breathing_led_index[index] = 0x7F; - LED_BIT_SET(breathing_led_direction, index); - LED_BIT_CLEAR(breathing_led_rebound_low, index); - LED_BIT_CLEAR(breathing_led_rebound_high, index); - break; - case BREATHING_LED_CYCLE: - breathing_led_index[index] = 0; - LED_BIT_CLEAR(breathing_led_direction, index); - LED_BIT_SET(breathing_led_rebound_low, index); - LED_BIT_SET(breathing_led_rebound_high, index); - } -} - -void breathing_led_set_duration(uint8_t index, uint8_t dur) -{ - breathing_led_duration[index] = dur; - //dprintf("breathing led set duration: %u\n", breathing_led_duration); -} - -void breathing_led_increase_all(uint8_t offset) -{ - for (uint8_t i = 0; i < LED_COUNT; i++) { - breathing_led_increase(i, offset); - } -} - -void breathing_led_decrease_all(uint8_t offset) -{ - for (uint8_t i = 0; i < LED_COUNT; i++) { - breathing_led_decrease(i, offset); - } -} - -void breathing_led_set_mode_all(uint8_t mode) -{ - for (uint8_t i = 0; i < LED_COUNT; i++) { - breathing_led_set_mode(i, mode); - } -} - -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 @@ -263,22 +267,21 @@ ISR(TIMER1_COMPA_vect) } } -#ifdef BREATHING_LED_ENABLE - static uint8_t count = 0; - static uint8_t step[LED_COUNT] = {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[breathing_led_index[i]]); - if (breathing_led_direction & LED_BIT(i)) { - breathing_led_decrease(i, 1); +#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 { - breathing_led_increase(i, 1); + softpwm_led_increase(i, 1); } } } @@ -286,4 +289,41 @@ ISR(TIMER1_COMPA_vect) } } #endif + +#ifdef BREATHING_LED_ENABLE + 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 (++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++; + } + } + } + } + } +#endif } diff --git a/common/softpwm_led.h b/common/softpwm_led.h index fc4d2757..0386d2a8 100644 --- a/common/softpwm_led.h +++ b/common/softpwm_led.h @@ -14,18 +14,41 @@ 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); -#ifdef BREATHING_LED_ENABLE enum { - BREATHING_LED_NO = 0, - BREATHING_LED_UP, - BREATHING_LED_DOWN, - BREATHING_LED_CYCLE + 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); void breathing_led_enable_all(void); @@ -33,14 +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_mode(uint8_t index, uint8_t mode); -void breathing_led_set_duration(uint8_t index, uint8_t dur); -void breathing_led_increase(uint8_t index, uint8_t offset); -void breathing_led_decrease(uint8_t index, uint8_t offset); -void breathing_led_set_mode_all(uint8_t mode); -void breathing_led_set_duration_all(uint8_t dur); -void breathing_led_increase_all(uint8_t offset); -void breathing_led_decrease_all(uint8_t offset); +void breathing_led_set_duration(uint8_t dur); #else #define breathing_led_init() #define breathing_led_enable() @@ -49,14 +65,8 @@ void breathing_led_decrease_all(uint8_t offset); #define breathing_led_disable_all() #define breathing_led_toggle() #define breathing_led_toggle_all() -#define breathing_led_set_mode() #define breathing_led_set_duration() -#define breathing_led_increase() -#define breathing_led_decrease() -#define breathing_led_set_mode_all() #define breathing_led_set_duration_all() -#define breathing_led_increase_all() -#define breathing_led_decrease_all() #endif #else @@ -68,6 +78,10 @@ void breathing_led_decrease_all(uint8_t offset); #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()