|
|
@@ -98,145 +98,149 @@ 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 BREATHING_LED_ENABLE |
|
|
|
#ifdef FADING_LED_ENABLE |
|
|
|
|
|
|
|
/* Breathing LED brighness(PWM On period) table |
|
|
|
* |
|
|
|
* http://www.wolframalpha.com/input/?i=Table%5Bfloor%28%28exp%28sin%28x%2F256*2*pi%2B3%2F2*pi%29%29-1%2Fe%29*%28256%2F%28e-1%2Fe%29%29%29%2C+%7Bx%2C0%2C255%2C1%7D%5D |
|
|
|
* 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[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_pack_t fading_led_state = 0; |
|
|
|
static led_pack_t fading_led_direction = 0; |
|
|
|
static uint8_t fading_led_duration = 0; |
|
|
|
|
|
|
|
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}; |
|
|
|
|
|
|
|
void breathing_led_enable(uint8_t index) |
|
|
|
void fading_led_enable(uint8_t index) |
|
|
|
{ |
|
|
|
LED_BIT_SET(breathing_led_state, index); |
|
|
|
LED_BIT_SET(fading_led_state, index); |
|
|
|
} |
|
|
|
|
|
|
|
void breathing_led_enable_all(void) |
|
|
|
void fading_led_enable_all(void) |
|
|
|
{ |
|
|
|
for (uint8_t i = 0; i < LED_COUNT; i++) { |
|
|
|
LED_BIT_SET(breathing_led_state, i); |
|
|
|
LED_BIT_SET(fading_led_state, i); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void breathing_led_disable(uint8_t index) |
|
|
|
void fading_led_disable(uint8_t index) |
|
|
|
{ |
|
|
|
LED_BIT_CLEAR(breathing_led_state, index); |
|
|
|
LED_BIT_CLEAR(fading_led_state, index); |
|
|
|
} |
|
|
|
|
|
|
|
void breathing_led_disable_all(void) |
|
|
|
void fading_led_disable_all(void) |
|
|
|
{ |
|
|
|
breathing_led_state = 0; |
|
|
|
fading_led_state = 0; |
|
|
|
} |
|
|
|
|
|
|
|
void breathing_led_toggle(uint8_t index) |
|
|
|
void fading_led_toggle(uint8_t index) |
|
|
|
{ |
|
|
|
LED_BIT_XOR(breathing_led_state, index); |
|
|
|
LED_BIT_XOR(fading_led_state, index); |
|
|
|
} |
|
|
|
|
|
|
|
void breathing_led_toggle_all(void) |
|
|
|
void fading_led_toggle_all(void) |
|
|
|
{ |
|
|
|
for (uint8_t i = 0; i < LED_COUNT; i++) { |
|
|
|
LED_BIT_XOR(breathing_led_state, i); |
|
|
|
LED_BIT_XOR(fading_led_state, i); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void breathing_led_increase(uint8_t index, uint8_t offset) |
|
|
|
void fading_led_set_direction(uint8_t dir) |
|
|
|
{ |
|
|
|
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; |
|
|
|
} |
|
|
|
fading_led_direction = dir; |
|
|
|
} |
|
|
|
|
|
|
|
void breathing_led_decrease(uint8_t index, uint8_t offset) |
|
|
|
void fading_led_set_duration(uint8_t dur) |
|
|
|
{ |
|
|
|
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; |
|
|
|
} |
|
|
|
fading_led_duration = dur; |
|
|
|
} |
|
|
|
|
|
|
|
void breathing_led_set_mode(uint8_t index, uint8_t mode) |
|
|
|
#endif |
|
|
|
|
|
|
|
#ifdef BREATHING_LED_ENABLE |
|
|
|
|
|
|
|
/* Breathing LED brighness(PWM On period) table |
|
|
|
* |
|
|
|
* http://www.wolframalpha.com/input/?i=Table%5Bfloor%28%28exp%28sin%28x%2F256*2*pi%2B3%2F2*pi%29%29-1%2Fe%29*%28256%2F%28e-1%2Fe%29%29%29%2C+%7Bx%2C0%2C255%2C1%7D%5D |
|
|
|
* 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[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_pack_t breathing_led_state = 0; |
|
|
|
static uint8_t breathing_led_duration = 0; |
|
|
|
|
|
|
|
void breathing_led_enable(uint8_t index) |
|
|
|
{ |
|
|
|
LED_BIT_SET(breathing_led_state, index); |
|
|
|
} |
|
|
|
|
|
|
|
void breathing_led_enable_all(void) |
|
|
|
{ |
|
|
|
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); |
|
|
|
for (uint8_t i = 0; i < LED_COUNT; i++) { |
|
|
|
LED_BIT_SET(breathing_led_state, i); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void breathing_led_set_duration(uint8_t index, uint8_t dur) |
|
|
|
void breathing_led_disable(uint8_t index) |
|
|
|
{ |
|
|
|
breathing_led_duration[index] = dur; |
|
|
|
//dprintf("breathing led set duration: %u\n", breathing_led_duration); |
|
|
|
LED_BIT_CLEAR(breathing_led_state, index); |
|
|
|
} |
|
|
|
|
|
|
|
void breathing_led_increase_all(uint8_t offset) |
|
|
|
void breathing_led_disable_all(void) |
|
|
|
{ |
|
|
|
for (uint8_t i = 0; i < LED_COUNT; i++) { |
|
|
|
breathing_led_increase(i, offset); |
|
|
|
} |
|
|
|
breathing_led_state = 0; |
|
|
|
} |
|
|
|
|
|
|
|
void breathing_led_decrease_all(uint8_t offset) |
|
|
|
void breathing_led_toggle(uint8_t index) |
|
|
|
{ |
|
|
|
for (uint8_t i = 0; i < LED_COUNT; i++) { |
|
|
|
breathing_led_decrease(i, offset); |
|
|
|
} |
|
|
|
LED_BIT_XOR(breathing_led_state, index); |
|
|
|
} |
|
|
|
|
|
|
|
void breathing_led_set_mode_all(uint8_t mode) |
|
|
|
void breathing_led_toggle_all(void) |
|
|
|
{ |
|
|
|
for (uint8_t i = 0; i < LED_COUNT; i++) { |
|
|
|
breathing_led_set_mode(i, mode); |
|
|
|
LED_BIT_XOR(breathing_led_state, i); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void breathing_led_set_duration_all(uint8_t dur) |
|
|
|
void breathing_led_set_duration(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 |
|
|
|
} |