From ef3ab3e284427ecad61ef2dac5d3ea314f3f9abf Mon Sep 17 00:00:00 2001 From: chillfactor032 <68230787+chillfactor032@users.noreply.github.com> Date: Thu, 30 May 2024 05:32:42 -0500 Subject: [PATCH] Move to handleOverlayDraw --- usermods/FlashEffect/flash_effect.h | 50 ++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/usermods/FlashEffect/flash_effect.h b/usermods/FlashEffect/flash_effect.h index 8238a8b592..291ba7b41c 100644 --- a/usermods/FlashEffect/flash_effect.h +++ b/usermods/FlashEffect/flash_effect.h @@ -18,9 +18,20 @@ platformio_override.ini example to enable this mod: const char flashEffectSpec[] PROGMEM = "Flash@Duration,,,,,,Overlay;!,!;!;01"; bool flash_reset[256]; +//TODO: set to false before push to prod +bool flash_enable = true; + +struct FlashData { + uint32_t start_ms; +}; + +FlashData flash_data[256]{}; + struct FlashEffect : Usermod { bool initDone = false; - + uint16_t default_duration = 100; + uint32_t default_color = RGBW32(255,255,255,255); + void setup() override { strip.addEffect(255, &flashEffect_, flashEffectSpec); Serial.println("flash setup"); @@ -42,16 +53,47 @@ struct FlashEffect : Usermod { } void readFromJsonState(JsonObject& root) override { - Serial.printf("Event Read JSON State"); + Serial.printf("Event Read JSON State\n"); if (!initDone) return; + if(!root["flash_enable"].isNull()){ + flash_enable = (root["flash_enable"] == 1); + Serial.printf("flash_effect enable=%d\n", flash_enable); + } JsonObject flash = root["flash"]; if(flash.isNull()) return; auto seg_obj = flash["seg_id"]; if(seg_obj.isNull()) return; uint16_t seg_id = seg_obj; if (seg_id >= 256) return; - flash_reset[seg_id] = true; - Serial.printf("reset flash_effect seg_id=%d\n", seg_id); + flash_data[seg_id].start_ms = millis(); + Serial.printf("flash_effect seg_id=%d\n", seg_id); + } + + /* + * handleOverlayDraw() is called just before every show() (LED strip update frame) after effects have set the colors. + * Use this to blank out some LEDs or set them to a different color regardless of the set effect mode. + * Commonly used for custom clocks (Cronixie, 7 segment) + */ + void handleOverlayDraw() override { + if(!flash_enable) return; + //uint16_t frametime = strip.getFrameTime(); + //Serial.printf("frametime=%d\n", frametime); + uint8_t seg_size = strip.getSegmentsNum(); + auto segments = strip.getSegments(); + for(uint8_t i=0; i < seg_size; i++){ + if(flash_data[i].start_ms + default_duration <= strip.now) continue; + uint32_t color = color_fade(default_color, impulseResponse(strip.now - flash_data[i].start_ms), false); + segments[i].fill(color); + } + strip.trigger(); + } + + uint32_t impulseResponse(uint32_t elapsed_ms){ + uint32_t response = 100 * (default_duration-elapsed_ms) / default_duration; + if (response > 255) response = 255; + if (response <= 0) response = 1; + //Serial.printf("impulseResponse brightness=%d\n", response); + return response; } private: