Compare commits
5 Commits
2fcaf2f064
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a768376d0 | |||
| 170a0e05ab | |||
| 4879fcfe90 | |||
| fbebe9f4f9 | |||
| a79c6f4dd3 |
@@ -3,6 +3,9 @@ import gc
|
|||||||
import utime
|
import utime
|
||||||
|
|
||||||
from hello import broadcast_hello_udp
|
from hello import broadcast_hello_udp
|
||||||
|
from wifi_sta import try_reconnect
|
||||||
|
|
||||||
|
_UDP_HELLO_ATTEMPT = 0
|
||||||
|
|
||||||
|
|
||||||
async def presets_loop(presets, wdt):
|
async def presets_loop(presets, wdt):
|
||||||
@@ -21,12 +24,26 @@ async def presets_loop(presets, wdt):
|
|||||||
|
|
||||||
|
|
||||||
async def udp_hello_loop_after_http_ready(sta_if, settings, wdt, runtime_state):
|
async def udp_hello_loop_after_http_ready(sta_if, settings, wdt, runtime_state):
|
||||||
"""Broadcast hello at startup-fast cadence, then slower cadence."""
|
"""UDP hello on cadence; if STA drops, one reconnect campaign per iteration."""
|
||||||
|
global _UDP_HELLO_ATTEMPT
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
started_ms = utime.ticks_ms()
|
started_ms = utime.ticks_ms()
|
||||||
while True:
|
while True:
|
||||||
if runtime_state.hello:
|
try:
|
||||||
print("UDP hello: broadcasting...")
|
wifi_ok = sta_if.isconnected()
|
||||||
|
except Exception:
|
||||||
|
wifi_ok = False
|
||||||
|
if not wifi_ok:
|
||||||
|
ssid = settings.get("ssid") or ""
|
||||||
|
if ssid:
|
||||||
|
try_reconnect(sta_if, ssid, settings.get("password") or "", wdt)
|
||||||
|
try:
|
||||||
|
wifi_ok = sta_if.isconnected()
|
||||||
|
except Exception:
|
||||||
|
wifi_ok = False
|
||||||
|
if wifi_ok and runtime_state.hello:
|
||||||
|
_UDP_HELLO_ATTEMPT += 1
|
||||||
|
print("UDP hello broadcast attempt", _UDP_HELLO_ATTEMPT)
|
||||||
try:
|
try:
|
||||||
broadcast_hello_udp(
|
broadcast_hello_udp(
|
||||||
sta_if,
|
sta_if,
|
||||||
@@ -38,5 +55,5 @@ async def udp_hello_loop_after_http_ready(sta_if, settings, wdt, runtime_state):
|
|||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print("UDP hello broadcast failed:", ex)
|
print("UDP hello broadcast failed:", ex)
|
||||||
elapsed_ms = utime.ticks_diff(utime.ticks_ms(), started_ms)
|
elapsed_ms = utime.ticks_diff(utime.ticks_ms(), started_ms)
|
||||||
interval_s = 5 if elapsed_ms < 60000 else 60
|
interval_s = 10 if elapsed_ms < 120000 else 30
|
||||||
await asyncio.sleep(interval_s)
|
await asyncio.sleep(interval_s)
|
||||||
|
|||||||
@@ -12,8 +12,37 @@ except ImportError:
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def _log_rx(payload) -> None:
|
||||||
|
"""Serial log when led-controller sends a message into ``process_data``."""
|
||||||
|
try:
|
||||||
|
if isinstance(payload, (bytes, bytearray)):
|
||||||
|
n = len(payload)
|
||||||
|
if n == 0:
|
||||||
|
print("rx 0 B")
|
||||||
|
return
|
||||||
|
cap = 160
|
||||||
|
chunk = payload if n <= cap else payload[:cap]
|
||||||
|
try:
|
||||||
|
txt = bytes(chunk).decode("utf-8")
|
||||||
|
except Exception:
|
||||||
|
txt = str(chunk)
|
||||||
|
if n > cap:
|
||||||
|
txt = txt + "..."
|
||||||
|
print("rx", n, "B", txt)
|
||||||
|
else:
|
||||||
|
s = str(payload)
|
||||||
|
cap = 200
|
||||||
|
if len(s) <= cap:
|
||||||
|
print("rx", len(s), "C", s)
|
||||||
|
else:
|
||||||
|
print("rx", len(s), "C", s[:cap] + "...")
|
||||||
|
except Exception:
|
||||||
|
print("rx (logging failed)")
|
||||||
|
|
||||||
|
|
||||||
def process_data(payload, settings, presets, controller_ip=None):
|
def process_data(payload, settings, presets, controller_ip=None):
|
||||||
"""Read one controller message; binary v1 envelope or JSON v1, then apply fields."""
|
"""Read one controller message; binary v1 envelope or JSON v1, then apply fields."""
|
||||||
|
_log_rx(payload)
|
||||||
data = None
|
data = None
|
||||||
if isinstance(payload, (bytes, bytearray)):
|
if isinstance(payload, (bytes, bytearray)):
|
||||||
data = parse_binary_envelope(payload)
|
data = parse_binary_envelope(payload)
|
||||||
@@ -27,9 +56,10 @@ def process_data(payload, settings, presets, controller_ip=None):
|
|||||||
data = json.loads(payload)
|
data = json.loads(payload)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
return
|
return
|
||||||
print(payload)
|
|
||||||
if data.get("v", "") != "1":
|
if data.get("v", "") != "1":
|
||||||
return
|
return
|
||||||
|
if "device_config" in data:
|
||||||
|
apply_device_config(data, settings, presets)
|
||||||
if "b" in data:
|
if "b" in data:
|
||||||
apply_brightness(data, settings, presets)
|
apply_brightness(data, settings, presets)
|
||||||
if "presets" in data:
|
if "presets" in data:
|
||||||
@@ -48,6 +78,88 @@ def process_data(payload, settings, presets, controller_ip=None):
|
|||||||
presets.save()
|
presets.save()
|
||||||
if "save" in data and "b" in data:
|
if "save" in data and "b" in data:
|
||||||
settings.save()
|
settings.save()
|
||||||
|
if "save" in data and "device_config" in data:
|
||||||
|
settings.save()
|
||||||
|
|
||||||
|
|
||||||
|
_VALID_DEVICE_COLOR_ORDERS = frozenset({"rgb", "rbg", "grb", "gbr", "brg", "bgr"})
|
||||||
|
_STARTUP_MODES = frozenset({"default", "last", "off"})
|
||||||
|
_MAX_DEVICE_LEDS = 2048
|
||||||
|
|
||||||
|
|
||||||
|
def apply_startup_pattern(settings, presets):
|
||||||
|
"""Apply power-on behaviour from ``startup_mode`` (default / last / off)."""
|
||||||
|
mode = str(settings.get("startup_mode", "default")).lower().strip()
|
||||||
|
if mode not in _STARTUP_MODES:
|
||||||
|
mode = "default"
|
||||||
|
if mode == "off":
|
||||||
|
if presets.select("off"):
|
||||||
|
return
|
||||||
|
presets.fill((0, 0, 0))
|
||||||
|
return
|
||||||
|
if mode == "last":
|
||||||
|
lp = settings.get("last_preset") or ""
|
||||||
|
if isinstance(lp, str) and lp.strip() and lp.strip() in presets.presets:
|
||||||
|
if presets.select(lp.strip()):
|
||||||
|
return
|
||||||
|
dp = settings.get("default", "")
|
||||||
|
if dp and dp in presets.presets:
|
||||||
|
if not presets.select(dp):
|
||||||
|
print("Startup preset failed (invalid pattern?):", dp)
|
||||||
|
|
||||||
|
|
||||||
|
def apply_device_config(data, settings, presets):
|
||||||
|
"""Apply fields from v1 ``device_config``; reload presets when strip length or colour order changes."""
|
||||||
|
dc = data.get("device_config")
|
||||||
|
if not isinstance(dc, dict):
|
||||||
|
return
|
||||||
|
strip_changed = False
|
||||||
|
meta_changed = False
|
||||||
|
if "name" in dc:
|
||||||
|
n = dc["name"]
|
||||||
|
if isinstance(n, str) and n.strip():
|
||||||
|
settings["name"] = n.strip()
|
||||||
|
meta_changed = True
|
||||||
|
if "num_leds" in dc:
|
||||||
|
try:
|
||||||
|
n = int(dc["num_leds"])
|
||||||
|
if 1 <= n <= _MAX_DEVICE_LEDS:
|
||||||
|
settings["num_leds"] = n
|
||||||
|
presets.update_num_leds(settings["led_pin"], n)
|
||||||
|
strip_changed = True
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
pass
|
||||||
|
if "color_order" in dc:
|
||||||
|
co = str(dc["color_order"]).lower().strip()
|
||||||
|
if co in _VALID_DEVICE_COLOR_ORDERS:
|
||||||
|
settings["color_order"] = co
|
||||||
|
settings.color_order = settings.get_color_order(co)
|
||||||
|
strip_changed = True
|
||||||
|
if "startup_mode" in dc:
|
||||||
|
sm = str(dc["startup_mode"]).lower().strip()
|
||||||
|
if sm in _STARTUP_MODES:
|
||||||
|
settings["startup_mode"] = sm
|
||||||
|
meta_changed = True
|
||||||
|
if not strip_changed and not meta_changed:
|
||||||
|
return
|
||||||
|
if strip_changed:
|
||||||
|
prev = presets.selected
|
||||||
|
try:
|
||||||
|
presets.load(settings)
|
||||||
|
except Exception as e:
|
||||||
|
print("device_config: presets.load failed:", e)
|
||||||
|
if prev and prev in presets.presets:
|
||||||
|
presets.select(prev)
|
||||||
|
elif settings.get("default") and settings["default"] in presets.presets:
|
||||||
|
presets.select(settings["default"])
|
||||||
|
|
||||||
|
|
||||||
|
def record_last_preset(settings, preset_name):
|
||||||
|
"""Persist the last selected preset id (single entry in flash)."""
|
||||||
|
if not isinstance(preset_name, str) or not preset_name:
|
||||||
|
return
|
||||||
|
settings["last_preset"] = preset_name.strip()
|
||||||
|
settings.save()
|
||||||
|
|
||||||
|
|
||||||
def apply_brightness(data, settings, presets):
|
def apply_brightness(data, settings, presets):
|
||||||
@@ -71,8 +183,14 @@ def apply_presets(data, settings, presets):
|
|||||||
)
|
)
|
||||||
except (TypeError, ValueError, KeyError):
|
except (TypeError, ValueError, KeyError):
|
||||||
continue
|
continue
|
||||||
|
if "bg" in preset_data:
|
||||||
|
try:
|
||||||
|
bg_color = convert_and_reorder_colors([preset_data["bg"]], settings)
|
||||||
|
if bg_color:
|
||||||
|
preset_data["bg"] = bg_color[0]
|
||||||
|
except (TypeError, ValueError, KeyError):
|
||||||
|
pass
|
||||||
presets.edit(id, preset_data)
|
presets.edit(id, preset_data)
|
||||||
print(f"Edited preset {id}: {preset_data.get('name', '')}")
|
|
||||||
|
|
||||||
|
|
||||||
def apply_select(data, settings, presets):
|
def apply_select(data, settings, presets):
|
||||||
@@ -83,7 +201,8 @@ def apply_select(data, settings, presets):
|
|||||||
return
|
return
|
||||||
preset_name = select_list[0]
|
preset_name = select_list[0]
|
||||||
step = select_list[1] if len(select_list) > 1 else None
|
step = select_list[1] if len(select_list) > 1 else None
|
||||||
presets.select(preset_name, step=step)
|
if presets.select(preset_name, step=step):
|
||||||
|
record_last_preset(settings, preset_name)
|
||||||
|
|
||||||
|
|
||||||
def apply_clear_presets(data, presets):
|
def apply_clear_presets(data, presets):
|
||||||
@@ -99,7 +218,6 @@ def apply_clear_presets(data, presets):
|
|||||||
if not should_clear:
|
if not should_clear:
|
||||||
return
|
return
|
||||||
presets.delete_all()
|
presets.delete_all()
|
||||||
print("Cleared all presets.")
|
|
||||||
|
|
||||||
|
|
||||||
def apply_default(data, settings, presets):
|
def apply_default(data, settings, presets):
|
||||||
@@ -244,8 +362,5 @@ def apply_patterns_ota(data, presets, controller_ip=None):
|
|||||||
updated += 1
|
updated += 1
|
||||||
if updated > 0:
|
if updated > 0:
|
||||||
presets.reload_patterns()
|
presets.reload_patterns()
|
||||||
print("patterns_ota: updated", updated, "pattern file(s)")
|
|
||||||
else:
|
|
||||||
print("patterns_ota: no valid files downloaded")
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("patterns_ota failed:", e)
|
print("patterns_ota failed:", e)
|
||||||
|
|||||||
23
src/hello.py
23
src/hello.py
@@ -92,7 +92,6 @@ def broadcast_hello_udp(
|
|||||||
"""
|
"""
|
||||||
ip, mask, _gw, _dns = sta.ifconfig()
|
ip, mask, _gw, _dns = sta.ifconfig()
|
||||||
msg = pack_hello_line(sta, device_name)
|
msg = pack_hello_line(sta, device_name)
|
||||||
print("hello:", msg)
|
|
||||||
|
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
try:
|
try:
|
||||||
@@ -121,11 +120,9 @@ def broadcast_hello_udp(
|
|||||||
for dest_ip, dest_port in targets:
|
for dest_ip, dest_port in targets:
|
||||||
if wdt is not None:
|
if wdt is not None:
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
label = "%s:%s" % (dest_ip, dest_port)
|
|
||||||
target = (dest_ip, dest_port)
|
target = (dest_ip, dest_port)
|
||||||
try:
|
try:
|
||||||
sock.sendto(msg, target)
|
sock.sendto(msg, target)
|
||||||
print("sent hello ->", target)
|
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print("sendto failed:", e)
|
print("sendto failed:", e)
|
||||||
continue
|
continue
|
||||||
@@ -134,20 +131,12 @@ def broadcast_hello_udp(
|
|||||||
if wdt is not None:
|
if wdt is not None:
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
try:
|
try:
|
||||||
data, addr = sock.recvfrom(2048)
|
_data, addr = sock.recvfrom(2048)
|
||||||
print("reply from", addr, ":", data)
|
|
||||||
remote_ip = addr[0]
|
remote_ip = addr[0]
|
||||||
if data != msg:
|
|
||||||
print("(warning: reply payload differs from hello; still using source IP.)")
|
|
||||||
discovered = remote_ip
|
discovered = remote_ip
|
||||||
print("Discovered controller at", remote_ip)
|
|
||||||
break
|
break
|
||||||
except OSError as e:
|
except OSError:
|
||||||
print("recv (no reply):", e, "via", label)
|
pass
|
||||||
if dest_ip == "255.255.255.255":
|
|
||||||
print(
|
|
||||||
"(hint: many APs drop Wi-Fi client broadcast; try wired server or AP without client isolation.)"
|
|
||||||
)
|
|
||||||
|
|
||||||
sock.close()
|
sock.close()
|
||||||
return discovered
|
return discovered
|
||||||
@@ -171,18 +160,12 @@ def discover_controller_udp(device_name="", wdt=None):
|
|||||||
print("hello: STA has no IP address.")
|
print("hello: STA has no IP address.")
|
||||||
raise SystemExit(1)
|
raise SystemExit(1)
|
||||||
|
|
||||||
print("STA IP:", ip, "mask:", mask)
|
|
||||||
|
|
||||||
discovered = broadcast_hello_udp(
|
discovered = broadcast_hello_udp(
|
||||||
sta,
|
sta,
|
||||||
device_name,
|
device_name,
|
||||||
wait_reply=True,
|
wait_reply=True,
|
||||||
wdt=wdt,
|
wdt=wdt,
|
||||||
)
|
)
|
||||||
if discovered:
|
|
||||||
print("discover done; controller =", repr(discovered))
|
|
||||||
else:
|
|
||||||
print("discover done; controller not found")
|
|
||||||
return discovered
|
return discovered
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ def register_routes(app, settings, presets, runtime_state):
|
|||||||
@app.route("/ws")
|
@app.route("/ws")
|
||||||
@with_websocket
|
@with_websocket
|
||||||
async def ws_handler(request, ws):
|
async def ws_handler(request, ws):
|
||||||
print("WS client connected")
|
|
||||||
runtime_state.ws_connected()
|
runtime_state.ws_connected()
|
||||||
controller_ip = None
|
controller_ip = None
|
||||||
try:
|
try:
|
||||||
@@ -34,15 +33,11 @@ def register_routes(app, settings, presets, runtime_state):
|
|||||||
controller_ip = client_addr
|
controller_ip = client_addr
|
||||||
except Exception:
|
except Exception:
|
||||||
controller_ip = None
|
controller_ip = None
|
||||||
print("WS controller_ip:", controller_ip)
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
data = await ws.receive()
|
data = await ws.receive()
|
||||||
if not data:
|
if not data:
|
||||||
print("WS client disconnected (closed)")
|
|
||||||
break
|
break
|
||||||
print("WS recv bytes:", len(data) if isinstance(data, (bytes, bytearray)) else len(str(data)))
|
|
||||||
print(data)
|
|
||||||
process_data(data, settings, presets, controller_ip=controller_ip)
|
process_data(data, settings, presets, controller_ip=controller_ip)
|
||||||
except WebSocketError as e:
|
except WebSocketError as e:
|
||||||
print("WS client disconnected:", e)
|
print("WS client disconnected:", e)
|
||||||
@@ -50,12 +45,6 @@ def register_routes(app, settings, presets, runtime_state):
|
|||||||
print("WS client dropped (OSError):", e)
|
print("WS client dropped (OSError):", e)
|
||||||
finally:
|
finally:
|
||||||
runtime_state.ws_disconnected()
|
runtime_state.ws_disconnected()
|
||||||
print(
|
|
||||||
"WS client disconnected: hello=",
|
|
||||||
runtime_state.hello,
|
|
||||||
"ws_client_count=",
|
|
||||||
runtime_state.ws_client_count,
|
|
||||||
)
|
|
||||||
|
|
||||||
@app.post("/patterns/upload")
|
@app.post("/patterns/upload")
|
||||||
async def upload_pattern(request):
|
async def upload_pattern(request):
|
||||||
@@ -63,19 +52,15 @@ def register_routes(app, settings, presets, runtime_state):
|
|||||||
raw_name = request.args.get("name")
|
raw_name = request.args.get("name")
|
||||||
reload_raw = request.args.get("reload", "1")
|
reload_raw = request.args.get("reload", "1")
|
||||||
reload_patterns = str(reload_raw).strip().lower() not in ("0", "false", "no", "off")
|
reload_patterns = str(reload_raw).strip().lower() not in ("0", "false", "no", "off")
|
||||||
print("patterns/upload request:", {"name": raw_name, "reload": reload_patterns})
|
|
||||||
|
|
||||||
if not isinstance(raw_name, str) or not raw_name.strip():
|
if not isinstance(raw_name, str) or not raw_name.strip():
|
||||||
return json.dumps({"error": "name is required"}), 400, {"Content-Type": "application/json"}
|
return json.dumps({"error": "name is required"}), 400, {"Content-Type": "application/json"}
|
||||||
body = request.body
|
body = request.body
|
||||||
if not isinstance(body, (bytes, bytearray)) or not body:
|
if not isinstance(body, (bytes, bytearray)) or not body:
|
||||||
print("patterns/upload rejected: empty body")
|
|
||||||
return json.dumps({"error": "code is required"}), 400, {"Content-Type": "application/json"}
|
return json.dumps({"error": "code is required"}), 400, {"Content-Type": "application/json"}
|
||||||
print("patterns/upload body_bytes:", len(body))
|
|
||||||
try:
|
try:
|
||||||
code = body.decode("utf-8")
|
code = body.decode("utf-8")
|
||||||
except UnicodeError:
|
except UnicodeError:
|
||||||
print("patterns/upload rejected: body not utf-8")
|
|
||||||
return json.dumps({"error": "body must be utf-8 text"}), 400, {"Content-Type": "application/json"}
|
return json.dumps({"error": "body must be utf-8 text"}), 400, {"Content-Type": "application/json"}
|
||||||
if not code.strip():
|
if not code.strip():
|
||||||
return json.dumps({"error": "code is required"}), 400, {"Content-Type": "application/json"}
|
return json.dumps({"error": "code is required"}), 400, {"Content-Type": "application/json"}
|
||||||
@@ -93,16 +78,13 @@ def register_routes(app, settings, presets, runtime_state):
|
|||||||
|
|
||||||
path = "patterns/" + name
|
path = "patterns/" + name
|
||||||
try:
|
try:
|
||||||
print("patterns/upload writing:", path)
|
|
||||||
with open(path, "w") as f:
|
with open(path, "w") as f:
|
||||||
f.write(code)
|
f.write(code)
|
||||||
if reload_patterns:
|
if reload_patterns:
|
||||||
print("patterns/upload reloading patterns")
|
|
||||||
presets.reload_patterns()
|
presets.reload_patterns()
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print("patterns/upload failed:", e)
|
print("patterns/upload failed:", e)
|
||||||
return json.dumps({"error": str(e)}), 500, {"Content-Type": "application/json"}
|
return json.dumps({"error": str(e)}), 500, {"Content-Type": "application/json"}
|
||||||
print("patterns/upload success:", {"name": name, "reloaded": reload_patterns})
|
|
||||||
|
|
||||||
return json.dumps(
|
return json.dumps(
|
||||||
{
|
{
|
||||||
|
|||||||
88
src/main.py
88
src/main.py
@@ -8,38 +8,34 @@ import gc
|
|||||||
from microdot import Microdot
|
from microdot import Microdot
|
||||||
from microdot.websocket import WebSocketError, with_websocket
|
from microdot.websocket import WebSocketError, with_websocket
|
||||||
from presets import Presets
|
from presets import Presets
|
||||||
from controller_messages import process_data
|
from controller_messages import apply_startup_pattern, process_data
|
||||||
from hello import broadcast_hello_udp
|
from runtime_state import RuntimeState
|
||||||
|
from background_tasks import udp_hello_loop_after_http_ready
|
||||||
|
from wifi_sta import connect_until_up
|
||||||
try:
|
try:
|
||||||
import uos as os
|
import uos as os
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
wdt = machine.WDT(timeout=10000)
|
||||||
|
wdt.feed()
|
||||||
|
|
||||||
machine.freq(160000000)
|
machine.freq(160000000)
|
||||||
|
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
print(settings)
|
|
||||||
|
|
||||||
wdt = machine.WDT(timeout=10000)
|
|
||||||
wdt.feed()
|
|
||||||
|
|
||||||
gc.collect()
|
gc.collect()
|
||||||
print("mem before presets:", {"free": gc.mem_free(), "alloc": gc.mem_alloc()})
|
|
||||||
|
|
||||||
presets = Presets(settings["led_pin"], settings["num_leds"])
|
presets = Presets(settings["led_pin"], settings["num_leds"])
|
||||||
presets.load(settings)
|
presets.load(settings)
|
||||||
presets.b = settings.get("brightness", 255)
|
presets.b = settings.get("brightness", 255)
|
||||||
presets.debug = bool(settings.get("debug", False))
|
presets.debug = bool(settings.get("debug", False))
|
||||||
gc.collect()
|
gc.collect()
|
||||||
print("mem after presets:", {"free": gc.mem_free(), "alloc": gc.mem_alloc()})
|
|
||||||
|
|
||||||
default_preset = settings.get("default", "")
|
apply_startup_pattern(settings, presets)
|
||||||
if default_preset and default_preset in presets.presets:
|
|
||||||
if presets.select(default_preset):
|
|
||||||
print(f"Selected startup preset: {default_preset}")
|
|
||||||
else:
|
|
||||||
print("Startup preset failed (invalid pattern?):", default_preset)
|
|
||||||
|
|
||||||
# On ESP32-C3, soft reboots can leave Wi-Fi driver state allocated.
|
# On ESP32-C3, soft reboots can leave Wi-Fi driver state allocated.
|
||||||
# Reset both interfaces and collect before bringing STA up.
|
# Reset both interfaces and collect before bringing STA up.
|
||||||
@@ -52,13 +48,24 @@ utime.sleep_ms(100)
|
|||||||
gc.collect()
|
gc.collect()
|
||||||
sta_if.active(True)
|
sta_if.active(True)
|
||||||
sta_if.config(pm=network.WLAN.PM_NONE)
|
sta_if.config(pm=network.WLAN.PM_NONE)
|
||||||
sta_if.connect(settings["ssid"], settings["password"])
|
_boot_ssid = settings.get("ssid") or ""
|
||||||
while not sta_if.isconnected():
|
if _boot_ssid:
|
||||||
print("Connecting")
|
connect_until_up(sta_if, _boot_ssid, settings.get("password") or "", wdt)
|
||||||
utime.sleep(1)
|
|
||||||
wdt.feed()
|
|
||||||
|
|
||||||
print(sta_if.ifconfig())
|
|
||||||
|
def _print_network_ips(controller_ip=None):
|
||||||
|
"""Always log STA address and led-controller (WS client) address when known."""
|
||||||
|
try:
|
||||||
|
led_ip = sta_if.ifconfig()[0]
|
||||||
|
except Exception:
|
||||||
|
led_ip = "?"
|
||||||
|
ctrl = controller_ip if controller_ip else "(not connected)"
|
||||||
|
print("led-driver IP:", led_ip, " led-controller IP:", ctrl)
|
||||||
|
|
||||||
|
|
||||||
|
_print_network_ips()
|
||||||
|
|
||||||
|
runtime_state = RuntimeState()
|
||||||
|
|
||||||
app = Microdot()
|
app = Microdot()
|
||||||
|
|
||||||
@@ -76,7 +83,7 @@ def _safe_pattern_filename(name):
|
|||||||
@app.route("/ws")
|
@app.route("/ws")
|
||||||
@with_websocket
|
@with_websocket
|
||||||
async def ws_handler(request, ws):
|
async def ws_handler(request, ws):
|
||||||
print("WS client connected")
|
runtime_state.ws_connected()
|
||||||
controller_ip = None
|
controller_ip = None
|
||||||
try:
|
try:
|
||||||
client_addr = getattr(request, "client_addr", None)
|
client_addr = getattr(request, "client_addr", None)
|
||||||
@@ -86,20 +93,19 @@ async def ws_handler(request, ws):
|
|||||||
controller_ip = client_addr
|
controller_ip = client_addr
|
||||||
except Exception:
|
except Exception:
|
||||||
controller_ip = None
|
controller_ip = None
|
||||||
print("WS controller_ip:", controller_ip)
|
_print_network_ips(controller_ip)
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
data = await ws.receive()
|
data = await ws.receive()
|
||||||
if not data:
|
if not data:
|
||||||
print("WS client disconnected (closed)")
|
|
||||||
break
|
break
|
||||||
print("WS recv bytes:", len(data) if isinstance(data, (bytes, bytearray)) else len(str(data)))
|
|
||||||
print(data)
|
|
||||||
process_data(data, settings, presets, controller_ip=controller_ip)
|
process_data(data, settings, presets, controller_ip=controller_ip)
|
||||||
except WebSocketError as e:
|
except WebSocketError as e:
|
||||||
print("WS client disconnected:", e)
|
print("WS client disconnected:", e)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print("WS client dropped (OSError):", e)
|
print("WS client dropped (OSError):", e)
|
||||||
|
finally:
|
||||||
|
runtime_state.ws_disconnected()
|
||||||
|
|
||||||
|
|
||||||
@app.post("/patterns/upload")
|
@app.post("/patterns/upload")
|
||||||
@@ -108,7 +114,6 @@ async def upload_pattern(request):
|
|||||||
raw_name = request.args.get("name")
|
raw_name = request.args.get("name")
|
||||||
reload_raw = request.args.get("reload", "1")
|
reload_raw = request.args.get("reload", "1")
|
||||||
reload_patterns = str(reload_raw).strip().lower() not in ("0", "false", "no", "off")
|
reload_patterns = str(reload_raw).strip().lower() not in ("0", "false", "no", "off")
|
||||||
print("patterns/upload request:", {"name": raw_name, "reload": reload_patterns})
|
|
||||||
|
|
||||||
if not isinstance(raw_name, str) or not raw_name.strip():
|
if not isinstance(raw_name, str) or not raw_name.strip():
|
||||||
return json.dumps({"error": "name is required"}), 400, {
|
return json.dumps({"error": "name is required"}), 400, {
|
||||||
@@ -116,15 +121,12 @@ async def upload_pattern(request):
|
|||||||
}
|
}
|
||||||
body = request.body
|
body = request.body
|
||||||
if not isinstance(body, (bytes, bytearray)) or not body:
|
if not isinstance(body, (bytes, bytearray)) or not body:
|
||||||
print("patterns/upload rejected: empty body")
|
|
||||||
return json.dumps({"error": "code is required"}), 400, {
|
return json.dumps({"error": "code is required"}), 400, {
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
}
|
}
|
||||||
print("patterns/upload body_bytes:", len(body))
|
|
||||||
try:
|
try:
|
||||||
code = body.decode("utf-8")
|
code = body.decode("utf-8")
|
||||||
except UnicodeError:
|
except UnicodeError:
|
||||||
print("patterns/upload rejected: body not utf-8")
|
|
||||||
return json.dumps({"error": "body must be utf-8 text"}), 400, {
|
return json.dumps({"error": "body must be utf-8 text"}), 400, {
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
}
|
}
|
||||||
@@ -148,18 +150,15 @@ async def upload_pattern(request):
|
|||||||
|
|
||||||
path = "patterns/" + name
|
path = "patterns/" + name
|
||||||
try:
|
try:
|
||||||
print("patterns/upload writing:", path)
|
|
||||||
with open(path, "w") as f:
|
with open(path, "w") as f:
|
||||||
f.write(code)
|
f.write(code)
|
||||||
if reload_patterns:
|
if reload_patterns:
|
||||||
print("patterns/upload reloading patterns")
|
|
||||||
presets.reload_patterns()
|
presets.reload_patterns()
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print("patterns/upload failed:", e)
|
print("patterns/upload failed:", e)
|
||||||
return json.dumps({"error": str(e)}), 500, {
|
return json.dumps({"error": str(e)}), 500, {
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
}
|
}
|
||||||
print("patterns/upload success:", {"name": name, "reloaded": reload_patterns})
|
|
||||||
|
|
||||||
return json.dumps({
|
return json.dumps({
|
||||||
"message": "pattern uploaded",
|
"message": "pattern uploaded",
|
||||||
@@ -173,35 +172,14 @@ async def presets_loop():
|
|||||||
while True:
|
while True:
|
||||||
presets.tick()
|
presets.tick()
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
if bool(getattr(presets, "debug", False)):
|
|
||||||
now = utime.ticks_ms()
|
|
||||||
if utime.ticks_diff(now, last_mem_log) >= 5000:
|
|
||||||
gc.collect()
|
|
||||||
print("mem runtime:", {"free": gc.mem_free(), "alloc": gc.mem_alloc()})
|
|
||||||
last_mem_log = now
|
|
||||||
# tick() does not await; yield so UDP hello and HTTP/WebSocket can run.
|
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
|
|
||||||
|
|
||||||
async def _udp_hello_after_http_ready():
|
|
||||||
"""Hello must run after the HTTP server binds, or discovery clients time out on /ws."""
|
|
||||||
await asyncio.sleep(1)
|
|
||||||
print("UDP hello: broadcasting…")
|
|
||||||
try:
|
|
||||||
broadcast_hello_udp(
|
|
||||||
sta_if,
|
|
||||||
settings.get("name", ""),
|
|
||||||
wait_reply=False,
|
|
||||||
wdt=wdt,
|
|
||||||
dual_destinations=True,
|
|
||||||
)
|
|
||||||
except Exception as ex:
|
|
||||||
print("UDP hello broadcast failed:", ex)
|
|
||||||
|
|
||||||
|
|
||||||
async def main(port=80):
|
async def main(port=80):
|
||||||
asyncio.create_task(presets_loop())
|
asyncio.create_task(presets_loop())
|
||||||
asyncio.create_task(_udp_hello_after_http_ready())
|
asyncio.create_task(
|
||||||
|
udp_hello_loop_after_http_ready(sta_if, settings, wdt, runtime_state)
|
||||||
|
)
|
||||||
await app.start_server(host="0.0.0.0", port=port)
|
await app.start_server(host="0.0.0.0", port=port)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,15 +7,23 @@ class BarGraph:
|
|||||||
|
|
||||||
def run(self, preset):
|
def run(self, preset):
|
||||||
colors = preset.c if preset.c else [(0, 255, 0), (255, 80, 0)]
|
colors = preset.c if preset.c else [(0, 255, 0), (255, 80, 0)]
|
||||||
|
last_update = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
|
delay_ms = max(1, int(preset.d))
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(now, last_update) >= delay_ms:
|
||||||
level = max(0, min(100, int(preset.n1) if int(preset.n1) >= 0 else 50))
|
level = max(0, min(100, int(preset.n1) if int(preset.n1) >= 0 else 50))
|
||||||
target = (self.driver.num_leds * level) // 100
|
target = (self.driver.num_leds * level) // 100
|
||||||
lit = self.driver.apply_brightness(colors[0], preset.b)
|
lit = self.driver.apply_brightness(colors[0], preset.b)
|
||||||
unlit = self.driver.apply_brightness(colors[1] if len(colors) > 1 else (0, 0, 0), preset.b)
|
unlit = self.driver.apply_brightness(
|
||||||
while True:
|
preset.background_or(colors),
|
||||||
|
preset.b,
|
||||||
|
)
|
||||||
for i in range(self.driver.num_leds):
|
for i in range(self.driver.num_leds):
|
||||||
self.driver.n[i] = lit if i < target else unlit
|
self.driver.n[i] = lit if i < target else unlit
|
||||||
self.driver.n.write()
|
self.driver.n.write()
|
||||||
yield
|
last_update = utime.ticks_add(last_update, delay_ms)
|
||||||
if not preset.a:
|
if not preset.a:
|
||||||
|
yield
|
||||||
return
|
return
|
||||||
utime.sleep_ms(max(1, int(preset.d)))
|
yield
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ class Blink:
|
|||||||
"""Blink pattern: toggles LEDs on/off using preset delay, cycling through colors."""
|
"""Blink pattern: toggles LEDs on/off using preset delay, cycling through colors."""
|
||||||
# Use provided colors, or default to white if none
|
# Use provided colors, or default to white if none
|
||||||
colors = preset.c if preset.c else [(255, 255, 255)]
|
colors = preset.c if preset.c else [(255, 255, 255)]
|
||||||
|
bg_color = self.driver.apply_brightness(preset.background_or(colors), preset.b)
|
||||||
color_index = 0
|
color_index = 0
|
||||||
state = True # True = on, False = off
|
state = True # True = on, False = off
|
||||||
last_update = utime.ticks_ms()
|
last_update = utime.ticks_ms()
|
||||||
@@ -25,8 +26,8 @@ class Blink:
|
|||||||
# Advance to next color for the next "on" phase
|
# Advance to next color for the next "on" phase
|
||||||
color_index += 1
|
color_index += 1
|
||||||
else:
|
else:
|
||||||
# "Off" phase: turn all LEDs off
|
# Inactive phase uses the preset background color.
|
||||||
self.driver.fill((0, 0, 0))
|
self.driver.fill(bg_color)
|
||||||
state = not state
|
state = not state
|
||||||
last_update = utime.ticks_add(last_update, delay_ms)
|
last_update = utime.ticks_add(last_update, delay_ms)
|
||||||
# Yield once per tick so other logic can run
|
# Yield once per tick so other logic can run
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ class Chase:
|
|||||||
|
|
||||||
color0 = self.driver.apply_brightness(color0, preset.b)
|
color0 = self.driver.apply_brightness(color0, preset.b)
|
||||||
color1 = self.driver.apply_brightness(color1, preset.b)
|
color1 = self.driver.apply_brightness(color1, preset.b)
|
||||||
|
bg_color = self.driver.apply_brightness(preset.background_or(colors), preset.b)
|
||||||
|
|
||||||
n1 = max(1, int(preset.n1)) # LEDs of color 0
|
n1 = max(1, int(preset.n1)) # LEDs of color 0
|
||||||
n2 = max(1, int(preset.n2)) # LEDs of color 1
|
n2 = max(1, int(preset.n2)) # LEDs of color 1
|
||||||
@@ -35,7 +36,7 @@ class Chase:
|
|||||||
segment_length = n1 + n2
|
segment_length = n1 + n2
|
||||||
|
|
||||||
# Calculate position from step_count
|
# Calculate position from step_count
|
||||||
step_count = self.driver.step
|
step_count = int(self.driver.step) % 2
|
||||||
# Position alternates: step 0 adds n3, step 1 adds n4, step 2 adds n3, etc.
|
# Position alternates: step 0 adds n3, step 1 adds n4, step 2 adds n3, etc.
|
||||||
if step_count % 2 == 0:
|
if step_count % 2 == 0:
|
||||||
# Even steps: (step_count//2) pairs of (n3+n4) plus one extra n3
|
# Even steps: (step_count//2) pairs of (n3+n4) plus one extra n3
|
||||||
@@ -53,7 +54,7 @@ class Chase:
|
|||||||
# If auto is False, run a single step and then stop
|
# If auto is False, run a single step and then stop
|
||||||
if not preset.a:
|
if not preset.a:
|
||||||
# Clear all LEDs
|
# Clear all LEDs
|
||||||
self.driver.n.fill((0, 0, 0))
|
self.driver.n.fill(bg_color)
|
||||||
|
|
||||||
# Draw repeating pattern starting at position
|
# Draw repeating pattern starting at position
|
||||||
for i in range(self.driver.num_leds):
|
for i in range(self.driver.num_leds):
|
||||||
@@ -69,9 +70,10 @@ class Chase:
|
|||||||
self.driver.n[i] = color1
|
self.driver.n[i] = color1
|
||||||
|
|
||||||
self.driver.n.write()
|
self.driver.n.write()
|
||||||
|
print("[chase] step", step_count)
|
||||||
|
|
||||||
# Increment step for next beat
|
# Increment step for next beat
|
||||||
self.driver.step = step_count + 1
|
self.driver.step = (step_count + 1) % 2
|
||||||
|
|
||||||
# Allow tick() to advance the generator once
|
# Allow tick() to advance the generator once
|
||||||
yield
|
yield
|
||||||
@@ -98,7 +100,7 @@ class Chase:
|
|||||||
position += max_pos
|
position += max_pos
|
||||||
|
|
||||||
# Clear all LEDs
|
# Clear all LEDs
|
||||||
self.driver.n.fill((0, 0, 0))
|
self.driver.n.fill(bg_color)
|
||||||
|
|
||||||
# Draw repeating pattern starting at position
|
# Draw repeating pattern starting at position
|
||||||
for i in range(self.driver.num_leds):
|
for i in range(self.driver.num_leds):
|
||||||
@@ -114,9 +116,10 @@ class Chase:
|
|||||||
self.driver.n[i] = color1
|
self.driver.n[i] = color1
|
||||||
|
|
||||||
self.driver.n.write()
|
self.driver.n.write()
|
||||||
|
print("[chase] step", step_count)
|
||||||
|
|
||||||
# Increment step
|
# Increment step
|
||||||
step_count += 1
|
step_count = (step_count + 1) % 2
|
||||||
self.driver.step = step_count
|
self.driver.step = step_count
|
||||||
last_update = utime.ticks_add(last_update, transition_duration)
|
last_update = utime.ticks_add(last_update, transition_duration)
|
||||||
transition_duration = max(10, int(preset.d))
|
transition_duration = max(10, int(preset.d))
|
||||||
|
|||||||
@@ -31,10 +31,10 @@ class Circle:
|
|||||||
base0 = base1 = (255, 255, 255)
|
base0 = base1 = (255, 255, 255)
|
||||||
elif len(colors) == 1:
|
elif len(colors) == 1:
|
||||||
base0 = colors[0]
|
base0 = colors[0]
|
||||||
base1 = (0, 0, 0)
|
base1 = preset.background_or(colors)
|
||||||
else:
|
else:
|
||||||
base0 = colors[0]
|
base0 = colors[0]
|
||||||
base1 = colors[1]
|
base1 = preset.background_or(colors)
|
||||||
|
|
||||||
color0 = self.driver.apply_brightness(base0, preset.b)
|
color0 = self.driver.apply_brightness(base0, preset.b)
|
||||||
color1 = self.driver.apply_brightness(base1, preset.b)
|
color1 = self.driver.apply_brightness(base1, preset.b)
|
||||||
@@ -46,7 +46,7 @@ class Circle:
|
|||||||
if phase == "off":
|
if phase == "off":
|
||||||
self.driver.n.fill(color1)
|
self.driver.n.fill(color1)
|
||||||
else:
|
else:
|
||||||
self.driver.n.fill((0, 0, 0))
|
self.driver.n.fill(color1)
|
||||||
|
|
||||||
# Calculate segment length
|
# Calculate segment length
|
||||||
segment_length = (head - tail) % self.driver.num_leds
|
segment_length = (head - tail) % self.driver.num_leds
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class ClockSweep:
|
|||||||
d = max(1, int(preset.d))
|
d = max(1, int(preset.d))
|
||||||
now = utime.ticks_ms()
|
now = utime.ticks_ms()
|
||||||
if utime.ticks_diff(now, last) >= d:
|
if utime.ticks_diff(now, last) >= d:
|
||||||
bg = self.driver.apply_brightness(colors[1] if len(colors) > 1 else (0, 0, 0), preset.b)
|
bg = self.driver.apply_brightness(preset.background_or(colors), preset.b)
|
||||||
fg = self.driver.apply_brightness(colors[0], preset.b)
|
fg = self.driver.apply_brightness(colors[0], preset.b)
|
||||||
for i in range(self.driver.num_leds):
|
for i in range(self.driver.num_leds):
|
||||||
self.driver.n[i] = bg
|
self.driver.n[i] = bg
|
||||||
|
|||||||
@@ -17,8 +17,9 @@ class CometDual:
|
|||||||
d = max(1, int(preset.d))
|
d = max(1, int(preset.d))
|
||||||
now = utime.ticks_ms()
|
now = utime.ticks_ms()
|
||||||
if utime.ticks_diff(now, last) >= d:
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
bg_color = self.driver.apply_brightness(preset.background_or(colors), preset.b)
|
||||||
for i in range(self.driver.num_leds):
|
for i in range(self.driver.num_leds):
|
||||||
self.driver.n[i] = (0, 0, 0)
|
self.driver.n[i] = bg_color
|
||||||
c1 = self.driver.apply_brightness(colors[0 % len(colors)], preset.b)
|
c1 = self.driver.apply_brightness(colors[0 % len(colors)], preset.b)
|
||||||
c2 = self.driver.apply_brightness(colors[1 % len(colors)] if len(colors) > 1 else colors[0], preset.b)
|
c2 = self.driver.apply_brightness(colors[1 % len(colors)] if len(colors) > 1 else colors[0], preset.b)
|
||||||
for t in range(tail):
|
for t in range(tail):
|
||||||
|
|||||||
@@ -16,8 +16,9 @@ class Fireflies:
|
|||||||
d = max(1, int(preset.d))
|
d = max(1, int(preset.d))
|
||||||
now = utime.ticks_ms()
|
now = utime.ticks_ms()
|
||||||
if utime.ticks_diff(now, last) >= d:
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
bg_color = self.driver.apply_brightness(preset.background_or(colors), preset.b)
|
||||||
for i in range(self.driver.num_leds):
|
for i in range(self.driver.num_leds):
|
||||||
self.driver.n[i] = (0, 0, 0)
|
self.driver.n[i] = bg_color
|
||||||
for b in bugs:
|
for b in bugs:
|
||||||
idx, ph = b
|
idx, ph = b
|
||||||
tri = 255 - abs(128 - ph) * 2
|
tri = 255 - abs(128 - ph) * 2
|
||||||
|
|||||||
@@ -7,19 +7,30 @@ class Heartbeat:
|
|||||||
|
|
||||||
def run(self, preset):
|
def run(self, preset):
|
||||||
colors = preset.c if preset.c else [(255, 0, 40)]
|
colors = preset.c if preset.c else [(255, 0, 40)]
|
||||||
|
phase = 0
|
||||||
|
phase_start = utime.ticks_ms()
|
||||||
|
did_manual_pulse = False
|
||||||
|
while True:
|
||||||
p1 = max(20, int(preset.n1) if int(preset.n1) > 0 else 120)
|
p1 = max(20, int(preset.n1) if int(preset.n1) > 0 else 120)
|
||||||
p2 = max(20, int(preset.n2) if int(preset.n2) > 0 else 80)
|
p2 = max(20, int(preset.n2) if int(preset.n2) > 0 else 80)
|
||||||
pause = max(20, int(preset.n3) if int(preset.n3) > 0 else 500)
|
pause = max(20, int(preset.n3) if int(preset.n3) > 0 else 500)
|
||||||
while True:
|
beat_gap = max(20, int(preset.d))
|
||||||
c = self.driver.apply_brightness(colors[0], preset.b)
|
colors = preset.c if preset.c else [(255, 0, 40)]
|
||||||
self.driver.fill(c)
|
lit_color = self.driver.apply_brightness(colors[0], preset.b)
|
||||||
utime.sleep_ms(p1)
|
bg_color = self.driver.apply_brightness(preset.background_or(colors), preset.b)
|
||||||
self.driver.fill((0, 0, 0))
|
phase_durations = (p1, beat_gap, p2, pause)
|
||||||
utime.sleep_ms(max(20, int(preset.d)))
|
phase_colors = (lit_color, bg_color, lit_color, bg_color)
|
||||||
self.driver.fill(c)
|
|
||||||
utime.sleep_ms(p2)
|
now = utime.ticks_ms()
|
||||||
self.driver.fill((0, 0, 0))
|
while utime.ticks_diff(now, phase_start) >= phase_durations[phase]:
|
||||||
utime.sleep_ms(pause)
|
phase_start = utime.ticks_add(phase_start, phase_durations[phase])
|
||||||
|
phase = (phase + 1) % 4
|
||||||
|
|
||||||
|
self.driver.fill(phase_colors[phase])
|
||||||
yield
|
yield
|
||||||
if not preset.a:
|
if not preset.a:
|
||||||
|
if did_manual_pulse or phase == 0:
|
||||||
|
self.driver.fill(bg_color)
|
||||||
|
yield
|
||||||
return
|
return
|
||||||
|
did_manual_pulse = True
|
||||||
|
|||||||
@@ -17,9 +17,10 @@ class Marquee:
|
|||||||
now = utime.ticks_ms()
|
now = utime.ticks_ms()
|
||||||
if utime.ticks_diff(now, last) >= d:
|
if utime.ticks_diff(now, last) >= d:
|
||||||
c = self.driver.apply_brightness(colors[0], preset.b)
|
c = self.driver.apply_brightness(colors[0], preset.b)
|
||||||
|
bg_color = self.driver.apply_brightness(preset.background_or(colors), preset.b)
|
||||||
for i in range(self.driver.num_leds):
|
for i in range(self.driver.num_leds):
|
||||||
m = (i + phase) % (on_len + off_len)
|
m = (i + phase) % (on_len + off_len)
|
||||||
self.driver.n[i] = c if m < on_len else (0, 0, 0)
|
self.driver.n[i] = c if m < on_len else bg_color
|
||||||
self.driver.n.write()
|
self.driver.n.write()
|
||||||
phase = (phase + step) % (on_len + off_len)
|
phase = (phase + step) % (on_len + off_len)
|
||||||
self.driver.step = phase
|
self.driver.step = phase
|
||||||
|
|||||||
@@ -15,8 +15,9 @@ class Orbit:
|
|||||||
d = max(1, int(preset.d))
|
d = max(1, int(preset.d))
|
||||||
now = utime.ticks_ms()
|
now = utime.ticks_ms()
|
||||||
if utime.ticks_diff(now, last) >= d:
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
bg_color = self.driver.apply_brightness(preset.background_or(colors), preset.b)
|
||||||
for i in range(self.driver.num_leds):
|
for i in range(self.driver.num_leds):
|
||||||
self.driver.n[i] = (0, 0, 0)
|
self.driver.n[i] = bg_color
|
||||||
for k in range(orbits):
|
for k in range(orbits):
|
||||||
idx = ((phase * (k + 1)) // 8 + (k * self.driver.num_leds // max(1, orbits))) % max(1, self.driver.num_leds)
|
idx = ((phase * (k + 1)) // 8 + (k * self.driver.num_leds // max(1, orbits))) % max(1, self.driver.num_leds)
|
||||||
self.driver.n[idx] = self.driver.apply_brightness(colors[k % len(colors)], preset.b)
|
self.driver.n[idx] = self.driver.apply_brightness(colors[k % len(colors)], preset.b)
|
||||||
|
|||||||
@@ -34,9 +34,15 @@ class PaletteMorph:
|
|||||||
base_idx = 0
|
base_idx = 0
|
||||||
start = utime.ticks_ms()
|
start = utime.ticks_ms()
|
||||||
phase = self.driver.step % 256
|
phase = self.driver.step % 256
|
||||||
|
last_update = start
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
now = utime.ticks_ms()
|
now = utime.ticks_ms()
|
||||||
|
delay_ms = max(1, int(preset.d))
|
||||||
|
if utime.ticks_diff(now, last_update) < delay_ms:
|
||||||
|
yield
|
||||||
|
continue
|
||||||
|
last_update = utime.ticks_add(last_update, delay_ms)
|
||||||
age = utime.ticks_diff(now, start)
|
age = utime.ticks_diff(now, start)
|
||||||
if age < morph:
|
if age < morph:
|
||||||
t = (age * 255) // morph
|
t = (age * 255) // morph
|
||||||
@@ -72,5 +78,4 @@ class PaletteMorph:
|
|||||||
return
|
return
|
||||||
phase = (phase + warp_rate) & 255
|
phase = (phase + warp_rate) & 255
|
||||||
self.driver.step = phase
|
self.driver.step = phase
|
||||||
utime.sleep_ms(max(1, int(preset.d)))
|
|
||||||
yield
|
yield
|
||||||
|
|||||||
@@ -6,18 +6,25 @@ class Pulse:
|
|||||||
self.driver = driver
|
self.driver = driver
|
||||||
|
|
||||||
def run(self, preset):
|
def run(self, preset):
|
||||||
self.driver.off()
|
|
||||||
|
|
||||||
# Get colors from preset
|
# Get colors from preset
|
||||||
colors = preset.c
|
colors = preset.c
|
||||||
if not colors:
|
if not colors:
|
||||||
colors = [(255, 255, 255)]
|
colors = [(255, 255, 255)]
|
||||||
|
bg_base = preset.background_or(colors)
|
||||||
|
self.driver.fill(self.driver.apply_brightness(bg_base, preset.b))
|
||||||
|
|
||||||
color_index = 0
|
color_index = self.driver.step % max(1, len(colors))
|
||||||
|
if not preset.a:
|
||||||
|
# Manual / beat trigger: each select restarts this generator and resets
|
||||||
|
# cycle_start below. Advancing step here makes each beat the next colour
|
||||||
|
# without requiring a full wall-clock cycle between beats.
|
||||||
|
nclr = max(1, len(colors))
|
||||||
|
self.driver.step = (color_index + 1) % nclr
|
||||||
cycle_start = utime.ticks_ms()
|
cycle_start = utime.ticks_ms()
|
||||||
|
|
||||||
# State machine based pulse using a single generator loop
|
# State machine based pulse using a single generator loop
|
||||||
while True:
|
while True:
|
||||||
|
bg_color = self.driver.apply_brightness(bg_base, preset.b)
|
||||||
# Read current timing parameters from preset
|
# Read current timing parameters from preset
|
||||||
attack_ms = max(0, int(preset.n1)) # Attack time in ms
|
attack_ms = max(0, int(preset.n1)) # Attack time in ms
|
||||||
hold_ms = max(0, int(preset.n2)) # Hold time in ms
|
hold_ms = max(0, int(preset.n2)) # Hold time in ms
|
||||||
@@ -49,14 +56,15 @@ class Pulse:
|
|||||||
self.driver.fill(self.driver.apply_brightness(color, preset.b))
|
self.driver.fill(self.driver.apply_brightness(color, preset.b))
|
||||||
elif elapsed < total_ms:
|
elif elapsed < total_ms:
|
||||||
# Delay phase: LEDs off between pulses
|
# Delay phase: LEDs off between pulses
|
||||||
self.driver.fill((0, 0, 0))
|
self.driver.fill(bg_color)
|
||||||
else:
|
else:
|
||||||
# End of cycle, move to next color and restart timing
|
# End of cycle: auto advances colour and loops; manual already
|
||||||
color_index += 1
|
# advanced step at run start for the next beat.
|
||||||
cycle_start = now
|
|
||||||
if not preset.a:
|
if not preset.a:
|
||||||
break
|
break
|
||||||
# Skip drawing this tick, start next cycle
|
color_index = (color_index + 1) % max(1, len(colors))
|
||||||
|
self.driver.step = color_index
|
||||||
|
cycle_start = now
|
||||||
yield
|
yield
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import utime
|
import utime
|
||||||
|
|
||||||
_RADIATE_DBG_INTERVAL_MS = 1000
|
# When ``driver.debug`` is True (``settings["debug"]``), log at most this often (ms).
|
||||||
|
_RADIATE_DBG_INTERVAL_MS = 800
|
||||||
|
|
||||||
|
|
||||||
class Radiate:
|
class Radiate:
|
||||||
def __init__(self, driver):
|
def __init__(self, driver):
|
||||||
self.driver = driver
|
self.driver = driver
|
||||||
|
self._color_step = 0
|
||||||
|
|
||||||
def run(self, preset):
|
def run(self, preset):
|
||||||
"""Radiate from nodes every n1 LEDs, retriggering every delay (d).
|
"""Radiate from nodes every n1 LEDs, retriggering every delay (d).
|
||||||
@@ -16,15 +18,14 @@ class Radiate:
|
|||||||
- d: retrigger interval in ms
|
- d: retrigger interval in ms
|
||||||
"""
|
"""
|
||||||
colors = preset.c if preset.c else [(255, 255, 255)]
|
colors = preset.c if preset.c else [(255, 255, 255)]
|
||||||
base_on = colors[0]
|
base_off = preset.background_or(colors)
|
||||||
base_off = colors[1] if len(colors) > 1 else (0, 0, 0)
|
|
||||||
|
|
||||||
spacing = max(1, int(preset.n1))
|
spacing = max(1, int(preset.n1))
|
||||||
outward_ms = max(1, int(preset.n2))
|
outward_ms = max(1, int(preset.n2))
|
||||||
return_ms = max(1, int(preset.n3))
|
return_ms = max(1, int(preset.n3))
|
||||||
max_dist = spacing // 2
|
max_dist = spacing // 2
|
||||||
|
|
||||||
lit_color = self.driver.apply_brightness(base_on, preset.b)
|
lit_color = self.driver.apply_brightness(colors[self._color_step % max(1, len(colors))], preset.b)
|
||||||
off_color = self.driver.apply_brightness(base_off, preset.b)
|
off_color = self.driver.apply_brightness(base_off, preset.b)
|
||||||
|
|
||||||
now = utime.ticks_ms()
|
now = utime.ticks_ms()
|
||||||
@@ -34,17 +35,70 @@ class Radiate:
|
|||||||
dbg_banner = False
|
dbg_banner = False
|
||||||
|
|
||||||
if not preset.a:
|
if not preset.a:
|
||||||
# Single-step render uses only the first instant pulse.
|
# Manual mode: one-shot pulse using the same ms-based timing as auto.
|
||||||
active_pulses = [utime.ticks_ms()]
|
cycle_start = utime.ticks_ms()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
now = utime.ticks_ms()
|
dbg = bool(getattr(self.driver, "debug", False))
|
||||||
delay_ms = max(1, int(preset.d))
|
|
||||||
spacing = max(1, int(preset.n1))
|
spacing = max(1, int(preset.n1))
|
||||||
outward_ms = max(1, int(preset.n2))
|
outward_ms = max(1, int(preset.n2))
|
||||||
return_ms = max(1, int(preset.n3))
|
return_ms = max(1, int(preset.n3))
|
||||||
max_dist = spacing // 2
|
max_dist = spacing // 2
|
||||||
lit_color = self.driver.apply_brightness(base_on, preset.b)
|
on_color = colors[self._color_step % max(1, len(colors))]
|
||||||
|
lit_color = self.driver.apply_brightness(on_color, preset.b)
|
||||||
|
off_color = self.driver.apply_brightness(base_off, preset.b)
|
||||||
|
|
||||||
|
pulse_lifetime = outward_ms + return_ms
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
age = utime.ticks_diff(now, cycle_start)
|
||||||
|
if age < 1:
|
||||||
|
age = 1
|
||||||
|
if age <= outward_ms:
|
||||||
|
front = (age * max_dist + outward_ms - 1) // outward_ms
|
||||||
|
elif age <= outward_ms + return_ms:
|
||||||
|
back_age = age - outward_ms
|
||||||
|
remaining = return_ms - back_age
|
||||||
|
front = (remaining * max_dist + return_ms - 1) // return_ms
|
||||||
|
else:
|
||||||
|
front = 0
|
||||||
|
|
||||||
|
lit_count = 0
|
||||||
|
for i in range(self.driver.num_leds):
|
||||||
|
offset = (i + (spacing // 2)) % spacing
|
||||||
|
dist = min(offset, spacing - offset)
|
||||||
|
lit = dist <= front
|
||||||
|
self.driver.n[i] = lit_color if lit else off_color
|
||||||
|
if lit:
|
||||||
|
lit_count += 1
|
||||||
|
self.driver.n.write()
|
||||||
|
|
||||||
|
if dbg:
|
||||||
|
if not dbg_banner:
|
||||||
|
dbg_banner = True
|
||||||
|
print(
|
||||||
|
"[radiate] debug on n1=%s n2=%s n3=%s d=%s auto=%s num_leds=%d"
|
||||||
|
% (preset.n1, preset.n2, preset.n3, preset.d, preset.a, self.driver.num_leds)
|
||||||
|
)
|
||||||
|
print(
|
||||||
|
"[radiate] manual frame age=%d/%d front=%d lit=%d"
|
||||||
|
% (age, pulse_lifetime, front, lit_count)
|
||||||
|
)
|
||||||
|
|
||||||
|
yield
|
||||||
|
if age >= pulse_lifetime:
|
||||||
|
self._color_step += 1
|
||||||
|
return
|
||||||
|
|
||||||
|
while True:
|
||||||
|
now = utime.ticks_ms()
|
||||||
|
dbg = bool(getattr(self.driver, "debug", False))
|
||||||
|
delay_ms = max(1, int(preset.d))
|
||||||
|
spacing = max(1, int(preset.n1))
|
||||||
|
outward_ms = max(1, int(preset.n2))
|
||||||
|
return_ms = max(1, int(preset.n3))
|
||||||
|
pulse_lifetime = outward_ms + return_ms
|
||||||
|
max_dist = spacing // 2
|
||||||
|
on_color = colors[self._color_step % max(1, len(colors))]
|
||||||
|
lit_color = self.driver.apply_brightness(on_color, preset.b)
|
||||||
off_color = self.driver.apply_brightness(base_off, preset.b)
|
off_color = self.driver.apply_brightness(base_off, preset.b)
|
||||||
|
|
||||||
if preset.a and utime.ticks_diff(now, last_trigger) >= delay_ms:
|
if preset.a and utime.ticks_diff(now, last_trigger) >= delay_ms:
|
||||||
@@ -52,33 +106,26 @@ class Radiate:
|
|||||||
# prevents overlap from keeping color[0] continuously visible.
|
# prevents overlap from keeping color[0] continuously visible.
|
||||||
active_pulses = [now]
|
active_pulses = [now]
|
||||||
last_trigger = utime.ticks_add(last_trigger, delay_ms)
|
last_trigger = utime.ticks_add(last_trigger, delay_ms)
|
||||||
if bool(getattr(self.driver, "debug", False)):
|
self._color_step += 1
|
||||||
print(
|
|
||||||
"[radiate] trigger spacing=%d out=%d in=%d delay=%d"
|
|
||||||
% (spacing, outward_ms, return_ms, delay_ms)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Drop pulses once their out-and-back lifetime ends.
|
# Drop pulses once their out-and-back lifetime ends.
|
||||||
pulse_lifetime = outward_ms + return_ms
|
|
||||||
kept = []
|
kept = []
|
||||||
for start in active_pulses:
|
for start in active_pulses:
|
||||||
age = utime.ticks_diff(now, start)
|
age = utime.ticks_diff(now, start)
|
||||||
if age < pulse_lifetime:
|
if age < pulse_lifetime:
|
||||||
kept.append(start)
|
kept.append(start)
|
||||||
active_pulses = kept
|
active_pulses = kept
|
||||||
debug_front = -1
|
|
||||||
lit_count = 0
|
|
||||||
|
|
||||||
|
lit_count = 0
|
||||||
for i in range(self.driver.num_leds):
|
for i in range(self.driver.num_leds):
|
||||||
# Nearest node distance for a repeating node grid every `spacing` LEDs.
|
# Nearest node distance for a repeating node grid every `spacing` LEDs.
|
||||||
offset = i % spacing
|
offset = (i + (spacing // 2)) % spacing
|
||||||
dist = min(offset, spacing - offset)
|
dist = min(offset, spacing - offset)
|
||||||
|
|
||||||
lit = False
|
lit = False
|
||||||
for start in active_pulses:
|
for start in active_pulses:
|
||||||
age = utime.ticks_diff(now, start)
|
age = utime.ticks_diff(now, start)
|
||||||
# Do not render on the exact trigger tick; this avoids
|
# Auto: skip the exact trigger tick (age==0) so nodes are not stuck on.
|
||||||
# node LEDs appearing "stuck on" between cycles.
|
|
||||||
if age <= 0:
|
if age <= 0:
|
||||||
continue
|
continue
|
||||||
if age <= outward_ms:
|
if age <= outward_ms:
|
||||||
@@ -94,8 +141,6 @@ class Radiate:
|
|||||||
|
|
||||||
if dist <= front:
|
if dist <= front:
|
||||||
lit = True
|
lit = True
|
||||||
if front > debug_front:
|
|
||||||
debug_front = front
|
|
||||||
break
|
break
|
||||||
|
|
||||||
self.driver.n[i] = lit_color if lit else off_color
|
self.driver.n[i] = lit_color if lit else off_color
|
||||||
@@ -104,33 +149,21 @@ class Radiate:
|
|||||||
|
|
||||||
self.driver.n.write()
|
self.driver.n.write()
|
||||||
|
|
||||||
if bool(getattr(self.driver, "debug", False)):
|
if dbg:
|
||||||
if not dbg_banner:
|
if not dbg_banner:
|
||||||
dbg_banner = True
|
dbg_banner = True
|
||||||
print(
|
print(
|
||||||
"[radiate] debug on: spacing=%s out=%s in=%s d=%s num=%d"
|
"[radiate] debug on n1=%s n2=%s n3=%s d=%s auto=%s num_leds=%d"
|
||||||
% (
|
% (preset.n1, preset.n2, preset.n3, preset.d, preset.a, self.driver.num_leds)
|
||||||
preset.n1,
|
|
||||||
preset.n2,
|
|
||||||
preset.n3,
|
|
||||||
preset.d,
|
|
||||||
self.driver.num_leds,
|
|
||||||
)
|
)
|
||||||
)
|
|
||||||
if utime.ticks_diff(now, last_dbg) >= _RADIATE_DBG_INTERVAL_MS:
|
|
||||||
pulse_age = -1
|
pulse_age = -1
|
||||||
if active_pulses:
|
if active_pulses:
|
||||||
pulse_age = utime.ticks_diff(now, active_pulses[0])
|
pulse_age = utime.ticks_diff(now, active_pulses[0])
|
||||||
|
if utime.ticks_diff(now, last_dbg) >= _RADIATE_DBG_INTERVAL_MS:
|
||||||
print(
|
print(
|
||||||
"[radiate] age=%d front=%d max=%d active=%d lit=%d"
|
"[radiate] pulses=%d first_age=%d lit=%d lifetime=%d"
|
||||||
% (pulse_age, debug_front, max_dist, len(active_pulses), lit_count)
|
% (len(active_pulses), pulse_age, lit_count, pulse_lifetime)
|
||||||
)
|
)
|
||||||
if lit_count == 0:
|
|
||||||
print("[radiate] fully off")
|
|
||||||
last_dbg = now
|
last_dbg = now
|
||||||
|
|
||||||
if not preset.a:
|
|
||||||
yield
|
|
||||||
return
|
|
||||||
|
|
||||||
yield
|
yield
|
||||||
|
|||||||
@@ -16,8 +16,9 @@ class RainDrops:
|
|||||||
d = max(1, int(preset.d))
|
d = max(1, int(preset.d))
|
||||||
now = utime.ticks_ms()
|
now = utime.ticks_ms()
|
||||||
if utime.ticks_diff(now, last) >= d:
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
bg_color = self.driver.apply_brightness(preset.background_or(colors), preset.b)
|
||||||
for i in range(self.driver.num_leds):
|
for i in range(self.driver.num_leds):
|
||||||
self.driver.n[i] = (0, 0, 0)
|
self.driver.n[i] = bg_color
|
||||||
if random.randint(0, 255) < rate:
|
if random.randint(0, 255) < rate:
|
||||||
drops.append([random.randint(0, max(0, self.driver.num_leds - 1)), 0])
|
drops.append([random.randint(0, max(0, self.driver.num_leds - 1)), 0])
|
||||||
nd = []
|
nd = []
|
||||||
|
|||||||
@@ -27,12 +27,13 @@ class Scanner:
|
|||||||
if utime.ticks_diff(now, last_update) >= delay_ms:
|
if utime.ticks_diff(now, last_update) >= delay_ms:
|
||||||
base = colors[color_index % len(colors)]
|
base = colors[color_index % len(colors)]
|
||||||
base = self.driver.apply_brightness(base, preset.b)
|
base = self.driver.apply_brightness(base, preset.b)
|
||||||
|
bg_color = self.driver.apply_brightness(preset.background_or(colors), preset.b)
|
||||||
for i in range(self.driver.num_leds):
|
for i in range(self.driver.num_leds):
|
||||||
dist = i - center
|
dist = i - center
|
||||||
if dist < 0:
|
if dist < 0:
|
||||||
dist = -dist
|
dist = -dist
|
||||||
if dist > width:
|
if dist > width:
|
||||||
self.driver.n[i] = (0, 0, 0)
|
self.driver.n[i] = bg_color
|
||||||
else:
|
else:
|
||||||
scale = ((width - dist) * 255) // max(1, width)
|
scale = ((width - dist) * 255) // max(1, width)
|
||||||
self.driver.n[i] = (
|
self.driver.n[i] = (
|
||||||
|
|||||||
@@ -24,13 +24,14 @@ class SegmentChase:
|
|||||||
d = max(1, int(preset.d))
|
d = max(1, int(preset.d))
|
||||||
now = utime.ticks_ms()
|
now = utime.ticks_ms()
|
||||||
if utime.ticks_diff(now, last) >= d:
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
bg_color = self.driver.apply_brightness(preset.background_or(colors), preset.b)
|
||||||
for i in range(self.driver.num_leds):
|
for i in range(self.driver.num_leds):
|
||||||
seg_idx = i // seg
|
seg_idx = i // seg
|
||||||
in_seg = i % seg
|
in_seg = i % seg
|
||||||
local_phase = (phase + seg_idx * seg_offset) % seg
|
local_phase = (phase + seg_idx * seg_offset) % seg
|
||||||
lit_idx = (in_seg + local_phase) % seg
|
lit_idx = (in_seg + local_phase) % seg
|
||||||
if gap > 0 and lit_idx >= max(1, seg - gap):
|
if gap > 0 and lit_idx >= max(1, seg - gap):
|
||||||
self.driver.n[i] = (0, 0, 0)
|
self.driver.n[i] = bg_color
|
||||||
else:
|
else:
|
||||||
color_idx = seg_idx % len(colors)
|
color_idx = seg_idx % len(colors)
|
||||||
self.driver.n[i] = self.driver.apply_brightness(colors[color_idx], preset.b)
|
self.driver.n[i] = self.driver.apply_brightness(colors[color_idx], preset.b)
|
||||||
|
|||||||
@@ -16,10 +16,11 @@ class Snowfall:
|
|||||||
d = max(1, int(preset.d))
|
d = max(1, int(preset.d))
|
||||||
now = utime.ticks_ms()
|
now = utime.ticks_ms()
|
||||||
if utime.ticks_diff(now, last) >= d:
|
if utime.ticks_diff(now, last) >= d:
|
||||||
|
bg_color = self.driver.apply_brightness(preset.background_or(colors), preset.b)
|
||||||
if random.randint(0, 255) < density:
|
if random.randint(0, 255) < density:
|
||||||
flakes.append([self.driver.num_leds - 1, random.randint(0, len(colors)-1)])
|
flakes.append([self.driver.num_leds - 1, random.randint(0, len(colors)-1)])
|
||||||
for i in range(self.driver.num_leds):
|
for i in range(self.driver.num_leds):
|
||||||
self.driver.n[i] = (0, 0, 0)
|
self.driver.n[i] = bg_color
|
||||||
nf = []
|
nf = []
|
||||||
for pos, ci in flakes:
|
for pos, ci in flakes:
|
||||||
if 0 <= pos < self.driver.num_leds:
|
if 0 <= pos < self.driver.num_leds:
|
||||||
|
|||||||
@@ -7,18 +7,39 @@ class StrobeBurst:
|
|||||||
|
|
||||||
def run(self, preset):
|
def run(self, preset):
|
||||||
colors = preset.c if preset.c else [(255, 255, 255)]
|
colors = preset.c if preset.c else [(255, 255, 255)]
|
||||||
|
state = "flash_on"
|
||||||
|
flash_idx = 0
|
||||||
|
state_start = utime.ticks_ms()
|
||||||
|
while True:
|
||||||
count = max(1, int(preset.n1) if int(preset.n1) > 0 else 3)
|
count = max(1, int(preset.n1) if int(preset.n1) > 0 else 3)
|
||||||
gap = max(1, int(preset.n2) if int(preset.n2) > 0 else 60)
|
gap = max(1, int(preset.n2) if int(preset.n2) > 0 else 60)
|
||||||
cooldown = max(1, int(preset.n3) if int(preset.n3) > 0 else 400)
|
cooldown = max(1, int(preset.n3) if int(preset.n3) > 0 else 400)
|
||||||
|
on_ms = max(1, int(preset.d) // 2)
|
||||||
c = self.driver.apply_brightness(colors[0], preset.b)
|
c = self.driver.apply_brightness(colors[0], preset.b)
|
||||||
while True:
|
bg_color = self.driver.apply_brightness(preset.background_or(colors), preset.b)
|
||||||
for _ in range(count):
|
now = utime.ticks_ms()
|
||||||
|
|
||||||
|
if state == "flash_on":
|
||||||
self.driver.fill(c)
|
self.driver.fill(c)
|
||||||
utime.sleep_ms(max(1, int(preset.d)//2))
|
if utime.ticks_diff(now, state_start) >= on_ms:
|
||||||
self.driver.fill((0, 0, 0))
|
state = "flash_off"
|
||||||
utime.sleep_ms(gap)
|
state_start = utime.ticks_add(state_start, on_ms)
|
||||||
yield
|
elif state == "flash_off":
|
||||||
utime.sleep_ms(cooldown)
|
self.driver.fill(bg_color)
|
||||||
yield
|
if utime.ticks_diff(now, state_start) >= gap:
|
||||||
|
flash_idx += 1
|
||||||
|
if flash_idx >= count:
|
||||||
if not preset.a:
|
if not preset.a:
|
||||||
return
|
return
|
||||||
|
state = "cooldown"
|
||||||
|
flash_idx = 0
|
||||||
|
state_start = utime.ticks_add(state_start, gap)
|
||||||
|
else:
|
||||||
|
state = "flash_on"
|
||||||
|
state_start = utime.ticks_add(state_start, gap)
|
||||||
|
else:
|
||||||
|
self.driver.fill(bg_color)
|
||||||
|
if utime.ticks_diff(now, state_start) >= cooldown:
|
||||||
|
state = "flash_on"
|
||||||
|
state_start = utime.ticks_add(state_start, cooldown)
|
||||||
|
yield
|
||||||
|
|||||||
@@ -2,9 +2,6 @@ import random
|
|||||||
import utime
|
import utime
|
||||||
|
|
||||||
# Default cool palette (icy blues, violet, mint) when preset has no colours.
|
# Default cool palette (icy blues, violet, mint) when preset has no colours.
|
||||||
# When `driver.debug` is True, print stats every N twinkle ticks (serial can be slow).
|
|
||||||
_TWINKLE_DBG_INTERVAL = 40
|
|
||||||
|
|
||||||
_DEFAULT_COOL = (
|
_DEFAULT_COOL = (
|
||||||
(120, 200, 255),
|
(120, 200, 255),
|
||||||
(80, 140, 255),
|
(80, 140, 255),
|
||||||
@@ -39,6 +36,7 @@ class Twinkle:
|
|||||||
"""Twinkle: n1 activity, n2 density; n3/n4 min/max length of adjacent on/off runs."""
|
"""Twinkle: n1 activity, n2 density; n3/n4 min/max length of adjacent on/off runs."""
|
||||||
palette = self._palette(preset)
|
palette = self._palette(preset)
|
||||||
num = self.driver.num_leds
|
num = self.driver.num_leds
|
||||||
|
bg_color = self.driver.apply_brightness(preset.background_or(palette), preset.b)
|
||||||
if num <= 0:
|
if num <= 0:
|
||||||
while True:
|
while True:
|
||||||
yield
|
yield
|
||||||
@@ -92,32 +90,6 @@ class Twinkle:
|
|||||||
on = [random.randint(0, 255) < dens for _ in range(num)]
|
on = [random.randint(0, 255) < dens for _ in range(num)]
|
||||||
colour_i = [random.randint(0, len(palette) - 1) for _ in range(num)]
|
colour_i = [random.randint(0, len(palette) - 1) for _ in range(num)]
|
||||||
last_update = utime.ticks_ms()
|
last_update = utime.ticks_ms()
|
||||||
dbg_tick = 0
|
|
||||||
dbg_banner = False
|
|
||||||
|
|
||||||
def on_run_min_max(bits):
|
|
||||||
"""Smallest and largest contiguous run of True in bits (0,0 if all off)."""
|
|
||||||
best_min = num + 1
|
|
||||||
best_max = 0
|
|
||||||
cur = 0
|
|
||||||
for j in range(num):
|
|
||||||
if bits[j]:
|
|
||||||
cur += 1
|
|
||||||
else:
|
|
||||||
if cur:
|
|
||||||
if cur < best_min:
|
|
||||||
best_min = cur
|
|
||||||
if cur > best_max:
|
|
||||||
best_max = cur
|
|
||||||
cur = 0
|
|
||||||
if cur:
|
|
||||||
if cur < best_min:
|
|
||||||
best_min = cur
|
|
||||||
if cur > best_max:
|
|
||||||
best_max = cur
|
|
||||||
if best_min == num + 1:
|
|
||||||
return 0, 0
|
|
||||||
return best_min, best_max
|
|
||||||
|
|
||||||
if not preset.a:
|
if not preset.a:
|
||||||
for i in range(num):
|
for i in range(num):
|
||||||
@@ -125,7 +97,7 @@ class Twinkle:
|
|||||||
base = palette[colour_i[i] % len(palette)]
|
base = palette[colour_i[i] % len(palette)]
|
||||||
self.driver.n[i] = self.driver.apply_brightness(base, preset.b)
|
self.driver.n[i] = self.driver.apply_brightness(base, preset.b)
|
||||||
else:
|
else:
|
||||||
self.driver.n[i] = (0, 0, 0)
|
self.driver.n[i] = bg_color
|
||||||
self.driver.n.write()
|
self.driver.n.write()
|
||||||
yield
|
yield
|
||||||
return
|
return
|
||||||
@@ -136,15 +108,12 @@ class Twinkle:
|
|||||||
if utime.ticks_diff(now, last_update) >= delay_ms:
|
if utime.ticks_diff(now, last_update) >= delay_ms:
|
||||||
rate = activity_rate()
|
rate = activity_rate()
|
||||||
dens = density255()
|
dens = density255()
|
||||||
dbg = bool(getattr(self.driver, "debug", False))
|
|
||||||
dbg_tick += 1
|
|
||||||
# Snapshot for decisions; apply all darks then all lights so
|
# Snapshot for decisions; apply all darks then all lights so
|
||||||
# overlaps in the same tick favour lit runs (lights win).
|
# overlaps in the same tick favour lit runs (lights win).
|
||||||
prev_on = on[:]
|
prev_on = on[:]
|
||||||
prev_ci = colour_i[:]
|
prev_ci = colour_i[:]
|
||||||
next_on = list(prev_on)
|
next_on = list(prev_on)
|
||||||
next_ci = list(prev_ci)
|
next_ci = list(prev_ci)
|
||||||
dbg_ops = {"L": 0, "D": 0}
|
|
||||||
|
|
||||||
light_i = []
|
light_i = []
|
||||||
dark_i = []
|
dark_i = []
|
||||||
@@ -159,7 +128,6 @@ class Twinkle:
|
|||||||
dark_i.append(i)
|
dark_i.append(i)
|
||||||
|
|
||||||
def light_adjacent(start):
|
def light_adjacent(start):
|
||||||
dbg_ops["L"] += 1
|
|
||||||
k = random_cluster_len()
|
k = random_cluster_len()
|
||||||
b = cluster_base_index(start, k)
|
b = cluster_base_index(start, k)
|
||||||
for dj in range(k):
|
for dj in range(k):
|
||||||
@@ -168,7 +136,6 @@ class Twinkle:
|
|||||||
next_ci[idx] = random.randint(0, len(palette) - 1)
|
next_ci[idx] = random.randint(0, len(palette) - 1)
|
||||||
|
|
||||||
def dark_adjacent(start):
|
def dark_adjacent(start):
|
||||||
dbg_ops["D"] += 1
|
|
||||||
k = random_cluster_len()
|
k = random_cluster_len()
|
||||||
b = cluster_base_index(start, k)
|
b = cluster_base_index(start, k)
|
||||||
for dj in range(k):
|
for dj in range(k):
|
||||||
@@ -185,43 +152,9 @@ class Twinkle:
|
|||||||
base = palette[next_ci[i] % len(palette)]
|
base = palette[next_ci[i] % len(palette)]
|
||||||
self.driver.n[i] = self.driver.apply_brightness(base, preset.b)
|
self.driver.n[i] = self.driver.apply_brightness(base, preset.b)
|
||||||
else:
|
else:
|
||||||
self.driver.n[i] = (0, 0, 0)
|
self.driver.n[i] = bg_color
|
||||||
self.driver.n.write()
|
self.driver.n.write()
|
||||||
on = next_on
|
on = next_on
|
||||||
colour_i = next_ci
|
colour_i = next_ci
|
||||||
last_update = utime.ticks_add(last_update, delay_ms)
|
last_update = utime.ticks_add(last_update, delay_ms)
|
||||||
|
|
||||||
if dbg:
|
|
||||||
lo, hi = cluster_len_bounds()
|
|
||||||
if not dbg_banner:
|
|
||||||
dbg_banner = True
|
|
||||||
print(
|
|
||||||
"[twinkle] debug on: n1=%s n2=%s n3=%s n4=%s d=%s -> lo=%d hi=%d num=%d"
|
|
||||||
% (
|
|
||||||
preset.n1,
|
|
||||||
preset.n2,
|
|
||||||
preset.n3,
|
|
||||||
preset.n4,
|
|
||||||
preset.d,
|
|
||||||
lo,
|
|
||||||
hi,
|
|
||||||
num,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
rmin, rmax = on_run_min_max(on)
|
|
||||||
bad = lo > 0 and rmin > 0 and rmin < lo and num >= lo
|
|
||||||
if bad or (dbg_tick % _TWINKLE_DBG_INTERVAL == 0):
|
|
||||||
print(
|
|
||||||
"[twinkle] tick=%d rate=%d dens=%d L=%d D=%d on_runs min=%d max=%d%s"
|
|
||||||
% (
|
|
||||||
dbg_tick,
|
|
||||||
rate,
|
|
||||||
dens,
|
|
||||||
dbg_ops["L"],
|
|
||||||
dbg_ops["D"],
|
|
||||||
rmin,
|
|
||||||
rmax,
|
|
||||||
" **run<lo**" if bad else "",
|
|
||||||
)
|
|
||||||
)
|
|
||||||
yield
|
yield
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ class Preset:
|
|||||||
self.n4 = 0
|
self.n4 = 0
|
||||||
self.n5 = 0
|
self.n5 = 0
|
||||||
self.n6 = 0
|
self.n6 = 0
|
||||||
|
self.bg = (0, 0, 0)
|
||||||
|
|
||||||
# Override defaults with provided data
|
# Override defaults with provided data
|
||||||
self.edit(data)
|
self.edit(data)
|
||||||
@@ -25,9 +26,10 @@ class Preset:
|
|||||||
"delay": "d",
|
"delay": "d",
|
||||||
"brightness": "b",
|
"brightness": "b",
|
||||||
"auto": "a",
|
"auto": "a",
|
||||||
|
"background": "bg",
|
||||||
}
|
}
|
||||||
int_fields = {"d", "b", "n1", "n2", "n3", "n4", "n5", "n6"}
|
int_fields = {"d", "b", "n1", "n2", "n3", "n4", "n5", "n6"}
|
||||||
allowed_fields = {"p", "c", "d", "b", "a", "n1", "n2", "n3", "n4", "n5", "n6"}
|
allowed_fields = {"p", "c", "d", "b", "a", "bg", "n1", "n2", "n3", "n4", "n5", "n6"}
|
||||||
for key, value in data.items():
|
for key, value in data.items():
|
||||||
key = aliases.get(key, key)
|
key = aliases.get(key, key)
|
||||||
if key not in allowed_fields:
|
if key not in allowed_fields:
|
||||||
@@ -56,6 +58,21 @@ class Preset:
|
|||||||
elif key == "c":
|
elif key == "c":
|
||||||
if isinstance(value, (list, tuple)):
|
if isinstance(value, (list, tuple)):
|
||||||
self.c = value
|
self.c = value
|
||||||
|
elif key == "bg":
|
||||||
|
if isinstance(value, str) and value.startswith("#") and len(value) == 7:
|
||||||
|
try:
|
||||||
|
self.bg = (
|
||||||
|
int(value[1:3], 16),
|
||||||
|
int(value[3:5], 16),
|
||||||
|
int(value[5:7], 16),
|
||||||
|
)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
continue
|
||||||
|
elif isinstance(value, (list, tuple)) and len(value) == 3:
|
||||||
|
try:
|
||||||
|
self.bg = tuple(max(0, min(255, int(x))) for x in value)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
continue
|
||||||
else:
|
else:
|
||||||
setattr(self, key, value)
|
setattr(self, key, value)
|
||||||
return True
|
return True
|
||||||
@@ -100,6 +117,15 @@ class Preset:
|
|||||||
def auto(self, value):
|
def auto(self, value):
|
||||||
self.a = value
|
self.a = value
|
||||||
|
|
||||||
|
def background_or(self, colors=None, default=(0, 0, 0)):
|
||||||
|
bg = getattr(self, "bg", None)
|
||||||
|
if isinstance(bg, (list, tuple)) and len(bg) == 3:
|
||||||
|
try:
|
||||||
|
return tuple(max(0, min(255, int(x))) for x in bg)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return default
|
||||||
|
return default
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
return {
|
return {
|
||||||
"p": self.p,
|
"p": self.p,
|
||||||
@@ -107,6 +133,7 @@ class Preset:
|
|||||||
"b": self.b,
|
"b": self.b,
|
||||||
"c": self.c,
|
"c": self.c,
|
||||||
"a": self.a,
|
"a": self.a,
|
||||||
|
"bg": self.bg,
|
||||||
"n1": self.n1,
|
"n1": self.n1,
|
||||||
"n2": self.n2,
|
"n2": self.n2,
|
||||||
"n3": self.n3,
|
"n3": self.n3,
|
||||||
|
|||||||
@@ -106,10 +106,6 @@ class Presets:
|
|||||||
preset_data[color_key], order
|
preset_data[color_key], order
|
||||||
)
|
)
|
||||||
self.presets[name] = Preset(preset_data)
|
self.presets[name] = Preset(preset_data)
|
||||||
if self.presets:
|
|
||||||
print("Loaded presets:")
|
|
||||||
#for name in sorted(self.presets.keys()):
|
|
||||||
# print(f" {name}: {self.presets[name].to_dict()}")
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def edit(self, name, data):
|
def edit(self, name, data):
|
||||||
@@ -117,6 +113,15 @@ class Presets:
|
|||||||
if name in self.presets:
|
if name in self.presets:
|
||||||
# Update existing preset
|
# Update existing preset
|
||||||
self.presets[name].edit(data)
|
self.presets[name].edit(data)
|
||||||
|
# Editing the live preset (e.g. toggling auto/manual) must reset runtime
|
||||||
|
# state; re-select alone keeps step because preset name is unchanged.
|
||||||
|
if self.selected == name:
|
||||||
|
self.step = 0
|
||||||
|
self.generator = None
|
||||||
|
self.fill((0, 0, 0))
|
||||||
|
# Re-start pattern so manual/auto and other edits apply without a
|
||||||
|
# separate select message (controller usually sends both).
|
||||||
|
self.select(name)
|
||||||
else:
|
else:
|
||||||
if len(self.presets) >= MAX_PRESETS and name not in ("on", "off"):
|
if len(self.presets) >= MAX_PRESETS and name not in ("on", "off"):
|
||||||
print("Preset limit reached:", MAX_PRESETS)
|
print("Preset limit reached:", MAX_PRESETS)
|
||||||
@@ -159,6 +164,16 @@ class Presets:
|
|||||||
if preset_name in self.presets:
|
if preset_name in self.presets:
|
||||||
preset = self.presets[preset_name]
|
preset = self.presets[preset_name]
|
||||||
if preset.p in self.patterns:
|
if preset.p in self.patterns:
|
||||||
|
# Manual single-shot patterns: if this select arrives before the main loop has
|
||||||
|
# tick()'d the previous frame, completing it first keeps step in sync with beats.
|
||||||
|
if (
|
||||||
|
preset_name == self.selected
|
||||||
|
and not preset.a
|
||||||
|
and preset.p == "chase"
|
||||||
|
and self.generator is not None
|
||||||
|
):
|
||||||
|
while self.generator is not None:
|
||||||
|
self.tick()
|
||||||
# Set step value if explicitly provided
|
# Set step value if explicitly provided
|
||||||
if step is not None:
|
if step is not None:
|
||||||
self.step = step
|
self.step = step
|
||||||
@@ -166,6 +181,7 @@ class Presets:
|
|||||||
self.step = 0
|
self.step = 0
|
||||||
self.generator = self.patterns[preset.p](preset)
|
self.generator = self.patterns[preset.p](preset)
|
||||||
self.selected = preset_name # Store the preset name, not the object
|
self.selected = preset_name # Store the preset name, not the object
|
||||||
|
self.tick()
|
||||||
return True
|
return True
|
||||||
print("select failed: pattern not found for preset", preset_name, "pattern=", preset.p)
|
print("select failed: pattern not found for preset", preset_name, "pattern=", preset.p)
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -27,6 +27,9 @@ class Settings(dict):
|
|||||||
|
|
||||||
self["debug"] = False
|
self["debug"] = False
|
||||||
self["default"] = "on"
|
self["default"] = "on"
|
||||||
|
self["last_preset"] = ""
|
||||||
|
# Power-on: "default" | "last" | "off"
|
||||||
|
self["startup_mode"] = "default"
|
||||||
self["brightness"] = 32
|
self["brightness"] = 32
|
||||||
self["transport_type"] = "espnow"
|
self["transport_type"] = "espnow"
|
||||||
self["wifi_channel"] = 1
|
self["wifi_channel"] = 1
|
||||||
@@ -39,7 +42,6 @@ class Settings(dict):
|
|||||||
j = json.dumps(self)
|
j = json.dumps(self)
|
||||||
with open(self.SETTINGS_FILE, 'w') as file:
|
with open(self.SETTINGS_FILE, 'w') as file:
|
||||||
file.write(j)
|
file.write(j)
|
||||||
print("Settings saved successfully.")
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error saving settings: {e}")
|
print(f"Error saving settings: {e}")
|
||||||
|
|
||||||
@@ -48,7 +50,17 @@ class Settings(dict):
|
|||||||
with open(self.SETTINGS_FILE, 'r') as file:
|
with open(self.SETTINGS_FILE, 'r') as file:
|
||||||
loaded_settings = json.load(file)
|
loaded_settings = json.load(file)
|
||||||
self.update(loaded_settings)
|
self.update(loaded_settings)
|
||||||
print("Settings loaded successfully.")
|
old_recent = self.pop("recent_presets", None)
|
||||||
|
if isinstance(old_recent, list) and old_recent and not self.get("last_preset"):
|
||||||
|
for x in reversed(old_recent):
|
||||||
|
if isinstance(x, str) and x.strip():
|
||||||
|
self["last_preset"] = x.strip()
|
||||||
|
break
|
||||||
|
if x is not None:
|
||||||
|
s = str(x).strip()
|
||||||
|
if s:
|
||||||
|
self["last_preset"] = s
|
||||||
|
break
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error loading settings")
|
print(f"Error loading settings")
|
||||||
self.set_defaults()
|
self.set_defaults()
|
||||||
|
|||||||
@@ -5,33 +5,26 @@ import utime
|
|||||||
|
|
||||||
from presets import Presets
|
from presets import Presets
|
||||||
from settings import Settings
|
from settings import Settings
|
||||||
|
from controller_messages import apply_startup_pattern
|
||||||
|
|
||||||
|
|
||||||
def initialize_runtime():
|
def initialize_runtime():
|
||||||
machine.freq(160000000)
|
machine.freq(160000000)
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
print(settings)
|
|
||||||
|
|
||||||
wdt = machine.WDT(timeout=10000)
|
wdt = machine.WDT(timeout=10000)
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
|
|
||||||
gc.collect()
|
gc.collect()
|
||||||
print("mem before presets:", {"free": gc.mem_free(), "alloc": gc.mem_alloc()})
|
|
||||||
|
|
||||||
presets = Presets(settings["led_pin"], settings["num_leds"])
|
presets = Presets(settings["led_pin"], settings["num_leds"])
|
||||||
presets.load(settings)
|
presets.load(settings)
|
||||||
presets.b = settings.get("brightness", 255)
|
presets.b = settings.get("brightness", 255)
|
||||||
presets.debug = bool(settings.get("debug", False))
|
presets.debug = bool(settings.get("debug", False))
|
||||||
gc.collect()
|
gc.collect()
|
||||||
print("mem after presets:", {"free": gc.mem_free(), "alloc": gc.mem_alloc()})
|
|
||||||
|
|
||||||
default_preset = settings.get("default", "")
|
apply_startup_pattern(settings, presets)
|
||||||
if default_preset and default_preset in presets.presets:
|
|
||||||
if presets.select(default_preset):
|
|
||||||
print("Selected startup preset:", default_preset)
|
|
||||||
else:
|
|
||||||
print("Startup preset failed (invalid pattern?):", default_preset)
|
|
||||||
|
|
||||||
# On ESP32-C3, soft reboots can leave Wi-Fi driver state allocated.
|
# On ESP32-C3, soft reboots can leave Wi-Fi driver state allocated.
|
||||||
# Reset both interfaces and collect before bringing STA up.
|
# Reset both interfaces and collect before bringing STA up.
|
||||||
@@ -49,5 +42,10 @@ def initialize_runtime():
|
|||||||
utime.sleep(1)
|
utime.sleep(1)
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
|
|
||||||
print(sta_if.ifconfig())
|
try:
|
||||||
|
led_ip = sta_if.ifconfig()[0]
|
||||||
|
except Exception:
|
||||||
|
led_ip = "?"
|
||||||
|
print("led-driver IP:", led_ip, " led-controller IP:", "(not connected)")
|
||||||
|
|
||||||
return settings, presets, wdt, sta_if
|
return settings, presets, wdt, sta_if
|
||||||
|
|||||||
93
src/wifi_sta.py
Normal file
93
src/wifi_sta.py
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
"""STA connect helpers aligned with tests/test_wifi.py (status polling, fatal codes)."""
|
||||||
|
|
||||||
|
import utime
|
||||||
|
import network
|
||||||
|
|
||||||
|
_CONNECT_TIMEOUT_S = 45
|
||||||
|
_RETRY_DELAY_S = 2
|
||||||
|
|
||||||
|
|
||||||
|
def _wifi_status_label(code):
|
||||||
|
names = {
|
||||||
|
getattr(network, "STAT_IDLE", 0): "idle",
|
||||||
|
getattr(network, "STAT_CONNECTING", 1): "connecting",
|
||||||
|
getattr(network, "STAT_WRONG_PASSWORD", -3): "wrong_password",
|
||||||
|
getattr(network, "STAT_NO_AP_FOUND", -2): "no_ap_found",
|
||||||
|
getattr(network, "STAT_CONNECT_FAIL", -1): "connect_fail",
|
||||||
|
getattr(network, "STAT_GOT_IP", 3): "got_ip",
|
||||||
|
}
|
||||||
|
return names.get(code, str(code))
|
||||||
|
|
||||||
|
|
||||||
|
# Only abort the wait loop immediately on wrong password. NO_AP_FOUND / CONNECT_FAIL are often
|
||||||
|
# transient while the radio is still scanning (ESP32-C3 may report them before the AP appears).
|
||||||
|
_ABORT_WAIT_IMMEDIATE = (
|
||||||
|
getattr(network, "STAT_WRONG_PASSWORD", -3),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _one_association_campaign(sta_if, ssid, password, wdt):
|
||||||
|
"""disconnect → connect → wait until connected, wrong password, or timeout. Returns True if connected."""
|
||||||
|
try:
|
||||||
|
sta_if.disconnect()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
utime.sleep_ms(200)
|
||||||
|
try:
|
||||||
|
sta_if.connect(ssid, password)
|
||||||
|
except Exception as ex:
|
||||||
|
print("wifi_sta: connect raised:", ex)
|
||||||
|
return False
|
||||||
|
|
||||||
|
start = utime.time()
|
||||||
|
last_status = None
|
||||||
|
while not sta_if.isconnected():
|
||||||
|
status = sta_if.status()
|
||||||
|
if status != last_status:
|
||||||
|
print("wifi_sta: status", status, _wifi_status_label(status))
|
||||||
|
last_status = status
|
||||||
|
if status in _ABORT_WAIT_IMMEDIATE:
|
||||||
|
return False
|
||||||
|
if utime.time() - start >= _CONNECT_TIMEOUT_S:
|
||||||
|
print("wifi_sta: association timeout")
|
||||||
|
return False
|
||||||
|
utime.sleep(1)
|
||||||
|
if wdt is not None:
|
||||||
|
wdt.feed()
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def connect_until_up(sta_if, ssid, password, wdt):
|
||||||
|
"""Boot: repeat campaigns until STA has a route (same strategy as tests/test_wifi.py)."""
|
||||||
|
if not ssid:
|
||||||
|
print("wifi_sta: no ssid in settings")
|
||||||
|
return False
|
||||||
|
attempt = 0
|
||||||
|
while True:
|
||||||
|
attempt += 1
|
||||||
|
print("wifi_sta: boot attempt", attempt, "ssid=", repr(ssid))
|
||||||
|
if _one_association_campaign(sta_if, ssid, password, wdt):
|
||||||
|
try:
|
||||||
|
print("wifi_sta: connected", sta_if.ifconfig()[0])
|
||||||
|
except Exception:
|
||||||
|
print("wifi_sta: connected")
|
||||||
|
return True
|
||||||
|
print("wifi_sta: retry in", _RETRY_DELAY_S, "s")
|
||||||
|
for _ in range(_RETRY_DELAY_S):
|
||||||
|
utime.sleep(1)
|
||||||
|
if wdt is not None:
|
||||||
|
wdt.feed()
|
||||||
|
|
||||||
|
|
||||||
|
def try_reconnect(sta_if, ssid, password, wdt):
|
||||||
|
"""Runtime: single association campaign after link loss; non-looping."""
|
||||||
|
if not ssid:
|
||||||
|
return False
|
||||||
|
print("wifi_sta: reconnect")
|
||||||
|
ok = _one_association_campaign(sta_if, ssid, password, wdt)
|
||||||
|
if ok:
|
||||||
|
try:
|
||||||
|
print("wifi_sta: connected", sta_if.ifconfig()[0])
|
||||||
|
except Exception:
|
||||||
|
print("wifi_sta: connected")
|
||||||
|
return ok
|
||||||
31
tests/all.py
31
tests/all.py
@@ -184,6 +184,36 @@ def test_pattern_smoke():
|
|||||||
ctx.tick_for_ms(120)
|
ctx.tick_for_ms(120)
|
||||||
|
|
||||||
|
|
||||||
|
def test_patterns_do_not_use_blocking_sleep():
|
||||||
|
pattern_dir = "patterns"
|
||||||
|
offenders = []
|
||||||
|
try:
|
||||||
|
files = os.listdir(pattern_dir)
|
||||||
|
except OSError:
|
||||||
|
raise AssertionError("patterns directory is missing")
|
||||||
|
|
||||||
|
for filename in files:
|
||||||
|
if not filename.endswith(".py") or filename in ("__init__.py", "main.py"):
|
||||||
|
continue
|
||||||
|
path = pattern_dir + "/" + filename
|
||||||
|
try:
|
||||||
|
with open(path, "r") as f:
|
||||||
|
src = f.read()
|
||||||
|
except OSError:
|
||||||
|
offenders.append(filename + " (unreadable)")
|
||||||
|
continue
|
||||||
|
|
||||||
|
if (
|
||||||
|
"utime.sleep(" in src
|
||||||
|
or "utime.sleep_ms(" in src
|
||||||
|
or "time.sleep(" in src
|
||||||
|
or "time.sleep_ms(" in src
|
||||||
|
):
|
||||||
|
offenders.append(filename)
|
||||||
|
|
||||||
|
assert not offenders, "blocking sleep found in patterns: %s" % ", ".join(offenders)
|
||||||
|
|
||||||
|
|
||||||
def test_default_requires_existing_preset():
|
def test_default_requires_existing_preset():
|
||||||
ctx = _TestContext()
|
ctx = _TestContext()
|
||||||
_process_message(ctx, {"v": "1", "default": "missing"})
|
_process_message(ctx, {"v": "1", "default": "missing"})
|
||||||
@@ -242,6 +272,7 @@ def run_all():
|
|||||||
test_preset_edit_sanitization,
|
test_preset_edit_sanitization,
|
||||||
test_colour_conversion_and_transition,
|
test_colour_conversion_and_transition,
|
||||||
test_pattern_smoke,
|
test_pattern_smoke,
|
||||||
|
test_patterns_do_not_use_blocking_sleep,
|
||||||
test_default_requires_existing_preset,
|
test_default_requires_existing_preset,
|
||||||
test_default_targets_gate_by_device_name,
|
test_default_targets_gate_by_device_name,
|
||||||
test_save_and_load_roundtrip,
|
test_save_and_load_roundtrip,
|
||||||
|
|||||||
52
tests/patterns/radiate.py
Normal file
52
tests/patterns/radiate.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print("[test] radiate: start")
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
p.debug = True
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
print("[test] radiate: auto phase begin")
|
||||||
|
p.edit("test_pattern", {"p": "radiate", "b": 64, "a": True, "d": 3000, "c": [(255, 0, 0), (0, 0, 255)]})
|
||||||
|
if not p.select("test_pattern"):
|
||||||
|
raise Exception("radiate select failed in auto phase")
|
||||||
|
auto_start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), auto_start) < 2500:
|
||||||
|
wdt.feed()
|
||||||
|
p.run_step()
|
||||||
|
utime.sleep_ms(20)
|
||||||
|
remaining_ms = utime.ticks_diff(p.next_tick_ms, utime.ticks_ms())
|
||||||
|
if p.next_tick_ms == 0 or remaining_ms <= 0:
|
||||||
|
raise Exception("radiate delay scheduling invalid")
|
||||||
|
print("[test] radiate: auto phase end")
|
||||||
|
|
||||||
|
print("[test] radiate: manual phase begin")
|
||||||
|
p.edit("test_pattern", {"p": "radiate", "b": 64, "a": False, "d": 3000, "c": [(255, 0, 0), (0, 0, 255)]})
|
||||||
|
if not p.select("test_pattern", step=0):
|
||||||
|
raise Exception("radiate select failed in manual phase")
|
||||||
|
for _ in range(6):
|
||||||
|
current_step = int(p.step)
|
||||||
|
if not p.select("test_pattern", step=current_step):
|
||||||
|
raise Exception("radiate external select failed")
|
||||||
|
p.run_step()
|
||||||
|
wdt.feed()
|
||||||
|
if int(p.step) == current_step:
|
||||||
|
raise Exception("radiate external step did not advance")
|
||||||
|
if p.generator is not None:
|
||||||
|
raise Exception("radiate manual mode rescheduled generator")
|
||||||
|
hold_start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), hold_start) < 700:
|
||||||
|
wdt.feed()
|
||||||
|
utime.sleep_ms(20)
|
||||||
|
print("[test] radiate: manual phase end")
|
||||||
|
print("[test] radiate: pass")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
52
tests/patterns/twinkle.py
Normal file
52
tests/patterns/twinkle.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import utime
|
||||||
|
from machine import WDT
|
||||||
|
from settings import Settings
|
||||||
|
from presets import Presets
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print("[test] twinkle: start")
|
||||||
|
s = Settings()
|
||||||
|
p = Presets(pin=s.get("led_pin", 10), num_leds=s.get("num_leds", 30))
|
||||||
|
p.debug = True
|
||||||
|
wdt = WDT(timeout=10000)
|
||||||
|
|
||||||
|
print("[test] twinkle: auto phase begin")
|
||||||
|
p.edit("test_pattern", {"p": "twinkle", "b": 64, "a": True, "d": 3000, "c": [(255, 0, 0), (0, 0, 255)]})
|
||||||
|
if not p.select("test_pattern"):
|
||||||
|
raise Exception("twinkle select failed in auto phase")
|
||||||
|
auto_start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), auto_start) < 2500:
|
||||||
|
wdt.feed()
|
||||||
|
p.run_step()
|
||||||
|
utime.sleep_ms(20)
|
||||||
|
remaining_ms = utime.ticks_diff(p.next_tick_ms, utime.ticks_ms())
|
||||||
|
if p.next_tick_ms == 0 or remaining_ms <= 0:
|
||||||
|
raise Exception("twinkle delay scheduling invalid")
|
||||||
|
print("[test] twinkle: auto phase end")
|
||||||
|
|
||||||
|
print("[test] twinkle: manual phase begin")
|
||||||
|
p.edit("test_pattern", {"p": "twinkle", "b": 64, "a": False, "d": 3000, "c": [(255, 0, 0), (0, 0, 255)]})
|
||||||
|
if not p.select("test_pattern", step=0):
|
||||||
|
raise Exception("twinkle select failed in manual phase")
|
||||||
|
for _ in range(6):
|
||||||
|
current_step = int(p.step)
|
||||||
|
if not p.select("test_pattern", step=current_step):
|
||||||
|
raise Exception("twinkle external select failed")
|
||||||
|
p.run_step()
|
||||||
|
wdt.feed()
|
||||||
|
if int(p.step) == current_step:
|
||||||
|
raise Exception("twinkle external step did not advance")
|
||||||
|
if p.generator is not None:
|
||||||
|
raise Exception("twinkle manual mode rescheduled generator")
|
||||||
|
hold_start = utime.ticks_ms()
|
||||||
|
while utime.ticks_diff(utime.ticks_ms(), hold_start) < 700:
|
||||||
|
wdt.feed()
|
||||||
|
utime.sleep_ms(20)
|
||||||
|
print("[test] twinkle: manual phase end")
|
||||||
|
print("[test] twinkle: pass")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user