Files
led-controller/main/res_misc.c
T
jimmyandClaude Opus 5.5 bfeeb958df feat: main's import/export, zones, audio API and web UI
Profiles, presets and sequences export and import as main's bundles.
Zones carry group_ids, preset_group_ids, sequence_ids, content_kind and
brightness; profiles seed rainbow as colour_cycle, and deleting a
profile also deletes its sequences. Groups, zones and sequences written
by older versions are migrated at boot.

/api/audio (status, event stream, start/stop, reset, anchor-bar,
devices) maps main's audio detector onto the mic's beat detector. Audio
start/stop picks whether mic beats or the BPM clock time sequences; the
clock's beats also go to the drivers while the mic is off.
/ws now greets from a post-handshake callback (httpd does not call the
handler for the upgrade).

The web UI is main's, plus v2's outputs editor, list search and
hostname/static IP settings; the bridge, Wi-Fi AP and led-tool parts
are gone. API tests follow main's behaviour (119 pass on the board).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 20:34:22 +13:00

367 lines
12 KiB
C

// Palettes and sequences: the v1 app's controllers/palette.py and sequence.py
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "api_util.h"
#include "engine.h"
#include "seqplay.h"
#include "store.h"
/* --------------------------------------------------------------- palettes */
// A palette is stored as a bare list of colours; older records hold {"colors": [...]}
static const cJSON *palette_colors(const cJSON *rec)
{
if (cJSON_IsObject(rec)) {
rec = cJSON_GetObjectItem(rec, "colors");
}
return cJSON_IsArray(rec) ? rec : NULL;
}
static cJSON *palette_reply(const char *id, const cJSON *rec)
{
const cJSON *colors = palette_colors(rec);
cJSON *o = cJSON_CreateObject();
cJSON_AddItemToObject(o, "colors", colors ? cJSON_Duplicate(colors, true) : cJSON_CreateArray());
cJSON_AddStringToObject(o, "id", id);
return o;
}
// Colours from {"colors": [...]}; anything else is an empty list
static cJSON *colors_from(const cJSON *body)
{
const cJSON *c = cJSON_GetObjectItem(body, "colors");
return cJSON_IsArray(c) ? cJSON_Duplicate(c, true) : cJSON_CreateArray();
}
bool palettes_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
{
cJSON *all = store_coll("palette");
const int method = req->method;
if (n == 1 && method == HTTP_GET) {
cJSON *o = cJSON_CreateObject(), *it;
cJSON_ArrayForEach(it, all)
{
const cJSON *colors = palette_colors(it);
cJSON_AddItemToObject(o, it->string, colors ? cJSON_Duplicate(colors, true) : cJSON_CreateArray());
}
*ret = api_json(req, "200 OK", o, true);
return true;
}
if (n == 1 && method == HTTP_POST) {
cJSON *body = api_body_opt(req);
if (!body) {
*ret = ESP_OK;
return true;
}
cJSON *colors = colors_from(body);
cJSON_Delete(body);
char id[16];
api_add("palette", colors, id, sizeof(id));
store_save("palette");
*ret = api_json(req, "201 Created", palette_reply(id, colors), true);
return true;
}
if (n != 2 || (method != HTTP_GET && method != HTTP_PUT)) {
return false;
}
cJSON *rec = cJSON_GetObjectItem(all, seg[1]);
if (!rec) {
*ret = api_error(req, "404 Not Found", "Palette not found");
return true;
}
if (method == HTTP_PUT) {
cJSON *body = api_body(req);
if (!body) {
*ret = ESP_OK;
return true;
}
rec = colors_from(body);
cJSON_Delete(body);
cJSON_ReplaceItemInObject(all, seg[1], rec);
store_save("palette");
}
*ret = api_json(req, "200 OK", palette_reply(seg[1], rec), true);
return true;
}
/* -------------------------------------------------------------- sequences */
// 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}";
cJSON *api_sequence_defaults(void)
{
return cJSON_Parse(SEQ_DEFAULTS);
}
static bool has_steps(const cJSON *lanes)
{
const cJSON *l;
cJSON_ArrayForEach(l, lanes)
{
if (cJSON_IsArray(l) && cJSON_GetArraySize(l) > 0) {
return true;
}
}
return false;
}
// 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)
{
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);
}
}
api_set(doc, "steps", steps);
changed = true;
}
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 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];
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_error(req, "400 Bad Request", msg ? msg : "sequence not found");
}
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", 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 == 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) {
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;
}
api_set(body, "profile_id", cJSON_CreateString(profile));
apply_update(rec, body);
cJSON_Delete(body);
store_save("sequence");
seqplay_stop_if(seg[1]);
*ret = api_json(req, "200 OK", rec, false);
return true;
}
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;
}
}