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
+13
-3
@@ -1,12 +1,22 @@
|
||||
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"
|
||||
if(IDF_TARGET STREQUAL "linux")
|
||||
# Host build for led-simulator (sim-controller/): no Ethernet, Wi-Fi, mic or OTA. The sim
|
||||
# component has app_main and stands in for main.c, mic.c and ota.c.
|
||||
idf_component_register(SRCS "beat.c" "beat_dsp.c" "espnow_tx.c" "bridge.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c" "drv_ota.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 "." "../sim-controller/components/sim/include"
|
||||
REQUIRES esp_http_server esp_timer esp_event json
|
||||
PRIV_REQUIRES mbedtls lwip esp_app_format)
|
||||
else()
|
||||
idf_component_register(SRCS "main.c" "mic.c" "beat.c" "beat_dsp.c" "espnow_tx.c" "bridge.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" "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
|
||||
PRIV_REQUIRES littlefs mdns mbedtls lwip)
|
||||
endif()
|
||||
|
||||
# The gzipped web UI is embedded in the app image (run tools/build_web.sh first)
|
||||
if(NOT EXISTS ${PROJECT_DIR}/www_dist/www.pack)
|
||||
if(NOT EXISTS ${COMPONENT_DIR}/../www_dist/www.pack)
|
||||
message(FATAL_ERROR "www_dist/www.pack is missing: run tools/build_web.sh (make build does)")
|
||||
endif()
|
||||
target_add_binary_data(${COMPONENT_LIB} ${PROJECT_DIR}/www_dist/www.pack BINARY)
|
||||
target_add_binary_data(${COMPONENT_LIB} ${COMPONENT_DIR}/../www_dist/www.pack BINARY)
|
||||
@@ -106,12 +106,11 @@ endmenu
|
||||
menu "AP config"
|
||||
|
||||
config AP_BUTTON_GPIO
|
||||
int "AP button GPIO (XIAO D7), -1 for none"
|
||||
default 44
|
||||
int "AP button GPIO (XIAO D3), -1 for none"
|
||||
default 4
|
||||
help
|
||||
Push button to ground. Held while the controller boots, it turns
|
||||
Active-low push button to ground (internal pull-up). Held while the controller boots, it turns
|
||||
the Wi-Fi access point on even when the ap_enabled setting is off.
|
||||
GPIO44 is also UART0 RX; the button only pulls it low at boot.
|
||||
|
||||
config AP_HOLD_MS
|
||||
int "Hold time at boot (ms)"
|
||||
|
||||
@@ -404,6 +404,10 @@ static esp_err_t put_settings(httpd_req_t *req)
|
||||
!(cJSON_IsString(pw) && (!pw->valuestring[0] || (strlen(pw->valuestring) >= 8 && strlen(pw->valuestring) <= 63)))) {
|
||||
bad = "ap_password must be empty (open) or 8 to 63 characters";
|
||||
}
|
||||
const cJSON *br = cJSON_GetObjectItem(body, "bridge");
|
||||
if (!bad && br && !cJSON_IsNull(br) && !(cJSON_IsString(br) && strlen(br->valuestring) < 64)) {
|
||||
bad = "bridge must be \"host:port\", empty or null";
|
||||
}
|
||||
const cJSON *sw = cJSON_GetObjectItem(body, "sequence_switch_wait");
|
||||
if (!bad && sw && !cJSON_IsNull(sw) &&
|
||||
!(cJSON_IsString(sw) && (!strcasecmp(sw->valuestring, "beat") || !strcasecmp(sw->valuestring, "downbeat")))) {
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
#include "bridge.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "lwip/netdb.h"
|
||||
#include "lwip/sockets.h"
|
||||
|
||||
static const char *TAG = "bridge";
|
||||
|
||||
static int s_sock = -1;
|
||||
static struct sockaddr_in s_peer;
|
||||
static bridge_rx_cb_t s_cb;
|
||||
static char s_addr[80];
|
||||
|
||||
static void rx_task(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
uint8_t buf[6 + 256];
|
||||
for (;;) {
|
||||
int n = recv(s_sock, buf, sizeof(buf), 0);
|
||||
if (n < 0) {
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
continue;
|
||||
}
|
||||
if (n > 6 && s_cb) {
|
||||
s_cb(buf, buf + 6, n - 6);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t bridge_start(const char *addr, bridge_rx_cb_t cb)
|
||||
{
|
||||
if (s_sock >= 0 || !addr || !addr[0]) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
char host[64];
|
||||
int port = BRIDGE_DEFAULT_PORT;
|
||||
snprintf(host, sizeof(host), "%s", addr);
|
||||
char *colon = strrchr(host, ':');
|
||||
if (colon) {
|
||||
*colon = 0;
|
||||
port = atoi(colon + 1);
|
||||
}
|
||||
if (port <= 0 || port > 65535 || !host[0]) {
|
||||
ESP_LOGE(TAG, "bad bridge address %s", addr);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
struct addrinfo hints = { .ai_family = AF_INET, .ai_socktype = SOCK_DGRAM }, *res = NULL;
|
||||
if (getaddrinfo(host, NULL, &hints, &res) != 0 || !res) {
|
||||
ESP_LOGE(TAG, "cannot resolve %s", host);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
memcpy(&s_peer, res->ai_addr, sizeof(s_peer));
|
||||
freeaddrinfo(res);
|
||||
s_peer.sin_port = htons(port);
|
||||
|
||||
int s = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (s < 0) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
// connect() fixes the peer, so only its datagrams are received
|
||||
if (connect(s, (struct sockaddr *)&s_peer, sizeof(s_peer)) != 0) {
|
||||
close(s);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
s_sock = s;
|
||||
s_cb = cb;
|
||||
snprintf(s_addr, sizeof(s_addr), "%s:%d", host, port);
|
||||
xTaskCreate(rx_task, "bridge_rx", 4096, NULL, 6, NULL);
|
||||
ESP_LOGW(TAG, "ESP-NOW frames go to the UDP bridge %s, not the radio", s_addr);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
bool bridge_active(void)
|
||||
{
|
||||
return s_sock >= 0;
|
||||
}
|
||||
|
||||
const char *bridge_addr(void)
|
||||
{
|
||||
return s_sock >= 0 ? s_addr : NULL;
|
||||
}
|
||||
|
||||
esp_err_t bridge_send(const uint8_t *dst_mac, const void *data, size_t len)
|
||||
{
|
||||
if (s_sock < 0 || len > 250) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
uint8_t buf[6 + 250];
|
||||
memcpy(buf, dst_mac, 6);
|
||||
memcpy(buf + 6, data, len);
|
||||
// A refused datagram (the simulator is not running yet) is a lost frame, as on air
|
||||
send(s_sock, buf, 6 + len, 0);
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
// UDP bridge: carries the controller's ESP-NOW frames to a host that stands in for the radio, such as
|
||||
// led-simulator (settings "bridge": "host:port"). Down: dst_mac(6) + packet. Up: src_mac(6) + packet.
|
||||
// While it runs, nothing goes out over the air.
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "esp_err.h"
|
||||
|
||||
typedef void (*bridge_rx_cb_t)(const uint8_t *src_mac, const uint8_t *data, int len);
|
||||
|
||||
#define BRIDGE_DEFAULT_PORT 8766
|
||||
|
||||
// addr "host" or "host:port". Starts the receive task; frames arrive on cb.
|
||||
esp_err_t bridge_start(const char *addr, bridge_rx_cb_t cb);
|
||||
bool bridge_active(void);
|
||||
esp_err_t bridge_send(const uint8_t *dst_mac, const void *data, size_t len);
|
||||
// "host:port" while active, else NULL
|
||||
const char *bridge_addr(void);
|
||||
+44
-8
@@ -59,7 +59,7 @@ static bool s_save; // append the CMD save byte (driver persists presets / br
|
||||
// drivers in that group apply them. JSON messages go as they are.
|
||||
static size_t wrap_wire(uint8_t *out, const uint8_t *frame, size_t len)
|
||||
{
|
||||
if (len == 0 || frame[0] != 2) { // not a v2 envelope
|
||||
if (len == 0 || (frame[0] != 2 && frame[0] != 3)) { // not a v2/v3 envelope (JSON goes as it is)
|
||||
memcpy(out, frame, len);
|
||||
return len;
|
||||
}
|
||||
@@ -257,6 +257,9 @@ static bool in_group(const uint8_t mac[6], const char *name, const char *gid)
|
||||
}
|
||||
char hex[13];
|
||||
snprintf(hex, sizeof(hex), "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
if (gid[0] == 'd' && strlen(gid) == 13) { // a driver's own group, d<mac>
|
||||
return !strcmp(gid + 1, hex);
|
||||
}
|
||||
return mac_listed(cJSON_GetObjectItem(cJSON_GetObjectItem(store_coll("group"), gid), "devices"), hex);
|
||||
}
|
||||
|
||||
@@ -355,6 +358,32 @@ static esp_err_t play_locked(const char *zone_id, const char *preset_id)
|
||||
return ret;
|
||||
}
|
||||
|
||||
const char *engine_zone_current(const char *zone_id)
|
||||
{
|
||||
for (int i = 0; i < 16; i++) {
|
||||
if (s_cur[i].zone[0] && !strcmp(s_cur[i].zone, zone_id)) {
|
||||
return s_cur[i].preset;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int engine_group_macs(const char *gid, char out[][13], int max)
|
||||
{
|
||||
int n = 0;
|
||||
const cJSON *d;
|
||||
cJSON_ArrayForEach(d, store_coll("device"))
|
||||
{
|
||||
uint8_t mac[6];
|
||||
const cJSON *name = cJSON_GetObjectItem(d, "name");
|
||||
if (n < max && d->string && parse_mac(d->string, mac) &&
|
||||
in_group(mac, cJSON_IsString(name) ? name->valuestring : NULL, gid)) {
|
||||
snprintf(out[n++], 13, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
esp_err_t engine_play(const char *zone_id, const char *preset_id)
|
||||
{
|
||||
store_lock();
|
||||
@@ -493,8 +522,9 @@ static const char *const TOP_SHORT[][2] = {
|
||||
};
|
||||
static const char *const PRESET_SHORT[][2] = {
|
||||
{ "pattern", "p" }, { "colors", "c" }, { "delay", "d" }, { "brightness", "b" }, { "auto", "a" }, { "background", "bg" },
|
||||
{ "fade", "f" }, { "beat_time", "tb" },
|
||||
};
|
||||
static const char *const PRESET_KEYS[] = { "p", "c", "d", "b", "a", "bg", "n1", "n2", "n3", "n4", "n5", "n6" };
|
||||
static const char *const PRESET_KEYS[] = { "p", "c", "d", "b", "a", "bg", "n1", "n2", "n3", "n4", "n5", "n6", "f", "tb" };
|
||||
|
||||
static const char *short_key(const char *const map[][2], size_t n, const char *k)
|
||||
{
|
||||
@@ -758,17 +788,22 @@ static esp_err_t send_cmd(const uint8_t mac[6], const uint8_t *env, size_t len,
|
||||
return rel_send(mac, wire, len + 2, commit, 1);
|
||||
}
|
||||
|
||||
// GROUPS packet: the ids of every zone whose driver names include `name`. An empty list replaces
|
||||
// whatever the driver stored, so it is only sent when allow_empty is set (a zone or device was edited);
|
||||
// a driver that merely announced itself and is in no zone keeps the groups it already has.
|
||||
// GROUPS packet for a driver: [4C][02][count] then per zone [len][zone id]. Returns its length;
|
||||
// *count gets the number of zones.
|
||||
// GROUPS packet: the driver's own group d<mac> (sequences address single drivers with it), then every
|
||||
// group and zone it belongs to. A list with nothing but its own group replaces whatever the driver
|
||||
// stored, so it is only sent when allow_empty is set (a zone or device was edited); a driver that merely
|
||||
// announced itself and is in no zone keeps the groups it already has.
|
||||
// Wire: [4C][02][count] then per id [len][id]. Returns its length; *count gets the number of groups and
|
||||
// zones (not counting its own group).
|
||||
static size_t build_groups(uint8_t pkt[REL_FRAG_MAX], const uint8_t mac[6], const char *name, int *count)
|
||||
{
|
||||
size_t off = 3;
|
||||
*count = 0;
|
||||
const cJSON *g;
|
||||
char gid[GROUP_ID_MAX];
|
||||
// Its own group first, so a long list never cuts it off
|
||||
pkt[off++] = 13;
|
||||
off += snprintf((char *)pkt + off, REL_FRAG_MAX - off, "d%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3],
|
||||
mac[4], mac[5]);
|
||||
cJSON_ArrayForEach(g, store_coll("group"))
|
||||
{
|
||||
size_t gl = strlen(g->string);
|
||||
@@ -795,7 +830,8 @@ static size_t build_groups(uint8_t pkt[REL_FRAG_MAX], const uint8_t mac[6], cons
|
||||
}
|
||||
pkt[0] = 0x4C;
|
||||
pkt[1] = 0x02; // GROUPS
|
||||
pkt[2] = *count;
|
||||
pkt[2] = *count + 1; // *count leaves out the driver's own group
|
||||
|
||||
return off;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,12 @@ int engine_zone_targets(const char *zone_id, char out[][GROUP_ID_MAX], int max);
|
||||
// NULL when there is no such preset. Call with the store locked.
|
||||
cJSON *engine_preset_payload(const char *preset_id);
|
||||
|
||||
// Drivers in group gid (a group id, "z<zone>" or a driver's own "d<mac>"), as 12-hex MACs from the
|
||||
// device list. Call with the store locked.
|
||||
int engine_group_macs(const char *gid, char out[][13], int max);
|
||||
// The preset a zone last played (engine_play / engine_stop), or NULL. Call with the store locked.
|
||||
const char *engine_zone_current(const char *zone_id);
|
||||
|
||||
// Select a preset on every driver in the zone's groups (broadcast, sent twice)
|
||||
esp_err_t engine_play(const char *zone_id, const char *preset_id);
|
||||
// Turn the zone's drivers off
|
||||
|
||||
+21
-1
@@ -7,6 +7,7 @@ void env_blob_init(env_blob_t *b)
|
||||
{
|
||||
b->data[0] = 0;
|
||||
b->len = 1;
|
||||
b->v3 = false;
|
||||
}
|
||||
|
||||
static bool put(env_blob_t *b, const void *src, size_t n)
|
||||
@@ -112,6 +113,15 @@ bool env_add_preset(env_blob_t *b, const char *name, const cJSON *p)
|
||||
if (nl > 250 || pl > 250 || ncol > 255) {
|
||||
return false;
|
||||
}
|
||||
const cJSON *fade = get2(p, "f", "fade"), *tb = get2(p, "tb", "beat_time");
|
||||
int fade_ms = clamp(num_or(fade, 0), 0, 10000);
|
||||
bool beat = truthy(tb);
|
||||
bool ext = fade_ms > 0 || beat;
|
||||
if (b->data[0] == 0) {
|
||||
b->v3 = ext;
|
||||
} else if (b->v3 != ext) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t rgbs[255][3];
|
||||
int nvalid = 0;
|
||||
@@ -136,6 +146,16 @@ bool env_add_preset(env_blob_t *b, const char *name, const cJSON *p)
|
||||
char key[4] = { 'n', '0' + i, 0 };
|
||||
ok = put_le16(b, (uint16_t)(int16_t)clamp(num_or(cJSON_GetObjectItem(p, key), 0), -32768, 32767));
|
||||
}
|
||||
if (ok && b->v3) {
|
||||
uint8_t ext_len = (fade_ms ? 4 : 0) + (beat ? 3 : 0);
|
||||
ok = put_u8(b, ext_len);
|
||||
if (ok && fade_ms) {
|
||||
ok = put_u8(b, ENV_EXT_FADE) && put_u8(b, 2) && put_le16(b, fade_ms);
|
||||
}
|
||||
if (ok && beat) {
|
||||
ok = put_u8(b, ENV_EXT_BEAT) && put_u8(b, 1) && put_u8(b, 1);
|
||||
}
|
||||
}
|
||||
if (!ok) {
|
||||
b->len = save_len;
|
||||
b->data[0] = save_count;
|
||||
@@ -172,7 +192,7 @@ size_t env_frame(uint8_t *out, int brightness, const env_blob_t *presets, const
|
||||
if (brightness >= 0) {
|
||||
wire = brightness > 255 ? 127 : (brightness * 127 + 127) / 255; // 0-127 scales to 0-255
|
||||
}
|
||||
out[0] = 2;
|
||||
out[0] = lp && presets->v3 ? 3 : 2;
|
||||
out[1] = wire;
|
||||
out[2] = lp;
|
||||
out[3] = ls;
|
||||
|
||||
+8
-1
@@ -14,11 +14,18 @@
|
||||
typedef struct {
|
||||
uint8_t data[ENV_BLOB_MAX + 1]; // data[0] is the entry count
|
||||
size_t len;
|
||||
bool v3; // preset records carry an extension block (fade, beat timing): a v3 frame
|
||||
} env_blob_t;
|
||||
|
||||
// Envelope v3: v2, except that each preset record is followed by [ext_len][key, len, value]... Keys:
|
||||
#define ENV_EXT_FADE 1 // u16: fade-in ms when the preset is selected
|
||||
#define ENV_EXT_BEAT 2 // u8: 1 = durations are quarter beats, not ms
|
||||
|
||||
void env_blob_init(env_blob_t *b);
|
||||
|
||||
// Append one preset (short keys p,c,d,b,a,n1..n6 or long pattern/colors/...); false if it does not fit
|
||||
// Append one preset (short keys p,c,d,b,a,n1..n6,f,tb or long pattern/colors/.../fade/beat_time); false if
|
||||
// it does not fit. A preset with a fade or beat timing makes the blob v3; v2 and v3 records never share a
|
||||
// blob, so mixing them also returns false (callers send what they have and start a new blob).
|
||||
bool env_add_preset(env_blob_t *b, const char *name, const cJSON *preset);
|
||||
// Append one select entry; step < 0 means none
|
||||
bool env_add_select(env_blob_t *b, const char *device, const char *preset, int step);
|
||||
|
||||
+106
-9
@@ -4,13 +4,18 @@
|
||||
#include "esp_check.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_now.h"
|
||||
#include "esp_rom_sys.h"
|
||||
#include "esp_random.h"
|
||||
#include "esp_timer.h"
|
||||
#include "bench.h"
|
||||
#include "bridge.h"
|
||||
#if !CONFIG_IDF_TARGET_LINUX
|
||||
#include "esp_netif.h"
|
||||
#include "esp_now.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_mac.h"
|
||||
#include "esp_wifi_ap_get_sta_list.h"
|
||||
#endif
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/semphr.h"
|
||||
@@ -32,7 +37,7 @@ typedef struct {
|
||||
|
||||
static QueueHandle_t s_bulk_q;
|
||||
static SemaphoreHandle_t s_bulk_done;
|
||||
static volatile esp_now_send_status_t s_bulk_status;
|
||||
static volatile bool s_bulk_ok;
|
||||
static volatile bool s_bulk_inflight;
|
||||
static volatile int64_t s_last_beat_us;
|
||||
static esp_timer_handle_t s_copy_timer;
|
||||
@@ -44,6 +49,26 @@ static bool s_have_peer;
|
||||
static espnow_stats_t s_stats;
|
||||
static volatile int s_loss_pct;
|
||||
static volatile bool s_loss_beats = true;
|
||||
static espnow_rx_cb_t s_rx_cb;
|
||||
static char s_bridge[80];
|
||||
|
||||
void espnow_tx_set_bridge(const char *addr)
|
||||
{
|
||||
snprintf(s_bridge, sizeof(s_bridge), "%s", addr ? addr : "");
|
||||
}
|
||||
|
||||
// One frame onto the air, or to the UDP bridge while one is set
|
||||
static esp_err_t radio_send(const uint8_t *dst, const void *data, size_t len)
|
||||
{
|
||||
if (bridge_active()) {
|
||||
return bridge_send(dst, data, len);
|
||||
}
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
#else
|
||||
return esp_now_send(dst, data, len);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool espnow_tx_drop(void)
|
||||
{
|
||||
@@ -64,17 +89,39 @@ int espnow_tx_loss(bool *include_beats)
|
||||
return s_loss_pct;
|
||||
}
|
||||
|
||||
#if !CONFIG_IDF_TARGET_LINUX
|
||||
static void IRAM_ATTR send_cb(const esp_now_send_info_t *info, esp_now_send_status_t status)
|
||||
{
|
||||
(void)info;
|
||||
if (s_bulk_inflight) {
|
||||
s_bulk_status = status;
|
||||
s_bulk_ok = status == ESP_NOW_SEND_SUCCESS;
|
||||
BaseType_t woken = pdFALSE;
|
||||
xSemaphoreGiveFromISR(s_bulk_done, &woken);
|
||||
portYIELD_FROM_ISR(woken);
|
||||
}
|
||||
}
|
||||
|
||||
static void recv_cb(const esp_now_recv_info_t *info, const uint8_t *data, int len)
|
||||
{
|
||||
if (s_rx_cb) {
|
||||
s_rx_cb(info->src_addr, data, len);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void bridge_rx(const uint8_t *src, const uint8_t *data, int len)
|
||||
{
|
||||
if (s_rx_cb) {
|
||||
s_rx_cb(src, data, len);
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t espnow_tx_set_rx_cb(espnow_rx_cb_t cb)
|
||||
{
|
||||
s_rx_cb = cb;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// Each copy carries a fire_in_ms reduced by the time already spent, so every
|
||||
// copy points at the same instant.
|
||||
static void send_beat_copy(void *arg)
|
||||
@@ -87,7 +134,7 @@ static void send_beat_copy(void *arg)
|
||||
|
||||
if (s_loss_beats && espnow_tx_drop()) {
|
||||
s_stats.sim_dropped++; // lossy-link test: this copy never reaches the radio
|
||||
} else if (esp_now_send(BROADCAST, (const uint8_t *)&s_pending, sizeof(s_pending)) != ESP_OK) {
|
||||
} else if (radio_send(BROADCAST, &s_pending, sizeof(s_pending)) != ESP_OK) {
|
||||
s_stats.beat_send_fail++;
|
||||
}
|
||||
s_last_beat_us = esp_timer_get_time();
|
||||
@@ -134,6 +181,12 @@ static void on_beat(const beat_info_t *info, float bpm, bool downbeat)
|
||||
|
||||
static esp_err_t select_peer(const uint8_t *mac)
|
||||
{
|
||||
if (bridge_active()) {
|
||||
return ESP_OK; // no peer table behind the bridge
|
||||
}
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
#else
|
||||
if (s_have_peer && memcmp(s_cur_peer, mac, 6) == 0) {
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -149,6 +202,7 @@ static esp_err_t select_peer(const uint8_t *mac)
|
||||
memcpy(s_cur_peer, mac, 6);
|
||||
s_have_peer = true;
|
||||
return ESP_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void bulk_task(void *arg)
|
||||
@@ -180,9 +234,11 @@ static void bulk_task(void *arg)
|
||||
if (espnow_tx_drop()) {
|
||||
s_stats.sim_dropped++; // lossy-link test: pretend the radio sent it
|
||||
ok = true;
|
||||
} else if (esp_now_send(dst, b.data, b.len) == ESP_OK &&
|
||||
} else if (bridge_active()) {
|
||||
ok = bridge_send(dst, b.data, b.len) == ESP_OK; // no MAC-layer ack: rel.c's acks retry
|
||||
} else if (radio_send(dst, b.data, b.len) == ESP_OK &&
|
||||
xSemaphoreTake(s_bulk_done, pdMS_TO_TICKS(30)) == pdTRUE) {
|
||||
ok = s_bulk_status == ESP_NOW_SEND_SUCCESS || b.broadcast;
|
||||
ok = s_bulk_ok || b.broadcast;
|
||||
}
|
||||
s_bulk_inflight = false;
|
||||
}
|
||||
@@ -210,6 +266,7 @@ void espnow_tx_stats(espnow_stats_t *out)
|
||||
*out = s_stats;
|
||||
}
|
||||
|
||||
#if !CONFIG_IDF_TARGET_LINUX
|
||||
// A link-local IPv6 address on the AP lets the mDNS responder answer the AAAA half of a browser's
|
||||
// lookup; without it the browser waits about a second for an answer that never comes.
|
||||
static esp_netif_t *s_ap_netif;
|
||||
@@ -220,7 +277,22 @@ static void ap_event(void *arg, esp_event_base_t base, int32_t id, void *data)
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t espnow_tx_init(const char *ap_ssid, const char *ap_password)
|
||||
int espnow_tx_ap_clients(char *out, size_t n)
|
||||
{
|
||||
int len = snprintf(out, n, "[");
|
||||
wifi_sta_list_t sl;
|
||||
wifi_sta_mac_ip_list_t ml;
|
||||
if (s_ap_netif && esp_wifi_ap_get_sta_list(&sl) == ESP_OK && esp_wifi_ap_get_sta_list_with_ip(&sl, &ml) == ESP_OK) {
|
||||
for (int i = 0; i < ml.num && len < (int)n - 64; i++) {
|
||||
len += snprintf(out + len, n - len, "%s{\"mac\":\"" MACSTR "\",\"ip\":\"" IPSTR "\"}", i ? "," : "",
|
||||
MAC2STR(ml.sta[i].mac), IP2STR(&ml.sta[i].ip));
|
||||
}
|
||||
}
|
||||
len += snprintf(out + len, n - len, "]");
|
||||
return len;
|
||||
}
|
||||
|
||||
static esp_err_t radio_init(const char *ap_ssid, const char *ap_password)
|
||||
{
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_RETURN_ON_ERROR(esp_wifi_init(&cfg), TAG, "wifi init");
|
||||
@@ -264,6 +336,32 @@ esp_err_t espnow_tx_init(const char *ap_ssid, const char *ap_password)
|
||||
esp_now_peer_info_t bc = { .channel = 0, .ifidx = WIFI_IF_STA, .encrypt = false };
|
||||
memcpy(bc.peer_addr, BROADCAST, 6);
|
||||
ESP_RETURN_ON_ERROR(esp_now_add_peer(&bc), TAG, "broadcast peer");
|
||||
ESP_RETURN_ON_ERROR(esp_now_register_recv_cb(recv_cb), TAG, "recv cb");
|
||||
ESP_LOGI(TAG, "ESP-NOW up on channel %d, %d beat copies", CONFIG_ESPNOW_CHANNEL, CONFIG_BEAT_TX_COPIES);
|
||||
return ESP_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
int espnow_tx_ap_clients(char *out, size_t n)
|
||||
{
|
||||
return snprintf(out, n, "[]");
|
||||
}
|
||||
#endif
|
||||
|
||||
esp_err_t espnow_tx_init(const char *ap_ssid, const char *ap_password)
|
||||
{
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
(void)ap_ssid;
|
||||
(void)ap_password;
|
||||
ESP_RETURN_ON_FALSE(s_bridge[0], ESP_ERR_INVALID_STATE, TAG, "the host build needs a bridge address");
|
||||
#else
|
||||
// The radio still comes up behind a bridge: the access point serves the web UI
|
||||
ESP_RETURN_ON_ERROR(radio_init(ap_ssid, ap_password), TAG, "radio");
|
||||
#endif
|
||||
if (s_bridge[0]) {
|
||||
ESP_RETURN_ON_ERROR(bridge_start(s_bridge, bridge_rx), TAG, "bridge");
|
||||
}
|
||||
|
||||
s_bulk_q = xQueueCreate(BULK_QUEUE_LEN, sizeof(bulk_t));
|
||||
s_bulk_done = xSemaphoreCreateBinary();
|
||||
@@ -274,6 +372,5 @@ esp_err_t espnow_tx_init(const char *ap_ssid, const char *ap_password)
|
||||
xTaskCreate(bulk_task, "espnow_bulk", 4096, NULL, 5, NULL);
|
||||
|
||||
beat_set_callback(on_beat);
|
||||
ESP_LOGI(TAG, "ESP-NOW up on channel %d, %d beat copies", CONFIG_ESPNOW_CHANNEL, CONFIG_BEAT_TX_COPIES);
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -35,6 +35,17 @@ typedef struct {
|
||||
// ap_password (8-63 chars), open when it is NULL or empty.
|
||||
esp_err_t espnow_tx_init(const char *ap_ssid, const char *ap_password);
|
||||
|
||||
// JSON array [{"mac","ip"}] of stations on the access point (DHCP leases); "[]" when it is off. Returns its length.
|
||||
int espnow_tx_ap_clients(char *out, size_t n);
|
||||
|
||||
// Before espnow_tx_init: send every frame to this UDP bridge ("host:port", see bridge.h) instead of
|
||||
// the radio. NULL or "" keeps the radio.
|
||||
void espnow_tx_set_bridge(const char *addr);
|
||||
|
||||
// Frames received from drivers (radio or bridge). Runs in the Wi-Fi or bridge task.
|
||||
typedef void (*espnow_rx_cb_t)(const uint8_t *src_mac, const uint8_t *data, int len);
|
||||
esp_err_t espnow_tx_set_rx_cb(espnow_rx_cb_t cb);
|
||||
|
||||
// Queue a bulk frame (<= 240 bytes). mac NULL broadcasts. Unicast frames are
|
||||
// retried; they never go out within the hold-off window after a beat.
|
||||
esp_err_t espnow_tx_send(const uint8_t *mac, const void *data, size_t len);
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
dependencies:
|
||||
joltwallet/littlefs: "^1.19.0"
|
||||
espressif/mdns: "^1.4.0"
|
||||
joltwallet/littlefs:
|
||||
version: "^1.19.0"
|
||||
rules:
|
||||
- if: "target != linux"
|
||||
espressif/mdns:
|
||||
version: "^1.4.0"
|
||||
rules:
|
||||
- if: "target != linux"
|
||||
+16
-5
@@ -8,6 +8,7 @@
|
||||
#include "esp_log.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_event.h"
|
||||
#include <stdlib.h>
|
||||
#include "esp_netif.h"
|
||||
#include "esp_eth.h"
|
||||
#include "esp_mac.h"
|
||||
@@ -71,7 +72,11 @@ static esp_err_t status_get_handler(httpd_req_t *req)
|
||||
rel_stats_t rs;
|
||||
esp_netif_ip_info_t ip_info = { 0 };
|
||||
uint8_t mac[6] = { 0 };
|
||||
char ip[16], macs[18], buf[1024];
|
||||
char ip[16], macs[18], mask[16], gw[16], clients[400];
|
||||
char *buf = malloc(2048);
|
||||
if (!buf) {
|
||||
return httpd_resp_send_500(req);
|
||||
}
|
||||
beat_get_info(&bi);
|
||||
mic_get_level(&level);
|
||||
espnow_tx_stats(&es);
|
||||
@@ -82,15 +87,18 @@ static esp_err_t status_get_handler(httpd_req_t *req)
|
||||
}
|
||||
snprintf(ip, sizeof(ip), IPSTR, IP2STR(&ip_info.ip));
|
||||
snprintf(macs, sizeof(macs), MACSTR, MAC2STR(mac));
|
||||
snprintf(buf, sizeof(buf),
|
||||
"{\"ap\":%s,\"ip\":\"%s\",\"mac\":\"%s\",\"uptime_s\":%lld,\"reset_reason\":\"%s\",\"free_heap\":%lu,\"free_internal\":%lu,"
|
||||
snprintf(mask, sizeof(mask), IPSTR, IP2STR(&ip_info.netmask));
|
||||
snprintf(gw, sizeof(gw), IPSTR, IP2STR(&ip_info.gw));
|
||||
espnow_tx_ap_clients(clients, sizeof(clients));
|
||||
snprintf(buf, 2048,
|
||||
"{\"ap\":%s,\"eth\":{\"ip\":\"%s\",\"mask\":\"%s\",\"gw\":\"%s\",\"up\":%s},\"ap_ip\":\"192.168.5.1\",\"ap_clients\":%s,\"ip\":\"%s\",\"mac\":\"%s\",\"uptime_s\":%lld,\"reset_reason\":\"%s\",\"free_heap\":%lu,\"free_internal\":%lu,"
|
||||
"\"mic_rms_dbfs\":%.1f,\"mic_peak_dbfs\":%.1f,\"mic_spl_db\":%.1f,"
|
||||
"\"bpm\":%.1f,\"beat_confidence\":%.2f,\"beat_locked\":%s,\"beats\":%lu,\"onsets\":%lu,"
|
||||
"\"meter\":%d,\"bar_beat\":%d,\"bar_sure\":%s,\"downbeats\":%lu,"
|
||||
"\"espnow\":{\"beats_sent\":%lu,\"beat_send_fail\":%lu,\"bulk_sent\":%lu,"
|
||||
"\"bulk_retries\":%lu,\"bulk_failed\":%lu,\"sim_dropped\":%lu},"
|
||||
"\"link\":{\"ver\":%u,\"acked\":%lu,\"ack_timeouts\":%lu,\"retransmits\":%lu,\"resyncs\":%lu,\"digests\":%lu}}",
|
||||
s_ap_on ? "true" : "false", ip, macs, (long long)(esp_timer_get_time() / 1000000), reset_reason(), (unsigned long)esp_get_free_heap_size(),
|
||||
s_ap_on ? "true" : "false", ip, mask, gw, ip_info.ip.addr ? "true" : "false", clients, ip, macs, (long long)(esp_timer_get_time() / 1000000), reset_reason(), (unsigned long)esp_get_free_heap_size(),
|
||||
(unsigned long)heap_caps_get_free_size(MALLOC_CAP_INTERNAL), level.rms_dbfs, level.peak_dbfs, level.spl_db,
|
||||
bi.bpm, bi.confidence, bi.locked ? "true" : "false", (unsigned long)bi.beat_count,
|
||||
(unsigned long)bi.onset_count, bi.meter, bi.bar_beat, bi.bar_sure ? "true" : "false",
|
||||
@@ -100,7 +108,9 @@ static esp_err_t status_get_handler(httpd_req_t *req)
|
||||
(unsigned long)rs.acked, (unsigned long)rs.ack_timeouts, (unsigned long)rs.retransmits,
|
||||
(unsigned long)rs.resyncs, (unsigned long)rs.digests);
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
return httpd_resp_sendstr(req, buf);
|
||||
esp_err_t err = httpd_resp_sendstr(req, buf);
|
||||
free(buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
#define DEFAULT_HOSTNAME "led-controller"
|
||||
@@ -446,6 +456,7 @@ void app_main(void)
|
||||
bool held = ap_button_held();
|
||||
s_ap_on = settings_bool("ap_enabled", true) || held;
|
||||
ESP_LOGI(TAG, "AP %s%s", s_ap_on ? "on" : "off", held ? " (button held)" : "");
|
||||
espnow_tx_set_bridge(settings_str("bridge")); // led-simulator stands in for the radio
|
||||
ESP_ERROR_CHECK(espnow_tx_init(s_ap_on ? (host ? host : DEFAULT_HOSTNAME) : NULL, settings_str("ap_password")));
|
||||
|
||||
// Ethernet is optional: without it the web UI is still served over the AP
|
||||
|
||||
+9
-1
@@ -139,13 +139,21 @@ static esp_err_t write_file(const char *base, const char *code, size_t len)
|
||||
return ok ? ESP_OK : ESP_FAIL;
|
||||
}
|
||||
|
||||
// Patterns the driver has built in (no .py file); the seed presets use them
|
||||
// Definitions used when the pattern collection has none: the driver's built-ins (no .py file, the seed
|
||||
// presets use them) and the animation patterns (field patterns span the rig, see led-driver/src/rig.py)
|
||||
static const struct {
|
||||
const char *name, *json;
|
||||
} BUILTIN[] = {
|
||||
{ "on", "{\"min_delay\":10,\"max_delay\":10000,\"max_colors\":1}" },
|
||||
{ "off", "{\"min_delay\":10,\"max_delay\":10000,\"max_colors\":0}" },
|
||||
{ "rainbow", "{\"n1\":\"Step Rate\",\"min_delay\":10,\"max_delay\":10000,\"max_colors\":0}" },
|
||||
{ "hit", "{\"n1\":\"Attack (ms / ¼ beats)\",\"n2\":\"Hold\",\"n3\":\"Decay\",\"n4\":\"End: 0 background, 1 stay lit\",\"min_delay\":0,\"max_delay\":10000,\"max_colors\":10}" },
|
||||
{ "keyframes", "{\"n1\":\"Easing: 0 linear, 1 in-out, 2 step, 3 in, 4 out\",\"n2\":\"End: 0 last colour, 1 background, 2 off\",\"min_delay\":10,\"max_delay\":60000,\"max_colors\":10}" },
|
||||
{ "sweep", "{\"n1\":\"Direction (degrees)\",\"n2\":\"Band width (‰ of rig)\",\"n3\":\"Trail (‰)\",\"n4\":\"1 = leave lit (wipe)\",\"min_delay\":10,\"max_delay\":60000,\"max_colors\":10}" },
|
||||
{ "ripple", "{\"n1\":\"Ring width (‰)\",\"n2\":\"Rings at once\",\"n3\":\"1 = from the edge in\",\"n4\":\"Fade with distance (‰)\",\"min_delay\":10,\"max_delay\":60000,\"max_colors\":10}" },
|
||||
{ "spin", "{\"n1\":\"Arms\",\"n2\":\"Arm width (degrees)\",\"n3\":\"Tail (degrees)\",\"n4\":\"1 = clockwise\",\"min_delay\":10,\"max_delay\":60000,\"max_colors\":10}" },
|
||||
{ "grow", "{\"n1\":\"From: 0 centre, 1 edges, 2 round, 3 across\",\"n2\":\"Then: 0 stay, 1 empty back, 2 empty on, 3 off\",\"n3\":\"Direction (degrees, from = 3)\",\"min_delay\":10,\"max_delay\":60000,\"max_colors\":10}" },
|
||||
{ "field_wave", "{\"n1\":\"Direction (degrees)\",\"n2\":\"Wavelength (‰ of rig)\",\"n3\":\"Kind: 0 linear, 1 rings, 2 swirl, 3 plasma\",\"min_delay\":10,\"max_delay\":60000,\"max_colors\":10}" },
|
||||
};
|
||||
|
||||
// Stored metadata plus the built-ins and {} for every other pattern file, as {name: {...}}
|
||||
|
||||
+4
-5
@@ -4,7 +4,6 @@
|
||||
#include <string.h>
|
||||
#include "esp_check.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_now.h"
|
||||
#include "esp_random.h"
|
||||
#include "esp_timer.h"
|
||||
#include "bench.h"
|
||||
@@ -292,7 +291,7 @@ void rel_set_announce_cb(rel_announce_cb_t cb)
|
||||
s_announce_cb = cb;
|
||||
}
|
||||
|
||||
static void recv_cb(const esp_now_recv_info_t *info, const uint8_t *data, int len)
|
||||
static void recv_cb(const uint8_t *src_mac, const uint8_t *data, int len)
|
||||
{
|
||||
int64_t now = esp_timer_get_time(); // before anything else: echoes are timing measurements
|
||||
if (len < 3 || len > (int)sizeof(((rx_t *)0)->data)) {
|
||||
@@ -311,11 +310,11 @@ static void recv_cb(const esp_now_recv_info_t *info, const uint8_t *data, int le
|
||||
return;
|
||||
}
|
||||
if (reply && data[1] == M_ECHO) {
|
||||
bench_on_echo(info->src_addr, data[2] | (data[3] << 8), now);
|
||||
bench_on_echo(src_mac, data[2] | (data[3] << 8), now);
|
||||
return;
|
||||
}
|
||||
rx_t rx = { .len = len };
|
||||
memcpy(rx.mac, info->src_addr, 6);
|
||||
memcpy(rx.mac, src_mac, 6);
|
||||
memcpy(rx.data, data, len);
|
||||
xQueueSend(s_rx, &rx, 0); // dropping an ack only costs a retransmit
|
||||
}
|
||||
@@ -445,7 +444,7 @@ esp_err_t rel_init(void)
|
||||
ESP_RETURN_ON_FALSE(s_lock && s_rx, ESP_ERR_NO_MEM, TAG, "alloc");
|
||||
s_seq = esp_random() & 0xFFFF; // drivers remember recent (mac, seq); a restart must not reuse them
|
||||
s_ver = (esp_random() % 0x7FFE) + 1; // new epoch per boot, so a rebooted controller never matches stale drivers
|
||||
ESP_RETURN_ON_ERROR(esp_now_register_recv_cb(recv_cb), TAG, "recv cb");
|
||||
ESP_RETURN_ON_ERROR(espnow_tx_set_rx_cb(recv_cb), TAG, "recv cb");
|
||||
xTaskCreate(rel_task, "rel", 4096, NULL, 6, NULL);
|
||||
ESP_LOGI(TAG, "reliable link up, state version %u", s_ver);
|
||||
return ESP_OK;
|
||||
|
||||
+59
-14
@@ -1,6 +1,7 @@
|
||||
// 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.
|
||||
// one record (a sequence with its presets). Animations are sequences with kind "animation"; a profile
|
||||
// bundle carries the ones its zones and sequence steps fire. Imports get new ids and belong to the importing profile.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -50,6 +51,54 @@ static void sequence_preset_ids(const cJSON *seq, cJSON *set)
|
||||
}
|
||||
}
|
||||
|
||||
// Animations (sequences played once) that steps of seq fire
|
||||
static void sequence_animation_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, "animation_id"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Point a list field (or each step's animation_id) at the new sequence ids
|
||||
static void remap_ids(cJSON *rec, const char *key, const cJSON *smap)
|
||||
{
|
||||
cJSON *list = cJSON_GetObjectItem(rec, key);
|
||||
if (!cJSON_IsArray(list)) {
|
||||
return;
|
||||
}
|
||||
cJSON *mapped = cJSON_CreateArray(), *it;
|
||||
cJSON_ArrayForEach(it, list)
|
||||
{
|
||||
char k[24];
|
||||
if (api_id(it, k, sizeof(k))) {
|
||||
const cJSON *ns = cJSON_GetObjectItem(smap, k);
|
||||
cJSON_AddItemToArray(mapped, cJSON_CreateString(ns ? ns->valuestring : k));
|
||||
}
|
||||
}
|
||||
api_set(rec, key, mapped);
|
||||
}
|
||||
|
||||
static void remap_animation_steps(cJSON *seq, const cJSON *smap)
|
||||
{
|
||||
cJSON *lane, *step;
|
||||
cJSON_ArrayForEach(lane, cJSON_GetObjectItem(seq, "lanes"))
|
||||
{
|
||||
cJSON_ArrayForEach(step, lane)
|
||||
{
|
||||
char k[24];
|
||||
const cJSON *ns = api_id(cJSON_GetObjectItem(step, "animation_id"), k, sizeof(k)) ? cJSON_GetObjectItem(smap, k) : NULL;
|
||||
if (ns) {
|
||||
api_set(step, "animation_id", cJSON_CreateString(ns->valuestring));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool owned_by(const cJSON *rec, const char *profile)
|
||||
{
|
||||
char pid[24];
|
||||
@@ -107,6 +156,7 @@ static cJSON *export_profile(const char *id)
|
||||
cJSON_AddItemToObject(zones, key, cJSON_Duplicate(z, true));
|
||||
zone_preset_ids(z, pset);
|
||||
add_ref(sset, cJSON_GetObjectItem(z, "sequence_ids"));
|
||||
add_ref(sset, cJSON_GetObjectItem(z, "animation_ids"));
|
||||
}
|
||||
const cJSON *it;
|
||||
cJSON_ArrayForEach(it, sset)
|
||||
@@ -115,6 +165,7 @@ static cJSON *export_profile(const char *id)
|
||||
if (s && owned_by(s, id)) {
|
||||
cJSON_AddItemToObject(seqs, it->string, cJSON_Duplicate(s, true));
|
||||
sequence_preset_ids(s, pset);
|
||||
sequence_animation_ids(s, sset); // appended, so this loop reaches them too
|
||||
}
|
||||
}
|
||||
cJSON_ArrayForEach(it, pset)
|
||||
@@ -249,6 +300,11 @@ static const char *import_profile(const cJSON *b, const char *name, char *pid, s
|
||||
api_add("sequence", copy, nid, sizeof(nid));
|
||||
cJSON_AddStringToObject(smap, s->string, nid);
|
||||
}
|
||||
cJSON *imported;
|
||||
cJSON_ArrayForEach(imported, smap)
|
||||
{
|
||||
remap_animation_steps(cJSON_GetObjectItem(store_coll("sequence"), imported->valuestring), smap);
|
||||
}
|
||||
|
||||
const cJSON *zones_in = cJSON_GetObjectItem(b, "zones");
|
||||
const cJSON *order = cJSON_GetObjectItem(src, "zones");
|
||||
@@ -293,19 +349,8 @@ static const char *import_profile(const cJSON *b, const char *name, char *pid, s
|
||||
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);
|
||||
}
|
||||
remap_ids(copy, "sequence_ids", smap);
|
||||
remap_ids(copy, "animation_ids", smap);
|
||||
char nid[16];
|
||||
api_add("zone", copy, nid, sizeof(nid));
|
||||
cJSON_AddItemToArray(zone_ids, cJSON_CreateString(nid));
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -249,6 +249,36 @@ static esp_err_t play_sequence(httpd_req_t *req, const char *id)
|
||||
return api_json(req, "200 OK", o, true);
|
||||
}
|
||||
|
||||
// POST /sequences/<id>/fire {zone_id?, wait?: "now" | "beat" | "downbeat"}: play it once over whatever runs
|
||||
static esp_err_t fire_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] = "", wait[12] = "now";
|
||||
api_id(cJSON_GetObjectItem(body, "zone_id"), zone, sizeof(zone));
|
||||
const cJSON *w = cJSON_GetObjectItem(body, "wait");
|
||||
if (cJSON_IsString(w)) {
|
||||
snprintf(wait, sizeof(wait), "%s", w->valuestring);
|
||||
}
|
||||
cJSON_Delete(body);
|
||||
const char *msg;
|
||||
esp_err_t err = seqplay_fire(id, zone, profile, wait, &msg);
|
||||
if (err != ESP_OK) {
|
||||
return api_error(req, err == ESP_ERR_NOT_FOUND ? "404 Not Found" : "400 Bad Request",
|
||||
msg ? msg : "animation not found");
|
||||
}
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddTrueToObject(o, "ok");
|
||||
cJSON_AddStringToObject(o, "wait", wait);
|
||||
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");
|
||||
@@ -291,6 +321,17 @@ bool sequences_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
|
||||
*ret = api_json(req, "200 OK", o, true);
|
||||
return true;
|
||||
}
|
||||
if (n == 3 && method == HTTP_POST && !strcmp(seg[1], "overlay") && !strcmp(seg[2], "stop")) {
|
||||
seqplay_overlay_stop();
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddTrueToObject(o, "ok");
|
||||
*ret = api_json(req, "200 OK", o, true);
|
||||
return true;
|
||||
}
|
||||
if (n == 3 && method == HTTP_POST && !strcmp(seg[2], "fire")) {
|
||||
*ret = fire_sequence(req, seg[1]);
|
||||
return true;
|
||||
}
|
||||
if (n == 3 && method == HTTP_POST && !strcmp(seg[2], "play")) {
|
||||
*ret = play_sequence(req, seg[1]);
|
||||
return true;
|
||||
|
||||
+961
-283
File diff suppressed because it is too large.
Load diff
+16
-4
@@ -4,9 +4,9 @@
|
||||
#include "cJSON.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
// Sequence playback, as main's util/sequence_playback.py: one sequence plays at a time in a zone.
|
||||
// Each lane is a list of {preset_id, beats} steps sent to the lane's groups; a step lasts its number
|
||||
// of beats. Beats come from the mic's beat detector while audio timing is on and the tempo is locked,
|
||||
// Sequence playback, as main's util/sequence_playback.py: one sequence plays at a time in a zone, and
|
||||
// one animation (a sequence played once) can play over it. Each lane is a list of steps sent to the
|
||||
// lane's targets; a step lasts its beats (beat lanes, counted in quarter beats) or seconds (time lanes). Beats come from the mic's beat detector while audio timing is on and the tempo is locked,
|
||||
// otherwise from a clock at settings.audio_simulated_bpm (default 120).
|
||||
|
||||
// Start the beat task (after beat_start)
|
||||
@@ -18,7 +18,19 @@ esp_err_t seqplay_init(void);
|
||||
// Call with the store locked.
|
||||
esp_err_t seqplay_start(const char *seq_id, const char *zone_id, const char *profile_id, const char **msg);
|
||||
|
||||
// Stop; with clear, the zone's drivers drop their presets
|
||||
// Play sequence seq_id once as an animation over whatever runs in the zone (zone_id NULL or "": the
|
||||
// playing sequence's zone). wait: "now" (default), "beat" or "downbeat". The sequence underneath keeps
|
||||
// time and skips the drivers the animation lights; when it ends they go back to the sequence's current
|
||||
// step, or the zone's last preset. A new animation replaces the one playing. Call with the store locked.
|
||||
esp_err_t seqplay_fire(const char *seq_id, const char *zone_id, const char *profile_id, const char *wait,
|
||||
const char **msg);
|
||||
// End the animation now and restore its drivers
|
||||
void seqplay_overlay_stop(void);
|
||||
// How long sequence seq_id plays once: *beats and *secs (each converted from the other at the current
|
||||
// tempo); unchanged when it has no steps. Call with the store locked.
|
||||
void seqplay_length(const char *seq_id, double *beats, double *secs);
|
||||
|
||||
// Stop the sequence and any animation; with clear, the zone's drivers drop their presets
|
||||
void seqplay_stop(bool clear);
|
||||
// Stop when seq_id is the one playing (it was edited or deleted)
|
||||
void seqplay_stop_if(const char *seq_id);
|
||||
|
||||
+22
-8
@@ -12,6 +12,20 @@
|
||||
|
||||
static const char *TAG = "store";
|
||||
|
||||
// Where the collections live: the littlefs partition, or on the host build a directory
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
#include <stdlib.h>
|
||||
static const char *db_root(void)
|
||||
{
|
||||
const char *d = getenv("LED_SIM_DB");
|
||||
return d && d[0] ? d : "/db";
|
||||
}
|
||||
#define PATH_LEN 256
|
||||
#else
|
||||
#define db_root() "/db"
|
||||
#define PATH_LEN 48
|
||||
#endif
|
||||
|
||||
static const char *const NAMES[] = {
|
||||
"device", "zone", "preset", "profile", "palette", "pattern",
|
||||
"sequence", "scene", "group", "output", "settings",
|
||||
@@ -44,13 +58,13 @@ static esp_err_t mount(const char *label, const char *base)
|
||||
|
||||
static cJSON *load_file(const char *name)
|
||||
{
|
||||
char path[48];
|
||||
snprintf(path, sizeof(path), "/db/%s.json", name);
|
||||
char path[PATH_LEN];
|
||||
snprintf(path, sizeof(path), "%s/%s.json", db_root(), name);
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) {
|
||||
// store_save removes the old file before renaming the finished .tmp, so a restart between the two leaves only .tmp
|
||||
char tmp[48];
|
||||
snprintf(tmp, sizeof(tmp), "/db/%s.tmp", name);
|
||||
char tmp[PATH_LEN];
|
||||
snprintf(tmp, sizeof(tmp), "%s/%s.tmp", db_root(), name);
|
||||
if (rename(tmp, path) == 0) {
|
||||
ESP_LOGW(TAG, "%s: recovered from %s", path, tmp);
|
||||
f = fopen(path, "rb");
|
||||
@@ -92,7 +106,7 @@ esp_err_t store_init(void)
|
||||
cJSON_InitHooks(&hooks);
|
||||
s_lock = xSemaphoreCreateRecursiveMutex();
|
||||
ESP_RETURN_ON_FALSE(s_lock, ESP_ERR_NO_MEM, TAG, "mutex");
|
||||
ESP_RETURN_ON_ERROR(mount("db", "/db"), TAG, "mount db");
|
||||
ESP_RETURN_ON_ERROR(mount("db", db_root()), TAG, "mount db");
|
||||
for (size_t i = 0; i < N_COLL; i++) {
|
||||
s_coll[i] = load_file(NAMES[i]);
|
||||
ESP_LOGI(TAG, "%s: %d records", NAMES[i], cJSON_GetArraySize(s_coll[i]));
|
||||
@@ -118,9 +132,9 @@ esp_err_t store_save(const char *name)
|
||||
char *json = cJSON_PrintUnformatted(s_coll[i]);
|
||||
ESP_RETURN_ON_FALSE(json, ESP_ERR_NO_MEM, TAG, "print");
|
||||
|
||||
char tmp[48], path[48];
|
||||
snprintf(tmp, sizeof(tmp), "/db/%s.tmp", name);
|
||||
snprintf(path, sizeof(path), "/db/%s.json", name);
|
||||
char tmp[PATH_LEN], path[PATH_LEN];
|
||||
snprintf(tmp, sizeof(tmp), "%s/%s.tmp", db_root(), name);
|
||||
snprintf(path, sizeof(path), "%s/%s.json", db_root(), name);
|
||||
FILE *f = fopen(tmp, "wb");
|
||||
esp_err_t ret = ESP_FAIL;
|
||||
if (f) {
|
||||
|
||||
Reference in new issue
Block a user