Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6209cb0b2d | |||
| 179f202dfe | |||
| 906922357d |
@@ -155,6 +155,7 @@ BL_UNIT = _cfg.get("backlight_unit", "%")
|
||||
# Gauge initialization
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
info("Initialising gauge objects...")
|
||||
gauge_objects = []
|
||||
for g in gauges:
|
||||
gauge_objects.append(
|
||||
@@ -169,6 +170,7 @@ for g in gauges:
|
||||
info(
|
||||
f"Gauge {g['id']}: {g['name']} pins={g['pins']} mode={g['mode']} range=[{g['min']}, {g['max']}]"
|
||||
)
|
||||
info("Gauge objects done")
|
||||
|
||||
gauge_targets = [g["min"] for g in gauges] # target value per gauge
|
||||
gauge_last_rezero = [utime.ticks_ms() for _ in gauges]
|
||||
@@ -318,6 +320,7 @@ def check_wifi():
|
||||
# LEDs (per gauge)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
info("Initialising LEDs...")
|
||||
num_gauges = len(gauges)
|
||||
leds_red = []
|
||||
leds_green = []
|
||||
@@ -327,7 +330,13 @@ for g in gauges:
|
||||
leds_green.append(Pin(g["green_pin"], Pin.OUT, value=0))
|
||||
|
||||
total_backlight_leds = num_gauges * (BACKLIGHT_LEDS_PER_GAUGE + STATUS_LEDS_PER_GAUGE)
|
||||
leds_bl = NeoPixel(Pin(BACKLIGHT_PIN), total_backlight_leds)
|
||||
info(f"Total backlight LEDs: {total_backlight_leds}")
|
||||
leds_bl = (
|
||||
NeoPixel(Pin(BACKLIGHT_PIN), total_backlight_leds)
|
||||
if total_backlight_leds > 0
|
||||
else None
|
||||
)
|
||||
info("LEDs done")
|
||||
|
||||
backlight_color = [(0, 0, 0) for _ in range(num_gauges)]
|
||||
backlight_brightness = [100 for _ in range(num_gauges)]
|
||||
@@ -339,10 +348,6 @@ status_led_green = [False for _ in range(num_gauges)]
|
||||
_bl_dirty_since = None
|
||||
_BL_SAVE_DELAY_MS = 5000
|
||||
|
||||
backlight_color = [(0, 0, 0) for _ in range(num_gauges)]
|
||||
backlight_brightness = [100 for _ in range(num_gauges)]
|
||||
backlight_on = [False for _ in range(num_gauges)]
|
||||
|
||||
|
||||
def _flush_backlight(client):
|
||||
for i in range(num_gauges):
|
||||
@@ -391,9 +396,11 @@ 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))
|
||||
if leds_bl:
|
||||
leds_bl[base_idx + j] = (int(g * scale), int(r * scale), int(b * scale))
|
||||
_update_status_leds(gauge_idx)
|
||||
leds_bl.write()
|
||||
if leds_bl:
|
||||
leds_bl.write()
|
||||
_mark_bl_dirty()
|
||||
|
||||
|
||||
@@ -411,13 +418,17 @@ 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))
|
||||
if leds_bl:
|
||||
leds_bl[base_idx + j] = (int(g * scale), int(r * scale), int(b * scale))
|
||||
_update_status_leds(gauge_idx)
|
||||
leds_bl.write()
|
||||
if leds_bl:
|
||||
leds_bl.write()
|
||||
_mark_bl_dirty()
|
||||
|
||||
|
||||
def _update_status_leds(gauge_idx):
|
||||
if not leds_bl:
|
||||
return
|
||||
leds_per_gauge = BACKLIGHT_LEDS_PER_GAUGE + STATUS_LEDS_PER_GAUGE
|
||||
base_idx = gauge_idx * leds_per_gauge + BACKLIGHT_LEDS_PER_GAUGE
|
||||
|
||||
@@ -443,7 +454,8 @@ def set_status_led(gauge_idx, led_type, state):
|
||||
elif led_type == "green":
|
||||
status_led_green[gauge_idx] = state
|
||||
_update_status_leds(gauge_idx)
|
||||
leds_bl.write()
|
||||
if leds_bl:
|
||||
leds_bl.write()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -812,21 +824,29 @@ def publish_state(client):
|
||||
|
||||
|
||||
def main():
|
||||
utime.sleep_ms(0)
|
||||
info("=" * 48)
|
||||
info("Gauge MQTT controller starting")
|
||||
info("=" * 48)
|
||||
|
||||
info("Connecting WiFi...")
|
||||
connect_wifi(WIFI_SSID, WIFI_PASSWORD)
|
||||
info("WiFi done")
|
||||
|
||||
info("Zeroing gauges on startup ...")
|
||||
for i, g in enumerate(gauge_objects):
|
||||
g.zero()
|
||||
info(f"Zeroed gauge {i}")
|
||||
info("Zero complete")
|
||||
info("Zero done")
|
||||
|
||||
info("Connecting MQTT...")
|
||||
connect_mqtt()
|
||||
info("MQTT done")
|
||||
|
||||
info("Publishing discovery...")
|
||||
publish_discovery(client_ref)
|
||||
publish_state(client_ref)
|
||||
info("Discovery done")
|
||||
|
||||
info("Entering main loop")
|
||||
info("-" * 48)
|
||||
@@ -843,6 +863,7 @@ def main():
|
||||
last_heartbeat = utime.ticks_ms()
|
||||
|
||||
while True:
|
||||
utime.sleep_ms(0)
|
||||
try:
|
||||
check_wifi()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user