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:
1 parent
19fea0b0a1
commit
04b56c6ef2
12 files changed
+1922
-579
No files matched your search
+198
-50
@@ -17,26 +17,6 @@
|
||||
|
||||
#define IDENTIFY_OFF_MS 2000
|
||||
|
||||
// "aa:bb:cc:dd:ee:ff", "AA-BB-..." or "aabbccddeeff" -> "aabbccddeeff"; false when not a MAC
|
||||
static bool normalize_mac(const cJSON *v, char out[13])
|
||||
{
|
||||
if (!cJSON_IsString(v)) {
|
||||
return false;
|
||||
}
|
||||
int n = 0;
|
||||
for (const char *s = v->valuestring; *s; s++) {
|
||||
if (*s == ':' || *s == '-') {
|
||||
continue;
|
||||
}
|
||||
if (!isxdigit((unsigned char)*s) || n == 12) {
|
||||
return false;
|
||||
}
|
||||
out[n++] = tolower((unsigned char)*s);
|
||||
}
|
||||
out[n] = 0;
|
||||
return n == 12;
|
||||
}
|
||||
|
||||
// Trimmed copy of a string field; false when missing or blank
|
||||
static bool trimmed(const cJSON *v, char *buf, size_t len)
|
||||
{
|
||||
@@ -98,6 +78,11 @@ static const char *normalize_device(cJSON *dev, const char *mac)
|
||||
}
|
||||
}
|
||||
api_set(dev, "zones", out);
|
||||
const cJSON *ob = cJSON_GetObjectItem(dev, "output_brightness");
|
||||
if (ob && !cJSON_IsNull(ob) &&
|
||||
!(cJSON_IsNumber(ob) && ob->valuedouble == (int)ob->valuedouble && ob->valuedouble >= 0 && ob->valuedouble <= 255)) {
|
||||
return "output_brightness must be between 0 and 255";
|
||||
}
|
||||
if (!cJSON_GetObjectItem(dev, "default_pattern")) {
|
||||
cJSON_AddNullToObject(dev, "default_pattern");
|
||||
}
|
||||
@@ -118,7 +103,7 @@ static cJSON *with_connected(const cJSON *dev)
|
||||
static cJSON *find(const char *id, char mac[13])
|
||||
{
|
||||
cJSON *v = cJSON_CreateString(id);
|
||||
bool ok = normalize_mac(v, mac);
|
||||
bool ok = api_mac(v, mac);
|
||||
cJSON_Delete(v);
|
||||
if (!ok) {
|
||||
snprintf(mac, 13, "%.12s", id);
|
||||
@@ -138,8 +123,8 @@ static esp_err_t create_device(httpd_req_t *req)
|
||||
bool wifi = cJSON_IsString(tr) && !strcmp(tr->valuestring, "wifi");
|
||||
if (!trimmed(cJSON_GetObjectItem(body, "name"), name, sizeof(name))) {
|
||||
err = "name is required";
|
||||
} else if (!normalize_mac(cJSON_GetObjectItem(body, "mac"), mac) &&
|
||||
(wifi || !normalize_mac(cJSON_GetObjectItem(body, "address"), mac))) {
|
||||
} else if (!api_mac(cJSON_GetObjectItem(body, "mac"), mac) &&
|
||||
(wifi || !api_mac(cJSON_GetObjectItem(body, "address"), mac))) {
|
||||
err = "mac is required (12 hex digits); for Wi-Fi include mac plus IP in address";
|
||||
}
|
||||
cJSON_DeleteItemFromObject(body, "id");
|
||||
@@ -220,7 +205,7 @@ static esp_err_t update_device(httpd_req_t *req, cJSON *dev, const char *mac)
|
||||
}
|
||||
char other[13];
|
||||
const cJSON *bm = cJSON_GetObjectItem(body, "mac");
|
||||
if (bm && !cJSON_IsNull(bm) && !(normalize_mac(bm, other) && !strcmp(other, mac))) {
|
||||
if (bm && !cJSON_IsNull(bm) && !(api_mac(bm, other) && !strcmp(other, mac))) {
|
||||
cJSON_Delete(body);
|
||||
return api_error(req, "400 Bad Request", "cannot change device mac; delete and re-add");
|
||||
}
|
||||
@@ -245,51 +230,206 @@ static esp_err_t update_device(httpd_req_t *req, cJSON *dev, const char *mac)
|
||||
return api_json(req, "200 OK", merged, false);
|
||||
}
|
||||
|
||||
// Identify: a red 10 Hz blink, then "off" two seconds later. A device is addressed by its MAC; a
|
||||
// group by one broadcast its members filter on (they blink together).
|
||||
static const char IDENTIFY_PRESET[] = "{\"__identify\":{\"p\":\"blink\",\"c\":[\"#ff0000\"],\"d\":50,\"b\":128,"
|
||||
"\"a\":true,\"n1\":0,\"n2\":0,\"n3\":0,\"n4\":0,\"n5\":0,\"n6\":0}}";
|
||||
|
||||
typedef struct {
|
||||
char mac[13];
|
||||
char name[64];
|
||||
char mac[13]; // "" when addressing a group
|
||||
char group[24];
|
||||
} identify_t;
|
||||
|
||||
static esp_err_t identify_send(const identify_t *id, bool on)
|
||||
{
|
||||
cJSON *msg = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(msg, "v", "1");
|
||||
if (on) {
|
||||
cJSON_AddItemToObject(msg, "presets", cJSON_Parse(IDENTIFY_PRESET));
|
||||
}
|
||||
cJSON *sel = cJSON_AddArrayToObject(msg, "select");
|
||||
cJSON_AddItemToArray(sel, cJSON_CreateString(on ? "__identify" : "off"));
|
||||
if (id->mac[0]) {
|
||||
return push_to(id->mac, msg);
|
||||
}
|
||||
cJSON *groups = cJSON_AddArrayToObject(msg, "groups");
|
||||
cJSON_AddItemToArray(groups, cJSON_CreateString(id->group));
|
||||
cJSON *messages = cJSON_CreateArray();
|
||||
cJSON_AddItemToArray(messages, msg);
|
||||
esp_err_t err = engine_push(messages, NULL);
|
||||
cJSON_Delete(messages);
|
||||
return err;
|
||||
}
|
||||
|
||||
static void identify_off_task(void *arg)
|
||||
{
|
||||
identify_t *id = arg;
|
||||
vTaskDelay(pdMS_TO_TICKS(IDENTIFY_OFF_MS));
|
||||
cJSON *msg = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(msg, "v", "1");
|
||||
cJSON *sel = cJSON_AddObjectToObject(msg, "select"), *off = cJSON_AddArrayToObject(sel, id->name);
|
||||
cJSON_AddItemToArray(off, cJSON_CreateString("off"));
|
||||
push_to(id->mac, msg);
|
||||
identify_send(id, false);
|
||||
free(id);
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
// Blink the driver red at 10 Hz for two seconds, then select "off"
|
||||
static esp_err_t identify_start(const char *mac, const char *group)
|
||||
{
|
||||
identify_t *id = calloc(1, sizeof(*id));
|
||||
if (!id) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
snprintf(id->mac, sizeof(id->mac), "%s", mac ? mac : "");
|
||||
snprintf(id->group, sizeof(id->group), "%s", group ? group : "");
|
||||
esp_err_t err = identify_send(id, true);
|
||||
if (err != ESP_OK || xTaskCreate(identify_off_task, "identify", 6144, id, 3, NULL) != pdPASS) {
|
||||
free(id);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_err_t api_identify_group(const char *group_id)
|
||||
{
|
||||
return identify_start(NULL, group_id);
|
||||
}
|
||||
|
||||
static esp_err_t identify(httpd_req_t *req, const cJSON *dev, const char *mac)
|
||||
{
|
||||
const cJSON *name = cJSON_GetObjectItem(dev, "name");
|
||||
if (!cJSON_IsString(name) || !name->valuestring[0]) {
|
||||
return api_error(req, "400 Bad Request", "Device has no name");
|
||||
}
|
||||
if (!is_espnow(dev)) {
|
||||
return api_error(req, "503 Service Unavailable", "Wi-Fi driver not connected");
|
||||
}
|
||||
cJSON *msg = cJSON_Parse("{\"v\":\"1\",\"presets\":{\"__identify\":{\"p\":\"blink\",\"c\":[\"#ff0000\"],\"d\":50,"
|
||||
"\"b\":128,\"a\":true,\"n1\":0,\"n2\":0,\"n3\":0,\"n4\":0,\"n5\":0,\"n6\":0}}}");
|
||||
cJSON *sel = cJSON_AddObjectToObject(msg, "select"), *pick = cJSON_AddArrayToObject(sel, name->valuestring);
|
||||
cJSON_AddItemToArray(pick, cJSON_CreateString("__identify"));
|
||||
esp_err_t err = push_to(mac, msg);
|
||||
if (err != ESP_OK) {
|
||||
if (identify_start(mac, NULL) != ESP_OK) {
|
||||
return api_error(req, "503 Service Unavailable", "Identify send failed");
|
||||
}
|
||||
identify_t *id = malloc(sizeof(*id));
|
||||
if (id) {
|
||||
snprintf(id->mac, sizeof(id->mac), "%s", mac);
|
||||
snprintf(id->name, sizeof(id->name), "%s", name->valuestring);
|
||||
if (xTaskCreate(identify_off_task, "identify", 6144, id, 3, NULL) != pdPASS) {
|
||||
free(id);
|
||||
return api_message(req, "200 OK", "Identify sent");
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------- brightness */
|
||||
|
||||
static int clamp255(const cJSON *v, int dflt)
|
||||
{
|
||||
if (!cJSON_IsNumber(v)) {
|
||||
return dflt;
|
||||
}
|
||||
int b = (int)v->valuedouble;
|
||||
return b < 0 ? 0 : (b > 255 ? 255 : b);
|
||||
}
|
||||
|
||||
// global x each group listing the MAC (none: no factor) x device output_brightness [x zone], each a
|
||||
// 0..255 factor: round(f1/255 * f2/255 * ... * 255). zone_b < 0 leaves the zone out.
|
||||
int api_effective_brightness(const char *mac, int zone_b)
|
||||
{
|
||||
double v = clamp255(cJSON_GetObjectItem(store_settings(), "global_brightness"), 255) / 255.0;
|
||||
const cJSON *g;
|
||||
cJSON_ArrayForEach(g, store_coll("group"))
|
||||
{
|
||||
const cJSON *it;
|
||||
cJSON_ArrayForEach(it, cJSON_GetObjectItem(g, "devices"))
|
||||
{
|
||||
char m[13];
|
||||
if (api_mac(it, m) && !strcmp(m, mac)) {
|
||||
v *= clamp255(cJSON_GetObjectItem(g, "output_brightness"), 255) / 255.0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return api_message(req, "200 OK", "Identify sent");
|
||||
const cJSON *dev = cJSON_GetObjectItem(store_coll("device"), mac);
|
||||
v *= clamp255(cJSON_GetObjectItem(dev, "output_brightness"), 255) / 255.0;
|
||||
if (zone_b >= 0) {
|
||||
v *= (zone_b > 255 ? 255 : zone_b) / 255.0;
|
||||
}
|
||||
int b = (int)(v * 255 + 0.5);
|
||||
return b < 0 ? 0 : (b > 255 ? 255 : b);
|
||||
}
|
||||
|
||||
// zone_brightness from a body: -1 when absent, -2 (error sent) when invalid
|
||||
static int zone_brightness(httpd_req_t *req, const cJSON *body)
|
||||
{
|
||||
const cJSON *z = cJSON_GetObjectItem(body, "zone_brightness");
|
||||
if (!z || cJSON_IsNull(z)) {
|
||||
return -1;
|
||||
}
|
||||
if (!cJSON_IsNumber(z) || z->valuedouble != (int)z->valuedouble || z->valuedouble < 0 || z->valuedouble > 255) {
|
||||
api_error(req, "400 Bad Request", "output_brightness must be an integer 0–255");
|
||||
return -2;
|
||||
}
|
||||
return (int)z->valuedouble;
|
||||
}
|
||||
|
||||
// POST /devices/resolve-brightness {macs, zone_brightness?} -> {values: {mac: brightness}}
|
||||
static esp_err_t resolve_brightness(httpd_req_t *req)
|
||||
{
|
||||
cJSON *body = api_body_opt(req);
|
||||
if (!body) {
|
||||
return ESP_OK;
|
||||
}
|
||||
const cJSON *macs = cJSON_GetObjectItem(body, "macs");
|
||||
if (!cJSON_IsArray(macs)) {
|
||||
cJSON_Delete(body);
|
||||
return api_error(req, "400 Bad Request", "macs must be an array");
|
||||
}
|
||||
int zb = zone_brightness(req, body);
|
||||
if (zb == -2) {
|
||||
cJSON_Delete(body);
|
||||
return ESP_OK;
|
||||
}
|
||||
cJSON *o = cJSON_CreateObject(), *values = cJSON_AddObjectToObject(o, "values");
|
||||
const cJSON *it;
|
||||
cJSON_ArrayForEach(it, macs)
|
||||
{
|
||||
char mac[13];
|
||||
if (api_mac(it, mac) && !cJSON_GetObjectItem(values, mac)) {
|
||||
cJSON_AddNumberToObject(values, mac, api_effective_brightness(mac, zb));
|
||||
}
|
||||
}
|
||||
cJSON_Delete(body);
|
||||
return api_json(req, "200 OK", o, true);
|
||||
}
|
||||
|
||||
// POST /devices/<id>/brightness {zone_brightness?}: the combined brightness, saved on the driver
|
||||
static esp_err_t device_brightness(httpd_req_t *req, const char *mac)
|
||||
{
|
||||
cJSON *body = api_body_opt(req);
|
||||
if (!body) {
|
||||
return ESP_OK;
|
||||
}
|
||||
int zb = zone_brightness(req, body);
|
||||
cJSON_Delete(body);
|
||||
if (zb == -2) {
|
||||
return ESP_OK;
|
||||
}
|
||||
int b = api_effective_brightness(mac, zb);
|
||||
cJSON *msg = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(msg, "v", "1");
|
||||
cJSON_AddNumberToObject(msg, "b", b);
|
||||
cJSON_AddTrueToObject(msg, "save");
|
||||
if (push_to(mac, msg) != ESP_OK) {
|
||||
return api_error(req, "503 Service Unavailable", "Send failed");
|
||||
}
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(o, "message", "brightness sent");
|
||||
cJSON_AddNumberToObject(o, "brightness", b);
|
||||
return api_json(req, "200 OK", o, true);
|
||||
}
|
||||
|
||||
// POST /devices/<id>/driver-config {name, num_leds, color_order, startup_mode}
|
||||
static esp_err_t device_driver_config(httpd_req_t *req, const char *mac)
|
||||
{
|
||||
cJSON *body = api_body_opt(req);
|
||||
if (!body) {
|
||||
return ESP_OK;
|
||||
}
|
||||
cJSON *dc = api_driver_config(cJSON_GetObjectItem(body, "name"), cJSON_GetObjectItem(body, "num_leds"),
|
||||
cJSON_GetObjectItem(body, "color_order"), cJSON_GetObjectItem(body, "startup_mode"));
|
||||
cJSON_Delete(body);
|
||||
if (!dc) {
|
||||
return api_error(req, "400 Bad Request", "Provide at least one of name, num_leds, color_order, startup_mode");
|
||||
}
|
||||
cJSON *msg = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(msg, "v", "1");
|
||||
cJSON_AddItemToObject(msg, "device_config", dc);
|
||||
cJSON_AddTrueToObject(msg, "save");
|
||||
if (push_to(mac, msg) != ESP_OK) {
|
||||
return api_error(req, "503 Service Unavailable", "Send failed");
|
||||
}
|
||||
return api_message(req, "200 OK", "driver-config sent");
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------- ping */
|
||||
@@ -501,6 +641,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], "resolve-brightness")) {
|
||||
*ret = resolve_brightness(req);
|
||||
return true;
|
||||
}
|
||||
if (n == 1) {
|
||||
if (method == HTTP_GET) {
|
||||
cJSON *o = cJSON_CreateObject(), *d;
|
||||
@@ -526,8 +670,12 @@ bool devices_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
|
||||
if (n == 3) {
|
||||
if (method == HTTP_POST && !strcmp(seg[2], "identify")) {
|
||||
*ret = identify(req, dev, mac);
|
||||
} else if (method == HTTP_POST && !strcmp(seg[2], "brightness")) {
|
||||
*ret = device_brightness(req, mac);
|
||||
} else if (method == HTTP_POST && !strcmp(seg[2], "driver-config")) {
|
||||
*ret = device_driver_config(req, mac);
|
||||
} else if (method == HTTP_POST && !strcmp(seg[2], "patterns")) {
|
||||
*ret = api_error(req, "404 Not Found", "use POST /patterns/<name>/send with device_id");
|
||||
*ret = api_error(req, "400 Bad Request", "Pattern OTA push is not supported for ESP-NOW devices; use POST /patterns/<name>/send");
|
||||
} else {
|
||||
*ret = api_error(req, "404 Not Found", "unknown action");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user