feat: DDP frame sync over UDP broadcast, DDP acks, DDP enable setting

- Driver acks each completed DDP frame; the test waits for the acks before broadcasting the sync.
- Sync listener on UDP 4049: a frame is held until the next sync, then shown.
- DDP pixels are shown at full brightness, ignoring the device brightness settings.
- "DDP and sync receive on" setting (NVS, network API, web UI); off ignores DDP and sync packets.
- ddp_test.py: finds devices on the subnet, drives them in lockstep at a fixed rate, runs animations.
- /api/ddp reports sync counters for diagnosis.
- Includes in-progress WebSocket, multi-driver web UI and Wi-Fi work from the same tree.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Haiku 5.5 committed 2026-11-11 14:10:55 +13:00
1 parent 47f4eba0d6
commit 4cd3bc7973
294 files changed
+48954 -301

No files matched your search

+89 -16
View File
@@ -94,8 +94,18 @@ static uint32_t s_ddp_packets;
static uint32_t s_ddp_frames; // frames completed (PUSH seen)
static uint32_t s_ddp_sum; // checksum of the last completed frame
static uint32_t s_ddp_log_ms; // last serial summary
// Frame sync: while syncs keep arriving, a completed DDP frame waits for the next sync before it is shown
#define DDP_SYNC_TIMEOUT_MS 2000
static uint8_t s_ddp_show[FX_LEDS_MAX * 3]; // the frame the strip shows
static uint8_t s_ddp_held[FX_LEDS_MAX * 3]; // a completed frame waiting for a sync
static bool s_ddp_held_ok;
static uint32_t s_sync_last_ms; // 0: never
static uint32_t s_sync_seq; // seq of the last sync
static uint32_t s_sync_count;
static uint32_t s_ddp_shown; // frames shown
static uint8_t s_max_brightness = 255;
static uint8_t s_global = 255;
static uint8_t s_master = 255;
static uint32_t s_fade_t0;
static uint32_t s_fade_ms;
@@ -190,6 +200,21 @@ uint8_t patterns_get_global(void)
return v;
}
void patterns_set_master(uint8_t level)
{
xSemaphoreTake(s_lock, portMAX_DELAY);
s_master = level;
xSemaphoreGive(s_lock);
}
uint8_t patterns_get_master(void)
{
xSemaphoreTake(s_lock, portMAX_DELAY);
uint8_t v = s_master;
xSemaphoreGive(s_lock);
return v;
}
void patterns_get(preset_t *out)
{
xSemaphoreTake(s_lock, portMAX_DELAY);
@@ -203,9 +228,11 @@ static uint32_t now_ms(void)
}
// Show the shadow frame, crossfading from the previous frame while a fade runs
static void output(uint32_t now)
static void output(uint32_t now, bool full_brightness)
{
int s_scale = s_global * s_max_brightness / 255; // global brightness under the ceiling
// device brightness x master scale, under the max brightness ceiling. DDP frames are shown at
// full brightness: the sender sets the colours.
int s_scale = full_brightness ? 255 : s_global * s_master / 255 * s_max_brightness / 255;
int k = -1;
if (s_fading) {
uint32_t el = now - s_fade_t0;
@@ -342,14 +369,14 @@ static void patterns_task(void *arg)
s_fx.t = now - g_st.t0;
if (ddp_on) {
for (int i = 0; i < s_fx.n; i++) {
s_fx.px[i] = { s_ddp[3 * i], s_ddp[3 * i + 1], s_ddp[3 * i + 2] };
s_fx.px[i] = { s_ddp_show[3 * i], s_ddp_show[3 * i + 1], s_ddp_show[3 * i + 2] };
}
} else if (!g_st.done) {
if (kPatterns[s_run.pattern].fn(s_fx, g_st)) {
g_st.done = true;
}
}
output(now);
output(now, ddp_on);
xSemaphoreGive(s_lock);
vTaskDelay(pdMS_TO_TICKS(FRAME_MS));
}
@@ -378,6 +405,7 @@ void patterns_start(const hw_cfg_t &hw)
s_fx.n = n;
s_max_brightness = hw.max_brightness;
s_global = hw.global_brightness;
s_master = hw.master_brightness;
s_strip = new WS2812((gpio_num_t)hw.pin, n);
s_strip->setOrder(hw.order);
s_strip->setBrightness(255); // brightness is applied per colour by the patterns
@@ -411,6 +439,27 @@ esp_err_t patterns_set_hardware(const hw_cfg_t &hw)
// ---- DDP ---------------------------------------------------------------------------------------
// Make a completed frame the one the strip shows, and log it. Caller holds s_lock.
static void ddp_show(const uint8_t *frame, uint32_t now)
{
size_t cap = (size_t)s_fx.n * 3;
memcpy(s_ddp_show, frame, cap);
s_ddp_shown++;
uint32_t sum = 0;
for (size_t i = 0; i < cap; i++) {
sum += frame[i];
}
s_ddp_sum = sum;
// Summary on the serial log, at most twice a second, so the test can check what was shown
if (now - s_ddp_log_ms >= 500) {
s_ddp_log_ms = now;
ESP_LOGI(TAG, "DDP frames=%u packets=%u sum=%u led0=%u,%u,%u led_last=%u,%u,%u",
(unsigned)s_ddp_frames, (unsigned)s_ddp_packets, (unsigned)s_ddp_sum,
frame[0], frame[1], frame[2],
frame[cap - 3], frame[cap - 2], frame[cap - 1]);
}
}
void patterns_ddp_write(uint32_t offset, const uint8_t *data, size_t len, bool push)
{
xSemaphoreTake(s_lock, portMAX_DELAY);
@@ -426,23 +475,34 @@ void patterns_ddp_write(uint32_t offset, const uint8_t *data, size_t len, bool p
s_ddp_packets++;
if (push) {
s_ddp_frames++;
uint32_t sum = 0;
for (size_t i = 0; i < cap; i++) {
sum += s_ddp[i];
}
s_ddp_sum = sum;
// Summary on the serial log, at most twice a second, so the test can check what was shown
if (now - s_ddp_log_ms >= 500) {
s_ddp_log_ms = now;
ESP_LOGI(TAG, "DDP frames=%u packets=%u sum=%u led0=%u,%u,%u led_last=%u,%u,%u",
(unsigned)s_ddp_frames, (unsigned)s_ddp_packets, (unsigned)s_ddp_sum,
s_ddp[0], s_ddp[1], s_ddp[2],
s_ddp[cap - 3], s_ddp[cap - 2], s_ddp[cap - 1]);
bool syncing = s_sync_last_ms != 0 && now - s_sync_last_ms < DDP_SYNC_TIMEOUT_MS;
if (syncing) {
// Syncs are arriving: hold the frame until the next one. The sender acks each frame before it
// sends the sync, so the frame is always here when its sync arrives.
memcpy(s_ddp_held, s_ddp, cap);
s_ddp_held_ok = true;
} else {
s_ddp_held_ok = false;
ddp_show(s_ddp, now);
}
}
xSemaphoreGive(s_lock);
}
void patterns_ddp_sync(uint32_t seq)
{
xSemaphoreTake(s_lock, portMAX_DELAY);
uint32_t now = now_ms();
s_sync_last_ms = now | 1; // never 0
s_sync_seq = seq;
s_sync_count++;
if (s_ddp_held_ok) {
s_ddp_held_ok = false;
ddp_show(s_ddp_held, now);
}
xSemaphoreGive(s_lock);
}
void patterns_ddp_status(bool *active, uint32_t *packets)
{
xSemaphoreTake(s_lock, portMAX_DELAY);
@@ -451,6 +511,19 @@ void patterns_ddp_status(bool *active, uint32_t *packets)
xSemaphoreGive(s_lock);
}
void patterns_ddp_debug(patterns_ddp_debug_t *out)
{
xSemaphoreTake(s_lock, portMAX_DELAY);
uint32_t now = now_ms();
out->syncs = s_sync_count;
out->sync_seq = s_sync_seq;
out->sync_age_ms = s_sync_last_ms != 0 ? now - s_sync_last_ms : UINT32_MAX;
out->frames = s_ddp_frames;
out->shown = s_ddp_shown;
out->held = s_ddp_held_ok;
xSemaphoreGive(s_lock);
}
void patterns_shown_pixel(int i, uint8_t out[3])
{
xSemaphoreTake(s_lock, portMAX_DELAY);