Files
led-driver/main/ddp.cpp
T
jimmyandClaude Opus 5.5 24dd5ffb15 feat: ESP-NOW link, beat sync and control messages in the C++ driver
- espnow_link, ctrl_msg and beat_sync: the reliable link, envelopes and
  beat-synced timing.
- ctrl_msg: accept an envelope with the trailing save byte.
- patterns_frame() and espnow_link_poll() split out of their tasks, so the
  WebAssembly simulator can drive one frame and one link pass from a timer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 19:32:36 +13:00

147 lines
4.3 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"
#include "settings.h"
static const char *TAG = "ddp";
static volatile bool s_enabled = true;
void ddp_set_enabled(bool on)
{
s_enabled = on;
}
// 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) {
struct sockaddr_in src = {};
socklen_t srclen = sizeof(src);
int n = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&src, &srclen);
if (!s_enabled) {
continue;
}
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];
// 8-bit RGB: 0x00 (undefined, taken as RGB), 0x01 (the shorthand ddp_test sends) and 0x0B (RGB, 8 bits
// per channel, the DDP specification's code, which is what the controller sends)
if (dtype != 0x00 && dtype != 0x01 && dtype != 0x0B) {
continue;
}
uint32_t offset = be32(buf + 4);
size_t len = be16(buf + 8);
if (len > (size_t)n - hdr) {
len = (size_t)n - hdr;
}
bool push = (flags & 0x01) != 0;
patterns_ddp_write(offset, buf + hdr, len, push);
if (push) {
// Ack the completed frame to its sender ("LAK1" + the DDP seq). The sender broadcasts the
// sync only after this, so the frame is already held when the sync arrives.
uint8_t ack[5] = { 'L', 'A', 'K', '1', buf[1] };
sendto(sock, ack, sizeof(ack), 0, (struct sockaddr *)&src, srclen);
}
}
}
// Sync broadcasts: a frame that arrived by DDP is shown when the next sync arrives (see patterns.cpp).
static void ddp_sync_task(void *arg)
{
(void)arg;
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sock < 0) {
ESP_LOGE(TAG, "sync socket failed");
vTaskDelete(NULL);
return;
}
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = htons(DDP_SYNC_PORT);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
ESP_LOGE(TAG, "bind to sync port %d failed", DDP_SYNC_PORT);
close(sock);
vTaskDelete(NULL);
return;
}
ESP_LOGI(TAG, "sync listening on UDP port %d", DDP_SYNC_PORT);
uint8_t buf[64];
while (true) {
int n = recv(sock, buf, sizeof(buf), 0);
if (!s_enabled || n < 8 || memcmp(buf, "LSY1", 4) != 0) {
continue;
}
patterns_ddp_sync(be32(buf + 4));
}
}
esp_err_t ddp_start(void)
{
net_cfg_t net;
if (settings_load_net(&net) == ESP_OK) {
s_enabled = net.ddp_enabled;
}
BaseType_t ok = xTaskCreate(ddp_task, "ddp", 4096, NULL, 4, NULL);
if (ok != pdPASS) {
return ESP_ERR_NO_MEM;
}
ok = xTaskCreate(ddp_sync_task, "ddp_sync", 3072, NULL, 4, NULL);
return ok == pdPASS ? ESP_OK : ESP_ERR_NO_MEM;
}