Replace custom UDP RGB with DDP on 4048, fix picotool -f ordering for v1.1.2, and chunk test frames under MTU so the W5500 receives them. Co-authored-by: Cursor <cursoragent@cursor.com>
153 lines
4.5 KiB
Python
Executable File
153 lines
4.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
||
"""Drive-test all 8 WS2812 strips on led-driver-8 over DDP.
|
||
|
||
One DDP packet (UDP 4048, destination ID 1) carries every strip:
|
||
strip0 RGB | strip1 RGB | … | strip7 RGB
|
||
(equal LED count per strip). PUSH shows the frame.
|
||
|
||
Defaults match firmware board_config.h strip map.
|
||
|
||
Usage:
|
||
python3 examples/test_strips.py
|
||
python3 examples/test_strips.py --host 10.1.1.10 --leds 64
|
||
python3 examples/test_strips.py --host 10.1.1.10 --all-only
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import argparse
|
||
import socket
|
||
import struct
|
||
import time
|
||
|
||
NUM_STRIPS = 8
|
||
|
||
DEFAULT_STRIPS = (
|
||
# (strip_index, gpio)
|
||
(0, 18),
|
||
(1, 19),
|
||
(2, 20),
|
||
(3, 21),
|
||
(4, 22),
|
||
(5, 26),
|
||
(6, 27),
|
||
(7, 28),
|
||
)
|
||
|
||
IDENTITY_COLOURS = (
|
||
(255, 0, 0), # red
|
||
(0, 255, 0), # green
|
||
(0, 0, 255), # blue
|
||
(255, 255, 0), # yellow
|
||
(255, 0, 255), # magenta
|
||
(0, 255, 255), # cyan
|
||
(255, 128, 0), # orange
|
||
(255, 255, 255), # white
|
||
)
|
||
|
||
ALL_COLOURS = (
|
||
("RED", (255, 0, 0)),
|
||
("GREEN", (0, 255, 0)),
|
||
("BLUE", (0, 0, 255)),
|
||
("WHITE", (255, 255, 255)),
|
||
)
|
||
|
||
DDP_VER1 = 0x40
|
||
DDP_PUSH = 0x01
|
||
DDP_TYPE_RGB888 = 0x0B
|
||
DDP_ID = 1
|
||
|
||
# Stay under typical UDP/IPv4 MTU (1472). W5500 does not reassemble fragments.
|
||
# 10-byte DDP header → keep RGB chunks ≤ 1398 (multiple of 3).
|
||
DDP_MAX_DATA = 1398
|
||
|
||
|
||
def ddp_packet(rgb: bytes, *, offset: int = 0, push: bool = True) -> bytes:
|
||
flags = DDP_VER1 | (DDP_PUSH if push else 0)
|
||
header = struct.pack("!BBBBLH", flags, 0, DDP_TYPE_RGB888, DDP_ID, offset, len(rgb))
|
||
return header + rgb
|
||
|
||
|
||
def frame_from_strips(leds: int, colours: list[tuple[int, int, int]]) -> bytes:
|
||
"""Build strip0|…|strip7 payload; each strip is a solid colour × leds."""
|
||
if len(colours) != NUM_STRIPS:
|
||
raise ValueError(f"need {NUM_STRIPS} colours, got {len(colours)}")
|
||
parts = [bytes(rgb) * leds for rgb in colours]
|
||
return b"".join(parts)
|
||
|
||
|
||
def send_frame(sock: socket.socket, addr: tuple[str, int], payload: bytes) -> None:
|
||
"""Send one logical frame; split into MTU-safe DDP packets (PUSH on last)."""
|
||
if not payload:
|
||
sock.sendto(ddp_packet(b"", push=True), addr)
|
||
return
|
||
offset = 0
|
||
remaining = len(payload)
|
||
while remaining > 0:
|
||
chunk = min(remaining, DDP_MAX_DATA)
|
||
push = chunk >= remaining
|
||
sock.sendto(ddp_packet(payload[offset : offset + chunk], offset=offset, push=push), addr)
|
||
offset += chunk
|
||
remaining -= chunk
|
||
if remaining:
|
||
time.sleep(0.002)
|
||
|
||
|
||
def off_all(sock: socket.socket, addr: tuple[str, int], leds: int) -> None:
|
||
send_frame(sock, addr, frame_from_strips(leds, [(0, 0, 0)] * NUM_STRIPS))
|
||
|
||
|
||
def main() -> int:
|
||
parser = argparse.ArgumentParser(description="Test all 8 led-driver-8 strips over DDP")
|
||
parser.add_argument("--host", default="10.1.1.10", help="Panel IP (default: 10.1.1.10)")
|
||
parser.add_argument("--port", type=int, default=4048, help="DDP UDP port")
|
||
parser.add_argument("--leds", type=int, default=64, help="LEDs lit per strip (capacity test)")
|
||
parser.add_argument("--pause", type=float, default=0.7, help="Seconds between steps")
|
||
parser.add_argument(
|
||
"--all-only",
|
||
action="store_true",
|
||
help="Skip per-strip identity; only flash all strips together",
|
||
)
|
||
args = parser.parse_args()
|
||
|
||
if args.leds < 1:
|
||
parser.error("--leds must be >= 1")
|
||
|
||
addr = (args.host, args.port)
|
||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||
black = (0, 0, 0)
|
||
|
||
print(
|
||
f"Testing {NUM_STRIPS} strips @ {args.host}:{args.port} "
|
||
f"DDP ({args.leds} LEDs each, MTU-safe chunks)"
|
||
)
|
||
|
||
try:
|
||
if not args.all_only:
|
||
print(" per-strip identity colours (one packet each)")
|
||
for (strip, gpio), rgb in zip(DEFAULT_STRIPS, IDENTITY_COLOURS):
|
||
colours = [black] * NUM_STRIPS
|
||
colours[strip] = rgb
|
||
send_frame(sock, addr, frame_from_strips(args.leds, colours))
|
||
print(f" strip {strip} GP{gpio} -> {rgb}")
|
||
time.sleep(args.pause)
|
||
|
||
print(" all strips together")
|
||
for label, rgb in ALL_COLOURS:
|
||
print(f" {label}")
|
||
send_frame(sock, addr, frame_from_strips(args.leds, [rgb] * NUM_STRIPS))
|
||
time.sleep(args.pause)
|
||
|
||
off_all(sock, addr, args.leds)
|
||
print("Done.")
|
||
except OSError as exc:
|
||
print(f"Network error: {exc}")
|
||
return 1
|
||
finally:
|
||
sock.close()
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|