diff --git a/config.multi.example.json b/config.multi.example.json index 67a9de2..e1903bf 100644 --- a/config.multi.example.json +++ b/config.multi.example.json @@ -47,6 +47,7 @@ "pin": 23, "num_leds_per_gauge": 3, "num_status_leds_per_gauge": 2 + "ws2812_order": "GRB" }, "device": { diff --git a/gaugemqttcontinuous.py b/gaugemqttcontinuous.py index fd6a736..c63e7b6 100644 --- a/gaugemqttcontinuous.py +++ b/gaugemqttcontinuous.py @@ -1,12 +1,12 @@ """ -gaugemqtt.py — MQTT-based gauge controller for ESP32 / MicroPython +gaugemqttcontinuous.py — MQTT-based gauge controller for ESP32 / MicroPython Deploy these files to the ESP32: - gauge.py — stepper driver - gaugemqtt.py — this file - umqtt/simple.py — MicroPython built-in - umqtt/robust.py — https://raw.githubusercontent.com/micropython/micropython-lib/master/micropython/umqtt.robust/umqtt/robust.py - config.json — configuration (see below) + gauge_vid6008.py — stepper driver + gaugemqttcontinuous.py — this file + umqtt/simple.py — MicroPython built-in + umqtt/robust.py — https://raw.githubusercontent.com/micropython/micropython-lib/master/micropython/umqtt.robust/umqtt/robust.py + config.json — configuration (see below) MQTT topics (all prefixed with mqtt_prefix from config.json): .../set ← HA publishes target value here @@ -322,6 +322,19 @@ def _mark_bl_dirty(): _bl_dirty_since = utime.ticks_ms() +def _apply_backlight(gauge_idx, r, g, b, brightness): + """Write RGB+brightness to the physical LEDs and mark dirty.""" + scale = brightness / 100 + leds_per_gauge = BACKLIGHT_LEDS_PER_GAUGE + STATUS_LEDS_PER_GAUGE + base_idx = gauge_idx * leds_per_gauge + pixel = _to_pixel(int(r * scale), int(g * scale), int(b * scale)) + for j in range(BACKLIGHT_LEDS_PER_GAUGE): + leds_bl[base_idx + j] = pixel + _update_status_leds(gauge_idx) + leds_bl.write() + _mark_bl_dirty() + + def set_backlight_color(gauge_idx, r, g, b, brightness=None): global backlight_color, backlight_brightness, backlight_on if brightness is None: @@ -333,15 +346,7 @@ def set_backlight_color(gauge_idx, r, g, b, brightness=None): if brightness > 0: backlight_brightness[gauge_idx] = brightness backlight_on[gauge_idx] = new_on - - scale = brightness / 100 - 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] = _to_pixel(int(r * scale), int(g * scale), int(b * scale)) - _update_status_leds(gauge_idx) - leds_bl.write() - _mark_bl_dirty() + _apply_backlight(gauge_idx, r, g, b, brightness) def set_backlight_brightness(gauge_idx, brightness): @@ -354,14 +359,7 @@ def set_backlight_brightness(gauge_idx, brightness): backlight_brightness[gauge_idx] = clamped backlight_on[gauge_idx] = new_on r, g, b = backlight_color[gauge_idx] - scale = clamped / 100 - 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] = _to_pixel(int(r * scale), int(g * scale), int(b * scale)) - _update_status_leds(gauge_idx) - leds_bl.write() - _mark_bl_dirty() + _apply_backlight(gauge_idx, r, g, b, clamped) def _update_status_leds(gauge_idx): diff --git a/ota.py b/ota.py index cb12bf1..53c6706 100644 --- a/ota.py +++ b/ota.py @@ -198,18 +198,19 @@ def _fetch_manifest(): ) try: r = urequests.get(url, headers=_headers()) - if r.status_code == 200: - data = r.json() - r.close() - if data.get("content"): - import ubinascii + try: + if r.status_code == 200: + data = r.json() + if data.get("content"): + import ubinascii - content = ubinascii.a2b_base64(data["content"]).decode() - patterns = [line.strip() for line in content.splitlines()] - return [p for p in patterns if p and not p.startswith("#")] - else: - warn(f"Manifest not found at {OTA_MANIFEST}") - r.close() + content = ubinascii.a2b_base64(data["content"]).decode() + patterns = [line.strip() for line in content.splitlines()] + return [p for p in patterns if p and not p.startswith("#")] + else: + warn(f"Manifest not found at {OTA_MANIFEST}") + finally: + r.close() except Exception as e: log_err(f"Failed to fetch manifest: {e}") return None