feat: sequences v2, driver frame sync and submodule updates
- Sequences v2, bridges and sim-controller changes from the working tree. - Records the led-driver, led-tool and led-simulator commits above. - Includes dmx-driver/ and graphify-out/. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3f8f67823b
commit
50ca46fee6
657 files changed
+15818
-1107
No files matched your search
+73
-10
@@ -7,6 +7,7 @@
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include "api_util.h"
|
||||
#include "esp_log.h"
|
||||
#include "engine.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
@@ -171,6 +172,26 @@ static cJSON *device_config_from(const cJSON *body)
|
||||
return dc;
|
||||
}
|
||||
|
||||
bool api_apply_driver_cfg(cJSON *rec, const char *cfg, int len)
|
||||
{
|
||||
cJSON *j = cfg && len > 0 ? cJSON_ParseWithLength(cfg, len) : NULL;
|
||||
bool changed = false;
|
||||
// "name" is handled by the callers
|
||||
for (size_t i = 1; j && rec && i < sizeof(DEVICE_CONFIG_KEYS) / sizeof(DEVICE_CONFIG_KEYS[0]); i++) {
|
||||
const cJSON *v = cJSON_GetObjectItem(j, DEVICE_CONFIG_KEYS[i]);
|
||||
if (!v || !(cJSON_IsNumber(v) || cJSON_IsString(v) || cJSON_IsBool(v))) {
|
||||
continue;
|
||||
}
|
||||
const cJSON *old = cJSON_GetObjectItem(rec, DEVICE_CONFIG_KEYS[i]);
|
||||
if (!old || !cJSON_Compare(old, v, true)) {
|
||||
api_set(rec, DEVICE_CONFIG_KEYS[i], cJSON_Duplicate(v, true));
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
cJSON_Delete(j);
|
||||
return changed;
|
||||
}
|
||||
|
||||
// v1 messages to one driver by its MAC
|
||||
static esp_err_t push_to(const char *mac, cJSON *msg)
|
||||
{
|
||||
@@ -314,10 +335,12 @@ static int clamp255(const cJSON *v, int dflt)
|
||||
}
|
||||
|
||||
// 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.
|
||||
// 0..255 factor: round(f1/255 * f2/255 * ... * 255), capped at the lowest group max_brightness.
|
||||
// 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;
|
||||
double cap = 1.0; // lowest max_brightness of the groups listing the MAC
|
||||
const cJSON *g;
|
||||
cJSON_ArrayForEach(g, store_coll("group"))
|
||||
{
|
||||
@@ -327,6 +350,10 @@ int api_effective_brightness(const char *mac, int zone_b)
|
||||
char m[13];
|
||||
if (api_mac(it, m) && !strcmp(m, mac)) {
|
||||
v *= clamp255(cJSON_GetObjectItem(g, "output_brightness"), 255) / 255.0;
|
||||
double c = clamp255(cJSON_GetObjectItem(g, "max_brightness"), 255) / 255.0;
|
||||
if (c < cap) {
|
||||
cap = c;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -336,6 +363,9 @@ int api_effective_brightness(const char *mac, int zone_b)
|
||||
if (zone_b >= 0) {
|
||||
v *= (zone_b > 255 ? 255 : zone_b) / 255.0;
|
||||
}
|
||||
if (v > cap) {
|
||||
v = cap;
|
||||
}
|
||||
int b = (int)(v * 255 + 0.5);
|
||||
return b < 0 ? 0 : (b > 255 ? 255 : b);
|
||||
}
|
||||
@@ -440,6 +470,7 @@ static esp_err_t device_driver_config(httpd_req_t *req, const char *mac)
|
||||
typedef struct {
|
||||
uint8_t mac[6];
|
||||
char name[33];
|
||||
char cfg[200]; // the driver's settings as JSON, "" when it sent none
|
||||
int rtt_ms;
|
||||
} ping_rsp_t;
|
||||
|
||||
@@ -472,6 +503,12 @@ static void on_ping_rsp(const uint8_t mac[6], const uint8_t *pkt, int len)
|
||||
nl = nl < (int)sizeof(r->name) - 1 ? nl : (int)sizeof(r->name) - 1;
|
||||
memcpy(r->name, pkt + 7, nl);
|
||||
r->name[nl] = 0;
|
||||
r->cfg[0] = 0;
|
||||
int co = 7 + pkt[6]; // then [len][json]
|
||||
if (co < len && pkt[co] > 0 && co + 1 + pkt[co] <= len && pkt[co] < sizeof(r->cfg)) {
|
||||
memcpy(r->cfg, pkt + co + 1, pkt[co]);
|
||||
r->cfg[pkt[co]] = 0;
|
||||
}
|
||||
r->rtt_ms = (int)((now - s_ping.sent_us) / 1000);
|
||||
}
|
||||
}
|
||||
@@ -479,7 +516,7 @@ static void on_ping_rsp(const uint8_t mac[6], const uint8_t *pkt, int len)
|
||||
}
|
||||
|
||||
// Add a driver that answered but is not registered yet, or take its new name. True when stored.
|
||||
static bool register_from_ping(const char *hex, const char *name)
|
||||
static bool register_from_ping(const char *hex, const char *name, const char *cfg)
|
||||
{
|
||||
char label[48];
|
||||
snprintf(label, sizeof(label), "%s", name);
|
||||
@@ -495,17 +532,22 @@ static bool register_from_ping(const char *hex, const char *name)
|
||||
cJSON_AddStringToObject(rec, "type", "led");
|
||||
cJSON_AddStringToObject(rec, "transport", "espnow");
|
||||
cJSON_AddStringToObject(rec, "address", hex);
|
||||
api_apply_driver_cfg(rec, cfg, strlen(cfg));
|
||||
return true;
|
||||
}
|
||||
bool changed = false;
|
||||
const cJSON *old = cJSON_GetObjectItem(rec, "name");
|
||||
if (is_espnow(rec) && (!cJSON_IsString(old) || strcmp(old->valuestring, label))) {
|
||||
api_set(rec, "name", cJSON_CreateString(label));
|
||||
return true;
|
||||
changed = true;
|
||||
}
|
||||
return false;
|
||||
if (is_espnow(rec) && api_apply_driver_cfg(rec, cfg, strlen(cfg))) {
|
||||
changed = true;
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
// POST /devices/ping {timeout_s}: broadcast PING_REQ and collect replies for timeout_s (0.5-30, default 3)
|
||||
// POST /devices/ping {timeout_s, broadcast}: PING_REQ to each known device, or a broadcast when broadcast is true or none are known and collect replies for timeout_s (0.5-30, default 3)
|
||||
static esp_err_t ping_devices(httpd_req_t *req)
|
||||
{
|
||||
static bool hooked; // replies only matter while a ping runs, so the callback is set on first use
|
||||
@@ -524,6 +566,7 @@ static esp_err_t ping_devices(httpd_req_t *req)
|
||||
}
|
||||
double timeout_s = cJSON_IsNumber(t) ? t->valuedouble : 3.0;
|
||||
timeout_s = timeout_s < 0.5 ? 0.5 : (timeout_s > 30 ? 30 : timeout_s);
|
||||
bool unicast = !cJSON_IsTrue(cJSON_GetObjectItem(body, "broadcast")) && store_coll("device")->child; // no known devices: broadcast to discover them
|
||||
cJSON_Delete(body);
|
||||
|
||||
portENTER_CRITICAL(&s_ping_mux);
|
||||
@@ -539,9 +582,23 @@ static esp_err_t ping_devices(httpd_req_t *req)
|
||||
return api_error(req, "409 Conflict", "a ping is already running");
|
||||
}
|
||||
uint8_t pkt[6] = { 0x4C, 0x05, id & 255, (id >> 8) & 255, (id >> 16) & 255, id >> 24 };
|
||||
esp_err_t err = espnow_tx_send(NULL, pkt, sizeof(pkt));
|
||||
vTaskDelay(pdMS_TO_TICKS(20));
|
||||
espnow_tx_send(NULL, pkt, sizeof(pkt)); // a second copy against a lost broadcast; replies are deduplicated
|
||||
esp_err_t err = ESP_OK;
|
||||
if (unicast) { // one acked frame to each known device
|
||||
cJSON *d;
|
||||
cJSON_ArrayForEach(d, store_coll("device"))
|
||||
{
|
||||
unsigned m[6];
|
||||
if (d->string && sscanf(d->string, "%2x%2x%2x%2x%2x%2x", &m[0], &m[1], &m[2], &m[3], &m[4], &m[5]) == 6) {
|
||||
uint8_t mac[6] = { m[0], m[1], m[2], m[3], m[4], m[5] };
|
||||
esp_err_t e = espnow_tx_send(mac, pkt, sizeof(pkt));
|
||||
err = e != ESP_OK ? e : err;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err = espnow_tx_send(NULL, pkt, sizeof(pkt));
|
||||
vTaskDelay(pdMS_TO_TICKS(20));
|
||||
espnow_tx_send(NULL, pkt, sizeof(pkt)); // a second copy against a lost broadcast; replies are deduplicated
|
||||
}
|
||||
if (err == ESP_OK) {
|
||||
store_unlock(); // replies take up to timeout_s; HTTP and the sequencer keep going
|
||||
vTaskDelay(pdMS_TO_TICKS((int)(timeout_s * 1000)));
|
||||
@@ -571,7 +628,13 @@ static esp_err_t ping_devices(httpd_req_t *req)
|
||||
cJSON_AddStringToObject(r, "mac", hex);
|
||||
cJSON_AddStringToObject(r, "name", got[i].name);
|
||||
cJSON_AddNumberToObject(r, "rtt_ms", got[i].rtt_ms);
|
||||
registered += register_from_ping(hex, got[i].name);
|
||||
if (got[i].cfg[0]) {
|
||||
cJSON *cj = cJSON_Parse(got[i].cfg);
|
||||
if (cj) {
|
||||
cJSON_AddItemToObject(r, "config", cj);
|
||||
}
|
||||
}
|
||||
registered += register_from_ping(hex, got[i].name, got[i].cfg);
|
||||
}
|
||||
cJSON_AddNumberToObject(o, "registered", registered);
|
||||
if (registered) {
|
||||
@@ -807,7 +870,7 @@ bool devices_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
|
||||
} 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, "400 Bad Request", "Pattern OTA push is not supported for ESP-NOW devices; use POST /patterns/<name>/send");
|
||||
*ret = api_error(req, "400 Bad Request", "Pattern OTA push is not supported for ESP-NOW devices");
|
||||
} else {
|
||||
*ret = api_error(req, "404 Not Found", "unknown action");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user