function d added
This commit is contained in:
@@ -23,6 +23,7 @@ Serial log format: [HH:MM:SS.mmm] LEVEL message
|
|||||||
import network
|
import network
|
||||||
import utime
|
import utime
|
||||||
import ujson
|
import ujson
|
||||||
|
import urandom
|
||||||
from umqtt.robust import MQTTClient
|
from umqtt.robust import MQTTClient
|
||||||
from machine import Pin
|
from machine import Pin
|
||||||
from neopixel import NeoPixel
|
from neopixel import NeoPixel
|
||||||
@@ -209,6 +210,7 @@ T_SET = f"{MQTT_PREFIX}/set"
|
|||||||
T_STATE = f"{MQTT_PREFIX}/state"
|
T_STATE = f"{MQTT_PREFIX}/state"
|
||||||
T_STATUS = f"{MQTT_PREFIX}/status"
|
T_STATUS = f"{MQTT_PREFIX}/status"
|
||||||
T_ZERO = f"{MQTT_PREFIX}/zero"
|
T_ZERO = f"{MQTT_PREFIX}/zero"
|
||||||
|
T_DISCO = f"{MQTT_PREFIX}/disco"
|
||||||
|
|
||||||
T_DISC_GAUGE = f"homeassistant/number/{MQTT_CLIENT_ID}/config"
|
T_DISC_GAUGE = f"homeassistant/number/{MQTT_CLIENT_ID}/config"
|
||||||
T_DISC_RED = f"homeassistant/switch/{MQTT_CLIENT_ID}_red/config"
|
T_DISC_RED = f"homeassistant/switch/{MQTT_CLIENT_ID}_red/config"
|
||||||
@@ -455,6 +457,8 @@ client_ref = None
|
|||||||
_mqtt_connected = False
|
_mqtt_connected = False
|
||||||
_last_mqtt_check = 0
|
_last_mqtt_check = 0
|
||||||
|
|
||||||
|
_disco_end_time = 0
|
||||||
|
|
||||||
|
|
||||||
def _publish(topic, payload, retain=False):
|
def _publish(topic, payload, retain=False):
|
||||||
"""Safely publish MQTT message, returning True on success."""
|
"""Safely publish MQTT message, returning True on success."""
|
||||||
@@ -505,6 +509,12 @@ def on_message(topic, payload):
|
|||||||
info("All gauges zeroed")
|
info("All gauges zeroed")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if topic == T_DISCO:
|
||||||
|
global _disco_end_time
|
||||||
|
_disco_end_time = utime.ticks_ms() + 5000
|
||||||
|
info("Disco mode started")
|
||||||
|
return
|
||||||
|
|
||||||
for i, gt in enumerate(gauge_topics):
|
for i, gt in enumerate(gauge_topics):
|
||||||
if topic == gt["led_red"]:
|
if topic == gt["led_red"]:
|
||||||
state = payload.upper() == "ON"
|
state = payload.upper() == "ON"
|
||||||
@@ -592,6 +602,7 @@ def connect_mqtt():
|
|||||||
client_ref = client
|
client_ref = client
|
||||||
client.subscribe(T_SET)
|
client.subscribe(T_SET)
|
||||||
client.subscribe(T_ZERO)
|
client.subscribe(T_ZERO)
|
||||||
|
client.subscribe(T_DISCO)
|
||||||
for gt in gauge_topics:
|
for gt in gauge_topics:
|
||||||
client.subscribe(gt["set"])
|
client.subscribe(gt["set"])
|
||||||
client.subscribe(gt["zero"])
|
client.subscribe(gt["zero"])
|
||||||
@@ -646,6 +657,7 @@ def check_mqtt():
|
|||||||
client_ref.connect()
|
client_ref.connect()
|
||||||
client_ref.subscribe(T_SET)
|
client_ref.subscribe(T_SET)
|
||||||
client_ref.subscribe(T_ZERO)
|
client_ref.subscribe(T_ZERO)
|
||||||
|
client_ref.subscribe(T_DISCO)
|
||||||
for gt in gauge_topics:
|
for gt in gauge_topics:
|
||||||
client_ref.subscribe(gt["set"])
|
client_ref.subscribe(gt["set"])
|
||||||
client_ref.subscribe(gt["zero"])
|
client_ref.subscribe(gt["zero"])
|
||||||
@@ -844,6 +856,29 @@ def main():
|
|||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
|
||||||
|
if _disco_end_time > 0 and utime.ticks_diff(_disco_end_time, now) <= 0:
|
||||||
|
_disco_end_time = 0
|
||||||
|
set_backlight_brightness(0, backlight_brightness[0])
|
||||||
|
for i in range(num_gauges):
|
||||||
|
set_backlight_color(i, *backlight_color[i], backlight_brightness[i])
|
||||||
|
info("Disco mode ended")
|
||||||
|
elif _disco_end_time > 0:
|
||||||
|
r = urandom.getrandbits(8)
|
||||||
|
g = urandom.getrandbits(8)
|
||||||
|
b = urandom.getrandbits(8)
|
||||||
|
r = max(r, 128)
|
||||||
|
g = max(g, 128)
|
||||||
|
b = max(b, 128)
|
||||||
|
for i in range(num_gauges):
|
||||||
|
leds_per_gauge = BACKLIGHT_LEDS_PER_GAUGE + STATUS_LEDS_PER_GAUGE
|
||||||
|
base_idx = i * leds_per_gauge
|
||||||
|
for j in range(BACKLIGHT_LEDS_PER_GAUGE):
|
||||||
|
leds_bl[base_idx + j] = (g, r, b)
|
||||||
|
leds_bl.write()
|
||||||
|
utime.sleep_ms(200)
|
||||||
|
|
||||||
check_wifi()
|
check_wifi()
|
||||||
|
|
||||||
if not check_mqtt():
|
if not check_mqtt():
|
||||||
@@ -852,8 +887,6 @@ def main():
|
|||||||
|
|
||||||
client_ref.check_msg()
|
client_ref.check_msg()
|
||||||
|
|
||||||
now = utime.ticks_ms()
|
|
||||||
|
|
||||||
moved_any = False
|
moved_any = False
|
||||||
for i, g in enumerate(gauge_objects):
|
for i, g in enumerate(gauge_objects):
|
||||||
current_target = g._val_to_step(gauge_targets[i])
|
current_target = g._val_to_step(gauge_targets[i])
|
||||||
|
|||||||
Reference in New Issue
Block a user