feat: sequences v2, bridges, and sim-controller
Rework sequences (engine, seqplay, envelope, API, web UI), add bridge support, add the host sim-controller and rig simulation tests, and update docs, presets and project tooling config. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d664026ee5
commit
2543673bd8
68 files changed
+5761
-460
No files matched your search
@@ -4,6 +4,7 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include "api_util.h"
|
||||
#include "engine.h"
|
||||
@@ -626,6 +627,133 @@ static esp_err_t device_status(httpd_req_t *req)
|
||||
return api_json(req, "200 OK", o, true);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------- rig layout */
|
||||
|
||||
static bool placement_of(const cJSON *pl, double v[4])
|
||||
{
|
||||
static const char *const K[4] = { "x0", "y0", "x1", "y1" };
|
||||
for (int i = 0; i < 4; i++) {
|
||||
const cJSON *x = cJSON_GetObjectItem(pl, K[i]);
|
||||
if (!cJSON_IsNumber(x)) {
|
||||
return false;
|
||||
}
|
||||
v[i] = x->valuedouble;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Send every placed ESP-NOW driver its strip ends in rig coordinates: centred on settings.rig_center
|
||||
// (else the layout's bounding box) and scaled so the furthest end is 1 from the centre. *sent / *failed
|
||||
// count the drivers.
|
||||
static void push_layout(int *sent, int *failed)
|
||||
{
|
||||
double lo[2] = { 1e9, 1e9 }, hi[2] = { -1e9, -1e9 }, v[4];
|
||||
const cJSON *d;
|
||||
cJSON_ArrayForEach(d, store_coll("device"))
|
||||
{
|
||||
if (placement_of(cJSON_GetObjectItem(d, "placement"), v)) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
lo[i & 1] = v[i] < lo[i & 1] ? v[i] : lo[i & 1];
|
||||
hi[i & 1] = v[i] > hi[i & 1] ? v[i] : hi[i & 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
double cx = (lo[0] + hi[0]) / 2, cy = (lo[1] + hi[1]) / 2, far = 0;
|
||||
const cJSON *rc = cJSON_GetObjectItem(store_settings(), "rig_center");
|
||||
if (cJSON_IsNumber(cJSON_GetObjectItem(rc, "x")) && cJSON_IsNumber(cJSON_GetObjectItem(rc, "y"))) {
|
||||
cx = cJSON_GetObjectItem(rc, "x")->valuedouble;
|
||||
cy = cJSON_GetObjectItem(rc, "y")->valuedouble;
|
||||
}
|
||||
cJSON_ArrayForEach(d, store_coll("device"))
|
||||
{
|
||||
if (placement_of(cJSON_GetObjectItem(d, "placement"), v)) {
|
||||
for (int i = 0; i < 4; i += 2) {
|
||||
double dx = v[i] - cx, dy = v[i + 1] - cy, r2 = dx * dx + dy * dy;
|
||||
far = r2 > far ? r2 : far;
|
||||
}
|
||||
}
|
||||
}
|
||||
double scale = far > 0 ? 1.0 / sqrt(far) : 1.0;
|
||||
*sent = *failed = 0;
|
||||
cJSON_ArrayForEach(d, store_coll("device"))
|
||||
{
|
||||
if (!is_espnow(d) || !placement_of(cJSON_GetObjectItem(d, "placement"), v)) {
|
||||
continue;
|
||||
}
|
||||
cJSON *dc = cJSON_CreateObject(), *place = cJSON_AddArrayToObject(dc, "place");
|
||||
for (int i = 0; i < 4; i++) {
|
||||
double c = (v[i] - (i & 1 ? cy : cx)) * scale;
|
||||
cJSON_AddItemToArray(place, cJSON_CreateNumber(round(c * 1000) / 1000));
|
||||
}
|
||||
if (push_device_config(d, dc) == ESP_OK) {
|
||||
(*sent)++;
|
||||
} else {
|
||||
(*failed)++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// POST /devices/layout {"placements": {mac: {x0, y0, x1, y1} | null}, "center"?: {x, y}}: where each bar
|
||||
// sits (x0,y0 is the data-in end, LED 1), in any units, and the rig's centre (ripples start there, spins
|
||||
// turn round it). Saved on the devices and in settings.rig_center, then sent to the drivers normalised.
|
||||
static esp_err_t set_layout(httpd_req_t *req)
|
||||
{
|
||||
cJSON *body = api_body(req);
|
||||
if (!body) {
|
||||
return ESP_OK;
|
||||
}
|
||||
const cJSON *pls = cJSON_GetObjectItem(body, "placements"), *it;
|
||||
if (!cJSON_IsObject(pls)) {
|
||||
cJSON_Delete(body);
|
||||
return api_error(req, "400 Bad Request", "placements required");
|
||||
}
|
||||
int changed = 0;
|
||||
cJSON_ArrayForEach(it, pls)
|
||||
{
|
||||
char mac[13];
|
||||
cJSON *key = cJSON_CreateString(it->string);
|
||||
bool ok = api_mac(key, mac);
|
||||
cJSON_Delete(key);
|
||||
cJSON *dev = ok ? cJSON_GetObjectItem(store_coll("device"), mac) : NULL;
|
||||
double v[4];
|
||||
if (!dev) {
|
||||
continue;
|
||||
}
|
||||
if (cJSON_IsNull(it)) {
|
||||
cJSON_DeleteItemFromObject(dev, "placement");
|
||||
} else if (placement_of(it, v)) {
|
||||
cJSON *pl = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(pl, "x0", v[0]);
|
||||
cJSON_AddNumberToObject(pl, "y0", v[1]);
|
||||
cJSON_AddNumberToObject(pl, "x1", v[2]);
|
||||
cJSON_AddNumberToObject(pl, "y1", v[3]);
|
||||
api_set(dev, "placement", pl);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
changed++;
|
||||
}
|
||||
const cJSON *center = cJSON_GetObjectItem(body, "center");
|
||||
if (cJSON_IsNumber(cJSON_GetObjectItem(center, "x")) && cJSON_IsNumber(cJSON_GetObjectItem(center, "y"))) {
|
||||
cJSON *rc = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(rc, "x", cJSON_GetObjectItem(center, "x")->valuedouble);
|
||||
cJSON_AddNumberToObject(rc, "y", cJSON_GetObjectItem(center, "y")->valuedouble);
|
||||
api_set(store_settings(), "rig_center", rc);
|
||||
store_save("settings");
|
||||
}
|
||||
cJSON_Delete(body);
|
||||
if (changed) {
|
||||
store_save("device");
|
||||
}
|
||||
int sent, failed;
|
||||
push_layout(&sent, &failed);
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(o, "updated", changed);
|
||||
cJSON_AddNumberToObject(o, "sent", sent);
|
||||
cJSON_AddNumberToObject(o, "failed", failed);
|
||||
return api_json(req, "200 OK", o, true);
|
||||
}
|
||||
|
||||
bool devices_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
|
||||
{
|
||||
const int method = req->method;
|
||||
@@ -641,6 +769,10 @@ bool devices_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
|
||||
*ret = push_groups(req);
|
||||
return true;
|
||||
}
|
||||
if (n == 2 && method == HTTP_POST && !strcmp(seg[1], "layout")) {
|
||||
*ret = set_layout(req);
|
||||
return true;
|
||||
}
|
||||
if (n == 2 && method == HTTP_POST && !strcmp(seg[1], "resolve-brightness")) {
|
||||
*ret = resolve_brightness(req);
|
||||
return true;
|
||||
|
||||
Reference in new issue
Block a user