Breathe and double blink added to LED effects
This commit is contained in:
@@ -90,9 +90,11 @@ Commands arrive as newline-terminated ASCII lines. Each `parse*` function in `pr
|
|||||||
| `HOME` | `HOME <id>` / `HOMEALL` | Run homing sequence |
|
| `HOME` | `HOME <id>` / `HOMEALL` | Run homing sequence |
|
||||||
| `SWEEP` | `SWEEP <id> <accel> <speed>` | Start sweep (0/0 stops) |
|
| `SWEEP` | `SWEEP <id> <accel> <speed>` | Start sweep (0/0 stops) |
|
||||||
| `POS?` | `POS?` | Query all gauges: `POS <id> <cur> <tgt> <homed> <homingState> <sweep>` |
|
| `POS?` | `POS?` | Query all gauges: `POS <id> <cur> <tgt> <homed> <homingState> <sweep>` |
|
||||||
| `LED` | `LED <id> <idx> <r> <g> <b>` | Set one LED (0-based index within gauge segment) to RGB colour (0–255 each); `<idx>` may be a range `N-M` to set LEDs N through M in one command; also stops any active blink on those LEDs |
|
| `LED` | `LED <id> <idx> <r> <g> <b>` | Set one LED (0-based index within gauge segment) to RGB colour (0–255 each); `<idx>` may be a range `N-M` to set LEDs N through M in one command; also stops any active effect on those LEDs |
|
||||||
| `LED?` | `LED?` | Query all LEDs: one `LED <id> <idx> <r> <g> <b>` line per LED, then `OK` |
|
| `LED?` | `LED?` | Query all LEDs: one `LED <id> <idx> <r> <g> <b>` line per LED, then `OK` |
|
||||||
| `BLINK` | `BLINK <id> <idx> <on_ms> <off_ms>` | Blink LED(s) using their current colour as the on-colour; `<idx>` may be a range `N-M`; `on_ms`/`off_ms` must both be > 0, or both 0 to stop blinking |
|
| `BLINK` | `BLINK <id> <idx> <on_ms> <off_ms> <r> <g> <b>` | Blink LED(s) at given colour; `<idx>` may be a range `N-M`; `on_ms`/`off_ms` both 0 stops blinking. 4-arg form (no colour) uses current LED colour |
|
||||||
|
| `BREATHE` | `BREATHE <id> <idx> <period_ms> <r> <g> <b>` | Smooth triangle-wave fade between black and the given colour; `<idx>` may be a range `N-M` |
|
||||||
|
| `DFLASH` | `DFLASH <id> <idx> <r> <g> <b>` | Two quick flashes (100 ms on/off each) followed by a 700 ms pause, then repeats; `<idx>` may be a range `N-M` |
|
||||||
| `PING` | `PING` | Responds `PONG` |
|
| `PING` | `PING` | Responds `PONG` |
|
||||||
|
|
||||||
All commands reply `OK` or `ERR BAD_ID` / `ERR BAD_CMD` etc.
|
All commands reply `OK` or `ERR BAD_ID` / `ERR BAD_CMD` etc.
|
||||||
|
|||||||
@@ -70,13 +70,22 @@ struct Gauge {
|
|||||||
bool sweepTowardMax = true;
|
bool sweepTowardMax = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum LedFx : uint8_t { FX_BLINK = 0, FX_BREATHE = 1, FX_DFLASH = 2 };
|
||||||
|
|
||||||
struct BlinkState {
|
struct BlinkState {
|
||||||
bool active = false;
|
bool active = false;
|
||||||
uint32_t onMs = 500;
|
LedFx fx = FX_BLINK;
|
||||||
uint32_t offMs = 500;
|
CRGB onColor;
|
||||||
CRGB onColor;
|
unsigned long lastMs = 0;
|
||||||
bool currentlyOn = false;
|
// FX_BLINK
|
||||||
unsigned long lastToggleMs = 0;
|
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];
|
Gauge gauges[GAUGE_COUNT];
|
||||||
@@ -587,11 +596,12 @@ bool parseBlink(const String& line) {
|
|||||||
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.fx = FX_BLINK;
|
||||||
bs.onColor = (count == 7) ? color : leds[globalIdx];
|
bs.onColor = (count == 7) ? color : leds[globalIdx];
|
||||||
bs.onMs = (uint32_t)onMs;
|
bs.onMs = (uint16_t)onMs;
|
||||||
bs.offMs = (uint32_t)offMs;
|
bs.offMs = (uint16_t)offMs;
|
||||||
bs.currentlyOn = true;
|
bs.currentlyOn = true;
|
||||||
bs.lastToggleMs = nowMs;
|
bs.lastMs = nowMs;
|
||||||
bs.active = true;
|
bs.active = true;
|
||||||
leds[globalIdx] = bs.onColor;
|
leds[globalIdx] = bs.onColor;
|
||||||
}
|
}
|
||||||
@@ -600,22 +610,112 @@ bool parseBlink(const String& line) {
|
|||||||
return true;
|
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() {
|
void updateBlink() {
|
||||||
unsigned long nowMs = millis();
|
unsigned long nowMs = millis();
|
||||||
bool changed = false;
|
bool changed = false;
|
||||||
|
|
||||||
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
for (uint8_t i = 0; i < GAUGE_COUNT; i++) {
|
||||||
for (uint8_t j = 0; j < gaugePins[i].ledCount; j++) {
|
for (uint8_t j = 0; j < gaugePins[i].ledCount; j++) {
|
||||||
uint8_t globalIdx = gaugeLedOffset[i] + j;
|
uint8_t gi = gaugeLedOffset[i] + j;
|
||||||
BlinkState& bs = blinkState[globalIdx];
|
BlinkState& bs = blinkState[gi];
|
||||||
if (!bs.active) continue;
|
if (!bs.active) continue;
|
||||||
|
|
||||||
uint32_t period = bs.currentlyOn ? bs.onMs : bs.offMs;
|
switch (bs.fx) {
|
||||||
if ((nowMs - bs.lastToggleMs) >= period) {
|
case FX_BLINK: {
|
||||||
bs.currentlyOn = !bs.currentlyOn;
|
uint32_t period = bs.currentlyOn ? bs.onMs : bs.offMs;
|
||||||
bs.lastToggleMs = nowMs;
|
if ((nowMs - bs.lastMs) >= period) {
|
||||||
leds[globalIdx] = bs.currentlyOn ? bs.onColor : CRGB::Black;
|
bs.currentlyOn = !bs.currentlyOn;
|
||||||
changed = true;
|
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 (parseLedQuery(line)) return;
|
||||||
if (parseLed(line)) return;
|
if (parseLed(line)) return;
|
||||||
if (parseBlink(line)) return;
|
if (parseBlink(line)) return;
|
||||||
|
if (parseBreathe(line)) return;
|
||||||
|
if (parseDflash(line)) return;
|
||||||
if (parsePing(line)) return;
|
if (parsePing(line)) return;
|
||||||
|
|
||||||
sendReply("ERR BAD_CMD");
|
sendReply("ERR BAD_CMD");
|
||||||
|
|||||||
54
gauge.py
54
gauge.py
@@ -223,16 +223,26 @@ def set_status_led(gauge_idx, led_type, on):
|
|||||||
_set_led(gauge_idx, _LED_STATUS_GREEN, r, g, b)
|
_set_led(gauge_idx, _LED_STATUS_GREEN, r, g, b)
|
||||||
|
|
||||||
|
|
||||||
def _apply_blink_or_led(gauge_idx, led_idx, color, effect):
|
def _send_effect(gauge_idx, led_ref, color, effect):
|
||||||
"""Set LED to color, optionally starting a blink effect.
|
"""Send a single Arduino command for the given effect (or plain LED if none).
|
||||||
When blinking, color is passed inside the BLINK command so only one
|
Always embeds color in the command — no preceding LED command needed."""
|
||||||
serial command is needed — avoids a FastLED.show() race on the Arduino."""
|
|
||||||
r, g, b = color
|
r, g, b = color
|
||||||
if effect in _BLINK_PRESETS:
|
if effect not in _EFFECTS:
|
||||||
on_ms, off_ms = _BLINK_PRESETS[effect]
|
arduino_send(f"LED {gauge_idx} {led_ref} {r} {g} {b}")
|
||||||
arduino_send(f"BLINK {gauge_idx} {led_idx} {on_ms} {off_ms} {r} {g} {b}")
|
return
|
||||||
else:
|
p = _EFFECTS[effect]
|
||||||
arduino_send(f"LED {gauge_idx} {led_idx} {r} {g} {b}")
|
if p[0] == "blink":
|
||||||
|
arduino_send(f"BLINK {gauge_idx} {led_ref} {p[1]} {p[2]} {r} {g} {b}")
|
||||||
|
elif p[0] == "breathe":
|
||||||
|
arduino_send(f"BREATHE {gauge_idx} {led_ref} {p[1]} {r} {g} {b}")
|
||||||
|
elif p[0] == "dflash":
|
||||||
|
arduino_send(f"DFLASH {gauge_idx} {led_ref} {r} {g} {b}")
|
||||||
|
|
||||||
|
|
||||||
|
def _apply_blink_or_led(gauge_idx, led_idx, color, effect):
|
||||||
|
"""Set LED to color, optionally starting an effect.
|
||||||
|
Color is always embedded in the command — avoids FastLED.show() race."""
|
||||||
|
_send_effect(gauge_idx, led_idx, color, effect)
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -252,12 +262,15 @@ _BL_SAVE_DELAY_MS = 5000
|
|||||||
client_ref = None
|
client_ref = None
|
||||||
_mqtt_connected = False
|
_mqtt_connected = False
|
||||||
|
|
||||||
_BLINK_PRESETS = {
|
_EFFECTS = {
|
||||||
"Blink Slow": (800, 800),
|
"Blink Slow": ("blink", 800, 800),
|
||||||
"Blink Fast": (150, 150),
|
"Blink Fast": ("blink", 150, 150),
|
||||||
"Blink Alert": (100, 400),
|
"Blink Alert": ("blink", 100, 400),
|
||||||
|
"Breathe Slow": ("breathe", 3000),
|
||||||
|
"Breathe Fast": ("breathe", 1200),
|
||||||
|
"Double Flash": ("dflash",),
|
||||||
}
|
}
|
||||||
_EFFECT_LIST = list(_BLINK_PRESETS.keys())
|
_EFFECT_LIST = list(_EFFECTS.keys())
|
||||||
|
|
||||||
_red_effect = [None] * num_gauges
|
_red_effect = [None] * num_gauges
|
||||||
_green_effect = [None] * num_gauges
|
_green_effect = [None] * num_gauges
|
||||||
@@ -497,7 +510,7 @@ def on_message(topic, payload):
|
|||||||
data = {"state": payload}
|
data = {"state": payload}
|
||||||
state_on = data.get("state", "ON").upper() != "OFF"
|
state_on = data.get("state", "ON").upper() != "OFF"
|
||||||
effect = data.get("effect") if state_on else None
|
effect = data.get("effect") if state_on else None
|
||||||
if effect not in _BLINK_PRESETS:
|
if effect not in _EFFECTS:
|
||||||
effect = None
|
effect = None
|
||||||
_red_effect[i] = effect
|
_red_effect[i] = effect
|
||||||
color = gauges[i]["ws2812_red"] if state_on else (0, 0, 0)
|
color = gauges[i]["ws2812_red"] if state_on else (0, 0, 0)
|
||||||
@@ -516,7 +529,7 @@ def on_message(topic, payload):
|
|||||||
data = {"state": payload}
|
data = {"state": payload}
|
||||||
state_on = data.get("state", "ON").upper() != "OFF"
|
state_on = data.get("state", "ON").upper() != "OFF"
|
||||||
effect = data.get("effect") if state_on else None
|
effect = data.get("effect") if state_on else None
|
||||||
if effect not in _BLINK_PRESETS:
|
if effect not in _EFFECTS:
|
||||||
effect = None
|
effect = None
|
||||||
_green_effect[i] = effect
|
_green_effect[i] = effect
|
||||||
color = gauges[i]["ws2812_green"] if state_on else (0, 0, 0)
|
color = gauges[i]["ws2812_green"] if state_on else (0, 0, 0)
|
||||||
@@ -549,7 +562,7 @@ def on_message(topic, payload):
|
|||||||
else:
|
else:
|
||||||
brightness = 100
|
brightness = 100
|
||||||
effect = data.get("effect")
|
effect = data.get("effect")
|
||||||
if effect not in _BLINK_PRESETS:
|
if effect not in _EFFECTS:
|
||||||
effect = None
|
effect = None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
warn(f"Invalid backlight payload for gauge {i}: '{payload}' ({e})")
|
warn(f"Invalid backlight payload for gauge {i}: '{payload}' ({e})")
|
||||||
@@ -558,8 +571,7 @@ def on_message(topic, payload):
|
|||||||
if effect:
|
if effect:
|
||||||
scale = brightness / 100
|
scale = brightness / 100
|
||||||
rs = int(r * scale); gs = int(g * scale); bs_ = int(b * scale)
|
rs = int(r * scale); gs = int(g * scale); bs_ = int(b * scale)
|
||||||
on_ms, off_ms = _BLINK_PRESETS[effect]
|
_send_effect(i, _LED_BACKLIGHT_RANGE, (rs, gs, bs_), 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
|
||||||
@@ -585,7 +597,7 @@ def on_message(topic, payload):
|
|||||||
data = {"state": payload}
|
data = {"state": payload}
|
||||||
state_on = data.get("state", "ON").upper() != "OFF"
|
state_on = data.get("state", "ON").upper() != "OFF"
|
||||||
effect = data.get("effect") if state_on else None
|
effect = data.get("effect") if state_on else None
|
||||||
if effect not in _BLINK_PRESETS:
|
if effect not in _EFFECTS:
|
||||||
effect = None
|
effect = None
|
||||||
_status_red_effect[i] = effect
|
_status_red_effect[i] = effect
|
||||||
color = gauges[i]["ws2812_red"] if state_on else (0, 0, 0)
|
color = gauges[i]["ws2812_red"] if state_on else (0, 0, 0)
|
||||||
@@ -604,7 +616,7 @@ def on_message(topic, payload):
|
|||||||
data = {"state": payload}
|
data = {"state": payload}
|
||||||
state_on = data.get("state", "ON").upper() != "OFF"
|
state_on = data.get("state", "ON").upper() != "OFF"
|
||||||
effect = data.get("effect") if state_on else None
|
effect = data.get("effect") if state_on else None
|
||||||
if effect not in _BLINK_PRESETS:
|
if effect not in _EFFECTS:
|
||||||
effect = None
|
effect = None
|
||||||
_status_green_effect[i] = effect
|
_status_green_effect[i] = effect
|
||||||
color = gauges[i]["ws2812_green"] if state_on else (0, 0, 0)
|
color = gauges[i]["ws2812_green"] if state_on else (0, 0, 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user