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>
This commit is contained in:
1 parent
04b56c6ef2
commit
bfeeb958df
44 files changed
+8709
-2338
No files matched your search
+1
-1
@@ -1,5 +1,5 @@
|
||||
idf_component_register(SRCS "main.c" "mic.c" "beat.c" "beat_dsp.c" "espnow_tx.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c" "ota.c" "drv_ota.c"
|
||||
"res_profile.c" "res_misc.c" "res_device.c" "res_group.c" "seqplay.c" "patterns.c" "outputs.c" "ws.c"
|
||||
"res_profile.c" "res_misc.c" "res_device.c" "res_group.c" "seqplay.c" "res_bundle.c" "audio_api.c" "patterns.c" "outputs.c" "ws.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES esp_eth esp_netif esp_event esp_driver_spi esp_driver_gpio nvs_flash app_update esp_app_format
|
||||
esp_http_server esp_timer esp_driver_i2s esp_wifi esp_rom json esp_driver_gpio
|
||||
|
||||
@@ -205,6 +205,9 @@ void api_add(const char *coll, cJSON *rec, char *id, size_t len)
|
||||
|
||||
static bool route(const char *res, httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
|
||||
{
|
||||
if (bundles_route(req, n, seg, ret)) { // /<profiles|presets|sequences>/import and /<id>/export
|
||||
return true;
|
||||
}
|
||||
static const struct {
|
||||
const char *res;
|
||||
bool (*fn)(httpd_req_t *, int, api_seg_t, esp_err_t *);
|
||||
@@ -508,6 +511,8 @@ esp_err_t api_register(httpd_handle_t server)
|
||||
store_lock();
|
||||
api_settings_defaults();
|
||||
api_migrate_sequences();
|
||||
api_migrate_zones();
|
||||
api_migrate_groups();
|
||||
store_unlock();
|
||||
static const char *const ROUTES[] = {
|
||||
"/devices*", "/zones*", "/presets*", "/profiles*", "/palettes*",
|
||||
|
||||
@@ -44,15 +44,22 @@ int api_effective_brightness(const char *mac, int zone_b);
|
||||
// Blink every member of a group, then "off"
|
||||
esp_err_t api_identify_group(const char *group_id);
|
||||
|
||||
// New records with every field set (callers merge in the request)
|
||||
cJSON *api_preset_defaults(const char *profile_id);
|
||||
cJSON *api_sequence_defaults(void);
|
||||
|
||||
// Current profile id (settings.current_profile_id, else the first profile), or NULL
|
||||
const char *api_current_profile(void);
|
||||
bool api_preset_in_profile(const cJSON *rec);
|
||||
|
||||
// Bring records written by older versions into the current shape (at boot)
|
||||
void api_migrate_sequences(void);
|
||||
void api_migrate_zones(void);
|
||||
void api_migrate_groups(void);
|
||||
|
||||
// Resource handlers, called with the store locked. Each returns true when it answered the request
|
||||
// (its result in *ret); false hands the request to the generic collection CRUD.
|
||||
bool bundles_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret);
|
||||
bool profiles_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret);
|
||||
bool zones_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret);
|
||||
bool presets_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret);
|
||||
|
||||
@@ -0,0 +1,303 @@
|
||||
// main's /api/audio routes on this board's microphone: status and an event stream in main's shape,
|
||||
// start/stop (whether mic beats time sequences), tracking reset and bar anchoring. There is one input,
|
||||
// the I2S microphone, so device selection is stored but changes nothing.
|
||||
|
||||
#include "audio_api.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include "api_util.h"
|
||||
#include "beat.h"
|
||||
#include "cJSON.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_timer.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "mic.h"
|
||||
#include "seqplay.h"
|
||||
#include "store.h"
|
||||
|
||||
static const char *TAG = "audio_api";
|
||||
|
||||
#define EVENTS_HEARTBEAT_US 500000
|
||||
|
||||
static int setting_int(const char *key, int dflt, int lo, int hi)
|
||||
{
|
||||
const cJSON *v = cJSON_GetObjectItem(store_settings(), key);
|
||||
int x = cJSON_IsNumber(v) ? (int)v->valuedouble : dflt;
|
||||
return x < lo ? lo : (x > hi ? hi : x);
|
||||
}
|
||||
|
||||
static cJSON *audio_run(void)
|
||||
{
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddBoolToObject(o, "enabled", seqplay_audio());
|
||||
const cJSON *sel = cJSON_GetObjectItem(store_settings(), "audio_device_select");
|
||||
cJSON_AddNullToObject(o, "device");
|
||||
cJSON_AddStringToObject(o, "device_override", "");
|
||||
cJSON_AddStringToObject(o, "device_select", cJSON_IsString(sel) ? sel->valuestring : "");
|
||||
return o;
|
||||
}
|
||||
|
||||
// main's audio_status_payload
|
||||
static cJSON *status_payload(void)
|
||||
{
|
||||
beat_info_t bi;
|
||||
beat_get_info(&bi);
|
||||
mic_level_t lv;
|
||||
mic_get_level(&lv);
|
||||
bool running = seqplay_audio();
|
||||
int meter = bi.meter > 0 ? bi.meter : 4;
|
||||
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
store_lock();
|
||||
int sim_bpm = setting_int("audio_simulated_bpm", 120, 60, 200);
|
||||
cJSON_AddBoolToObject(o, "running", running);
|
||||
cJSON_AddStringToObject(o, "device", "i2s");
|
||||
cJSON_AddNullToObject(o, "error");
|
||||
if (!running) {
|
||||
cJSON_AddNumberToObject(o, "bpm", sim_bpm);
|
||||
} else if (bi.locked && bi.bpm > 0) {
|
||||
cJSON_AddNumberToObject(o, "bpm", bi.bpm < 60 ? 60 : (bi.bpm > 200 ? 200 : bi.bpm));
|
||||
} else {
|
||||
cJSON_AddNullToObject(o, "bpm");
|
||||
}
|
||||
cJSON_AddNumberToObject(o, "beat_seq", bi.beat_count);
|
||||
cJSON_AddNumberToObject(o, "beats_per_bar", meter);
|
||||
cJSON_AddStringToObject(o, "beat_type", "unknown");
|
||||
cJSON_AddNumberToObject(o, "beat_type_confidence", 0);
|
||||
cJSON_AddNumberToObject(o, "input_level", running ? powf(10.0f, lv.rms_dbfs / 20.0f) : 0);
|
||||
cJSON_AddItemToObject(o, "last_beat_ts", bi.last_beat_us ? cJSON_CreateNumber(bi.last_beat_us / 1e6) : cJSON_CreateNull());
|
||||
int bar_beat;
|
||||
double phase_conf;
|
||||
if (running) {
|
||||
bar_beat = bi.bar_beat > 0 ? bi.bar_beat : 1;
|
||||
phase_conf = bi.bar_sure ? 1.0 : 0.0;
|
||||
} else {
|
||||
uint32_t tick;
|
||||
seqplay_clock(&bar_beat, &tick);
|
||||
phase_conf = 0;
|
||||
}
|
||||
char readout[24];
|
||||
snprintf(readout, sizeof(readout), "%d/%d", bar_beat, running ? meter : 4);
|
||||
cJSON_AddNumberToObject(o, "bar_beat", bar_beat);
|
||||
cJSON_AddBoolToObject(o, "is_downbeat", bar_beat == 1);
|
||||
cJSON_AddStringToObject(o, "bar_phase_readout", readout);
|
||||
cJSON_AddNumberToObject(o, "phase_confidence", phase_conf);
|
||||
|
||||
cJSON *seq = seqplay_status();
|
||||
const cJSON *ro = cJSON_GetObjectItem(seq, "beat_readout");
|
||||
if (cJSON_IsString(ro) && ro->valuestring[0]) {
|
||||
snprintf(readout, sizeof(readout), "%s", ro->valuestring);
|
||||
} else if (seqplay_done_readout()[0]) {
|
||||
snprintf(readout, sizeof(readout), "%s", seqplay_done_readout());
|
||||
} else if (running && bi.beat_count > 0) {
|
||||
snprintf(readout, sizeof(readout), "%lu", (unsigned long)bi.beat_count);
|
||||
} else {
|
||||
readout[0] = 0;
|
||||
}
|
||||
cJSON_AddItemToObject(o, "sequence", seq);
|
||||
cJSON *stride = cJSON_AddObjectToObject(o, "manual_beat_stride");
|
||||
cJSON_AddFalseToObject(stride, "active"); // drivers step manual presets on the beats themselves
|
||||
cJSON_AddStringToObject(o, "beat_readout", readout);
|
||||
cJSON_AddNumberToObject(o, "beat_phase_ms", setting_int("audio_beat_phase_ms", 0, 0, 500));
|
||||
cJSON_AddNumberToObject(o, "input_volume", setting_int("audio_input_volume", 100, 0, 200));
|
||||
const cJSON *sw = cJSON_GetObjectItem(store_settings(), "sequence_switch_wait");
|
||||
cJSON_AddStringToObject(o, "sequence_switch_wait_saved",
|
||||
cJSON_IsString(sw) && !strcasecmp(sw->valuestring, "downbeat") ? "downbeat" : "beat");
|
||||
cJSON_AddStringToObject(o, "sequence_switch_wait", seqplay_switch_wait());
|
||||
cJSON_AddItemToObject(o, "audio_run", audio_run());
|
||||
cJSON_AddNumberToObject(o, "audio_simulated_bpm", sim_bpm);
|
||||
cJSON_AddItemToObject(o, "sequence_pending", seqplay_pending());
|
||||
cJSON_AddBoolToObject(o, "bpm_simulated", !(running && bi.locked));
|
||||
int sim_bar;
|
||||
uint32_t tick;
|
||||
seqplay_clock(&sim_bar, &tick);
|
||||
cJSON_AddNumberToObject(o, "simulated_beat_tick", tick);
|
||||
store_unlock();
|
||||
return o;
|
||||
}
|
||||
|
||||
static esp_err_t send_json(httpd_req_t *req, const char *status, cJSON *o)
|
||||
{
|
||||
char *s = cJSON_PrintUnformatted(o);
|
||||
cJSON_Delete(o);
|
||||
if (!s) {
|
||||
return httpd_resp_send_500(req);
|
||||
}
|
||||
httpd_resp_set_status(req, status);
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
esp_err_t r = httpd_resp_sendstr(req, s);
|
||||
free(s);
|
||||
return r;
|
||||
}
|
||||
|
||||
static esp_err_t ok_with_status(httpd_req_t *req)
|
||||
{
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddTrueToObject(o, "ok");
|
||||
cJSON_AddItemToObject(o, "status", status_payload());
|
||||
return send_json(req, "200 OK", o);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ event stream */
|
||||
|
||||
static bool send_all(int fd, const char *p, size_t len)
|
||||
{
|
||||
while (len > 0) {
|
||||
int sent = send(fd, p, len, 0);
|
||||
if (sent <= 0) {
|
||||
return false;
|
||||
}
|
||||
p += sent;
|
||||
len -= sent;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool send_event(int fd, const char *type)
|
||||
{
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(o, "type", type);
|
||||
cJSON_AddItemToObject(o, "status", status_payload());
|
||||
char *s = cJSON_PrintUnformatted(o);
|
||||
cJSON_Delete(o);
|
||||
bool ok = s && send_all(fd, "data: ", 6) && send_all(fd, s, strlen(s)) && send_all(fd, "\n\n", 2);
|
||||
free(s);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static void events_task(void *arg)
|
||||
{
|
||||
httpd_req_t *req = arg;
|
||||
httpd_handle_t server = req->handle;
|
||||
int fd = httpd_req_to_sockfd(req);
|
||||
QueueHandle_t q = xQueueCreate(8, sizeof(beat_event_t));
|
||||
static const char hdr[] = "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nCache-Control: no-cache\r\n"
|
||||
"Connection: close\r\n\r\n";
|
||||
bool ok = q && beat_subscribe(q) == ESP_OK;
|
||||
if (!ok) {
|
||||
static const char busy[] = "HTTP/1.1 503 Service Unavailable\r\nConnection: close\r\n\r\nToo many listeners\n";
|
||||
send_all(fd, busy, sizeof(busy) - 1);
|
||||
} else {
|
||||
ok = send_all(fd, hdr, sizeof(hdr) - 1) && send_event(fd, "status");
|
||||
}
|
||||
int64_t next = esp_timer_get_time() + EVENTS_HEARTBEAT_US;
|
||||
while (ok) {
|
||||
int64_t now = esp_timer_get_time();
|
||||
beat_event_t ev;
|
||||
if (now >= next) {
|
||||
ok = send_event(fd, "status");
|
||||
next = now + EVENTS_HEARTBEAT_US;
|
||||
} else if (xQueueReceive(q, &ev, pdMS_TO_TICKS((next - now) / 1000) + 1) == pdTRUE && ev == BEAT_EVENT_BEAT) {
|
||||
vTaskDelay(pdMS_TO_TICKS(20)); // let the sequencer count this beat first
|
||||
ok = send_event(fd, "beat");
|
||||
}
|
||||
}
|
||||
if (q) {
|
||||
beat_unsubscribe(q);
|
||||
vQueueDelete(q);
|
||||
}
|
||||
httpd_req_async_handler_complete(req);
|
||||
httpd_sess_trigger_close(server, fd);
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
static esp_err_t events(httpd_req_t *req)
|
||||
{
|
||||
httpd_req_t *async_req = NULL;
|
||||
if (httpd_req_async_handler_begin(req, &async_req) != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
if (xTaskCreate(events_task, "audio_events", 6144, async_req, 5, NULL) != pdPASS) {
|
||||
httpd_req_async_handler_complete(async_req);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------- handlers */
|
||||
|
||||
static esp_err_t audio_handler(httpd_req_t *req)
|
||||
{
|
||||
const char *action = req->uri + strlen("/api/audio/");
|
||||
size_t alen = strcspn(action, "?");
|
||||
char act[16] = "";
|
||||
if (alen < sizeof(act)) {
|
||||
memcpy(act, action, alen);
|
||||
}
|
||||
if (req->method == HTTP_GET && !strcmp(act, "status")) {
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddItemToObject(o, "status", status_payload());
|
||||
return send_json(req, "200 OK", o);
|
||||
}
|
||||
if (req->method == HTTP_GET && !strcmp(act, "events")) {
|
||||
return events(req);
|
||||
}
|
||||
if (req->method == HTTP_GET && !strcmp(act, "devices")) {
|
||||
cJSON *o = cJSON_CreateObject(), *devs = cJSON_AddArrayToObject(o, "devices"), *d = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(d, "id", "i2s");
|
||||
cJSON_AddStringToObject(d, "name", "I2S microphone");
|
||||
cJSON_AddStringToObject(d, "display_name", "I2S microphone");
|
||||
cJSON_AddTrueToObject(d, "is_default");
|
||||
cJSON_AddItemToArray(devs, d);
|
||||
cJSON_AddObjectToObject(o, "diagnostics");
|
||||
return send_json(req, "200 OK", o);
|
||||
}
|
||||
if (req->method == HTTP_PUT && !strcmp(act, "device")) {
|
||||
cJSON *body = api_body_opt(req);
|
||||
if (!body) {
|
||||
return ESP_OK;
|
||||
}
|
||||
const cJSON *sel = cJSON_GetObjectItem(body, "device_select");
|
||||
store_lock();
|
||||
api_set(store_settings(), "audio_device_select", cJSON_CreateString(cJSON_IsString(sel) ? sel->valuestring : ""));
|
||||
store_save("settings");
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddTrueToObject(o, "ok");
|
||||
cJSON_AddItemToObject(o, "audio_run", audio_run());
|
||||
store_unlock();
|
||||
cJSON_Delete(body);
|
||||
return send_json(req, "200 OK", o);
|
||||
}
|
||||
if (req->method != HTTP_POST) {
|
||||
return httpd_resp_send_404(req);
|
||||
}
|
||||
if (!strcmp(act, "start") || !strcmp(act, "stop")) {
|
||||
seqplay_set_audio(act[2] == 'a'); // st[a]rt
|
||||
ESP_LOGI(TAG, "mic beats %s sequences", act[2] == 'a' ? "time" : "no longer time");
|
||||
return ok_with_status(req);
|
||||
}
|
||||
if (!strcmp(act, "reset") || !strcmp(act, "anchor-bar")) {
|
||||
if (!seqplay_audio()) {
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddFalseToObject(o, "ok");
|
||||
cJSON_AddStringToObject(o, "error", "Audio detector is not running");
|
||||
return send_json(req, "409 Conflict", o);
|
||||
}
|
||||
if (act[0] == 'r') {
|
||||
beat_reset();
|
||||
} else {
|
||||
beat_tap();
|
||||
}
|
||||
return ok_with_status(req);
|
||||
}
|
||||
return httpd_resp_send_404(req);
|
||||
}
|
||||
|
||||
esp_err_t audio_api_register(httpd_handle_t server)
|
||||
{
|
||||
static const httpd_method_t METHODS[] = { HTTP_GET, HTTP_POST, HTTP_PUT };
|
||||
for (size_t m = 0; m < 3; m++) {
|
||||
httpd_uri_t u = { .uri = "/api/audio/*", .method = METHODS[m], .handler = audio_handler };
|
||||
esp_err_t err = httpd_register_uri_handler(server, &u);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "esp_http_server.h"
|
||||
|
||||
// /api/audio/{status,events,devices,device,start,stop,reset,anchor-bar}: main's audio API (see audio_api.c)
|
||||
esp_err_t audio_api_register(httpd_handle_t server);
|
||||
+24
@@ -105,9 +105,13 @@ static void dsp_event(void *ctx, int events, const beat_dsp_t *d)
|
||||
}
|
||||
}
|
||||
|
||||
static float s_rate;
|
||||
static bool s_reset;
|
||||
|
||||
esp_err_t beat_start(float sample_rate)
|
||||
{
|
||||
float gate_rms = powf(10.0f, CONFIG_BEAT_GATE_DBFS / 20.0f);
|
||||
s_rate = sample_rate;
|
||||
beat_dsp_init(&s_dsp, sample_rate, gate_rms, CONFIG_BEAT_LEAD_MS / 1000.0f);
|
||||
s_info.meter = s_dsp.meter;
|
||||
s_subs_mutex = xSemaphoreCreateMutex();
|
||||
@@ -128,6 +132,13 @@ esp_err_t beat_start(float sample_rate)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void beat_reset(void)
|
||||
{
|
||||
portENTER_CRITICAL(&s_lock);
|
||||
s_reset = true;
|
||||
portEXIT_CRITICAL(&s_lock);
|
||||
}
|
||||
|
||||
void beat_tap(void)
|
||||
{
|
||||
portENTER_CRITICAL(&s_lock);
|
||||
@@ -137,6 +148,19 @@ void beat_tap(void)
|
||||
|
||||
void beat_feed(const float *x, size_t n)
|
||||
{
|
||||
portENTER_CRITICAL(&s_lock);
|
||||
bool reset = s_reset;
|
||||
s_reset = false;
|
||||
portEXIT_CRITICAL(&s_lock);
|
||||
if (reset) { // start tempo and bar tracking over, in the detector's task
|
||||
beat_dsp_init(&s_dsp, s_rate, powf(10.0f, CONFIG_BEAT_GATE_DBFS / 20.0f), CONFIG_BEAT_LEAD_MS / 1000.0f);
|
||||
portENTER_CRITICAL(&s_lock);
|
||||
s_info.bar_beat = 0;
|
||||
s_info.bar_sure = false;
|
||||
s_info.meter = s_dsp.meter;
|
||||
portEXIT_CRITICAL(&s_lock);
|
||||
ESP_LOGI(TAG, "Tracking reset");
|
||||
}
|
||||
beat_dsp_feed(&s_dsp, x, n, dsp_event, NULL);
|
||||
|
||||
// Taps arrive from the HTTP task; apply them here, in the detector's task
|
||||
|
||||
@@ -37,6 +37,8 @@ void beat_get_info(beat_info_t *out);
|
||||
|
||||
// The user tapped beat 1 just now: correct the bar position to match
|
||||
void beat_tap(void);
|
||||
// Forget the tempo and bar position and start tracking again
|
||||
void beat_reset(void);
|
||||
|
||||
// Have beat_event_t values posted (without blocking) to a queue of
|
||||
// beat_event_t. Returns ESP_ERR_NO_MEM when all subscriber slots are taken.
|
||||
|
||||
@@ -113,9 +113,19 @@ void espnow_tx_beat(float bpm, bool downbeat, int bar)
|
||||
send_beat_copy(NULL);
|
||||
}
|
||||
|
||||
static volatile bool s_mic_beats = true;
|
||||
|
||||
void espnow_tx_set_mic_beats(bool on)
|
||||
{
|
||||
s_mic_beats = on;
|
||||
}
|
||||
|
||||
// Runs in the audio task straight from the detector: no queue, no allocation.
|
||||
static void on_beat(const beat_info_t *info, float bpm, bool downbeat)
|
||||
{
|
||||
if (!s_mic_beats) {
|
||||
return;
|
||||
}
|
||||
if (bench_running()) {
|
||||
return; // the bench generator owns the beat stream while it runs
|
||||
}
|
||||
|
||||
@@ -41,6 +41,8 @@ void espnow_tx_stats(espnow_stats_t *out);
|
||||
|
||||
// Send one beat now (also used by the bench generator). bar 0 = unknown.
|
||||
void espnow_tx_beat(float bpm, bool downbeat, int bar);
|
||||
// Whether the mic's detected beats go to the drivers (off while audio timing is switched off)
|
||||
void espnow_tx_set_mic_beats(bool on);
|
||||
|
||||
// Lossy-link test mode: discard this percentage of outgoing frames and of
|
||||
// received acks/echoes. include_beats = false leaves beat copies alone.
|
||||
|
||||
+3
-1
@@ -33,6 +33,7 @@
|
||||
#include "api.h"
|
||||
#include "engine.h"
|
||||
#include "seqplay.h"
|
||||
#include "audio_api.h"
|
||||
#include "rel.h"
|
||||
#include "web.h"
|
||||
#include "audio.h"
|
||||
@@ -209,7 +210,7 @@ static void start_webserver(void)
|
||||
}
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.uri_match_fn = httpd_uri_match_wildcard;
|
||||
config.max_uri_handlers = 72;
|
||||
config.max_uri_handlers = 80;
|
||||
config.stack_size = 8192;
|
||||
config.lru_purge_enable = true;
|
||||
if (httpd_start(&s_server, &config) != ESP_OK) {
|
||||
@@ -220,6 +221,7 @@ static void start_webserver(void)
|
||||
httpd_register_uri_handler(s_server, &status_uri);
|
||||
web_register(s_server);
|
||||
audio_register(s_server);
|
||||
audio_api_register(s_server);
|
||||
ota_register(s_server);
|
||||
drv_ota_register(s_server);
|
||||
api_register(s_server);
|
||||
|
||||
@@ -0,0 +1,492 @@
|
||||
// Import/export bundles: main's util/profile_bundle.py. A profile bundle carries the profile, its
|
||||
// palette colours, zones, the presets they use and their sequences; preset and sequence bundles carry
|
||||
// one record (a sequence with its presets). Imports get new ids and belong to the importing profile.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include "api_util.h"
|
||||
#include "engine.h"
|
||||
#include "store.h"
|
||||
|
||||
#define BUNDLE_VERSION 1
|
||||
|
||||
static void add_ref(cJSON *set, const cJSON *v)
|
||||
{
|
||||
if (cJSON_IsArray(v)) {
|
||||
const cJSON *it;
|
||||
cJSON_ArrayForEach(it, v)
|
||||
{
|
||||
add_ref(set, it);
|
||||
}
|
||||
return;
|
||||
}
|
||||
char id[24];
|
||||
if (api_id(v, id, sizeof(id)) && !cJSON_GetObjectItem(set, id)) {
|
||||
cJSON_AddTrueToObject(set, id);
|
||||
}
|
||||
}
|
||||
|
||||
static void zone_preset_ids(const cJSON *zone, cJSON *set)
|
||||
{
|
||||
add_ref(set, cJSON_GetObjectItem(zone, "presets"));
|
||||
add_ref(set, cJSON_GetObjectItem(zone, "presets_flat"));
|
||||
add_ref(set, cJSON_GetObjectItem(zone, "default_preset"));
|
||||
}
|
||||
|
||||
static void sequence_preset_ids(const cJSON *seq, cJSON *set)
|
||||
{
|
||||
const cJSON *lane, *step;
|
||||
cJSON_ArrayForEach(lane, cJSON_GetObjectItem(seq, "lanes"))
|
||||
{
|
||||
cJSON_ArrayForEach(step, lane)
|
||||
{
|
||||
add_ref(set, cJSON_GetObjectItem(step, "preset_id"));
|
||||
}
|
||||
}
|
||||
cJSON_ArrayForEach(step, cJSON_GetObjectItem(seq, "steps"))
|
||||
{
|
||||
add_ref(set, cJSON_GetObjectItem(step, "preset_id"));
|
||||
}
|
||||
}
|
||||
|
||||
static bool owned_by(const cJSON *rec, const char *profile)
|
||||
{
|
||||
char pid[24];
|
||||
return api_id(cJSON_GetObjectItem(rec, "profile_id"), pid, sizeof(pid)) && !strcmp(pid, profile);
|
||||
}
|
||||
|
||||
static cJSON *bundle(const char *kind)
|
||||
{
|
||||
cJSON *b = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(b, "version", BUNDLE_VERSION);
|
||||
cJSON_AddStringToObject(b, "kind", kind);
|
||||
return b;
|
||||
}
|
||||
|
||||
static cJSON *palette_colors(const cJSON *profile)
|
||||
{
|
||||
char id[24];
|
||||
const cJSON *pal = api_id(cJSON_GetObjectItem(profile, "palette_id"), id, sizeof(id))
|
||||
? cJSON_GetObjectItem(store_coll("palette"), id)
|
||||
: NULL;
|
||||
if (cJSON_IsObject(pal)) {
|
||||
pal = cJSON_GetObjectItem(pal, "colors");
|
||||
}
|
||||
return cJSON_IsArray(pal) ? cJSON_Duplicate(pal, true) : cJSON_CreateArray();
|
||||
}
|
||||
|
||||
static cJSON *export_profile(const char *id)
|
||||
{
|
||||
const cJSON *src = cJSON_GetObjectItem(store_coll("profile"), id);
|
||||
if (!src) {
|
||||
return NULL;
|
||||
}
|
||||
cJSON *b = bundle("profile");
|
||||
cJSON *prof = cJSON_Duplicate(src, true);
|
||||
cJSON_DeleteItemFromObject(prof, "palette");
|
||||
cJSON_AddItemToObject(b, "profile", prof);
|
||||
cJSON *pal = cJSON_AddObjectToObject(b, "palette");
|
||||
cJSON_AddItemToObject(pal, "colors", palette_colors(src));
|
||||
cJSON *zones = cJSON_AddObjectToObject(b, "zones");
|
||||
cJSON *presets = cJSON_AddObjectToObject(b, "presets");
|
||||
cJSON *seqs = cJSON_AddObjectToObject(b, "sequences");
|
||||
cJSON *pset = cJSON_CreateObject(), *sset = cJSON_CreateObject();
|
||||
const cJSON *zl = cJSON_GetObjectItem(src, "zones");
|
||||
if (!cJSON_GetArraySize(zl)) {
|
||||
zl = cJSON_GetObjectItem(src, "zone_order");
|
||||
}
|
||||
const cJSON *zid;
|
||||
cJSON_ArrayForEach(zid, zl)
|
||||
{
|
||||
char key[24];
|
||||
const cJSON *z = api_id(zid, key, sizeof(key)) ? cJSON_GetObjectItem(store_coll("zone"), key) : NULL;
|
||||
if (!z || cJSON_GetObjectItem(zones, key)) {
|
||||
continue;
|
||||
}
|
||||
cJSON_AddItemToObject(zones, key, cJSON_Duplicate(z, true));
|
||||
zone_preset_ids(z, pset);
|
||||
add_ref(sset, cJSON_GetObjectItem(z, "sequence_ids"));
|
||||
}
|
||||
const cJSON *it;
|
||||
cJSON_ArrayForEach(it, sset)
|
||||
{
|
||||
const cJSON *s = cJSON_GetObjectItem(store_coll("sequence"), it->string);
|
||||
if (s && owned_by(s, id)) {
|
||||
cJSON_AddItemToObject(seqs, it->string, cJSON_Duplicate(s, true));
|
||||
sequence_preset_ids(s, pset);
|
||||
}
|
||||
}
|
||||
cJSON_ArrayForEach(it, pset)
|
||||
{
|
||||
const cJSON *p = cJSON_GetObjectItem(store_coll("preset"), it->string);
|
||||
if (p && owned_by(p, id)) {
|
||||
cJSON_AddItemToObject(presets, it->string, cJSON_Duplicate(p, true));
|
||||
}
|
||||
}
|
||||
cJSON_Delete(pset);
|
||||
cJSON_Delete(sset);
|
||||
return b;
|
||||
}
|
||||
|
||||
// Copy of v with every preset id mapped (nested lists allowed); unknown ids are copied from the store
|
||||
// into the new profile, or become null when there is no such preset
|
||||
static cJSON *map_presets(const cJSON *v, cJSON *map, const char *profile)
|
||||
{
|
||||
if (cJSON_IsArray(v)) {
|
||||
cJSON *out = cJSON_CreateArray();
|
||||
const cJSON *it;
|
||||
cJSON_ArrayForEach(it, v)
|
||||
{
|
||||
cJSON_AddItemToArray(out, map_presets(it, map, profile));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
char id[24];
|
||||
if (!api_id(v, id, sizeof(id))) {
|
||||
return cJSON_CreateNull();
|
||||
}
|
||||
const cJSON *to = cJSON_GetObjectItem(map, id);
|
||||
if (to) {
|
||||
return cJSON_Duplicate(to, false);
|
||||
}
|
||||
const cJSON *p = cJSON_GetObjectItem(store_coll("preset"), id);
|
||||
if (!p) {
|
||||
return cJSON_CreateNull();
|
||||
}
|
||||
cJSON *copy = cJSON_Duplicate(p, true);
|
||||
api_set(copy, "profile_id", cJSON_CreateString(profile));
|
||||
char nid[16];
|
||||
api_add("preset", copy, nid, sizeof(nid));
|
||||
cJSON_AddStringToObject(map, id, nid);
|
||||
return cJSON_CreateString(nid);
|
||||
}
|
||||
|
||||
static void map_steps(cJSON *steps, const cJSON *map)
|
||||
{
|
||||
cJSON *step;
|
||||
cJSON_ArrayForEach(step, steps)
|
||||
{
|
||||
char id[24];
|
||||
const cJSON *to = api_id(cJSON_GetObjectItem(step, "preset_id"), id, sizeof(id)) ? cJSON_GetObjectItem(map, id) : NULL;
|
||||
if (to) {
|
||||
api_set(step, "preset_id", cJSON_Duplicate(to, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sequence copy with step preset ids mapped where the map knows them
|
||||
static cJSON *map_sequence(const cJSON *seq, const cJSON *map)
|
||||
{
|
||||
cJSON *out = cJSON_Duplicate(seq, true), *lane;
|
||||
cJSON_ArrayForEach(lane, cJSON_GetObjectItem(out, "lanes"))
|
||||
{
|
||||
map_steps(lane, map);
|
||||
}
|
||||
map_steps(cJSON_GetObjectItem(out, "steps"), map);
|
||||
return out;
|
||||
}
|
||||
|
||||
// Presets from a bundle into profile; returns old id -> new id
|
||||
static cJSON *import_presets(const cJSON *presets, const char *profile)
|
||||
{
|
||||
cJSON *map = cJSON_CreateObject();
|
||||
const cJSON *p;
|
||||
cJSON_ArrayForEach(p, presets)
|
||||
{
|
||||
if (!cJSON_IsObject(p)) {
|
||||
continue;
|
||||
}
|
||||
cJSON *copy = cJSON_Duplicate(p, true);
|
||||
api_set(copy, "profile_id", cJSON_CreateString(profile));
|
||||
char nid[16];
|
||||
api_add("preset", copy, nid, sizeof(nid));
|
||||
cJSON_AddStringToObject(map, p->string, nid);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
static const char *import_profile(const cJSON *b, const char *name, char *pid, size_t len, cJSON **out)
|
||||
{
|
||||
const cJSON *ver = cJSON_GetObjectItem(b, "version"), *kind = cJSON_GetObjectItem(b, "kind");
|
||||
if (!((cJSON_IsNumber(ver) && ver->valuedouble == BUNDLE_VERSION) ||
|
||||
(cJSON_IsString(ver) && !strcmp(ver->valuestring, "1")))) {
|
||||
return "Unsupported bundle version";
|
||||
}
|
||||
if (kind && !cJSON_IsNull(kind) && !(cJSON_IsString(kind) && !strcmp(kind->valuestring, "profile"))) {
|
||||
return "Not a profile bundle";
|
||||
}
|
||||
const cJSON *src = cJSON_GetObjectItem(b, "profile");
|
||||
if (!cJSON_IsObject(src)) {
|
||||
return "Bundle missing profile";
|
||||
}
|
||||
const cJSON *sname = cJSON_GetObjectItem(src, "name");
|
||||
const char *new_name = name && name[0] ? name
|
||||
: cJSON_IsString(sname) && sname->valuestring[0] ? sname->valuestring
|
||||
: "Imported profile";
|
||||
const cJSON *type = cJSON_GetObjectItem(src, "type");
|
||||
|
||||
cJSON *prof = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(prof, "name", new_name);
|
||||
cJSON_AddStringToObject(prof, "type", cJSON_IsString(type) ? type->valuestring : "zones");
|
||||
api_add("profile", prof, pid, len);
|
||||
|
||||
char palid[16];
|
||||
const cJSON *pal = cJSON_GetObjectItem(cJSON_GetObjectItem(b, "palette"), "colors");
|
||||
api_add("palette", cJSON_IsArray(pal) ? cJSON_Duplicate(pal, true) : cJSON_CreateArray(), palid, sizeof(palid));
|
||||
|
||||
cJSON *pmap = import_presets(cJSON_GetObjectItem(b, "presets"), pid);
|
||||
cJSON *smap = cJSON_CreateObject();
|
||||
const cJSON *s;
|
||||
cJSON_ArrayForEach(s, cJSON_GetObjectItem(b, "sequences"))
|
||||
{
|
||||
if (!cJSON_IsObject(s)) {
|
||||
continue;
|
||||
}
|
||||
cJSON *copy = map_sequence(s, pmap);
|
||||
api_set(copy, "profile_id", cJSON_CreateString(pid));
|
||||
char nid[16];
|
||||
api_add("sequence", copy, nid, sizeof(nid));
|
||||
cJSON_AddStringToObject(smap, s->string, nid);
|
||||
}
|
||||
|
||||
const cJSON *zones_in = cJSON_GetObjectItem(b, "zones");
|
||||
const cJSON *order = cJSON_GetObjectItem(src, "zones");
|
||||
cJSON *keys = NULL;
|
||||
if (!cJSON_IsArray(order)) {
|
||||
keys = cJSON_CreateArray();
|
||||
const cJSON *z;
|
||||
cJSON_ArrayForEach(z, zones_in)
|
||||
{
|
||||
cJSON_AddItemToArray(keys, cJSON_CreateString(z->string));
|
||||
}
|
||||
order = keys;
|
||||
}
|
||||
cJSON *zone_ids = cJSON_CreateArray();
|
||||
const cJSON *oz;
|
||||
cJSON_ArrayForEach(oz, order)
|
||||
{
|
||||
char key[24];
|
||||
const cJSON *z = api_id(oz, key, sizeof(key)) ? cJSON_GetObjectItem(zones_in, key) : NULL;
|
||||
if (!cJSON_IsObject(z)) {
|
||||
continue;
|
||||
}
|
||||
cJSON *copy = cJSON_Duplicate(z, true);
|
||||
const cJSON *zn = cJSON_GetObjectItem(z, "name");
|
||||
if (!cJSON_IsString(zn) || !zn->valuestring[0]) {
|
||||
char nm[40];
|
||||
snprintf(nm, sizeof(nm), "Zone %s", key);
|
||||
api_set(copy, "name", cJSON_CreateString(nm));
|
||||
}
|
||||
if (!cJSON_IsArray(cJSON_GetObjectItem(copy, "names"))) {
|
||||
api_set(copy, "names", cJSON_CreateArray());
|
||||
}
|
||||
static const char *const REFS[] = { "presets", "presets_flat" };
|
||||
for (int i = 0; i < 2; i++) {
|
||||
const cJSON *v = cJSON_GetObjectItem(z, REFS[i]);
|
||||
if (v && !cJSON_IsNull(v)) {
|
||||
api_set(copy, REFS[i], map_presets(v, pmap, pid));
|
||||
}
|
||||
}
|
||||
char dp[24];
|
||||
const cJSON *to = api_id(cJSON_GetObjectItem(z, "default_preset"), dp, sizeof(dp)) ? cJSON_GetObjectItem(pmap, dp) : NULL;
|
||||
if (to) {
|
||||
api_set(copy, "default_preset", cJSON_Duplicate(to, false));
|
||||
}
|
||||
const cJSON *sids = cJSON_GetObjectItem(z, "sequence_ids");
|
||||
if (cJSON_IsArray(sids)) {
|
||||
cJSON *mapped = cJSON_CreateArray(), *sid;
|
||||
cJSON_ArrayForEach(sid, sids)
|
||||
{
|
||||
char k[24];
|
||||
if (api_id(sid, k, sizeof(k))) {
|
||||
const cJSON *ns = cJSON_GetObjectItem(smap, k);
|
||||
cJSON_AddItemToArray(mapped, cJSON_CreateString(ns ? ns->valuestring : k));
|
||||
}
|
||||
}
|
||||
api_set(copy, "sequence_ids", mapped);
|
||||
}
|
||||
char nid[16];
|
||||
api_add("zone", copy, nid, sizeof(nid));
|
||||
cJSON_AddItemToArray(zone_ids, cJSON_CreateString(nid));
|
||||
}
|
||||
cJSON_Delete(keys);
|
||||
cJSON_Delete(pmap);
|
||||
cJSON_Delete(smap);
|
||||
cJSON_AddItemToObject(prof, "zones", zone_ids);
|
||||
const cJSON *scenes = cJSON_GetObjectItem(src, "scenes");
|
||||
cJSON_AddItemToObject(prof, "scenes", cJSON_IsArray(scenes) ? cJSON_Duplicate(scenes, true) : cJSON_CreateArray());
|
||||
cJSON_AddStringToObject(prof, "palette_id", palid);
|
||||
store_save("palette");
|
||||
store_save("preset");
|
||||
store_save("zone");
|
||||
store_save("sequence");
|
||||
store_save("profile");
|
||||
engine_groups_changed();
|
||||
*out = prof;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// The bundle from a body: {"bundle": {...}} or the bundle itself
|
||||
static const cJSON *bundle_of(const cJSON *body)
|
||||
{
|
||||
const cJSON *b = cJSON_GetObjectItem(body, "bundle");
|
||||
return cJSON_IsObject(b) ? b : body;
|
||||
}
|
||||
|
||||
static bool truthy(const cJSON *v, bool dflt)
|
||||
{
|
||||
if (!v || cJSON_IsNull(v)) {
|
||||
return dflt;
|
||||
}
|
||||
if (cJSON_IsString(v)) {
|
||||
const char *s = v->valuestring;
|
||||
return !strcasecmp(s, "1") || !strcasecmp(s, "true") || !strcasecmp(s, "yes") || !strcasecmp(s, "on");
|
||||
}
|
||||
return cJSON_IsTrue(v) || (cJSON_IsNumber(v) && v->valuedouble != 0);
|
||||
}
|
||||
|
||||
static esp_err_t profiles_import(httpd_req_t *req)
|
||||
{
|
||||
cJSON *body = api_body(req);
|
||||
if (!body) {
|
||||
return ESP_OK;
|
||||
}
|
||||
const cJSON *nm = cJSON_GetObjectItem(body, "name");
|
||||
char name[64] = "";
|
||||
if (cJSON_IsString(nm)) {
|
||||
snprintf(name, sizeof(name), "%s", nm->valuestring);
|
||||
}
|
||||
bool apply = truthy(cJSON_GetObjectItem(body, "apply"), true);
|
||||
char pid[16];
|
||||
cJSON *prof = NULL;
|
||||
const char *err = import_profile(bundle_of(body), name, pid, sizeof(pid), &prof);
|
||||
cJSON_Delete(body);
|
||||
if (err) {
|
||||
return api_error(req, "400 Bad Request", err);
|
||||
}
|
||||
if (apply) {
|
||||
api_set(store_settings(), "current_profile_id", cJSON_CreateString(pid));
|
||||
store_save("settings");
|
||||
}
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddItemReferenceToObject(o, pid, prof);
|
||||
cJSON_AddStringToObject(o, "id", pid);
|
||||
return api_json(req, "201 Created", o, true);
|
||||
}
|
||||
|
||||
static esp_err_t presets_import(httpd_req_t *req, const char *profile)
|
||||
{
|
||||
cJSON *body = api_body(req);
|
||||
if (!body) {
|
||||
return ESP_OK;
|
||||
}
|
||||
const cJSON *b = bundle_of(body), *kind = cJSON_GetObjectItem(b, "kind"), *preset = cJSON_GetObjectItem(b, "preset");
|
||||
const char *err = !(cJSON_IsString(kind) && !strcmp(kind->valuestring, "preset")) ? "Not a preset bundle"
|
||||
: !cJSON_IsObject(preset) ? "Bundle missing preset"
|
||||
: NULL;
|
||||
if (err) {
|
||||
cJSON_Delete(body);
|
||||
return api_error(req, "400 Bad Request", err);
|
||||
}
|
||||
cJSON *rec = api_preset_defaults(profile);
|
||||
api_merge(rec, preset);
|
||||
cJSON_Delete(body);
|
||||
api_set(rec, "profile_id", cJSON_CreateString(profile));
|
||||
char id[16];
|
||||
api_add("preset", rec, id, sizeof(id));
|
||||
store_save("preset");
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddItemReferenceToObject(o, id, rec);
|
||||
return api_json(req, "201 Created", o, true);
|
||||
}
|
||||
|
||||
static esp_err_t sequences_import(httpd_req_t *req, const char *profile)
|
||||
{
|
||||
cJSON *body = api_body(req);
|
||||
if (!body) {
|
||||
return ESP_OK;
|
||||
}
|
||||
const cJSON *b = bundle_of(body), *kind = cJSON_GetObjectItem(b, "kind"), *seq = cJSON_GetObjectItem(b, "sequence");
|
||||
const char *err = !(cJSON_IsString(kind) && !strcmp(kind->valuestring, "sequence")) ? "Not a sequence bundle"
|
||||
: !cJSON_IsObject(seq) ? "Bundle missing sequence"
|
||||
: NULL;
|
||||
if (err) {
|
||||
cJSON_Delete(body);
|
||||
return api_error(req, "400 Bad Request", err);
|
||||
}
|
||||
cJSON *map = import_presets(cJSON_GetObjectItem(b, "presets"), profile);
|
||||
// Steps naming presets that were not in the bundle: copy those from this controller
|
||||
cJSON *used = cJSON_CreateObject(), *it;
|
||||
sequence_preset_ids(seq, used);
|
||||
cJSON_ArrayForEach(it, used)
|
||||
{
|
||||
if (!cJSON_GetObjectItem(map, it->string)) {
|
||||
cJSON *ref = cJSON_CreateString(it->string);
|
||||
cJSON_Delete(map_presets(ref, map, profile)); // copies the preset and records the mapping
|
||||
cJSON_Delete(ref);
|
||||
}
|
||||
}
|
||||
cJSON_Delete(used);
|
||||
cJSON *rec = api_sequence_defaults();
|
||||
cJSON *mapped = map_sequence(seq, map);
|
||||
api_merge(rec, mapped);
|
||||
cJSON_Delete(mapped);
|
||||
cJSON_Delete(map);
|
||||
cJSON_Delete(body);
|
||||
api_set(rec, "profile_id", cJSON_CreateString(profile));
|
||||
char id[16];
|
||||
api_add("sequence", rec, id, sizeof(id));
|
||||
store_save("preset");
|
||||
store_save("sequence");
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddItemReferenceToObject(o, id, rec);
|
||||
return api_json(req, "201 Created", o, true);
|
||||
}
|
||||
|
||||
bool bundles_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
|
||||
{
|
||||
const bool profiles = !strcmp(seg[0], "profiles"), presets = !strcmp(seg[0], "presets"),
|
||||
sequences = !strcmp(seg[0], "sequences");
|
||||
if (!profiles && !presets && !sequences) {
|
||||
return false;
|
||||
}
|
||||
const bool is_import = n == 2 && req->method == HTTP_POST && !strcmp(seg[1], "import");
|
||||
const bool is_export = n == 3 && req->method == HTTP_GET && !strcmp(seg[2], "export");
|
||||
if (!is_import && !is_export) {
|
||||
return false;
|
||||
}
|
||||
const char *profile = api_current_profile();
|
||||
if (is_import) {
|
||||
*ret = profiles ? profiles_import(req)
|
||||
: !profile ? api_error(req, "404 Not Found", "No profile available")
|
||||
: presets ? presets_import(req, profile)
|
||||
: sequences_import(req, profile);
|
||||
return true;
|
||||
}
|
||||
cJSON *b = NULL;
|
||||
if (profiles) {
|
||||
b = export_profile(seg[1]);
|
||||
} else {
|
||||
const cJSON *rec = cJSON_GetObjectItem(store_coll(presets ? "preset" : "sequence"), seg[1]);
|
||||
if (rec && profile && owned_by(rec, profile)) {
|
||||
b = bundle(presets ? "preset" : "sequence");
|
||||
cJSON_AddItemToObject(b, presets ? "preset" : "sequence", cJSON_Duplicate(rec, true));
|
||||
if (sequences) {
|
||||
cJSON *ps = cJSON_AddObjectToObject(b, "presets"), *used = cJSON_CreateObject(), *it;
|
||||
sequence_preset_ids(rec, used);
|
||||
cJSON_ArrayForEach(it, used)
|
||||
{
|
||||
const cJSON *p = cJSON_GetObjectItem(store_coll("preset"), it->string);
|
||||
if (p && owned_by(p, profile)) {
|
||||
cJSON_AddItemToObject(ps, it->string, cJSON_Duplicate(p, true));
|
||||
}
|
||||
}
|
||||
cJSON_Delete(used);
|
||||
}
|
||||
}
|
||||
}
|
||||
*ret = b ? api_json(req, "200 OK", b, true)
|
||||
: api_error(req, "404 Not Found", profiles ? "Profile not found" : presets ? "Preset not found" : "Sequence not found");
|
||||
return true;
|
||||
}
|
||||
@@ -296,6 +296,32 @@ static esp_err_t identify(httpd_req_t *req, const char *id, const cJSON *g)
|
||||
return api_json(req, "200 OK", o, true);
|
||||
}
|
||||
|
||||
// main's Group.load: every group gets the newer fields
|
||||
void api_migrate_groups(void)
|
||||
{
|
||||
bool changed = false;
|
||||
cJSON *defs = cJSON_Parse(DEFAULTS), *g;
|
||||
static const char *const KEYS[] = { "devices", "wifi_driver_display_name", "wifi_driver_num_leds", "wifi_color_order",
|
||||
"wifi_startup_mode", "output_brightness", "bridge_id" };
|
||||
cJSON_ArrayForEach(g, store_coll("group"))
|
||||
{
|
||||
if (!cJSON_IsObject(g)) {
|
||||
continue;
|
||||
}
|
||||
for (size_t i = 0; i < sizeof(KEYS) / sizeof(KEYS[0]); i++) {
|
||||
if (!cJSON_GetObjectItem(g, KEYS[i])) {
|
||||
cJSON_AddItemToObject(g, KEYS[i], cJSON_Duplicate(cJSON_GetObjectItem(defs, KEYS[i]), true));
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
normalize_devices(g);
|
||||
}
|
||||
cJSON_Delete(defs);
|
||||
if (changed) {
|
||||
store_save("group");
|
||||
}
|
||||
}
|
||||
|
||||
bool groups_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
|
||||
{
|
||||
cJSON *all = store_coll("group");
|
||||
|
||||
@@ -93,6 +93,11 @@ static const char SEQ_DEFAULTS[] = "{\"name\":\"\",\"group_ids\":[],\"lanes\":[[
|
||||
"\"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;
|
||||
|
||||
+92
-15
@@ -12,7 +12,7 @@
|
||||
/* ---------------------------------------------------------------- presets */
|
||||
|
||||
// A new preset before the request's fields are applied
|
||||
static cJSON *preset_defaults(const char *profile_id)
|
||||
cJSON *api_preset_defaults(const char *profile_id)
|
||||
{
|
||||
cJSON *p = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(p, "name", "");
|
||||
@@ -120,7 +120,7 @@ bool presets_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
|
||||
*ret = ESP_OK;
|
||||
return true;
|
||||
}
|
||||
cJSON *rec = preset_defaults(profile);
|
||||
cJSON *rec = api_preset_defaults(profile);
|
||||
api_merge(rec, body);
|
||||
cJSON_Delete(body);
|
||||
api_set(rec, "profile_id", cJSON_CreateString(profile));
|
||||
@@ -164,7 +164,7 @@ typedef struct {
|
||||
static const seed_t SEED[] = {
|
||||
{ "on", "on", "[\"#FFFFFF\"]", 255, 100, { 0 } },
|
||||
{ "off", "off", "[]", 0, 100, { 0 } },
|
||||
{ "rainbow", "rainbow", "[]", 255, 100, { 2 } },
|
||||
{ "rainbow", "colour_cycle", "[]", 255, 100, { 2 } },
|
||||
{ "Colour Cycle", "colour_cycle", "[\"#FF0000\",\"#00FF00\",\"#0000FF\"]", 255, 100, { 1 } },
|
||||
{ "transition", "transition", "[\"#FF0000\",\"#00FF00\",\"#0000FF\"]", 255, 500, { 0 } },
|
||||
{ "flicker", "flicker", "[\"#FFB84D\"]", 255, 80, { 30 } },
|
||||
@@ -172,7 +172,7 @@ static const seed_t SEED[] = {
|
||||
{ "twinkle", "twinkle", "[\"#78C8FF\",\"#508CFF\",\"#B478FF\",\"#64DCE8\",\"#A0C8FF\"]", 255, 55, { 72, 140, 2, 6 } },
|
||||
};
|
||||
static const seed_t SEED_DJ[] = {
|
||||
{ "DJ Rainbow", "rainbow", "[]", 220, 60, { 12 } },
|
||||
{ "DJ Rainbow", "colour_cycle", "[]", 220, 60, { 12 } },
|
||||
{ "DJ Single Color", "on", "[\"#ff00ff\"]", 220, 100, { 0 } },
|
||||
{ "DJ Transition", "transition", "[\"#ff0000\",\"#00ff00\",\"#0000ff\"]", 220, 250, { 0 } },
|
||||
};
|
||||
@@ -183,7 +183,7 @@ static void seed_zone(const char *profile, const char *zone_name, const char *dr
|
||||
{
|
||||
cJSON *flat = cJSON_CreateArray();
|
||||
for (int i = 0; i < count; i++) {
|
||||
cJSON *p = preset_defaults(profile);
|
||||
cJSON *p = api_preset_defaults(profile);
|
||||
api_set(p, "name", cJSON_CreateString(seed[i].name));
|
||||
api_set(p, "pattern", cJSON_CreateString(seed[i].pattern));
|
||||
api_set(p, "colors", cJSON_Parse(seed[i].colors));
|
||||
@@ -395,8 +395,8 @@ static bool used_elsewhere(const char *skip, const char *key, const char *id)
|
||||
return false;
|
||||
}
|
||||
|
||||
// DELETE /profiles/<id>: the profile with its presets, and its zones and palette unless another profile
|
||||
// uses them. Ids are reused (next id = highest + 1), so leftovers would turn up in the next new profile.
|
||||
// DELETE /profiles/<id>: the profile with its presets and sequences, and its zones and palette unless
|
||||
// another profile uses them. Ids are reused (next id = highest + 1), so leftovers would turn up in the next new profile.
|
||||
static esp_err_t delete_profile(httpd_req_t *req, const char *id)
|
||||
{
|
||||
char pid[24];
|
||||
@@ -405,14 +405,20 @@ static esp_err_t delete_profile(httpd_req_t *req, const char *id)
|
||||
if (!prof) {
|
||||
return api_error(req, "404 Not Found", "Profile not found");
|
||||
}
|
||||
cJSON *presets = store_coll("preset"), *it = presets->child;
|
||||
while (it) {
|
||||
cJSON *next = it->next;
|
||||
char owner[24];
|
||||
if (api_id(cJSON_GetObjectItem(it, "profile_id"), owner, sizeof(owner)) && !strcmp(owner, pid)) {
|
||||
cJSON_Delete(cJSON_DetachItemViaPointer(presets, it));
|
||||
static const char *const OWNED[] = { "preset", "sequence" };
|
||||
for (size_t i = 0; i < sizeof(OWNED) / sizeof(OWNED[0]); i++) {
|
||||
cJSON *coll = store_coll(OWNED[i]), *it = coll->child;
|
||||
while (it) {
|
||||
cJSON *next = it->next;
|
||||
char owner[24];
|
||||
if (api_id(cJSON_GetObjectItem(it, "profile_id"), owner, sizeof(owner)) && !strcmp(owner, pid)) {
|
||||
if (i == 1) {
|
||||
seqplay_stop_if(it->string);
|
||||
}
|
||||
cJSON_Delete(cJSON_DetachItemViaPointer(coll, it));
|
||||
}
|
||||
it = next;
|
||||
}
|
||||
it = next;
|
||||
}
|
||||
const cJSON *z;
|
||||
cJSON_ArrayForEach(z, profile_zones(prof))
|
||||
@@ -433,6 +439,7 @@ static esp_err_t delete_profile(httpd_req_t *req, const char *id)
|
||||
store_save("settings");
|
||||
}
|
||||
store_save("preset");
|
||||
store_save("sequence");
|
||||
store_save("zone");
|
||||
store_save("palette");
|
||||
store_save("profile");
|
||||
@@ -553,7 +560,7 @@ static esp_err_t create_zone(httpd_req_t *req)
|
||||
const cJSON *name = cJSON_GetObjectItem(body, "name");
|
||||
if (!cJSON_IsString(name) || !name->valuestring[0]) {
|
||||
cJSON_Delete(body);
|
||||
return api_error(req, "400 Bad Request", "name is required");
|
||||
return api_error(req, "400 Bad Request", "Zone name cannot be empty");
|
||||
}
|
||||
cJSON *names = cJSON_GetObjectItem(body, "names");
|
||||
if (!names) {
|
||||
@@ -565,9 +572,26 @@ static esp_err_t create_zone(httpd_req_t *req)
|
||||
const cJSON *presets = cJSON_GetObjectItem(body, "presets");
|
||||
cJSON_AddItemToObject(z, "presets", cJSON_IsArray(presets) ? cJSON_Duplicate(presets, true) : cJSON_CreateArray());
|
||||
cJSON_AddNullToObject(z, "default_preset");
|
||||
cJSON_AddArrayToObject(z, "group_ids");
|
||||
cJSON_AddObjectToObject(z, "preset_group_ids");
|
||||
cJSON_AddArrayToObject(z, "sequence_ids");
|
||||
cJSON_AddNumberToObject(z, "brightness", 255);
|
||||
cJSON_DeleteItemFromObject(body, "ids");
|
||||
const cJSON *kind = cJSON_GetObjectItem(body, "content_kind");
|
||||
if (kind && !(cJSON_IsString(kind) && (!strcmp(kind->valuestring, "presets") || !strcmp(kind->valuestring, "sequences")))) {
|
||||
cJSON_DeleteItemFromObject(body, "content_kind");
|
||||
}
|
||||
api_merge(z, body);
|
||||
cJSON_Delete(body);
|
||||
cJSON *gids = cJSON_CreateArray(), *g;
|
||||
cJSON_ArrayForEach(g, cJSON_GetObjectItem(z, "group_ids"))
|
||||
{
|
||||
char id[24];
|
||||
if (api_id(g, id, sizeof(id))) {
|
||||
cJSON_AddItemToArray(gids, cJSON_CreateString(id));
|
||||
}
|
||||
}
|
||||
api_set(z, "group_ids", gids);
|
||||
char id[16];
|
||||
api_add("zone", z, id, sizeof(id));
|
||||
cJSON *list = profile_zone_list();
|
||||
@@ -786,3 +810,56 @@ bool zones_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------- migration */
|
||||
|
||||
// Preset ids a zone lists (presets_flat, else presets as a flat list or rows)
|
||||
static bool zone_has_presets(const cJSON *z)
|
||||
{
|
||||
const cJSON *flat = cJSON_GetObjectItem(z, "presets_flat");
|
||||
if (cJSON_IsArray(flat)) {
|
||||
return cJSON_GetArraySize(flat) > 0;
|
||||
}
|
||||
const cJSON *row;
|
||||
cJSON_ArrayForEach(row, cJSON_GetObjectItem(z, "presets"))
|
||||
{
|
||||
if (cJSON_IsString(row) || cJSON_GetArraySize(row) > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// main's Zone.load: group_ids, preset_group_ids, sequence_ids and content_kind on every zone
|
||||
void api_migrate_zones(void)
|
||||
{
|
||||
bool changed = false;
|
||||
cJSON *z;
|
||||
cJSON_ArrayForEach(z, store_coll("zone"))
|
||||
{
|
||||
if (!cJSON_IsObject(z)) {
|
||||
continue;
|
||||
}
|
||||
if (!cJSON_IsArray(cJSON_GetObjectItem(z, "group_ids"))) {
|
||||
api_set(z, "group_ids", cJSON_CreateArray());
|
||||
changed = true;
|
||||
}
|
||||
if (!cJSON_IsObject(cJSON_GetObjectItem(z, "preset_group_ids"))) {
|
||||
api_set(z, "preset_group_ids", cJSON_CreateObject());
|
||||
changed = true;
|
||||
}
|
||||
if (!cJSON_IsArray(cJSON_GetObjectItem(z, "sequence_ids"))) {
|
||||
api_set(z, "sequence_ids", cJSON_CreateArray());
|
||||
changed = true;
|
||||
}
|
||||
const cJSON *kind = cJSON_GetObjectItem(z, "content_kind");
|
||||
if (!(cJSON_IsString(kind) && (!strcmp(kind->valuestring, "presets") || !strcmp(kind->valuestring, "sequences")))) {
|
||||
bool seqs = cJSON_GetArraySize(cJSON_GetObjectItem(z, "sequence_ids")) > 0;
|
||||
api_set(z, "content_kind", cJSON_CreateString(seqs && !zone_has_presets(z) ? "sequences" : "presets"));
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (changed) {
|
||||
store_save("zone");
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@
|
||||
#include "api_util.h"
|
||||
#include "beat.h"
|
||||
#include "engine.h"
|
||||
#include "espnow_tx.h"
|
||||
#include "bench.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_timer.h"
|
||||
@@ -466,6 +468,10 @@ static void beat_task(void *arg)
|
||||
if (!audio_drives()) {
|
||||
bool down = s_sim_beats % BEATS_PER_BAR == 0;
|
||||
s_sim_beats++;
|
||||
// Drivers get the clock's beats too, so manual (beat-stepped) presets keep moving
|
||||
if (!bench_running()) {
|
||||
espnow_tx_beat(60e6f / interval, down, (int)((s_sim_beats - 1) % BEATS_PER_BAR) + 1);
|
||||
}
|
||||
on_beat(down);
|
||||
}
|
||||
continue;
|
||||
@@ -492,6 +498,7 @@ esp_err_t seqplay_init(void)
|
||||
const cJSON *a = cJSON_GetObjectItem(store_settings(), "audio_timing");
|
||||
s_audio = !cJSON_IsFalse(a);
|
||||
store_unlock();
|
||||
espnow_tx_set_mic_beats(s_audio);
|
||||
return xTaskCreate(beat_task, "seqplay", 6144, NULL, 4, NULL) == pdPASS ? ESP_OK : ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
@@ -673,6 +680,7 @@ void seqplay_set_audio(bool on)
|
||||
{
|
||||
store_lock();
|
||||
s_audio = on;
|
||||
espnow_tx_set_mic_beats(on);
|
||||
api_set(store_settings(), "audio_timing", cJSON_CreateBool(on));
|
||||
store_save("settings");
|
||||
store_unlock();
|
||||
@@ -682,3 +690,17 @@ bool seqplay_audio(void)
|
||||
{
|
||||
return s_audio;
|
||||
}
|
||||
|
||||
const char *seqplay_switch_wait(void)
|
||||
{
|
||||
store_lock();
|
||||
bool down = wait_for_downbeat();
|
||||
store_unlock();
|
||||
return down ? "downbeat" : "beat";
|
||||
}
|
||||
|
||||
void seqplay_clock(int *bar_beat, uint32_t *tick)
|
||||
{
|
||||
*tick = s_sim_beats;
|
||||
*bar_beat = s_sim_beats ? (int)((s_sim_beats - 1) % BEATS_PER_BAR) + 1 : 1;
|
||||
}
|
||||
@@ -37,3 +37,8 @@ const char *seqplay_done_readout(void);
|
||||
// Use the mic's beats for timing (main's audio start/stop); off falls back to the clock
|
||||
void seqplay_set_audio(bool on);
|
||||
bool seqplay_audio(void);
|
||||
|
||||
// The start-switch wait in effect: "downbeat" (saved, and the mic is timing) or "beat"
|
||||
const char *seqplay_switch_wait(void);
|
||||
// The clock's bar position (1..4) and how many beats it has ticked
|
||||
void seqplay_clock(int *bar_beat, uint32_t *tick);
|
||||
@@ -45,17 +45,20 @@ static void forward(httpd_req_t *req, const char *text)
|
||||
}
|
||||
}
|
||||
|
||||
// Right after the upgrade: the v1 greeting. The controller keeps no Wi-Fi driver connections, so the
|
||||
// snapshot is always empty.
|
||||
static esp_err_t on_open(httpd_req_t *req)
|
||||
{
|
||||
send_text(req, "{\"type\":\"device_tcp_snapshot\",\"connected_ips\":[]}");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t ws_handler(httpd_req_t *req)
|
||||
{
|
||||
if (req->method == HTTP_GET) {
|
||||
if (httpd_ws_get_fd_info(req->handle, httpd_req_to_sockfd(req)) != HTTPD_WS_CLIENT_WEBSOCKET) {
|
||||
httpd_resp_set_status(req, "426 Upgrade Required");
|
||||
httpd_resp_set_hdr(req, "Upgrade", "websocket");
|
||||
return httpd_resp_sendstr(req, "websocket only");
|
||||
}
|
||||
// The controller keeps no Wi-Fi driver connections, so the v1 snapshot is always empty
|
||||
send_text(req, "{\"type\":\"device_tcp_snapshot\",\"connected_ips\":[]}");
|
||||
return ESP_OK;
|
||||
if (req->method == HTTP_GET) { // only a plain GET reaches the handler; the upgrade is answered by httpd
|
||||
httpd_resp_set_status(req, "426 Upgrade Required");
|
||||
httpd_resp_set_hdr(req, "Upgrade", "websocket");
|
||||
return httpd_resp_sendstr(req, "websocket only");
|
||||
}
|
||||
httpd_ws_frame_t f = { 0 };
|
||||
esp_err_t err = httpd_ws_recv_frame(req, &f, 0);
|
||||
@@ -85,6 +88,8 @@ static esp_err_t ws_handler(httpd_req_t *req)
|
||||
|
||||
esp_err_t ws_register(httpd_handle_t server)
|
||||
{
|
||||
const httpd_uri_t u = { .uri = "/ws", .method = HTTP_GET, .handler = ws_handler, .is_websocket = true };
|
||||
const httpd_uri_t u = {
|
||||
.uri = "/ws", .method = HTTP_GET, .handler = ws_handler, .is_websocket = true, .ws_post_handshake_cb = on_open
|
||||
};
|
||||
return httpd_register_uri_handler(server, &u);
|
||||
}
|
||||
Reference in new issue
Block a user