Several cleanups
This commit is contained in:
@@ -28,5 +28,5 @@
|
||||
"red_led_entity_name": "Selsyn 1 Red LED",
|
||||
"green_led_entity_name": "Selsyn 1 Green LED",
|
||||
"backlight_entity_name": "Selsyn 1 Backlight",
|
||||
"backlight_unit": "%"
|
||||
"ws2812_order": "GRB"
|
||||
}
|
||||
|
||||
@@ -128,6 +128,11 @@ class Gauge:
|
||||
self._pin_step.value(0)
|
||||
utime.sleep_us(delay_us)
|
||||
|
||||
def steps_toward(self, value, limit=5):
|
||||
"""Return the step delta needed to move toward value, clamped to ±limit."""
|
||||
delta = self._val_to_step(value) - self._current_step
|
||||
return max(-limit, min(limit, delta))
|
||||
|
||||
def get(self):
|
||||
return self._step_to_val(self._current_step)
|
||||
|
||||
|
||||
@@ -82,6 +82,7 @@ _cfg = _load_config()
|
||||
|
||||
DEBUG = _cfg.get("debug", False)
|
||||
_DEBUG = DEBUG
|
||||
_WS2812_ORDER = _cfg.get("ws2812_order", "GRB").upper()
|
||||
|
||||
WIFI_SSID = _cfg["wifi_ssid"]
|
||||
WIFI_PASSWORD = _cfg["wifi_password"]
|
||||
@@ -150,7 +151,6 @@ else:
|
||||
"ws2812_green": tuple(_cfg.get("ws2812_green", [0, 255, 0])),
|
||||
}
|
||||
)
|
||||
BL_UNIT = _cfg.get("backlight_unit", "%")
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Gauge initialization
|
||||
@@ -312,6 +312,13 @@ _bl_dirty_since = None
|
||||
_BL_SAVE_DELAY_MS = 5000
|
||||
|
||||
|
||||
def _to_pixel(r, g, b):
|
||||
"""Reorder RGB to match the WS2812 variant's byte order (GRB or RGB)."""
|
||||
if _WS2812_ORDER == "GRB":
|
||||
return (g, r, b)
|
||||
return (r, g, b)
|
||||
|
||||
|
||||
def _backlight_changed(gauge_idx, new_color, new_on, new_brightness):
|
||||
return (
|
||||
new_color != backlight_color[gauge_idx]
|
||||
@@ -341,7 +348,7 @@ def set_backlight_color(gauge_idx, r, g, b, brightness=None):
|
||||
leds_per_gauge = BACKLIGHT_LEDS_PER_GAUGE + STATUS_LEDS_PER_GAUGE
|
||||
base_idx = gauge_idx * leds_per_gauge
|
||||
for j in range(BACKLIGHT_LEDS_PER_GAUGE):
|
||||
leds_bl[base_idx + j] = (int(g * scale), int(r * scale), int(b * scale))
|
||||
leds_bl[base_idx + j] = _to_pixel(int(r * scale), int(g * scale), int(b * scale))
|
||||
_update_status_leds(gauge_idx)
|
||||
leds_bl.write()
|
||||
_mark_bl_dirty()
|
||||
@@ -361,7 +368,7 @@ def set_backlight_brightness(gauge_idx, brightness):
|
||||
leds_per_gauge = BACKLIGHT_LEDS_PER_GAUGE + STATUS_LEDS_PER_GAUGE
|
||||
base_idx = gauge_idx * leds_per_gauge
|
||||
for j in range(BACKLIGHT_LEDS_PER_GAUGE):
|
||||
leds_bl[base_idx + j] = (int(g * scale), int(r * scale), int(b * scale))
|
||||
leds_bl[base_idx + j] = _to_pixel(int(r * scale), int(g * scale), int(b * scale))
|
||||
_update_status_leds(gauge_idx)
|
||||
leds_bl.write()
|
||||
_mark_bl_dirty()
|
||||
@@ -376,12 +383,12 @@ def _update_status_leds(gauge_idx):
|
||||
green_color = g_cfg["ws2812_green"]
|
||||
|
||||
if status_led_red[gauge_idx]:
|
||||
leds_bl[base_idx] = (red_color[1], red_color[0], red_color[2])
|
||||
leds_bl[base_idx] = _to_pixel(*red_color)
|
||||
else:
|
||||
leds_bl[base_idx] = (0, 0, 0)
|
||||
|
||||
if status_led_green[gauge_idx]:
|
||||
leds_bl[base_idx + 1] = (green_color[1], green_color[0], green_color[2])
|
||||
leds_bl[base_idx + 1] = _to_pixel(*green_color)
|
||||
else:
|
||||
leds_bl[base_idx + 1] = (0, 0, 0)
|
||||
|
||||
@@ -872,11 +879,7 @@ def main():
|
||||
|
||||
client_ref.check_msg()
|
||||
|
||||
pending = []
|
||||
for i, g in enumerate(gauge_objects):
|
||||
delta = g._val_to_step(gauge_targets[i]) - g._current_step
|
||||
steps = max(-5, min(5, delta))
|
||||
pending.append(steps)
|
||||
pending = [g.steps_toward(gauge_targets[i],limit=50) for i, g in enumerate(gauge_objects)]
|
||||
|
||||
moved_any = any(s != 0 for s in pending)
|
||||
if moved_any:
|
||||
|
||||
Reference in New Issue
Block a user