Skip to content

Commit

Permalink
Move to handleOverlayDraw
Browse files Browse the repository at this point in the history
  • Loading branch information
chillfactor032 committed May 30, 2024
1 parent 3cf1e28 commit ef3ab3e
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions usermods/FlashEffect/flash_effect.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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:
Expand Down

0 comments on commit ef3ab3e

Please sign in to comment.