Compare commits
5 Commits
a97f6c7c2c
...
088fe161a8
| Author | SHA1 | Date | |
|---|---|---|---|
| 088fe161a8 | |||
| c9895df512 | |||
| 39a84696c3 | |||
| c7560b2e87 | |||
| ea21563900 |
@@ -82,7 +82,7 @@ def process_data(payload, settings, presets, controller_ip=None, save=False):
|
||||
apply_presets(data, settings, presets)
|
||||
if "clear_presets" in data:
|
||||
apply_clear_presets(data, presets)
|
||||
elif "select" in data or "s" in data:
|
||||
if ("select" in data or "s" in data) and "presets" not in data:
|
||||
apply_select(data, settings, presets)
|
||||
if "default" in data:
|
||||
apply_default(data, settings, presets)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""ESP-NOW receive loop and boot announce."""
|
||||
|
||||
import asyncio
|
||||
import urandom
|
||||
|
||||
import espnow
|
||||
import network
|
||||
@@ -12,13 +13,21 @@ from espnow_wire import (
|
||||
MSG_CMD,
|
||||
MSG_GROUP_CMD,
|
||||
MSG_GROUPS,
|
||||
MSG_PING_REQ,
|
||||
cmd_envelope,
|
||||
pack_announce,
|
||||
pack_ping_rsp,
|
||||
parse_group_cmd,
|
||||
parse_groups,
|
||||
parse_ping_req,
|
||||
wire_msg_type,
|
||||
)
|
||||
|
||||
from controller_messages import process_data
|
||||
from settings import WIFI_CHANNEL_DEFAULT
|
||||
|
||||
_PING_DELAY_MS_MIN = 50
|
||||
_PING_DELAY_MS_MAX = 500
|
||||
|
||||
_esp = None
|
||||
_groups_received = False
|
||||
@@ -26,14 +35,14 @@ _groups_received = False
|
||||
|
||||
def init_espnow(settings):
|
||||
global _esp
|
||||
ch = 6
|
||||
try:
|
||||
ch = int(settings.get("wifi_channel", 1))
|
||||
ch = int(settings.get("wifi_channel", WIFI_CHANNEL_DEFAULT))
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
ch = WIFI_CHANNEL_DEFAULT
|
||||
ch = max(1, min(11, ch))
|
||||
sta = network.WLAN(network.STA_IF)
|
||||
sta.active(True)
|
||||
sta.config(pm=network.WLAN.PM_NONE)
|
||||
sta.config(channel=ch)
|
||||
_esp = espnow.ESPNow()
|
||||
_esp.active(True)
|
||||
@@ -44,6 +53,42 @@ def init_espnow(settings):
|
||||
return _esp
|
||||
|
||||
|
||||
def _send_ping_rsp(host, settings, ping_id, delay_ms):
|
||||
import utime
|
||||
|
||||
utime.sleep_ms(delay_ms)
|
||||
if _esp is None or not host or len(host) != 6:
|
||||
return
|
||||
pkt = pack_ping_rsp(ping_id, settings.get("name", "led"))
|
||||
try:
|
||||
try:
|
||||
_esp.add_peer(host)
|
||||
except Exception:
|
||||
pass
|
||||
_esp.send(host, pkt)
|
||||
print("espnow ping rsp", ping_id, delay_ms, "ms")
|
||||
except Exception as e:
|
||||
print("espnow ping rsp failed:", e)
|
||||
|
||||
|
||||
async def _send_ping_rsp_delayed(host, settings, ping_id):
|
||||
span = _PING_DELAY_MS_MAX - _PING_DELAY_MS_MIN
|
||||
delay_ms = _PING_DELAY_MS_MIN + (urandom.getrandbits(10) % (span + 1))
|
||||
await asyncio.sleep(delay_ms / 1000)
|
||||
_send_ping_rsp(host, settings, ping_id, delay_ms)
|
||||
|
||||
|
||||
def _schedule_ping_rsp(host, settings, ping_id):
|
||||
span = _PING_DELAY_MS_MAX - _PING_DELAY_MS_MIN
|
||||
delay_ms = _PING_DELAY_MS_MIN + (urandom.getrandbits(10) % (span + 1))
|
||||
try:
|
||||
import _thread
|
||||
|
||||
_thread.start_new_thread(_send_ping_rsp, (host, settings, ping_id, delay_ms))
|
||||
except ImportError:
|
||||
asyncio.create_task(_send_ping_rsp_delayed(host, settings, ping_id))
|
||||
|
||||
|
||||
def send_boot_announce(settings):
|
||||
if _esp is None:
|
||||
return
|
||||
@@ -61,7 +106,7 @@ def send_boot_announce(settings):
|
||||
print("espnow announce failed:", e)
|
||||
|
||||
|
||||
def _handle_packet(pkt, settings, presets):
|
||||
def _handle_packet(host, pkt, settings, presets):
|
||||
global _groups_received
|
||||
mt = wire_msg_type(pkt)
|
||||
if mt == MSG_GROUPS:
|
||||
@@ -91,6 +136,11 @@ def _handle_packet(pkt, settings, presets):
|
||||
if env:
|
||||
process_data(env, settings, presets, save=save)
|
||||
return
|
||||
if mt == MSG_PING_REQ:
|
||||
ping_id = parse_ping_req(pkt)
|
||||
if ping_id is not None and host and len(host) == 6:
|
||||
_schedule_ping_rsp(host, settings, ping_id)
|
||||
return
|
||||
if mt == MSG_ANNOUNCE:
|
||||
return
|
||||
|
||||
@@ -112,7 +162,7 @@ async def espnow_receive_loop(settings, presets, wdt=None):
|
||||
wdt.feed()
|
||||
continue
|
||||
try:
|
||||
_handle_packet(msg, settings, presets)
|
||||
_handle_packet(host, msg, settings, presets)
|
||||
except Exception as e:
|
||||
print("espnow rx error:", e)
|
||||
if wdt:
|
||||
|
||||
@@ -9,6 +9,8 @@ MSG_ANNOUNCE = 0x01
|
||||
MSG_GROUPS = 0x02
|
||||
MSG_CMD = 0x03
|
||||
MSG_GROUP_CMD = 0x04
|
||||
MSG_PING_REQ = 0x05
|
||||
MSG_PING_RSP = 0x06
|
||||
|
||||
BROADCAST_MAC = b"\xff\xff\xff\xff\xff\xff"
|
||||
|
||||
@@ -104,6 +106,29 @@ def cmd_envelope(payload):
|
||||
return env[:need], save
|
||||
|
||||
|
||||
def pack_ping_req(ping_id):
|
||||
body = struct.pack("<I", int(ping_id) & 0xFFFFFFFF)
|
||||
return _pack_header(MSG_PING_REQ, body)
|
||||
|
||||
|
||||
def parse_ping_req(payload):
|
||||
if len(payload) >= 2 and payload[0] == WIRE_MAGIC:
|
||||
if payload[1] != MSG_PING_REQ:
|
||||
return None
|
||||
body = payload[2:]
|
||||
else:
|
||||
body = payload
|
||||
if len(body) < 4:
|
||||
return None
|
||||
return struct.unpack("<I", body[:4])[0]
|
||||
|
||||
|
||||
def pack_ping_rsp(ping_id, name):
|
||||
name_b = name.encode("utf-8")
|
||||
body = struct.pack("<I", int(ping_id) & 0xFFFFFFFF) + bytes([len(name_b)]) + name_b
|
||||
return _pack_header(MSG_PING_RSP, body)
|
||||
|
||||
|
||||
def wire_msg_type(payload):
|
||||
if len(payload) >= 2 and payload[0] == WIRE_MAGIC:
|
||||
return payload[1]
|
||||
|
||||
22
src/main.py
22
src/main.py
@@ -42,31 +42,27 @@ esp.send(BROADCAST_MAC, hello)
|
||||
print("espnow hello", len(hello), "B")
|
||||
|
||||
|
||||
def _on_espnow_message(msg):
|
||||
def _on_espnow_message(host, msg):
|
||||
if not msg:
|
||||
return
|
||||
if msg[0] == WIRE_MAGIC:
|
||||
_handle_packet(msg, settings, presets)
|
||||
_handle_packet(host, msg, settings, presets)
|
||||
return
|
||||
if msg[0:1] == b"{":
|
||||
process_data(msg, settings, presets)
|
||||
|
||||
|
||||
async def main():
|
||||
while True:
|
||||
presets.tick()
|
||||
|
||||
while True:
|
||||
wdt.feed()
|
||||
if esp.any():
|
||||
while esp.any():
|
||||
host, msg = esp.recv(0)
|
||||
if host and msg:
|
||||
if not host or not msg:
|
||||
continue
|
||||
print(host, len(msg), "B")
|
||||
try:
|
||||
_on_espnow_message(msg)
|
||||
_on_espnow_message(host, msg)
|
||||
print(msg)
|
||||
except Exception as e:
|
||||
print("espnow rx error:", e)
|
||||
await asyncio.sleep(0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
presets.tick()
|
||||
|
||||
@@ -201,15 +201,12 @@ class Presets:
|
||||
self.fill((0, 0, 0))
|
||||
self.selected = preset_name
|
||||
return True
|
||||
# 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 re-selecting the same preset before the main loop has tick()'d the
|
||||
# previous frame, run one pending tick so step stays in sync.
|
||||
if (
|
||||
preset_name == self.selected
|
||||
and not preset.a
|
||||
and preset.p in ("chase", "pulse")
|
||||
and self.generator is not None
|
||||
):
|
||||
while self.generator is not None:
|
||||
self.tick()
|
||||
# Set step value if explicitly provided
|
||||
if step is not None:
|
||||
@@ -274,11 +271,22 @@ class Presets:
|
||||
colors = preset.c if preset.c else [(255, 255, 255)]
|
||||
bg_color = self.apply_brightness(preset.background_or(colors), preset.b)
|
||||
color_index = 0
|
||||
state = True
|
||||
last_update = utime.ticks_ms()
|
||||
delay_ms = max(1, int(preset.d))
|
||||
period = delay_ms * 2
|
||||
now = utime.ticks_ms()
|
||||
# Phase-lock to wall time so group identify (broadcast select) stays in sync even
|
||||
# when devices process the packet on different main-loop iterations.
|
||||
phase = now % period if period else 0
|
||||
state = phase < delay_ms
|
||||
last_update = utime.ticks_add(now, -phase)
|
||||
if state:
|
||||
base = colors[color_index % len(colors)]
|
||||
self.fill(self.apply_brightness(base, preset.b))
|
||||
color_index += 1
|
||||
else:
|
||||
self.fill(bg_color)
|
||||
while True:
|
||||
now = utime.ticks_ms()
|
||||
delay_ms = max(1, int(preset.d))
|
||||
if utime.ticks_diff(now, last_update) >= delay_ms:
|
||||
if state:
|
||||
base = colors[color_index % len(colors)]
|
||||
|
||||
@@ -3,6 +3,8 @@ import ubinascii
|
||||
import machine
|
||||
import network
|
||||
|
||||
WIFI_CHANNEL_DEFAULT = 5
|
||||
|
||||
class Settings(dict):
|
||||
SETTINGS_FILE = "/settings.json"
|
||||
|
||||
@@ -32,7 +34,7 @@ class Settings(dict):
|
||||
self["startup_mode"] = "default"
|
||||
self["brightness"] = 32
|
||||
|
||||
self["wifi_channel"] = 1
|
||||
self["wifi_channel"] = WIFI_CHANNEL_DEFAULT
|
||||
|
||||
|
||||
def save(self):
|
||||
|
||||
Reference in New Issue
Block a user