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>
This commit is contained in:
jimmyandClaude Opus 5.5 committed 2026-11-16 19:32:36 +13:00
1 parent 94e300f6ad
commit 24dd5ffb15
30 files changed
+2159 -158

No files matched your search

+50 -39
View File
@@ -59,6 +59,7 @@ static const pattern_def_t kPatterns[] = {
{ "pulse", fx_pulse },
{ "radiate", fx_radiate },
{ "rain_drops", fx_rain_drops },
{ "rainbow", fx_rainbow },
{ "rime", fx_rime },
{ "ripple", fx_ripple },
{ "sparkle", fx_sparkle },
@@ -67,6 +68,7 @@ static const pattern_def_t kPatterns[] = {
{ "sweep", fx_sweep },
{ "transition", fx_transition },
{ "twinkle", fx_twinkle },
{ "wipe", fx_wipe },
};
#define PATTERN_COUNT ((int)(sizeof(kPatterns) / sizeof(kPatterns[0])))
@@ -152,9 +154,13 @@ static void sanitize(preset_t *p)
if (p->ncol < 0) p->ncol = 0;
if (p->ncol > PATTERN_MAX_COLOURS) p->ncol = PATTERN_MAX_COLOURS;
if (p->fade > 10000) p->fade = 10000;
// Chase steps (n3/n4) may be negative to run the dot backwards; other values stay >= 0.
const bool chase = p->pattern == patterns_index("chase");
for (int i = 1; i <= 6; i++) {
if (p->n[i] < 0) p->n[i] = 0;
bool step = chase && (i == 3 || i == 4);
if (p->n[i] < 0 && !step) p->n[i] = 0;
if (p->n[i] > 100000) p->n[i] = 100000;
if (p->n[i] < -100000) p->n[i] = -100000;
}
}
@@ -343,48 +349,53 @@ void st_reset(void)
memset(next_on, 0, s_cap);
}
// The whole frame (pattern and output) runs under s_lock, so the buffers can't be replaced mid-frame
// One frame: the whole frame (pattern and output) runs under s_lock, so the buffers can't be replaced mid-frame
void patterns_frame(void)
{
xSemaphoreTake(s_lock, portMAX_DELAY);
s_fx.pr = &s_run;
bool reset = s_reset_pending;
s_reset_pending = false;
s_run = s_cur;
uint32_t now = now_ms();
if (reset) {
s_ddp_last_ms = 0; // selecting a pattern takes the strip back from DDP
}
// 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));
s_fading = true;
s_fade_t0 = now;
s_fade_ms = s_run.fade;
} else {
s_fading = false;
}
st_reset();
g_st.t0 = now;
}
s_fx.now = now;
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_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, ddp_on);
xSemaphoreGive(s_lock);
}
static void patterns_task(void *arg)
{
(void)arg;
s_fx.pr = &s_run;
while (true) {
xSemaphoreTake(s_lock, portMAX_DELAY);
bool reset = s_reset_pending;
s_reset_pending = false;
s_run = s_cur;
uint32_t now = now_ms();
if (reset) {
s_ddp_last_ms = 0; // selecting a pattern takes the strip back from DDP
}
// 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));
s_fading = true;
s_fade_t0 = now;
s_fade_ms = s_run.fade;
} else {
s_fading = false;
}
st_reset();
g_st.t0 = now;
}
s_fx.now = now;
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_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, ddp_on);
xSemaphoreGive(s_lock);
patterns_frame();
vTaskDelay(pdMS_TO_TICKS(FRAME_MS));
}
}