Interrupt issue with FastLed circumvented
This commit is contained in:
@@ -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,43 +554,50 @@ 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
|
||||||
if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; }
|
// BLINK <id> <idx> <on_ms> <off_ms> <r> <g> <b> — set colour in same command
|
||||||
char* dash = strchr(idxToken, '-');
|
int count = sscanf(line.c_str(), "BLINK %d %15s %d %d %d %d %d",
|
||||||
int idxFirst = atoi(idxToken);
|
&id, idxToken, &onMs, &offMs, &r, &g, &b);
|
||||||
int idxLast = dash ? atoi(dash + 1) : idxFirst;
|
if (count != 4 && count != 7) return false;
|
||||||
if (idxFirst < 0 || idxLast >= gaugePins[id].ledCount || idxFirst > idxLast) {
|
|
||||||
sendReply("ERR BAD_IDX"); return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (onMs == 0 && offMs == 0) {
|
if (id < 0 || id >= GAUGE_COUNT) { sendReply("ERR BAD_ID"); return true; }
|
||||||
for (int i = idxFirst; i <= idxLast; i++)
|
char* dash = strchr(idxToken, '-');
|
||||||
blinkState[gaugeLedOffset[id] + i].active = false;
|
int idxFirst = atoi(idxToken);
|
||||||
sendReply("OK");
|
int idxLast = dash ? atoi(dash + 1) : idxFirst;
|
||||||
return true;
|
if (idxFirst < 0 || idxLast >= gaugePins[id].ledCount || idxFirst > idxLast) {
|
||||||
}
|
sendReply("ERR BAD_IDX"); return true;
|
||||||
if (onMs <= 0 || offMs <= 0) { sendReply("ERR BAD_TIME"); return true; }
|
}
|
||||||
|
|
||||||
unsigned long nowMs = millis();
|
if (onMs == 0 && offMs == 0) {
|
||||||
for (int i = idxFirst; i <= idxLast; i++) {
|
for (int i = idxFirst; i <= idxLast; i++)
|
||||||
uint8_t globalIdx = gaugeLedOffset[id] + i;
|
blinkState[gaugeLedOffset[id] + i].active = false;
|
||||||
BlinkState& bs = blinkState[globalIdx];
|
|
||||||
bs.onColor = leds[globalIdx];
|
|
||||||
bs.onMs = (uint32_t)onMs;
|
|
||||||
bs.offMs = (uint32_t)offMs;
|
|
||||||
bs.currentlyOn = true;
|
|
||||||
bs.lastToggleMs = nowMs;
|
|
||||||
bs.active = true;
|
|
||||||
leds[globalIdx] = bs.onColor;
|
|
||||||
}
|
|
||||||
FastLED.show();
|
|
||||||
sendReply("OK");
|
sendReply("OK");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
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();
|
||||||
|
for (int i = idxFirst; i <= idxLast; i++) {
|
||||||
|
uint8_t globalIdx = gaugeLedOffset[id] + i;
|
||||||
|
BlinkState& bs = blinkState[globalIdx];
|
||||||
|
bs.onColor = (count == 7) ? color : leds[globalIdx];
|
||||||
|
bs.onMs = (uint32_t)onMs;
|
||||||
|
bs.offMs = (uint32_t)offMs;
|
||||||
|
bs.currentlyOn = true;
|
||||||
|
bs.lastToggleMs = nowMs;
|
||||||
|
bs.active = true;
|
||||||
|
leds[globalIdx] = bs.onColor;
|
||||||
|
}
|
||||||
|
ledsDirty = true;
|
||||||
|
sendReply("OK");
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateBlink() {
|
void updateBlink() {
|
||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
16
gauge.py
16
gauge.py
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user