fix: DDP keeps the last frame up, and sync timestamps compare as signed

- once pixels have arrived the strip keeps the last frame when they stop, until a pattern is selected
- a stamp stored as now|1 can be 1 ms ahead of a now read in the same millisecond; the unsigned
  difference wrapped and looked like a timeout, so compare it as signed (within())

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Sonnet 5.5 committed 2026-11-12 00:46:04 +13:00
1 parent 995b05f538
commit af82cb4c4a
1 file changed
+15 -8
+15 -8
View File
@@ -85,11 +85,10 @@ static rgb *s_from; // frame the crossfade starts from
static int s_cap; // LEDs the buffers are sized for
static bool s_fading;
// DDP: pixels sent by a controller. While they keep arriving they replace the pattern.
// DDP: pixels sent by a controller replace the pattern, and the last frame stays up when they stop.
#define DDP_TIMEOUT_MS 2000
static uint8_t s_ddp[FX_LEDS_MAX * 3];
static uint32_t s_ddp_last_ms; // 0: never
static bool s_ddp_was;
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
@@ -227,6 +226,13 @@ static uint32_t now_ms(void)
return (uint32_t)(esp_timer_get_time() / 1000);
}
// True if a timestamp stored as `now | 1` is recent. That value can be 1 ms ahead of a `now` read in the
// same millisecond, so the difference is signed: an unsigned one would wrap and look like a timeout.
static bool within(uint32_t now, uint32_t stamp, uint32_t window_ms)
{
return stamp != 0 && (int32_t)(now - stamp) < (int32_t)window_ms;
}
// Show the shadow frame, crossfading from the previous frame while a fade runs
static void output(uint32_t now, bool full_brightness)
{
@@ -348,11 +354,12 @@ static void patterns_task(void *arg)
s_reset_pending = false;
s_run = s_cur;
uint32_t now = now_ms();
bool ddp_on = s_ddp_last_ms != 0 && now - s_ddp_last_ms < DDP_TIMEOUT_MS;
if (!ddp_on && s_ddp_was) {
reset = true; // the pattern starts again when the pixels stop
if (reset) {
s_ddp_last_ms = 0; // selecting a pattern takes the strip back from DDP
}
s_ddp_was = ddp_on;
// Once pixels have arrived the strip keeps the last frame when they stop: the pattern does not
// come back until one is selected
bool ddp_on = s_ddp_last_ms != 0;
if (reset) {
if (s_run.fade > 0) {
memcpy(s_from, s_shown, s_fx.n * sizeof(rgb));
@@ -475,7 +482,7 @@ void patterns_ddp_write(uint32_t offset, const uint8_t *data, size_t len, bool p
s_ddp_packets++;
if (push) {
s_ddp_frames++;
bool syncing = s_sync_last_ms != 0 && now - s_sync_last_ms < DDP_SYNC_TIMEOUT_MS;
bool syncing = within(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.
@@ -506,7 +513,7 @@ void patterns_ddp_sync(uint32_t seq)
void patterns_ddp_status(bool *active, uint32_t *packets)
{
xSemaphoreTake(s_lock, portMAX_DELAY);
*active = s_ddp_last_ms != 0 && now_ms() - s_ddp_last_ms < DDP_TIMEOUT_MS;
*active = within(now_ms(), s_ddp_last_ms, DDP_TIMEOUT_MS);
*packets = s_ddp_packets;
xSemaphoreGive(s_lock);
}