diff --git a/README.md b/README.md index 209ba7b..ab7b246 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Portal panel firmware (Pico + W5500 + WS2812) -UDP RGB panel adapter for the [portal](https://git.technical.kiwi/technicalkiwi/portal.git) project. -Host renders patterns; this firmware only receives frames on **UDP 50007**. +DDP RGB panel adapter for the [portal](https://git.technical.kiwi/technicalkiwi/portal.git) project. +Host renders patterns; this firmware receives frames on **UDP 4048** (DDP). ## Layout @@ -17,15 +17,15 @@ led-driver-8/ | Signal | GPIO | |--------|------| -| W5500 CS | GP13 | +| W5500 CS | GP9 | | SPI1 SCK | GP10 | | SPI1 MOSI | GP11 | | SPI1 MISO | GP12 | -| W5500 RST | GP9 | +| W5500 RST | GP13 | | Status LED | GP25 | | WS2812 strip 0–7 | GP18, 19, 20, 21, 22, 26, 27, 28 | -Eight strips (one PIO SM each). Pixel output uses **DMA → PIO**. Pixel count comes from each UDP frame. `MAX_LEDS` (512) is capacity only. +Eight strips (one PIO SM each). Pixel output uses **DMA → PIO**. One DDP packet carries all strips (equal LED count each). `MAX_LEDS` (512) is per-strip capacity only. ## Build / flash @@ -64,12 +64,22 @@ python3 examples/test_strips.py --host 10.1.1.10 --leds 64 Lights each strip alone with a unique colour, then flashes all strips R/G/B/white. -## UDP protocol (port 50007) +## DDP protocol (port 4048) -| Payload | Action | -|---------|--------| -| N×3 bytes | Raw RGB → strip 0 | -| 1 + N×3 bytes | `strip_id` + RGB (`0`–`7`, or `255` = all) | -| 5 bytes `PIN\0` + gpio | Bind strip 0 | -| 8 bytes `PIN\0` + strip + gpio + len_lo + len_hi | Bind strip | -| 4 bytes `SHOW` | Reserved | +[Distributed Display Protocol](http://www.3waylabs.com/ddp/) — WLED-compatible. + +| Field | Value | +|-------|--------| +| Port | UDP **4048** | +| Destination ID | **1** (or 255) | +| Header | 10 bytes (14 if timecode flag set) | +| Data type | RGB888 (`0x0B`) | +| PUSH | split frame onto strips and show | + +**Payload layout** (one packet for the whole panel): + +```text +strip0 RGB | strip1 RGB | … | strip7 RGB +``` + +LED count per strip is `total_pixels / 8` (must divide evenly). `offset` / `length` address that linear buffer if a frame is split across packets. diff --git a/examples/test_strips.py b/examples/test_strips.py index 262284b..010accc 100755 --- a/examples/test_strips.py +++ b/examples/test_strips.py @@ -1,9 +1,9 @@ #!/usr/bin/env python3 -"""Drive-test all 8 WS2812 strips on led-driver-8 over UDP. +"""Drive-test all 8 WS2812 strips on led-driver-8 over DDP. -Protocol (port 50007): - - 1 + N×3 bytes: strip_id (0–7, or 255=all) + RGB - - 8 bytes PIN\\0 + strip + gpio + len_lo + len_hi +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. @@ -17,10 +17,13 @@ from __future__ import annotations import argparse import socket +import struct import time +NUM_STRIPS = 8 + DEFAULT_STRIPS = ( - # (strip_id, gpio) + # (strip_index, gpio) (0, 18), (1, 19), (2, 20), @@ -49,42 +52,55 @@ ALL_COLOURS = ( ("WHITE", (255, 255, 255)), ) +DDP_VER1 = 0x40 +DDP_PUSH = 0x01 +DDP_TYPE_RGB888 = 0x0B +DDP_ID = 1 -def pin_bind(sock: socket.socket, addr: tuple[str, int], strip: int, gpio: int, leds: int) -> None: - payload = bytes( - [ - ord("P"), - ord("I"), - ord("N"), - 0, - strip & 0xFF, - gpio & 0xFF, - leds & 0xFF, - (leds >> 8) & 0xFF, - ] - ) - sock.sendto(payload, addr) +# 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 send_solid( - sock: socket.socket, - addr: tuple[str, int], - strip_id: int, - leds: int, - rgb: tuple[int, int, int], -) -> None: - sock.sendto(bytes([strip_id & 0xFF]) + bytes(rgb) * leds, addr) +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: - for strip, _gpio in DEFAULT_STRIPS: - send_solid(sock, addr, strip, leds, (0, 0, 0)) + 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 UDP") + 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=50007, help="UDP RGB port") + 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( @@ -92,7 +108,6 @@ def main() -> int: action="store_true", help="Skip per-strip identity; only flash all strips together", ) - parser.add_argument("--no-bind", action="store_true", help="Skip PIN bind commands") args = parser.parse_args() if args.leds < 1: @@ -100,28 +115,27 @@ def main() -> int: addr = (args.host, args.port) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + black = (0, 0, 0) - print(f"Testing {len(DEFAULT_STRIPS)} strips @ {args.host}:{args.port} ({args.leds} LEDs each)") + print( + f"Testing {NUM_STRIPS} strips @ {args.host}:{args.port} " + f"DDP ({args.leds} LEDs each, MTU-safe chunks)" + ) try: - if not args.no_bind: - for strip, gpio in DEFAULT_STRIPS: - pin_bind(sock, addr, strip, gpio, args.leds) - print(f" bind strip {strip} GP{gpio}") - time.sleep(0.02) - if not args.all_only: - print(" per-strip identity colours") + print(" per-strip identity colours (one packet each)") for (strip, gpio), rgb in zip(DEFAULT_STRIPS, IDENTITY_COLOURS): - off_all(sock, addr, args.leds) - send_solid(sock, addr, strip, args.leds, rgb) + 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 (strip_id=255)") + print(" all strips together") for label, rgb in ALL_COLOURS: print(f" {label}") - send_solid(sock, addr, 255, args.leds, rgb) + send_frame(sock, addr, frame_from_strips(args.leds, [rgb] * NUM_STRIPS)) time.sleep(args.pause) off_all(sock, addr, args.leds) diff --git a/firmware/Makefile b/firmware/Makefile index f2b27a8..42f41ac 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -108,7 +108,7 @@ reconfigure: deploy: build @command -v picotool >/dev/null || { echo "Error: picotool not found"; exit 1; } @echo "==> Uploading $(UF2) over USB" - @picotool load -x -f "$(UF2)" + @picotool load -x "$(UF2)" -f flash: deploy upload: deploy diff --git a/firmware/board_config.h b/firmware/board_config.h index 00a2532..2e96174 100644 --- a/firmware/board_config.h +++ b/firmware/board_config.h @@ -1,19 +1,19 @@ #ifndef BOARD_CONFIG_H #define BOARD_CONFIG_H -/* W5500 on SPI1; eight WS2812 strips (portal UDP protocol) */ +/* W5500 on SPI1; eight WS2812 strips (DDP protocol) */ #define PIN_LED_STATUS 25 #define SPI_PORT spi1 #define SPI_CLK_MHZ 15 -/* This adapter board: CS=13, RST=9 (some portal PCBs use CS=9 / RST=13) */ -#define PIN_CS 13 +/* This adapter board: CS=9, RST=13 (some portal PCBs use CS=13 / RST=9) */ +#define PIN_CS 9 #define PIN_SCK 10 #define PIN_MOSI 11 #define PIN_MISO 12 -#define PIN_RST 9 +#define PIN_RST 13 /* Strip 0 / 1 overridable; remaining pins fixed in DEFAULT_STRIP_PINS */ #ifndef PIN_WS2812 @@ -42,8 +42,8 @@ #define SOCKET_DHCP 0 #define SOCKET_UDP 1 -/* UDP port (same as CircuitPython adapter / portal) */ -#define UDP_PORT 50007 +/* DDP (Distributed Display Protocol) default port */ +#define UDP_PORT 4048 /* Compile-time panel index for filtered frames (0–4). 255 = accept all. */ #ifndef PANEL_ID diff --git a/firmware/main.c b/firmware/main.c index 7ef0d3d..512983e 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -1,17 +1,15 @@ /** - * Portal panel firmware — W5500 UDP + WS2812 (Pico SDK). + * Portal panel firmware — W5500 DDP + WS2812 (Pico SDK). * - * UDP protocol (port 50007): - * - N×3 bytes: raw RGB → strip 0 (N = 1…MAX_LEDS) - * - 1 + N×3 bytes: strip_id (byte 0) + RGB - * strip_id 0…MAX_STRIPS-1 → that strip - * strip_id 255 → all active strips - * - 5 bytes "PIN\\0" + gpio: bind strip 0 to gpio - * - 8 bytes "PIN\\0" + strip + gpio + len_lo + len_hi: bind strip (len=0 → unset) - * - 4 bytes "SHOW": reserved (frames already show on receive) + * DDP (Distributed Display Protocol) on UDP 4048: + * - 10-byte header (+4 if timecode flag set), then RGB payload + * - destination ID 1 (or 255): one linear frame for all strips + * - payload layout: strip0 RGB | strip1 RGB | … | strip7 RGB + * (equal LED count per strip = total_pixels / MAX_STRIPS) + * - offset = byte offset into that linear frame (network order) + * - PUSH flag → split buffer onto strips and show * - * One Pico can drive up to MAX_STRIPS displays on separate GPIOs. - * Each strip’s pixel count comes from its own UDP frame length (strips may differ). + * One Pico drives up to MAX_STRIPS displays on separate GPIOs. * MAX_LEDS is per-strip capacity. */ @@ -35,6 +33,17 @@ #define ETH_BUF_SIZE 2048 #define DHCP_RETRY_MAX 10 +#define DDP_HEADER_LEN 10 +#define DDP_HEADER_LEN_TIME 14 +#define DDP_FLAGS1_VER1 0x40 +#define DDP_FLAGS1_VER_MASK 0xc0 +#define DDP_FLAGS1_TIME 0x10 +#define DDP_FLAGS1_PUSH 0x01 +#define DDP_ID_DEFAULT 1 +#define DDP_ID_ALL 255 + +#define FRAME_RGB_BYTES ((uint32_t)MAX_STRIPS * (uint32_t)LED_RGB_BYTES) + static wiz_NetInfo g_net_info = { .mac = MAC_ADDR, .ip = STATIC_IP, @@ -49,10 +58,9 @@ static wiz_NetInfo g_net_info = { }; static uint8_t g_eth_buf[ETH_BUF_SIZE]; -static uint8_t g_pixel_buf[MAX_STRIPS][LED_RGB_BYTES]; -static uint16_t g_frame_pixels[MAX_STRIPS]; -static uint16_t g_strip_leds[MAX_STRIPS]; -static uint8_t g_frame_dirty[MAX_STRIPS]; +static uint8_t g_frame_buf[FRAME_RGB_BYTES]; +static uint32_t g_frame_end; +static uint8_t g_need_show; static volatile uint16_t g_ms_tick; static uint8_t g_dhcp_ready; @@ -116,7 +124,7 @@ static int udp_socket_open(void) { printf("UDP socket open failed: %d\n", sn); return -1; } - printf("UDP listening on port %d\n", UDP_PORT); + printf("DDP listening on UDP %d (1 packet, %d strips)\n", UDP_PORT, MAX_STRIPS); return 0; } @@ -143,131 +151,93 @@ static void led_boot_test(unsigned int strip) { ws2812_show(strip); } -static int buffer_frame(unsigned int strip, const uint8_t *rgb, uint16_t nbytes) { - if (strip >= MAX_STRIPS || !ws2812_strip_active(strip)) { - return -1; +static void buffer_frame(uint32_t offset, const uint8_t *rgb, uint16_t nbytes) { + if (nbytes == 0) { + return; } - if (nbytes == 0 || (nbytes % 3u) != 0) { - return -1; + if (offset >= FRAME_RGB_BYTES) { + return; } - uint16_t pixels = nbytes / 3u; - if (pixels > MAX_LEDS) { - pixels = MAX_LEDS; - nbytes = (uint16_t)(pixels * 3u); + uint32_t room = FRAME_RGB_BYTES - offset; + if (nbytes > room) { + nbytes = (uint16_t)room; } - memcpy(g_pixel_buf[strip], rgb, nbytes); - if (nbytes < LED_RGB_BYTES) { - memset(g_pixel_buf[strip] + nbytes, 0, LED_RGB_BYTES - nbytes); - } - g_frame_pixels[strip] = pixels; - g_frame_dirty[strip] = 1; - return 0; -} - -static void buffer_frame_all(const uint8_t *rgb, uint16_t nbytes) { - for (unsigned int s = 0; s < MAX_STRIPS; s++) { - if (ws2812_strip_active(s)) { - buffer_frame(s, rgb, nbytes); - } + memcpy(g_frame_buf + offset, rgb, nbytes); + uint32_t end = offset + nbytes; + if (end > g_frame_end) { + g_frame_end = end; } } -static void show_dirty_frames(void) { +static void show_frame(void) { + if (g_frame_end < (uint32_t)MAX_STRIPS * 3u) { + return; + } + + uint32_t total_pixels = g_frame_end / 3u; + uint16_t per_strip = (uint16_t)(total_pixels / (uint32_t)MAX_STRIPS); + if (per_strip == 0) { + return; + } + if (per_strip > MAX_LEDS) { + per_strip = MAX_LEDS; + } + + uint32_t strip_bytes = (uint32_t)per_strip * 3u; for (unsigned int s = 0; s < MAX_STRIPS; s++) { - if (!g_frame_dirty[s]) { + if (!ws2812_strip_active(s)) { continue; } - ws2812_set_rgb(s, g_pixel_buf[s], g_frame_pixels[s]); + const uint8_t *rgb = g_frame_buf + (uint32_t)s * strip_bytes; + ws2812_set_rgb(s, rgb, per_strip); ws2812_show(s); - g_frame_dirty[s] = 0; } } -static int pin_is_reserved(uint8_t pin) { - switch (pin) { - case PIN_CS: - case PIN_SCK: - case PIN_MOSI: - case PIN_MISO: - case PIN_RST: - case PIN_LED_STATUS: - return 1; - default: - return pin > 29; - } -} - -static void handle_pin_command(uint8_t strip, uint8_t pin, uint16_t leds) { - if (strip >= MAX_STRIPS) { - printf("PIN strip %u out of range (max %u)\n", (unsigned)strip, MAX_STRIPS); - return; - } - if (pin_is_reserved(pin)) { - printf("PIN GP%u rejected (reserved)\n", (unsigned)pin); - return; - } - if (leds > MAX_LEDS) { - printf("PIN leds %u clamped to max_leds=%u\n", (unsigned)leds, MAX_LEDS); - leds = MAX_LEDS; - } - for (unsigned int s = 0; s < MAX_STRIPS; s++) { - if (s != strip && ws2812_strip_active(s) && ws2812_get_pin(s) == pin) { - printf("PIN GP%u: releasing strip %u\n", (unsigned)pin, s); - ws2812_release_strip(s); - g_strip_leds[s] = 0; - g_frame_pixels[s] = 0; - g_frame_dirty[s] = 0; - } - } - if (ws2812_set_pin(strip, pin) != 0) { - printf("PIN strip %u GP%u failed (no PIO SM?)\n", (unsigned)strip, (unsigned)pin); - return; - } - g_strip_leds[strip] = leds; - if (leds) { - printf("strip %u ws2812=GP%u leds=%u\n", (unsigned)strip, - (unsigned)ws2812_get_pin(strip), (unsigned)leds); - } else { - printf("strip %u ws2812=GP%u\n", (unsigned)strip, (unsigned)ws2812_get_pin(strip)); - } -} - -static void handle_packet(const uint8_t *data, int16_t len) { - if (len == 4 && memcmp(data, "SHOW", 4) == 0) { +static void handle_ddp(const uint8_t *data, int16_t len) { + if (len < DDP_HEADER_LEN) { return; } - if (len == 5 && data[0] == 'P' && data[1] == 'I' && data[2] == 'N' && data[3] == 0) { - handle_pin_command(0, data[4], 0); + uint8_t flags1 = data[0]; + if ((flags1 & DDP_FLAGS1_VER_MASK) != DDP_FLAGS1_VER1) { return; } - if (len == 8 && data[0] == 'P' && data[1] == 'I' && data[2] == 'N' && data[3] == 0) { - uint16_t leds = (uint16_t)data[6] | ((uint16_t)data[7] << 8); - handle_pin_command(data[4], data[5], leds); - return; - } - - if (len >= 3 && (len % 3) == 0) { - buffer_frame(0, data, (uint16_t)len); - return; - } - - if (len >= 4 && ((len - 1) % 3) == 0) { - uint8_t id = data[0]; - uint16_t nbytes = (uint16_t)(len - 1); - if (id == 255) { - buffer_frame_all(data + 1, nbytes); + uint8_t hdr_len = DDP_HEADER_LEN; + if (flags1 & DDP_FLAGS1_TIME) { + hdr_len = DDP_HEADER_LEN_TIME; + if (len < hdr_len) { return; } - if (id < MAX_STRIPS) { - buffer_frame(id, data + 1, nbytes); - return; - } - if (id == (uint8_t)PANEL_ID) { - buffer_frame(0, data + 1, nbytes); - return; + } + + uint8_t id = data[3]; + if (id != DDP_ID_DEFAULT && id != DDP_ID_ALL) { + return; + } + + uint32_t offset = ((uint32_t)data[4] << 24) | ((uint32_t)data[5] << 16) | + ((uint32_t)data[6] << 8) | (uint32_t)data[7]; + uint16_t dlen = ((uint16_t)data[8] << 8) | (uint16_t)data[9]; + + int16_t avail = (int16_t)(len - hdr_len); + if (avail < 0) { + return; + } + if (dlen > (uint16_t)avail) { + dlen = (uint16_t)avail; + } + + if (dlen > 0) { + if (offset == 0) { + g_frame_end = 0; } + buffer_frame(offset, data + hdr_len, dlen); + } + + if (flags1 & DDP_FLAGS1_PUSH) { + g_need_show = 1; } } @@ -287,19 +257,15 @@ static void poll_udp(void) { if (n <= 0) { return; } - handle_packet(g_eth_buf, n); + handle_ddp(g_eth_buf, n); } } static void print_strip_map(void) { - printf("strips=%d", MAX_STRIPS); + printf("strips=%d ddp=1pkt", MAX_STRIPS); for (unsigned int s = 0; s < MAX_STRIPS; s++) { if (ws2812_strip_active(s)) { - if (g_strip_leds[s]) { - printf(" [%u]=GP%u/%u", s, ws2812_get_pin(s), g_strip_leds[s]); - } else { - printf(" [%u]=GP%u", s, ws2812_get_pin(s)); - } + printf(" [%u]=GP%u", s, ws2812_get_pin(s)); } } printf("\n"); @@ -312,7 +278,7 @@ int main(void) { gpio_init(PIN_LED_STATUS); gpio_set_dir(PIN_LED_STATUS, GPIO_OUT); - printf("portal panel firmware (Pico SDK)\n"); + printf("portal panel firmware (Pico SDK, DDP)\n"); printf("max_leds=%d max_strips=%d panel_id=%d mac=02:50:52:54:4C:%02X " "ip=%u.%u.%u.%u %s\n", MAX_LEDS, MAX_STRIPS, PANEL_ID, (unsigned)PANEL_ID, g_net_info.ip[0], @@ -326,10 +292,9 @@ int main(void) { wizchip_check(); ws2812_init(MAX_LEDS); - memset(g_pixel_buf, 0, sizeof(g_pixel_buf)); - memset(g_frame_pixels, 0, sizeof(g_frame_pixels)); - memset(g_strip_leds, 0, sizeof(g_strip_leds)); - memset(g_frame_dirty, 0, sizeof(g_frame_dirty)); + memset(g_frame_buf, 0, sizeof(g_frame_buf)); + g_frame_end = 0; + g_need_show = 0; static const unsigned int default_pins[MAX_STRIPS] = DEFAULT_STRIP_PINS; for (unsigned int s = 0; s < MAX_STRIPS; s++) { @@ -355,7 +320,10 @@ int main(void) { DHCP_run(); #endif poll_udp(); - show_dirty_frames(); + if (g_need_show) { + show_frame(); + g_need_show = 0; + } if (++heartbeat >= 500) { gpio_xor_mask(1u << PIN_LED_STATUS); heartbeat = 0;