Breathe and double blink added to LED effects

This commit is contained in:
2026-04-15 23:07:04 +02:00
parent 2282038391
commit 358ddcaeb5
3 changed files with 157 additions and 41 deletions

View File

@@ -70,13 +70,22 @@ struct Gauge {
bool sweepTowardMax = true;
};
enum LedFx : uint8_t { FX_BLINK = 0, FX_BREATHE = 1, FX_DFLASH = 2 };
struct BlinkState {
bool active = false;
uint32_t onMs = 500;
uint32_t offMs = 500;
CRGB onColor;
bool currentlyOn = false;
unsigned long lastToggleMs = 0;
bool active = false;
LedFx fx = FX_BLINK;
CRGB onColor;
unsigned long lastMs = 0;
// FX_BLINK
uint16_t onMs = 500;
uint16_t offMs = 500;
bool currentlyOn = false;
// FX_BREATHE: smooth triangle-wave fade
uint16_t periodMs = 2000;
uint16_t cyclePos = 0;
// FX_DFLASH: two quick flashes then pause
uint8_t dphase = 0;
};
Gauge gauges[GAUGE_COUNT];
@@ -587,11 +596,12 @@ bool parseBlink(const String& line) {
for (int i = idxFirst; i <= idxLast; i++) {
uint8_t globalIdx = gaugeLedOffset[id] + i;
BlinkState& bs = blinkState[globalIdx];
bs.fx = FX_BLINK;
bs.onColor = (count == 7) ? color : leds[globalIdx];
bs.onMs = (uint32_t)onMs;
bs.offMs = (uint32_t)offMs;
bs.currentlyOn = true;
bs.lastToggleMs = nowMs;
bs.onMs = (uint16_t)onMs;
bs.offMs = (uint16_t)offMs;
bs.currentlyOn = true;
bs.lastMs = nowMs;
bs.active = true;
leds[globalIdx] = bs.onColor;
}
@@ -600,22 +610,112 @@ bool parseBlink(const String& line) {
return true;
}
bool parseBreathe(const String& line) {
int id, periodMs, r, g, b;
char idxToken[16];
if (sscanf(line.c_str(), "BREATHE %d %15s %d %d %d %d",
&id, idxToken, &periodMs, &r, &g, &b) != 6) return false;
if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; }
char* dash = strchr(idxToken, '-');
int idxFirst = atoi(idxToken);
int idxLast = dash ? atoi(dash + 1) : idxFirst;
if (idxFirst < 0 || idxLast >= gaugePins[id].ledCount || idxFirst > idxLast) {
sendReply("ERR BAD_IDX"); return true;
}
if (periodMs <= 0) { sendReply("ERR BAD_TIME"); return true; }
CRGB color(constrain(r, 0, 255), constrain(g, 0, 255), constrain(b, 0, 255));
unsigned long nowMs = millis();
for (int i = idxFirst; i <= idxLast; i++) {
uint8_t gi = gaugeLedOffset[id] + i;
BlinkState& bs = blinkState[gi];
bs.fx = FX_BREATHE;
bs.onColor = color;
bs.periodMs = (uint16_t)constrain(periodMs, 100, 30000);
bs.cyclePos = 0;
bs.lastMs = nowMs;
bs.active = true;
leds[gi] = CRGB::Black;
}
ledsDirty = true;
sendReply("OK");
return true;
}
bool parseDflash(const String& line) {
int id, r, g, b;
char idxToken[16];
if (sscanf(line.c_str(), "DFLASH %d %15s %d %d %d",
&id, idxToken, &r, &g, &b) != 5) return false;
if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; }
char* dash = strchr(idxToken, '-');
int idxFirst = atoi(idxToken);
int idxLast = dash ? atoi(dash + 1) : idxFirst;
if (idxFirst < 0 || idxLast >= gaugePins[id].ledCount || idxFirst > idxLast) {
sendReply("ERR BAD_IDX"); return true;
}
CRGB color(constrain(r, 0, 255), constrain(g, 0, 255), constrain(b, 0, 255));
unsigned long nowMs = millis();
for (int i = idxFirst; i <= idxLast; i++) {
uint8_t gi = gaugeLedOffset[id] + i;
BlinkState& bs = blinkState[gi];
bs.fx = FX_DFLASH;
bs.onColor = color;
bs.dphase = 0;
bs.lastMs = nowMs;
bs.active = true;
leds[gi] = color; // phase 0 = on
}
ledsDirty = true;
sendReply("OK");
return true;
}
void updateBlink() {
unsigned long nowMs = millis();
bool changed = false;
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
for (uint8_t j = 0; j < gaugePins[i].ledCount; j++) {
uint8_t globalIdx = gaugeLedOffset[i] + j;
BlinkState& bs = blinkState[globalIdx];
uint8_t gi = gaugeLedOffset[i] + j;
BlinkState& bs = blinkState[gi];
if (!bs.active) continue;
uint32_t period = bs.currentlyOn ? bs.onMs : bs.offMs;
if ((nowMs - bs.lastToggleMs) >= period) {
bs.currentlyOn = !bs.currentlyOn;
bs.lastToggleMs = nowMs;
leds[globalIdx] = bs.currentlyOn ? bs.onColor : CRGB::Black;
changed = true;
switch (bs.fx) {
case FX_BLINK: {
uint32_t period = bs.currentlyOn ? bs.onMs : bs.offMs;
if ((nowMs - bs.lastMs) >= period) {
bs.currentlyOn = !bs.currentlyOn;
bs.lastMs = nowMs;
leds[gi] = bs.currentlyOn ? bs.onColor : CRGB::Black;
changed = true;
}
break;
}
case FX_BREATHE: {
unsigned long dt = nowMs - bs.lastMs;
if (dt < 16) break;
uint32_t newPos = (uint32_t)bs.cyclePos + dt;
bs.cyclePos = (uint16_t)(newPos % bs.periodMs);
bs.lastMs = nowMs;
uint16_t half = bs.periodMs >> 1;
uint8_t bri = (bs.cyclePos < half)
? (uint8_t)((uint32_t)bs.cyclePos * 255 / half)
: (uint8_t)((uint32_t)(bs.periodMs - bs.cyclePos) * 255 / half);
leds[gi] = bs.onColor;
leds[gi].nscale8(bri ? bri : 1);
changed = true;
break;
}
case FX_DFLASH: {
static const uint16_t dur[4] = {100, 100, 100, 700};
if ((nowMs - bs.lastMs) >= dur[bs.dphase]) {
bs.lastMs = nowMs;
bs.dphase = (bs.dphase + 1) & 3;
leds[gi] = (bs.dphase == 0 || bs.dphase == 2) ? bs.onColor : CRGB::Black;
changed = true;
}
break;
}
}
}
}
@@ -635,6 +735,8 @@ void processLine(const String& line) {
if (parseLedQuery(line)) return;
if (parseLed(line)) return;
if (parseBlink(line)) return;
if (parseBreathe(line)) return;
if (parseDflash(line)) return;
if (parsePing(line)) return;
sendReply("ERR BAD_CMD");