feat(driver): add HTTP routes, startup split, and binary envelope support
Wire controller messages through new modules (background tasks, runtime state, startup) and add binary envelope handling. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
42
src/background_tasks.py
Normal file
42
src/background_tasks.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import asyncio
|
||||
import gc
|
||||
import utime
|
||||
|
||||
from hello import broadcast_hello_udp
|
||||
|
||||
|
||||
async def presets_loop(presets, wdt):
|
||||
last_mem_log = utime.ticks_ms()
|
||||
while True:
|
||||
presets.tick()
|
||||
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)
|
||||
|
||||
|
||||
async def udp_hello_loop_after_http_ready(sta_if, settings, wdt, runtime_state):
|
||||
"""Broadcast hello at startup-fast cadence, then slower cadence."""
|
||||
await asyncio.sleep(1)
|
||||
started_ms = utime.ticks_ms()
|
||||
while True:
|
||||
if runtime_state.hello:
|
||||
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)
|
||||
elapsed_ms = utime.ticks_diff(utime.ticks_ms(), started_ms)
|
||||
interval_s = 5 if elapsed_ms < 60000 else 60
|
||||
await asyncio.sleep(interval_s)
|
||||
Reference in New Issue
Block a user