feat: device groups, combined brightness and beat-driven sequences from main

Groups (registry of driver MACs, optionally scoped to a profile) become
the drivers' group membership, alongside z<zone id> for zones that still
name drivers. Zones play to their group_ids. Group and device routes for
driver config, brightness (global x groups x device x zone) and identify.
The push path takes main's message shape: select as a list, groups as a
filter (one GROUP_CMD per group, or JSON when a preset has a background),
short keys so JSON fits a frame.

Sequences follow main: lanes of {preset_id, beats} steps played to each
lane's groups, advanced by the mic's beats or a clock at
audio_simulated_bpm; a switch waits for the next beat or downbeat; sync-
phase, stop (clears the zone's drivers) and status. This replaces the
time-based per-zone sequencer and scene playback, as in main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Opus 5.5 committed 2026-11-03 18:09:42 +13:00
1 parent 19fea0b0a1
commit 04b56c6ef2
12 files changed
+1922 -579

No files matched your search

+222 -113
View File
@@ -5,6 +5,7 @@
#include <string.h>
#include "api_util.h"
#include "engine.h"
#include "seqplay.h"
#include "store.h"
/* --------------------------------------------------------------- palettes */
@@ -87,166 +88,274 @@ bool palettes_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
/* -------------------------------------------------------------- sequences */
#define DURATION_MIN_S 0.5
#define DURATION_MAX_S 600.0
#define DURATION_DEFAULT_S 5.0
// main's models/sequence.py: lanes of {preset_id, beats} steps, each lane with its own groups
static const char SEQ_DEFAULTS[] = "{\"name\":\"\",\"group_ids\":[],\"lanes\":[[]],\"lanes_group_ids\":[[]],"
"\"advance_mode\":\"beats\",\"steps\":[],\"step_duration_ms\":3000,"
"\"sequence_transition\":500,\"loop\":true}";
static bool truthy(const cJSON *v, bool dflt)
static bool has_steps(const cJSON *lanes)
{
if (!v || cJSON_IsNull(v)) {
return dflt;
const cJSON *l;
cJSON_ArrayForEach(l, lanes)
{
if (cJSON_IsArray(l) && cJSON_GetArraySize(l) > 0) {
return true;
}
}
if (cJSON_IsString(v)) {
return v->valuestring[0] != 0;
}
return cJSON_IsTrue(v) || (cJSON_IsNumber(v) && v->valuedouble != 0);
return false;
}
// Bring a sequence record into shape: {name, zone_id, loop, repeat_count, steps: [{preset_id,
// duration_s}], active}. Returns an error message, or NULL when doc is valid.
static const char *normalize_seq(cJSON *doc)
// Fill what older records lack (main's Sequence._migrate_after_load). True when doc changed.
static bool migrate_seq(cJSON *doc, const char *default_profile)
{
char buf[64];
const cJSON *name = cJSON_GetObjectItem(doc, "name");
const char *s = cJSON_IsString(name) ? name->valuestring : "";
while (isspace((unsigned char)*s)) {
s++;
}
snprintf(buf, sizeof(buf), "%s", s);
for (int i = strlen(buf) - 1; i >= 0 && isspace((unsigned char)buf[i]); i--) {
buf[i] = 0;
}
api_set(doc, "name", cJSON_CreateString(buf));
api_set(doc, "zone_id", api_id(cJSON_GetObjectItem(doc, "zone_id"), buf, sizeof(buf)) ? cJSON_CreateString(buf)
: cJSON_CreateNull());
api_set(doc, "loop", cJSON_CreateBool(truthy(cJSON_GetObjectItem(doc, "loop"), true)));
const cJSON *rc = cJSON_GetObjectItem(doc, "repeat_count");
int repeat = 0;
if (cJSON_IsNumber(rc)) {
repeat = (int)rc->valuedouble;
} else if (rc && !cJSON_IsNull(rc) && !cJSON_IsFalse(rc)) {
return "repeat_count must be an integer";
}
api_set(doc, "repeat_count", cJSON_CreateNumber(repeat < 0 ? 0 : repeat));
const cJSON *steps = cJSON_GetObjectItem(doc, "steps");
if (steps && !cJSON_IsNull(steps) && !cJSON_IsArray(steps)) {
return "steps must be a list";
}
cJSON *out = cJSON_CreateArray();
const cJSON *st;
cJSON_ArrayForEach(st, steps)
{
const cJSON *pid = cJSON_IsObject(st) ? cJSON_GetObjectItem(st, "preset_id") : st;
if (!api_id(pid, buf, sizeof(buf))) {
cJSON_Delete(out);
return "each step needs a preset_id";
bool changed = false;
if (!cJSON_IsArray(cJSON_GetObjectItem(doc, "steps"))) {
cJSON *steps = cJSON_CreateArray(), *p;
cJSON_ArrayForEach(p, cJSON_GetObjectItem(doc, "presets"))
{
char id[24];
if (api_id(p, id, sizeof(id))) {
cJSON *st = cJSON_CreateObject();
cJSON_AddStringToObject(st, "preset_id", id);
cJSON_AddArrayToObject(st, "group_ids");
cJSON_AddItemToArray(steps, st);
}
}
const cJSON *dv = cJSON_IsObject(st) ? cJSON_GetObjectItem(st, "duration_s") : NULL;
double d = cJSON_IsNumber(dv) ? dv->valuedouble : DURATION_DEFAULT_S;
d = d < DURATION_MIN_S ? DURATION_MIN_S : (d > DURATION_MAX_S ? DURATION_MAX_S : d);
cJSON *step = cJSON_CreateObject();
cJSON_AddStringToObject(step, "preset_id", buf);
cJSON_AddNumberToObject(step, "duration_s", d);
cJSON_AddItemToArray(out, step);
api_set(doc, "steps", steps);
changed = true;
}
api_set(doc, "steps", out);
api_set(doc, "active", cJSON_CreateBool(truthy(cJSON_GetObjectItem(doc, "active"), false)));
return NULL;
static const char *const DEFAULT_KEYS[] = { "step_duration_ms", "loop", "name", "advance_mode",
"sequence_transition", "group_ids" };
cJSON *defs = cJSON_Parse(SEQ_DEFAULTS);
for (size_t i = 0; i < sizeof(DEFAULT_KEYS) / sizeof(DEFAULT_KEYS[0]); i++) {
const cJSON *v = cJSON_GetObjectItem(doc, DEFAULT_KEYS[i]);
bool bad = !v || (!strcmp(DEFAULT_KEYS[i], "group_ids") && !cJSON_IsArray(v)) ||
(!strcmp(DEFAULT_KEYS[i], "advance_mode") && !(cJSON_IsString(v) && !strcmp(v->valuestring, "beats")));
if (bad) {
api_set(doc, DEFAULT_KEYS[i], cJSON_Duplicate(cJSON_GetObjectItem(defs, DEFAULT_KEYS[i]), true));
changed = true;
}
}
cJSON_Delete(defs);
if (!cJSON_GetObjectItem(doc, "profile_id") && default_profile) {
cJSON_AddStringToObject(doc, "profile_id", default_profile);
changed = true;
}
cJSON *lanes = cJSON_GetObjectItem(doc, "lanes");
if (!cJSON_IsArray(lanes)) {
lanes = cJSON_CreateArray();
const cJSON *steps = cJSON_GetObjectItem(doc, "steps");
cJSON_AddItemToArray(lanes, cJSON_GetArraySize(steps) ? cJSON_Duplicate(steps, true) : cJSON_CreateArray());
api_set(doc, "lanes", lanes);
changed = true;
}
int n_lanes = 0;
cJSON *lane, *step;
cJSON_ArrayForEach(lane, lanes)
{
if (!cJSON_IsArray(lane)) {
continue;
}
n_lanes++;
cJSON_ArrayForEach(step, lane)
{
if (cJSON_IsObject(step) && !cJSON_GetObjectItem(step, "beats")) {
cJSON_AddNumberToObject(step, "beats", 1);
changed = true;
}
}
}
const cJSON *lg = cJSON_GetObjectItem(doc, "lanes_group_ids");
if (n_lanes && (!cJSON_IsArray(lg) || cJSON_GetArraySize(lg) != n_lanes)) {
const cJSON *shared = cJSON_GetObjectItem(doc, "group_ids");
cJSON *out = cJSON_CreateArray();
const cJSON *first_step = cJSON_GetArrayItem(cJSON_GetArrayItem(lanes, 0), 0);
const cJSON *step_g = cJSON_GetObjectItem(first_step, "group_ids");
for (int i = 0; i < n_lanes; i++) {
bool own = n_lanes == 1 && cJSON_GetArraySize(step_g) > 0;
const cJSON *src = own ? step_g : shared;
cJSON_AddItemToArray(out, cJSON_IsArray(src) ? cJSON_Duplicate(src, true) : cJSON_CreateArray());
}
api_set(doc, "lanes_group_ids", out);
changed = true;
}
return changed;
}
void api_migrate_sequences(void)
{
const char *first = store_coll("profile")->child ? store_coll("profile")->child->string : NULL;
bool changed = false;
cJSON *it;
cJSON_ArrayForEach(it, store_coll("sequence"))
{
if (cJSON_IsObject(it)) {
changed |= migrate_seq(it, first);
}
}
if (changed) {
store_save("sequence");
}
}
static bool in_profile(const cJSON *seq)
{
const char *cur = api_current_profile();
char pid[24];
return cur && api_id(cJSON_GetObjectItem(seq, "profile_id"), pid, sizeof(pid)) && !strcmp(pid, cur);
}
// main's Sequence.update: a steps list with no usable lanes becomes lane 0
static void apply_update(cJSON *doc, cJSON *data)
{
const cJSON *steps = cJSON_GetObjectItem(data, "steps");
if (cJSON_GetArraySize(steps) && !has_steps(cJSON_GetObjectItem(data, "lanes"))) {
cJSON *lanes = cJSON_CreateArray();
cJSON_AddItemToArray(lanes, cJSON_Duplicate(steps, true));
api_set(data, "lanes", lanes);
}
api_merge(doc, data);
}
static esp_err_t play_sequence(httpd_req_t *req, const char *id)
{
const cJSON *rec = cJSON_GetObjectItem(store_coll("sequence"), id);
if (!rec) {
return api_error(req, "404 Not Found", "Sequence not found");
const char *profile = api_current_profile();
if (!profile) {
return api_error(req, "404 Not Found", "No profile available");
}
cJSON *body = api_body_opt(req);
if (!body) {
return ESP_OK;
}
char zone[24];
if (!api_id(cJSON_GetObjectItem(rec, "zone_id"), zone, sizeof(zone))) {
return api_error(req, "400 Bad Request", "Sequence has no zone_id");
}
if (!cJSON_GetArraySize(cJSON_GetObjectItem(rec, "steps"))) {
return api_error(req, "400 Bad Request", "Sequence has no steps");
}
if (!cJSON_GetObjectItem(store_coll("zone"), zone)) {
return api_error(req, "404 Not Found", "Zone not found");
}
esp_err_t err = engine_seq_play(id);
if (err == ESP_ERR_NO_MEM) {
return api_error(req, "503 Service Unavailable", "too many sequences running");
const cJSON *z = cJSON_GetObjectItem(body, "zone_id");
bool have = api_id(z ? z : cJSON_GetObjectItem(body, "zoneId"), zone, sizeof(zone));
cJSON_Delete(body);
if (!have) {
return api_error(req, "400 Bad Request", "zone_id required");
}
const char *msg;
esp_err_t err = seqplay_start(id, zone, profile, &msg);
if (err != ESP_OK) {
return api_result(req, err, "");
return api_error(req, "400 Bad Request", msg ? msg : "sequence not found");
}
cJSON *all = engine_seq_status();
cJSON *state = cJSON_DetachItemFromObject(all, id);
cJSON_Delete(all);
return api_json(req, "200 OK", state ? state : cJSON_CreateObject(), true);
cJSON *o = seqplay_pending();
cJSON_AddTrueToObject(o, "ok");
return api_json(req, "200 OK", o, true);
}
bool sequences_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
{
cJSON *all = store_coll("sequence");
const int method = req->method;
const char *profile = api_current_profile();
if (n == 2 && method == HTTP_GET && !strcmp(seg[1], "status")) {
*ret = api_json(req, "200 OK", engine_seq_status(), true);
*ret = api_json(req, "200 OK", seqplay_status(), true);
return true;
}
if (n == 2 && method == HTTP_POST && !strcmp(seg[1], "stop")) {
seqplay_stop(true);
cJSON *o = cJSON_CreateObject();
cJSON_AddTrueToObject(o, "ok");
*ret = api_json(req, "200 OK", o, true);
return true;
}
if (n == 2 && method == HTTP_POST && !strcmp(seg[1], "sync-phase")) {
cJSON *body = api_body_opt(req);
if (!body) {
*ret = ESP_OK;
return true;
}
const cJSON *m = cJSON_GetObjectItem(body, "mode");
if (!cJSON_IsString(m)) {
m = cJSON_GetObjectItem(body, "align");
}
char mode[16];
snprintf(mode, sizeof(mode), "%s", cJSON_IsString(m) ? m->valuestring : "step");
for (char *c = mode; *c; c++) {
*c = tolower((unsigned char)*c);
}
cJSON_Delete(body);
if (!seqplay_sync(mode)) {
*ret = api_error(req, "409 Conflict", "No sequence is playing");
return true;
}
cJSON *o = cJSON_CreateObject();
cJSON_AddTrueToObject(o, "ok");
cJSON_AddStringToObject(o, "mode", mode);
*ret = api_json(req, "200 OK", o, true);
return true;
}
if (n == 3 && method == HTTP_POST && !strcmp(seg[2], "play")) {
*ret = play_sequence(req, seg[1]);
return true;
}
if (n == 3 && method == HTTP_POST && !strcmp(seg[2], "stop")) {
bool stopped = engine_seq_stop(seg[1]) == ESP_OK;
cJSON *o = cJSON_CreateObject();
cJSON_AddBoolToObject(o, "stopped", stopped);
if (n == 1 && method == HTTP_GET) {
cJSON *o = cJSON_CreateObject(), *it;
cJSON_ArrayForEach(it, all)
{
if (cJSON_IsObject(it) && in_profile(it)) {
cJSON_AddItemReferenceToObject(o, it->string, it);
}
}
*ret = api_json(req, "200 OK", o, true);
return true;
}
if ((n == 1 && method == HTTP_POST) || (n == 2 && method == HTTP_PUT)) {
cJSON *old = n == 2 ? cJSON_GetObjectItem(all, seg[1]) : NULL;
if (n == 2 && !old) {
*ret = api_error(req, "404 Not Found", "Sequence not found");
if (n == 1 && method == HTTP_POST) {
if (!profile) {
*ret = api_error(req, "404 Not Found", "No profile available");
return true;
}
cJSON *body = api_body_opt(req);
if (!body) {
*ret = ESP_OK;
return true;
}
cJSON *doc = cJSON_Parse(SEQ_DEFAULTS);
api_set(body, "profile_id", cJSON_CreateString(profile));
apply_update(doc, body);
cJSON_Delete(body);
char id[16];
api_add("sequence", doc, id, sizeof(id));
store_save("sequence");
cJSON *o = cJSON_CreateObject();
cJSON_AddItemReferenceToObject(o, id, doc);
*ret = api_json(req, "201 Created", o, true);
return true;
}
if (n != 2) {
return false;
}
cJSON *rec = cJSON_GetObjectItem(all, seg[1]);
if (!rec || !in_profile(rec)) {
*ret = api_error(req, "404 Not Found", "Sequence not found");
return true;
}
switch (method) {
case HTTP_GET:
*ret = api_json(req, "200 OK", rec, false);
return true;
case HTTP_PUT: {
cJSON *body = api_body(req);
if (!body) {
*ret = ESP_OK;
return true;
}
cJSON *doc = old ? cJSON_Duplicate(old, true) : cJSON_CreateObject();
api_merge(doc, body);
api_set(body, "profile_id", cJSON_CreateString(profile));
apply_update(rec, body);
cJSON_Delete(body);
const char *bad = normalize_seq(doc);
if (bad) {
cJSON_Delete(doc);
*ret = api_error(req, "400 Bad Request", bad);
return true;
}
char id[16];
if (old) {
snprintf(id, sizeof(id), "%s", seg[1]);
api_set(doc, "id", cJSON_CreateString(id));
cJSON_ReplaceItemInObject(all, id, doc);
} else {
api_add("sequence", doc, id, sizeof(id));
}
store_save("sequence");
*ret = api_json(req, old ? "200 OK" : "201 Created", doc, false);
seqplay_stop_if(seg[1]);
*ret = api_json(req, "200 OK", rec, false);
return true;
}
if (n == 2 && method == HTTP_DELETE) {
if (!cJSON_GetObjectItem(all, seg[1])) {
*ret = api_error(req, "404 Not Found", "Sequence not found");
return true;
}
engine_seq_stop(seg[1]);
case HTTP_DELETE:
seqplay_stop_if(seg[1]);
cJSON_DeleteItemFromObject(all, seg[1]);
store_save("sequence");
*ret = api_message(req, "200 OK", "Sequence deleted successfully");
return true;
default:
return false;
}
return false;
}