Code cleanup phase 2

This commit is contained in:
2026-04-13 22:35:49 +02:00
parent 44af62d7a5
commit bd07318fe2
3 changed files with 34 additions and 34 deletions

View File

@@ -47,6 +47,7 @@
"pin": 23,
"num_leds_per_gauge": 3,
"num_status_leds_per_gauge": 2
"ws2812_order": "GRB"
},
"device": {

View File

@@ -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):

23
ota.py
View File

@@ -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