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

@@ -223,16 +223,26 @@ def set_status_led(gauge_idx, led_type, on):
_set_led(gauge_idx, _LED_STATUS_GREEN, r, g, b)
def _apply_blink_or_led(gauge_idx, led_idx, color, effect):
"""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."""
def _send_effect(gauge_idx, led_ref, color, effect):
"""Send a single Arduino command for the given effect (or plain LED if none).
Always embeds color in the command — no preceding LED command needed."""
r, g, b = color
if effect in _BLINK_PRESETS:
on_ms, off_ms = _BLINK_PRESETS[effect]
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}")
if effect not in _EFFECTS:
arduino_send(f"LED {gauge_idx} {led_ref} {r} {g} {b}")
return
p = _EFFECTS[effect]
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
_mqtt_connected = False
_BLINK_PRESETS = {
"Blink Slow": (800, 800),
"Blink Fast": (150, 150),
"Blink Alert": (100, 400),
_EFFECTS = {
"Blink Slow": ("blink", 800, 800),
"Blink Fast": ("blink", 150, 150),
"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
_green_effect = [None] * num_gauges
@@ -497,7 +510,7 @@ def on_message(topic, payload):
data = {"state": payload}
state_on = data.get("state", "ON").upper() != "OFF"
effect = data.get("effect") if state_on else None
if effect not in _BLINK_PRESETS:
if effect not in _EFFECTS:
effect = None
_red_effect[i] = effect
color = gauges[i]["ws2812_red"] if state_on else (0, 0, 0)
@@ -516,7 +529,7 @@ def on_message(topic, payload):
data = {"state": payload}
state_on = data.get("state", "ON").upper() != "OFF"
effect = data.get("effect") if state_on else None
if effect not in _BLINK_PRESETS:
if effect not in _EFFECTS:
effect = None
_green_effect[i] = effect
color = gauges[i]["ws2812_green"] if state_on else (0, 0, 0)
@@ -549,7 +562,7 @@ def on_message(topic, payload):
else:
brightness = 100
effect = data.get("effect")
if effect not in _BLINK_PRESETS:
if effect not in _EFFECTS:
effect = None
except Exception as e:
warn(f"Invalid backlight payload for gauge {i}: '{payload}' ({e})")
@@ -558,8 +571,7 @@ def on_message(topic, payload):
if effect:
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_}")
_send_effect(i, _LED_BACKLIGHT_RANGE, (rs, gs, bs_), effect)
backlight_color[i] = (r, g, b)
backlight_brightness[i] = brightness
backlight_on[i] = True
@@ -585,7 +597,7 @@ def on_message(topic, payload):
data = {"state": payload}
state_on = data.get("state", "ON").upper() != "OFF"
effect = data.get("effect") if state_on else None
if effect not in _BLINK_PRESETS:
if effect not in _EFFECTS:
effect = None
_status_red_effect[i] = effect
color = gauges[i]["ws2812_red"] if state_on else (0, 0, 0)
@@ -604,7 +616,7 @@ def on_message(topic, payload):
data = {"state": payload}
state_on = data.get("state", "ON").upper() != "OFF"
effect = data.get("effect") if state_on else None
if effect not in _BLINK_PRESETS:
if effect not in _EFFECTS:
effect = None
_status_green_effect[i] = effect
color = gauges[i]["ws2812_green"] if state_on else (0, 0, 0)