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)
|
apply_presets(data, settings, presets)
|
||||||
if "clear_presets" in data:
|
if "clear_presets" in data:
|
||||||
apply_clear_presets(data, presets)
|
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)
|
apply_select(data, settings, presets)
|
||||||
if "default" in data:
|
if "default" in data:
|
||||||
apply_default(data, settings, presets)
|
apply_default(data, settings, presets)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
"""ESP-NOW receive loop and boot announce."""
|
"""ESP-NOW receive loop and boot announce."""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import urandom
|
||||||
|
|
||||||
import espnow
|
import espnow
|
||||||
import network
|
import network
|
||||||
@@ -12,13 +13,21 @@ from espnow_wire import (
|
|||||||
MSG_CMD,
|
MSG_CMD,
|
||||||
MSG_GROUP_CMD,
|
MSG_GROUP_CMD,
|
||||||
MSG_GROUPS,
|
MSG_GROUPS,
|
||||||
|
MSG_PING_REQ,
|
||||||
cmd_envelope,
|
cmd_envelope,
|
||||||
pack_announce,
|
pack_announce,
|
||||||
|
pack_ping_rsp,
|
||||||
parse_group_cmd,
|
parse_group_cmd,
|
||||||
parse_groups,
|
parse_groups,
|
||||||
|
parse_ping_req,
|
||||||
wire_msg_type,
|
wire_msg_type,
|
||||||
)
|
)
|
||||||
|
|
||||||
from controller_messages import process_data
|
from controller_messages import process_data
|
||||||
|
from settings import WIFI_CHANNEL_DEFAULT
|
||||||
|
|
||||||
|
_PING_DELAY_MS_MIN = 50
|
||||||
|
_PING_DELAY_MS_MAX = 500
|
||||||
|
|
||||||
_esp = None
|
_esp = None
|
||||||
_groups_received = False
|
_groups_received = False
|
||||||
@@ -26,14 +35,14 @@ _groups_received = False
|
|||||||
|
|
||||||
def init_espnow(settings):
|
def init_espnow(settings):
|
||||||
global _esp
|
global _esp
|
||||||
ch = 6
|
|
||||||
try:
|
try:
|
||||||
ch = int(settings.get("wifi_channel", 1))
|
ch = int(settings.get("wifi_channel", WIFI_CHANNEL_DEFAULT))
|
||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError):
|
||||||
pass
|
ch = WIFI_CHANNEL_DEFAULT
|
||||||
ch = max(1, min(11, ch))
|
ch = max(1, min(11, ch))
|
||||||
sta = network.WLAN(network.STA_IF)
|
sta = network.WLAN(network.STA_IF)
|
||||||
sta.active(True)
|
sta.active(True)
|
||||||
|
sta.config(pm=network.WLAN.PM_NONE)
|
||||||
sta.config(channel=ch)
|
sta.config(channel=ch)
|
||||||
_esp = espnow.ESPNow()
|
_esp = espnow.ESPNow()
|
||||||
_esp.active(True)
|
_esp.active(True)
|
||||||
@@ -44,6 +53,42 @@ def init_espnow(settings):
|
|||||||
return _esp
|
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):
|
def send_boot_announce(settings):
|
||||||
if _esp is None:
|
if _esp is None:
|
||||||
return
|
return
|
||||||
@@ -61,7 +106,7 @@ def send_boot_announce(settings):
|
|||||||
print("espnow announce failed:", e)
|
print("espnow announce failed:", e)
|
||||||
|
|
||||||
|
|
||||||
def _handle_packet(pkt, settings, presets):
|
def _handle_packet(host, pkt, settings, presets):
|
||||||
global _groups_received
|
global _groups_received
|
||||||
mt = wire_msg_type(pkt)
|
mt = wire_msg_type(pkt)
|
||||||
if mt == MSG_GROUPS:
|
if mt == MSG_GROUPS:
|
||||||
@@ -91,6 +136,11 @@ def _handle_packet(pkt, settings, presets):
|
|||||||
if env:
|
if env:
|
||||||
process_data(env, settings, presets, save=save)
|
process_data(env, settings, presets, save=save)
|
||||||
return
|
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:
|
if mt == MSG_ANNOUNCE:
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -112,7 +162,7 @@ async def espnow_receive_loop(settings, presets, wdt=None):
|
|||||||
wdt.feed()
|
wdt.feed()
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
_handle_packet(msg, settings, presets)
|
_handle_packet(host, msg, settings, presets)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("espnow rx error:", e)
|
print("espnow rx error:", e)
|
||||||
if wdt:
|
if wdt:
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ MSG_ANNOUNCE = 0x01
|
|||||||
MSG_GROUPS = 0x02
|
MSG_GROUPS = 0x02
|
||||||
MSG_CMD = 0x03
|
MSG_CMD = 0x03
|
||||||
MSG_GROUP_CMD = 0x04
|
MSG_GROUP_CMD = 0x04
|
||||||
|
MSG_PING_REQ = 0x05
|
||||||
|
MSG_PING_RSP = 0x06
|
||||||
|
|
||||||
BROADCAST_MAC = b"\xff\xff\xff\xff\xff\xff"
|
BROADCAST_MAC = b"\xff\xff\xff\xff\xff\xff"
|
||||||
|
|
||||||
@@ -104,6 +106,29 @@ def cmd_envelope(payload):
|
|||||||
return env[:need], save
|
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):
|
def wire_msg_type(payload):
|
||||||
if len(payload) >= 2 and payload[0] == WIRE_MAGIC:
|
if len(payload) >= 2 and payload[0] == WIRE_MAGIC:
|
||||||
return payload[1]
|
return payload[1]
|
||||||
|
|||||||
20
src/main.py
20
src/main.py
@@ -42,31 +42,27 @@ esp.send(BROADCAST_MAC, hello)
|
|||||||
print("espnow hello", len(hello), "B")
|
print("espnow hello", len(hello), "B")
|
||||||
|
|
||||||
|
|
||||||
def _on_espnow_message(msg):
|
def _on_espnow_message(host, msg):
|
||||||
if not msg:
|
if not msg:
|
||||||
return
|
return
|
||||||
if msg[0] == WIRE_MAGIC:
|
if msg[0] == WIRE_MAGIC:
|
||||||
_handle_packet(msg, settings, presets)
|
_handle_packet(host, msg, settings, presets)
|
||||||
return
|
return
|
||||||
if msg[0:1] == b"{":
|
if msg[0:1] == b"{":
|
||||||
process_data(msg, settings, presets)
|
process_data(msg, settings, presets)
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
|
||||||
while True:
|
while True:
|
||||||
presets.tick()
|
|
||||||
wdt.feed()
|
wdt.feed()
|
||||||
if esp.any():
|
while esp.any():
|
||||||
host, msg = esp.recv(0)
|
host, msg = esp.recv(0)
|
||||||
if host and msg:
|
if not host or not msg:
|
||||||
|
continue
|
||||||
print(host, len(msg), "B")
|
print(host, len(msg), "B")
|
||||||
try:
|
try:
|
||||||
_on_espnow_message(msg)
|
_on_espnow_message(host, msg)
|
||||||
print(msg)
|
print(msg)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("espnow rx error:", e)
|
print("espnow rx error:", e)
|
||||||
await asyncio.sleep(0)
|
presets.tick()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
asyncio.run(main())
|
|
||||||
|
|||||||
@@ -201,15 +201,12 @@ class Presets:
|
|||||||
self.fill((0, 0, 0))
|
self.fill((0, 0, 0))
|
||||||
self.selected = preset_name
|
self.selected = preset_name
|
||||||
return True
|
return True
|
||||||
# Manual single-shot patterns: if this select arrives before the main loop has
|
# If re-selecting the same preset before the main loop has tick()'d the
|
||||||
# tick()'d the previous frame, completing it first keeps step in sync with beats.
|
# previous frame, run one pending tick so step stays in sync.
|
||||||
if (
|
if (
|
||||||
preset_name == self.selected
|
preset_name == self.selected
|
||||||
and not preset.a
|
|
||||||
and preset.p in ("chase", "pulse")
|
|
||||||
and self.generator is not None
|
and self.generator is not None
|
||||||
):
|
):
|
||||||
while self.generator is not None:
|
|
||||||
self.tick()
|
self.tick()
|
||||||
# Set step value if explicitly provided
|
# Set step value if explicitly provided
|
||||||
if step is not None:
|
if step is not None:
|
||||||
@@ -274,11 +271,22 @@ class Presets:
|
|||||||
colors = preset.c if preset.c else [(255, 255, 255)]
|
colors = preset.c if preset.c else [(255, 255, 255)]
|
||||||
bg_color = self.apply_brightness(preset.background_or(colors), preset.b)
|
bg_color = self.apply_brightness(preset.background_or(colors), preset.b)
|
||||||
color_index = 0
|
color_index = 0
|
||||||
state = True
|
delay_ms = max(1, int(preset.d))
|
||||||
last_update = utime.ticks_ms()
|
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:
|
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:
|
if utime.ticks_diff(now, last_update) >= delay_ms:
|
||||||
if state:
|
if state:
|
||||||
base = colors[color_index % len(colors)]
|
base = colors[color_index % len(colors)]
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import ubinascii
|
|||||||
import machine
|
import machine
|
||||||
import network
|
import network
|
||||||
|
|
||||||
|
WIFI_CHANNEL_DEFAULT = 5
|
||||||
|
|
||||||
class Settings(dict):
|
class Settings(dict):
|
||||||
SETTINGS_FILE = "/settings.json"
|
SETTINGS_FILE = "/settings.json"
|
||||||
|
|
||||||
@@ -32,7 +34,7 @@ class Settings(dict):
|
|||||||
self["startup_mode"] = "default"
|
self["startup_mode"] = "default"
|
||||||
self["brightness"] = 32
|
self["brightness"] = 32
|
||||||
|
|
||||||
self["wifi_channel"] = 1
|
self["wifi_channel"] = WIFI_CHANNEL_DEFAULT
|
||||||
|
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user