Code cleanup phase 2
This commit is contained in:
@@ -47,6 +47,7 @@
|
|||||||
"pin": 23,
|
"pin": 23,
|
||||||
"num_leds_per_gauge": 3,
|
"num_leds_per_gauge": 3,
|
||||||
"num_status_leds_per_gauge": 2
|
"num_status_leds_per_gauge": 2
|
||||||
|
"ws2812_order": "GRB"
|
||||||
},
|
},
|
||||||
|
|
||||||
"device": {
|
"device": {
|
||||||
|
|||||||
@@ -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:
|
Deploy these files to the ESP32:
|
||||||
gauge.py — stepper driver
|
gauge_vid6008.py — stepper driver
|
||||||
gaugemqtt.py — this file
|
gaugemqttcontinuous.py — this file
|
||||||
umqtt/simple.py — MicroPython built-in
|
umqtt/simple.py — MicroPython built-in
|
||||||
umqtt/robust.py — https://raw.githubusercontent.com/micropython/micropython-lib/master/micropython/umqtt.robust/umqtt/robust.py
|
umqtt/robust.py — https://raw.githubusercontent.com/micropython/micropython-lib/master/micropython/umqtt.robust/umqtt/robust.py
|
||||||
config.json — configuration (see below)
|
config.json — configuration (see below)
|
||||||
|
|
||||||
MQTT topics (all prefixed with mqtt_prefix from config.json):
|
MQTT topics (all prefixed with mqtt_prefix from config.json):
|
||||||
.../set ← HA publishes target value here
|
.../set ← HA publishes target value here
|
||||||
@@ -322,6 +322,19 @@ def _mark_bl_dirty():
|
|||||||
_bl_dirty_since = utime.ticks_ms()
|
_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):
|
def set_backlight_color(gauge_idx, r, g, b, brightness=None):
|
||||||
global backlight_color, backlight_brightness, backlight_on
|
global backlight_color, backlight_brightness, backlight_on
|
||||||
if brightness is None:
|
if brightness is None:
|
||||||
@@ -333,15 +346,7 @@ def set_backlight_color(gauge_idx, r, g, b, brightness=None):
|
|||||||
if brightness > 0:
|
if brightness > 0:
|
||||||
backlight_brightness[gauge_idx] = brightness
|
backlight_brightness[gauge_idx] = brightness
|
||||||
backlight_on[gauge_idx] = new_on
|
backlight_on[gauge_idx] = new_on
|
||||||
|
_apply_backlight(gauge_idx, r, g, b, brightness)
|
||||||
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()
|
|
||||||
|
|
||||||
|
|
||||||
def set_backlight_brightness(gauge_idx, 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_brightness[gauge_idx] = clamped
|
||||||
backlight_on[gauge_idx] = new_on
|
backlight_on[gauge_idx] = new_on
|
||||||
r, g, b = backlight_color[gauge_idx]
|
r, g, b = backlight_color[gauge_idx]
|
||||||
scale = clamped / 100
|
_apply_backlight(gauge_idx, r, g, b, clamped)
|
||||||
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()
|
|
||||||
|
|
||||||
|
|
||||||
def _update_status_leds(gauge_idx):
|
def _update_status_leds(gauge_idx):
|
||||||
|
|||||||
23
ota.py
23
ota.py
@@ -198,18 +198,19 @@ def _fetch_manifest():
|
|||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
r = urequests.get(url, headers=_headers())
|
r = urequests.get(url, headers=_headers())
|
||||||
if r.status_code == 200:
|
try:
|
||||||
data = r.json()
|
if r.status_code == 200:
|
||||||
r.close()
|
data = r.json()
|
||||||
if data.get("content"):
|
if data.get("content"):
|
||||||
import ubinascii
|
import ubinascii
|
||||||
|
|
||||||
content = ubinascii.a2b_base64(data["content"]).decode()
|
content = ubinascii.a2b_base64(data["content"]).decode()
|
||||||
patterns = [line.strip() for line in content.splitlines()]
|
patterns = [line.strip() for line in content.splitlines()]
|
||||||
return [p for p in patterns if p and not p.startswith("#")]
|
return [p for p in patterns if p and not p.startswith("#")]
|
||||||
else:
|
else:
|
||||||
warn(f"Manifest not found at {OTA_MANIFEST}")
|
warn(f"Manifest not found at {OTA_MANIFEST}")
|
||||||
r.close()
|
finally:
|
||||||
|
r.close()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log_err(f"Failed to fetch manifest: {e}")
|
log_err(f"Failed to fetch manifest: {e}")
|
||||||
return None
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user