Interrupt issue with FastLed circumvented

This commit is contained in:
2026-04-15 22:46:39 +02:00
parent f7f7b389a0
commit 2282038391
2 changed files with 56 additions and 39 deletions

View File

@@ -85,6 +85,7 @@ String rxLine;
CRGB leds[TOTAL_LEDS]; CRGB leds[TOTAL_LEDS];
uint8_t gaugeLedOffset[GAUGE_COUNT]; uint8_t gaugeLedOffset[GAUGE_COUNT];
BlinkState blinkState[TOTAL_LEDS]; BlinkState blinkState[TOTAL_LEDS];
bool ledsDirty = false;
void sendReply(const String& s) { void sendReply(const String& s) {
CMD_PORT.println(s); CMD_PORT.println(s);
@@ -545,7 +546,7 @@ bool parseLed(const String& line) {
blinkState[gaugeLedOffset[id] + i].active = false; blinkState[gaugeLedOffset[id] + i].active = false;
leds[gaugeLedOffset[id] + i] = color; leds[gaugeLedOffset[id] + i] = color;
} }
FastLED.show(); ledsDirty = true;
sendReply("OK"); sendReply("OK");
return true; return true;
} }
@@ -553,10 +554,15 @@ bool parseLed(const String& line) {
} }
bool parseBlink(const String& line) { bool parseBlink(const String& line) {
int id; int id, onMs, offMs, r, g, b;
char idxToken[16]; char idxToken[16];
long onMs, offMs; // Accept both forms:
if (sscanf(line.c_str(), "BLINK %d %15s %ld %ld", &id, idxToken, &onMs, &offMs) == 4) { // BLINK <id> <idx> <on_ms> <off_ms> — use current LED colour
// BLINK <id> <idx> <on_ms> <off_ms> <r> <g> <b> — set colour in same command
int count = sscanf(line.c_str(), "BLINK %d %15s %d %d %d %d %d",
&id, idxToken, &onMs, &offMs, &r, &g, &b);
if (count != 4 && count != 7) return false;
if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; } if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; }
char* dash = strchr(idxToken, '-'); char* dash = strchr(idxToken, '-');
int idxFirst = atoi(idxToken); int idxFirst = atoi(idxToken);
@@ -573,11 +579,15 @@ bool parseBlink(const String& line) {
} }
if (onMs <= 0 || offMs <= 0) { sendReply("ERR BAD_TIME"); return true; } if (onMs <= 0 || offMs <= 0) { sendReply("ERR BAD_TIME"); return true; }
CRGB color = (count == 7)
? CRGB(constrain(r, 0, 255), constrain(g, 0, 255), constrain(b, 0, 255))
: CRGB(0, 0, 0); // placeholder; overwritten per-LED below when count==4
unsigned long nowMs = millis(); unsigned long nowMs = millis();
for (int i = idxFirst; i <= idxLast; i++) { for (int i = idxFirst; i <= idxLast; i++) {
uint8_t globalIdx = gaugeLedOffset[id] + i; uint8_t globalIdx = gaugeLedOffset[id] + i;
BlinkState& bs = blinkState[globalIdx]; BlinkState& bs = blinkState[globalIdx];
bs.onColor = leds[globalIdx]; bs.onColor = (count == 7) ? color : leds[globalIdx];
bs.onMs = (uint32_t)onMs; bs.onMs = (uint32_t)onMs;
bs.offMs = (uint32_t)offMs; bs.offMs = (uint32_t)offMs;
bs.currentlyOn = true; bs.currentlyOn = true;
@@ -585,12 +595,10 @@ bool parseBlink(const String& line) {
bs.active = true; bs.active = true;
leds[globalIdx] = bs.onColor; leds[globalIdx] = bs.onColor;
} }
FastLED.show(); ledsDirty = true;
sendReply("OK"); sendReply("OK");
return true; return true;
} }
return false;
}
void updateBlink() { void updateBlink() {
unsigned long nowMs = millis(); unsigned long nowMs = millis();
@@ -612,7 +620,7 @@ void updateBlink() {
} }
} }
if (changed) FastLED.show(); if (changed) ledsDirty = true;
} }
void processLine(const String& line) { void processLine(const String& line) {
@@ -691,6 +699,11 @@ void loop() {
readCommands(); readCommands();
updateBlink(); updateBlink();
if (ledsDirty) {
FastLED.show();
ledsDirty = false;
}
for (uint8_t i = 0; i < GAUGE_COUNT; i++) { for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
updateGauge(i); updateGauge(i);
} }

View File

@@ -224,12 +224,15 @@ def set_status_led(gauge_idx, led_type, on):
def _apply_blink_or_led(gauge_idx, led_idx, color, effect): def _apply_blink_or_led(gauge_idx, led_idx, color, effect):
"""Set LED to color; if effect is a known blink preset, start blinking.""" """Set LED to color, optionally starting a blink effect.
When blinking, color is passed inside the BLINK command so only one
serial command is needed — avoids a FastLED.show() race on the Arduino."""
r, g, b = color r, g, b = color
arduino_send(f"LED {gauge_idx} {led_idx} {r} {g} {b}")
if effect in _BLINK_PRESETS: if effect in _BLINK_PRESETS:
on_ms, off_ms = _BLINK_PRESETS[effect] on_ms, off_ms = _BLINK_PRESETS[effect]
arduino_send(f"BLINK {gauge_idx} {led_idx} {on_ms} {off_ms}") arduino_send(f"BLINK {gauge_idx} {led_idx} {on_ms} {off_ms} {r} {g} {b}")
else:
arduino_send(f"LED {gauge_idx} {led_idx} {r} {g} {b}")
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
@@ -553,12 +556,13 @@ def on_message(topic, payload):
return return
_bl_effect[i] = effect _bl_effect[i] = effect
if effect: if effect:
set_backlight(i, r, g, b, brightness) scale = brightness / 100
rs = int(r * scale); gs = int(g * scale); bs_ = int(b * scale)
on_ms, off_ms = _BLINK_PRESETS[effect]
arduino_send(f"BLINK {i} {_LED_BACKLIGHT_RANGE} {on_ms} {off_ms} {rs} {gs} {bs_}")
backlight_color[i] = (r, g, b) backlight_color[i] = (r, g, b)
backlight_brightness[i] = brightness backlight_brightness[i] = brightness
backlight_on[i] = True backlight_on[i] = True
on_ms, off_ms = _BLINK_PRESETS[effect]
arduino_send(f"BLINK {i} {_LED_BACKLIGHT_RANGE} {on_ms} {off_ms}")
_mark_bl_dirty() _mark_bl_dirty()
else: else:
set_backlight_color(i, r, g, b, brightness) set_backlight_color(i, r, g, b, brightness)