Files
led-driver/main/ddp.cpp
T
jimmyandClaude Haiku 5.5 9a04d28ecf feat: web UI with AP, Wi-Fi client, mDNS, presets, OTA and DDP
- Wi-Fi access point led-XXXXXX plus a client network, configurable
  mDNS name and static client address
- Preset tiles with an editor, drag to reorder, default presets,
  global brightness, Run and Edit modes, collapsible menu on phones
- 35 patterns ported from led-driver, up to 512 LEDs, colour order,
  max brightness and data pin settings
- Over-the-air updates (POST /ota) with rollback, and a CLI in tools/ota.py
- DDP receiver on UDP 4048 with tools/ddp_test.py
- LED refresh every 10 ms; lwIP sockets raised to 16 so mDNS, HTTP
  and DDP can run together

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-11-11 01:54:32 +13:00

85 lines
2.2 KiB
C++

/*
* SPDX-License-Identifier: Apache-2.0
*/
#include <cstring>
#include "ddp.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "lwip/sockets.h"
#include "patterns.h"
static const char *TAG = "ddp";
// Header: flags, sequence, data type, destination, data offset (4), data length (2); with the timecode
// flag, 4 more bytes follow. Version 1 is flags bits 6-7 = 01.
#define DDP_FLAG_VER1 0x40
#define DDP_VER_MASK 0xC0
#define DDP_FLAG_TIMECODE 0x10
#define DDP_HEADER 10
static uint32_t be32(const uint8_t *p)
{
return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | p[3];
}
static uint16_t be16(const uint8_t *p)
{
return (uint16_t)((p[0] << 8) | p[1]);
}
static void ddp_task(void *arg)
{
(void)arg;
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sock < 0) {
ESP_LOGE(TAG, "socket failed");
vTaskDelete(NULL);
return;
}
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = htons(DDP_PORT);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
ESP_LOGE(TAG, "bind to port %d failed", DDP_PORT);
close(sock);
vTaskDelete(NULL);
return;
}
ESP_LOGI(TAG, "DDP listening on UDP port %d", DDP_PORT);
uint8_t buf[1500];
while (true) {
int n = recv(sock, buf, sizeof(buf), 0);
if (n < DDP_HEADER) {
continue;
}
uint8_t flags = buf[0];
if ((flags & DDP_VER_MASK) != DDP_FLAG_VER1) {
continue;
}
size_t hdr = DDP_HEADER + ((flags & DDP_FLAG_TIMECODE) ? 4 : 0);
if ((size_t)n < hdr) {
continue;
}
uint8_t dtype = buf[2];
if (dtype != 0x00 && dtype != 0x01) {
continue; // only 8-bit RGB
}
uint32_t offset = be32(buf + 4);
size_t len = be16(buf + 8);
if (len > (size_t)n - hdr) {
len = (size_t)n - hdr;
}
patterns_ddp_write(offset, buf + hdr, len, (flags & 0x01) != 0);
}
}
esp_err_t ddp_start(void)
{
BaseType_t ok = xTaskCreate(ddp_task, "ddp", 4096, NULL, 4, NULL);
return ok == pdPASS ? ESP_OK : ESP_ERR_NO_MEM;
}