From 7851656847738192e0e12cfe77861ba58d98cf6f Mon Sep 17 00:00:00 2001 From: Selene ToyKeeper Date: Fri, 6 Sep 2019 04:38:00 -0600 Subject: added button LED support to D4v2 (in a way which doesn't break backward compatibility... ... but it really needs some refactoring to reduce code duplication) --- spaghetti-monster/anduril/anduril.c | 33 ++++++++++++++++++++++++----- spaghetti-monster/anduril/cfg-emisar-d4v2.h | 4 ++++ spaghetti-monster/fsm-misc.c | 23 ++++++++++++++++++++ spaghetti-monster/fsm-misc.h | 6 ++++++ spaghetti-monster/fsm-ramping.c | 3 +++ 5 files changed, 64 insertions(+), 5 deletions(-) (limited to 'spaghetti-monster') diff --git a/spaghetti-monster/anduril/anduril.c b/spaghetti-monster/anduril/anduril.c index 8ab66f5..5032dcd 100644 --- a/spaghetti-monster/anduril/anduril.c +++ b/spaghetti-monster/anduril/anduril.c @@ -2257,7 +2257,13 @@ void rgb_led_update(uint8_t mode, uint8_t arg) { // turn off aux LEDs when battery is empty // (but if voltage==0, that means we just booted and don't know yet) uint8_t volts = voltage; // save a few bytes by caching volatile value - if ((volts) && (volts < VOLTAGE_LOW)) { rgb_led_set(0); return; } + if ((volts) && (volts < VOLTAGE_LOW)) { + rgb_led_set(0); + #ifdef USE_BUTTON_LED + button_led_set(0); + #endif + return; + } uint8_t pattern = (mode>>4); // off, low, high, blinking, ... more? uint8_t color = mode & 0x0f; @@ -2307,17 +2313,34 @@ void rgb_led_update(uint8_t mode, uint8_t arg) { frame = (frame + 1) % sizeof(animation); pattern = animation[frame]; } + uint8_t result; + #ifdef USE_BUTTON_LED + uint8_t button_led_result; + #endif switch (pattern) { case 0: // off - rgb_led_set(0); + result = 0; + #ifdef USE_BUTTON_LED + button_led_result = 0; + #endif break; case 1: // low - rgb_led_set(actual_color); + result = actual_color; + #ifdef USE_BUTTON_LED + button_led_result = 1; + #endif break; - case 2: // high - rgb_led_set(actual_color << 1); + default: // high + result = (actual_color << 1); + #ifdef USE_BUTTON_LED + button_led_result = 2; + #endif break; } + rgb_led_set(result); + #ifdef USE_BUTTON_LED + button_led_set(button_led_result); + #endif } #endif diff --git a/spaghetti-monster/anduril/cfg-emisar-d4v2.h b/spaghetti-monster/anduril/cfg-emisar-d4v2.h index b83c65c..d0d2cf6 100644 --- a/spaghetti-monster/anduril/cfg-emisar-d4v2.h +++ b/spaghetti-monster/anduril/cfg-emisar-d4v2.h @@ -4,7 +4,11 @@ // this light has three aux LED channels: R, G, B #define USE_AUX_RGB_LEDS +// it also has an independent LED in the button +#define USE_BUTTON_LED // the aux LEDs are front-facing, so turn them off while main LEDs are on +// TODO: the whole "indicator LED" thing needs to be refactored into +// "aux LED(s)" and "button LED(s)" since they work a bit differently #ifdef USE_INDICATOR_LED_WHILE_RAMPING #undef USE_INDICATOR_LED_WHILE_RAMPING #endif diff --git a/spaghetti-monster/fsm-misc.c b/spaghetti-monster/fsm-misc.c index 8e88cbd..18dc7c5 100644 --- a/spaghetti-monster/fsm-misc.c +++ b/spaghetti-monster/fsm-misc.c @@ -147,6 +147,29 @@ void indicator_led_auto() { */ #endif // USE_INDICATOR_LED +#ifdef USE_BUTTON_LED +// TODO: Refactor this and RGB LED function to merge code and save space +void button_led_set(uint8_t lvl) { + switch (lvl) { + case 0: // LED off + BUTTON_LED_DDR &= 0xff ^ (1 << BUTTON_LED_PIN); + BUTTON_LED_PUE &= 0xff ^ (1 << BUTTON_LED_PIN); + BUTTON_LED_PORT &= 0xff ^ (1 << BUTTON_LED_PIN); + break; + case 1: // LED low + BUTTON_LED_DDR &= 0xff ^ (1 << BUTTON_LED_PIN); + BUTTON_LED_PUE |= (1 << BUTTON_LED_PIN); + BUTTON_LED_PORT |= (1 << BUTTON_LED_PIN); + break; + default: // LED high + BUTTON_LED_DDR |= (1 << BUTTON_LED_PIN); + BUTTON_LED_PUE |= (1 << BUTTON_LED_PIN); + BUTTON_LED_PORT |= (1 << BUTTON_LED_PIN); + break; + } +} +#endif + #ifdef USE_AUX_RGB_LEDS void rgb_led_set(uint8_t value) { // value: 0b00BBGGRR diff --git a/spaghetti-monster/fsm-misc.h b/spaghetti-monster/fsm-misc.h index a39d31a..66d31ba 100644 --- a/spaghetti-monster/fsm-misc.h +++ b/spaghetti-monster/fsm-misc.h @@ -43,10 +43,16 @@ uint8_t blink(uint8_t num, uint8_t speed); */ #ifdef USE_INDICATOR_LED +// FIXME: Remove this, replace with button_led() // lvl: 0=off, 1=low, 2=high void indicator_led(uint8_t lvl); #endif +#ifdef USE_BUTTON_LED +// lvl: 0=off, 1=low, 2=high +void button_led_set(uint8_t lvl); +#endif + #ifdef USE_AUX_RGB_LEDS // value: 0b00BBGGRR // each pair of bits: 0=off, 1=low, 2=high diff --git a/spaghetti-monster/fsm-ramping.c b/spaghetti-monster/fsm-ramping.c index efa07e4..37c8073 100644 --- a/spaghetti-monster/fsm-ramping.c +++ b/spaghetti-monster/fsm-ramping.c @@ -46,6 +46,9 @@ void set_level(uint8_t level) { #endif #ifdef USE_AUX_RGB_LEDS rgb_led_set(0); + #ifdef USE_BUTTON_LED + button_led_set((level > 0) + (level > MAX_1x7135)); + #endif #endif } #endif -- cgit v1.2.3 From f1ff71108531137d4a94b913175084781987199d Mon Sep 17 00:00:00 2001 From: Selene ToyKeeper Date: Sat, 7 Sep 2019 00:30:39 -0600 Subject: slowed down rainbow RGB mode, added a ramp-down on stuck-button for safety purposes --- spaghetti-monster/anduril/anduril.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'spaghetti-monster') diff --git a/spaghetti-monster/anduril/anduril.c b/spaghetti-monster/anduril/anduril.c index 5032dcd..e8524a8 100644 --- a/spaghetti-monster/anduril/anduril.c +++ b/spaghetti-monster/anduril/anduril.c @@ -792,6 +792,10 @@ uint8_t steady_state(Event event, uint16_t arg) { // (off->hold->stepped_min->release causes this state) else if (actual_level <= mode_min) { ramp_direction = 1; } } + // if the button is stuck, err on the side of safety and ramp down + else if ((arg > TICKS_PER_SECOND * 5) && (actual_level >= mode_max)) { + ramp_direction = -1; + } memorized_level = nearest_level((int16_t)actual_level \ + (ramp_step_size * ramp_direction)); #else @@ -2286,7 +2290,9 @@ void rgb_led_update(uint8_t mode, uint8_t arg) { actual_color = colors[color]; } else if (color == 7) { // rainbow - if (0 == (arg & 0x03)) { + uint8_t speed = 0x03; // awake speed + if (go_to_standby) speed = 0x0f; // asleep speed + if (0 == (arg & speed)) { rainbow = (rainbow + 1) % 6; } actual_color = colors[rainbow]; -- cgit v1.2.3