Several cleanups

This commit is contained in:
2026-04-13 22:10:32 +02:00
parent 1a8b47382c
commit 5c6b3fb295
4 changed files with 19 additions and 13 deletions

View File

@@ -28,5 +28,5 @@
"red_led_entity_name": "Selsyn 1 Red LED", "red_led_entity_name": "Selsyn 1 Red LED",
"green_led_entity_name": "Selsyn 1 Green LED", "green_led_entity_name": "Selsyn 1 Green LED",
"backlight_entity_name": "Selsyn 1 Backlight", "backlight_entity_name": "Selsyn 1 Backlight",
"backlight_unit": "%" "ws2812_order": "GRB"
} }

View File

@@ -128,6 +128,11 @@ class Gauge:
self._pin_step.value(0) self._pin_step.value(0)
utime.sleep_us(delay_us) 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): def get(self):
return self._step_to_val(self._current_step) return self._step_to_val(self._current_step)

View File

@@ -82,6 +82,7 @@ _cfg = _load_config()
DEBUG = _cfg.get("debug", False) DEBUG = _cfg.get("debug", False)
_DEBUG = DEBUG _DEBUG = DEBUG
_WS2812_ORDER = _cfg.get("ws2812_order", "GRB").upper()
WIFI_SSID = _cfg["wifi_ssid"] WIFI_SSID = _cfg["wifi_ssid"]
WIFI_PASSWORD = _cfg["wifi_password"] WIFI_PASSWORD = _cfg["wifi_password"]
@@ -150,7 +151,6 @@ else:
"ws2812_green": tuple(_cfg.get("ws2812_green", [0, 255, 0])), "ws2812_green": tuple(_cfg.get("ws2812_green", [0, 255, 0])),
} }
) )
BL_UNIT = _cfg.get("backlight_unit", "%")
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Gauge initialization # Gauge initialization
@@ -312,6 +312,13 @@ _bl_dirty_since = None
_BL_SAVE_DELAY_MS = 5000 _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): def _backlight_changed(gauge_idx, new_color, new_on, new_brightness):
return ( return (
new_color != backlight_color[gauge_idx] 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 leds_per_gauge = BACKLIGHT_LEDS_PER_GAUGE + STATUS_LEDS_PER_GAUGE
base_idx = gauge_idx * leds_per_gauge base_idx = gauge_idx * leds_per_gauge
for j in range(BACKLIGHT_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) _update_status_leds(gauge_idx)
leds_bl.write() leds_bl.write()
_mark_bl_dirty() _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 leds_per_gauge = BACKLIGHT_LEDS_PER_GAUGE + STATUS_LEDS_PER_GAUGE
base_idx = gauge_idx * leds_per_gauge base_idx = gauge_idx * leds_per_gauge
for j in range(BACKLIGHT_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) _update_status_leds(gauge_idx)
leds_bl.write() leds_bl.write()
_mark_bl_dirty() _mark_bl_dirty()
@@ -376,12 +383,12 @@ def _update_status_leds(gauge_idx):
green_color = g_cfg["ws2812_green"] green_color = g_cfg["ws2812_green"]
if status_led_red[gauge_idx]: 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: else:
leds_bl[base_idx] = (0, 0, 0) leds_bl[base_idx] = (0, 0, 0)
if status_led_green[gauge_idx]: 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: else:
leds_bl[base_idx + 1] = (0, 0, 0) leds_bl[base_idx + 1] = (0, 0, 0)
@@ -872,11 +879,7 @@ def main():
client_ref.check_msg() client_ref.check_msg()
pending = [] pending = [g.steps_toward(gauge_targets[i],limit=50) for i, g in enumerate(gauge_objects)]
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)
moved_any = any(s != 0 for s in pending) moved_any = any(s != 0 for s in pending)
if moved_any: if moved_any:

2
ota.py
View File

@@ -349,8 +349,6 @@ def _fetch_file_list():
break break
else: else:
name = entry["name"] name = entry["name"]
if not name.endswith(".py"):
continue
for p in patterns: for p in patterns:
p = p.rstrip("/") p = p.rstrip("/")
if _match_pattern(name, p) or _match_pattern(entry["path"], p): if _match_pattern(name, p) or _match_pattern(entry["path"], p):