feat: ESP-NOW link, beat sync and control messages in the C++ driver
- espnow_link, ctrl_msg and beat_sync: the reliable link, envelopes and beat-synced timing. - ctrl_msg: accept an envelope with the trailing save byte. - patterns_frame() and espnow_link_poll() split out of their tasks, so the WebAssembly simulator can drive one frame and one link pass from a timer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
94e300f6ad
commit
24dd5ffb15
30 files changed
+2159
-158
No files matched your search
+1
-1
@@ -1,5 +1,5 @@
|
||||
idf_component_register(SRCS "main.cpp" "ws2812.cpp" "net.cpp" "settings.cpp" "web.cpp" "patterns.cpp" "presets.cpp"
|
||||
"effects_util.cpp" "ws_client.cpp" "effects_a.cpp" "effects_b.cpp" "effects_rig.cpp" "ota.c" "ddp.cpp" "usb_cfg.cpp"
|
||||
"effects_util.cpp" "ws_client.cpp" "effects_a.cpp" "effects_b.cpp" "effects_rig.cpp" "ota.c" "ddp.cpp" "usb_cfg.cpp" "beat_sync.cpp" "ctrl_msg.cpp" "espnow_link.cpp"
|
||||
PRIV_REQUIRES esp_driver_rmt esp_driver_gpio esp_wifi esp_netif esp_event
|
||||
esp_timer nvs_flash esp_http_server esp_http_client cjson mdns esp_hw_support app_update esp_app_format lwip esp_driver_usb_serial_jtag
|
||||
INCLUDE_DIRS "."
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include "beat_sync.h"
|
||||
|
||||
#include "esp_timer.h"
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr uint8_t kMagic = 0x4C;
|
||||
constexpr uint8_t kBeat = 0x25;
|
||||
constexpr uint32_t kDefaultPeriodMs = 500;
|
||||
constexpr uint32_t kMinPeriodMs = 30;
|
||||
constexpr uint32_t kMaxPeriodMs = 4000;
|
||||
constexpr int kLostPeriods = 4; // no beat for this many periods: the controller has stopped
|
||||
|
||||
volatile uint16_t s_last_idx;
|
||||
volatile uint32_t s_period_ms;
|
||||
volatile int64_t s_last_us;
|
||||
volatile bool s_have;
|
||||
|
||||
} // namespace
|
||||
|
||||
void beat_on_frame(const uint8_t *data, size_t len)
|
||||
{
|
||||
if (len != 8 || data[0] != kMagic || data[1] != kBeat) {
|
||||
return;
|
||||
}
|
||||
uint16_t idx = (uint16_t)(data[3] | (data[4] << 8));
|
||||
uint32_t period = (uint32_t)(data[5] | (data[6] << 8));
|
||||
if (s_have && idx == s_last_idx) {
|
||||
return; // the second copy of a beat already taken
|
||||
}
|
||||
if (period < kMinPeriodMs || period > kMaxPeriodMs) {
|
||||
return;
|
||||
}
|
||||
s_last_idx = idx;
|
||||
s_period_ms = period;
|
||||
s_last_us = esp_timer_get_time();
|
||||
s_have = true;
|
||||
}
|
||||
|
||||
uint32_t beat_period_ms(void)
|
||||
{
|
||||
if (!s_have) {
|
||||
return kDefaultPeriodMs;
|
||||
}
|
||||
int64_t age_us = esp_timer_get_time() - s_last_us;
|
||||
if (age_us > (int64_t)s_period_ms * kLostPeriods * 1000) {
|
||||
return kDefaultPeriodMs;
|
||||
}
|
||||
return s_period_ms;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Beat frames from led-controller (ESP-NOW [4C][25][flags][idx u16][period u16][fire_in]). Only the tempo
|
||||
* is used: beat-timed presets (tb) take their quarter-beat durations from it.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
/** @brief Note a received frame. Called from the ESP-NOW receive callback, so it does no more than store the values. */
|
||||
void beat_on_frame(const uint8_t *data, size_t len);
|
||||
|
||||
/** @brief The controller's beat period in ms while beats are arriving, else 500 (120 BPM). */
|
||||
uint32_t beat_period_ms(void);
|
||||
@@ -0,0 +1,669 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include "ctrl_msg.h"
|
||||
|
||||
#include <cctype>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_mac.h"
|
||||
#include "nvs.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "patterns.h"
|
||||
#include "presets.h"
|
||||
#include "settings.h"
|
||||
|
||||
static const char *TAG = "ctrl_msg";
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr uint8_t kMagic = 0x4C;
|
||||
constexpr uint8_t kGroups = 0x02;
|
||||
constexpr uint8_t kCmd = 0x03;
|
||||
constexpr uint8_t kGroupCmd = 0x04;
|
||||
constexpr size_t kGroupsMax = 16;
|
||||
constexpr size_t kGroupIdMax = 24;
|
||||
constexpr size_t kEnvHdr = 5;
|
||||
|
||||
char s_groups[kGroupsMax][kGroupIdMax];
|
||||
size_t s_ngroups;
|
||||
char s_pending[32]; // a select for a preset not saved yet: applied when that preset arrives
|
||||
|
||||
// Copy n bytes as a NUL-terminated string
|
||||
void cstr(char *dst, size_t cap, const uint8_t *src, size_t n)
|
||||
{
|
||||
size_t k = n < cap - 1 ? n : cap - 1;
|
||||
memcpy(dst, src, k);
|
||||
dst[k] = '\0';
|
||||
}
|
||||
|
||||
// Bounds-checked reader over an envelope blob
|
||||
struct Rd {
|
||||
const uint8_t *p;
|
||||
size_t n;
|
||||
size_t off;
|
||||
bool ok;
|
||||
bool bytes(size_t k, const uint8_t **out)
|
||||
{
|
||||
if (!ok || k > n - off) {
|
||||
ok = false;
|
||||
return false;
|
||||
}
|
||||
*out = p + off;
|
||||
off += k;
|
||||
return true;
|
||||
}
|
||||
bool u8(uint8_t *v)
|
||||
{
|
||||
const uint8_t *b;
|
||||
if (!bytes(1, &b)) return false;
|
||||
*v = b[0];
|
||||
return true;
|
||||
}
|
||||
bool u16(uint16_t *v)
|
||||
{
|
||||
const uint8_t *b;
|
||||
if (!bytes(2, &b)) return false;
|
||||
*v = (uint16_t)(b[0] | (b[1] << 8));
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
// Presets blob: count, then per record name, pattern, colours, delay, brightness, auto, n1..n6 and (v3) an
|
||||
// extension block with fade (key 1) and beat timing (key 2)
|
||||
cJSON *decode_presets(Rd &r, bool v3)
|
||||
{
|
||||
uint8_t count;
|
||||
if (!r.u8(&count)) return nullptr;
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
for (int i = 0; i < count; i++) {
|
||||
uint8_t nl, pl, nc;
|
||||
const uint8_t *name, *pat;
|
||||
if (!r.u8(&nl) || !r.bytes(nl, &name) || !r.u8(&pl) || !r.bytes(pl, &pat) || !r.u8(&nc)) {
|
||||
goto fail;
|
||||
}
|
||||
{
|
||||
char nm[64], pn[64];
|
||||
cstr(nm, sizeof(nm), name, nl);
|
||||
cstr(pn, sizeof(pn), pat, pl);
|
||||
cJSON *rec = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(rec, "pattern", pn);
|
||||
cJSON *cols = cJSON_AddArrayToObject(rec, "colors");
|
||||
for (int c = 0; c < nc; c++) {
|
||||
const uint8_t *rgb;
|
||||
if (!r.bytes(3, &rgb)) {
|
||||
cJSON_Delete(rec);
|
||||
goto fail;
|
||||
}
|
||||
int v[3] = { rgb[0], rgb[1], rgb[2] };
|
||||
cJSON_AddItemToArray(cols, cJSON_CreateIntArray(v, 3));
|
||||
}
|
||||
uint16_t delay, nn[6];
|
||||
uint8_t br, au;
|
||||
bool ok = r.u16(&delay) && r.u8(&br) && r.u8(&au);
|
||||
for (int k = 0; ok && k < 6; k++) {
|
||||
ok = r.u16(&nn[k]);
|
||||
}
|
||||
if (!ok) {
|
||||
cJSON_Delete(rec);
|
||||
goto fail;
|
||||
}
|
||||
cJSON_AddNumberToObject(rec, "delay", delay);
|
||||
cJSON_AddNumberToObject(rec, "brightness", br);
|
||||
cJSON_AddBoolToObject(rec, "auto", au != 0);
|
||||
for (int k = 0; k < 6; k++) {
|
||||
char key[4] = { 'n', (char)('1' + k), '\0' };
|
||||
cJSON_AddNumberToObject(rec, key, (int16_t)nn[k]);
|
||||
}
|
||||
if (v3) {
|
||||
uint8_t ext_len;
|
||||
const uint8_t *ext;
|
||||
if (!r.u8(&ext_len) || !r.bytes(ext_len, &ext)) {
|
||||
cJSON_Delete(rec);
|
||||
goto fail;
|
||||
}
|
||||
Rd e = { ext, ext_len, 0, true };
|
||||
while (e.off < e.n) {
|
||||
uint8_t key, len;
|
||||
const uint8_t *val;
|
||||
if (!e.u8(&key) || !e.u8(&len) || !e.bytes(len, &val)) {
|
||||
break;
|
||||
}
|
||||
if (key == 1 && len == 2) {
|
||||
cJSON_AddNumberToObject(rec, "fade", val[0] | (val[1] << 8));
|
||||
} else if (key == 2 && len == 1) {
|
||||
cJSON_AddBoolToObject(rec, "beat_time", val[0] != 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
cJSON_AddItemToObject(o, nm, rec);
|
||||
}
|
||||
}
|
||||
return o;
|
||||
fail:
|
||||
cJSON_Delete(o);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Select blob: per device, its preset name and an optional step
|
||||
cJSON *decode_select(Rd &r)
|
||||
{
|
||||
uint8_t count;
|
||||
if (!r.u8(&count)) return nullptr;
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
for (int i = 0; i < count; i++) {
|
||||
uint8_t dl, pl, has;
|
||||
const uint8_t *dev, *pname;
|
||||
if (!r.u8(&dl) || !r.bytes(dl, &dev) || !r.u8(&pl) || !r.bytes(pl, &pname) || !r.u8(&has)) {
|
||||
cJSON_Delete(o);
|
||||
return nullptr;
|
||||
}
|
||||
char dn[64], pn[64];
|
||||
cstr(dn, sizeof(dn), dev, dl);
|
||||
cstr(pn, sizeof(pn), pname, pl);
|
||||
cJSON *arr = cJSON_CreateArray();
|
||||
cJSON_AddItemToArray(arr, cJSON_CreateString(pn));
|
||||
if (has) {
|
||||
uint16_t step;
|
||||
if (!r.u16(&step)) {
|
||||
cJSON_Delete(arr);
|
||||
cJSON_Delete(o);
|
||||
return nullptr;
|
||||
}
|
||||
cJSON_AddItemToArray(arr, cJSON_CreateNumber(step));
|
||||
}
|
||||
cJSON_AddItemToObject(o, dn, arr);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
// Default blob: the startup preset name and the devices it applies to
|
||||
bool decode_default(Rd &r, cJSON *o)
|
||||
{
|
||||
uint8_t nl, nt;
|
||||
const uint8_t *name;
|
||||
if (!r.u8(&nl) || !r.bytes(nl, &name)) return false;
|
||||
char dn[64];
|
||||
cstr(dn, sizeof(dn), name, nl);
|
||||
cJSON_AddStringToObject(o, "default", dn);
|
||||
if (!r.u8(&nt)) return false;
|
||||
cJSON *targets = cJSON_AddArrayToObject(o, "targets");
|
||||
for (int i = 0; i < nt; i++) {
|
||||
uint8_t tl;
|
||||
const uint8_t *t;
|
||||
if (!r.u8(&tl) || !r.bytes(tl, &t)) return false;
|
||||
char tn[64];
|
||||
cstr(tn, sizeof(tn), t, tl);
|
||||
cJSON_AddItemToArray(targets, cJSON_CreateString(tn));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// A v2/v3 envelope as the v1 JSON object the MicroPython driver works from
|
||||
cJSON *env_to_json(const uint8_t *b, size_t n)
|
||||
{
|
||||
if (n < kEnvHdr || (b[0] != 2 && b[0] != 3)) return nullptr;
|
||||
size_t lp = b[2], ls = b[3], ld = b[4];
|
||||
// CMD and GROUP_CMD frames may end with the controller's save byte after the envelope: ignored, as settings
|
||||
// are saved on every change
|
||||
size_t want = kEnvHdr + lp + ls + ld;
|
||||
if (n != want && n != want + 1) return nullptr;
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(o, "v", "1");
|
||||
uint8_t w = b[1];
|
||||
if (w < 128) { // device brightness, 0-127 on the wire
|
||||
int v = (w * 255) / 127;
|
||||
cJSON_AddNumberToObject(o, "b", v > 255 ? 255 : v);
|
||||
}
|
||||
Rd p = { b + kEnvHdr, lp, 0, true };
|
||||
Rd s = { b + kEnvHdr + lp, ls, 0, true };
|
||||
Rd d = { b + kEnvHdr + lp + ls, ld, 0, true };
|
||||
if (lp) {
|
||||
cJSON *pr = decode_presets(p, b[0] == 3);
|
||||
if (!pr) goto fail;
|
||||
cJSON_AddItemToObject(o, "presets", pr);
|
||||
}
|
||||
if (ls) {
|
||||
cJSON *sel = decode_select(s);
|
||||
if (!sel) goto fail;
|
||||
cJSON_AddItemToObject(o, "select", sel);
|
||||
}
|
||||
if (ld && !decode_default(d, o)) goto fail;
|
||||
return o;
|
||||
fail:
|
||||
cJSON_Delete(o);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// This driver's MAC as 12 lowercase hex digits, the way the controller names a device
|
||||
void own_mac_hex(char out[13])
|
||||
{
|
||||
uint8_t mac[6];
|
||||
esp_read_mac(mac, ESP_MAC_WIFI_STA);
|
||||
snprintf(out, 13, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
}
|
||||
|
||||
// A select or default names this driver if it names its hostname or its MAC (any separators), or "*"
|
||||
bool names_me(const char *name)
|
||||
{
|
||||
if (strcmp(name, "*") == 0) {
|
||||
return true;
|
||||
}
|
||||
net_cfg_t n;
|
||||
settings_load_net(&n);
|
||||
if (strcmp(name, n.hostname) == 0) {
|
||||
return true;
|
||||
}
|
||||
char mine[13], hex[13];
|
||||
own_mac_hex(mine);
|
||||
size_t k = 0;
|
||||
for (const char *c = name; *c; c++) {
|
||||
if (isxdigit((unsigned char)*c)) {
|
||||
if (k < 12) hex[k] = (char)tolower((unsigned char)*c);
|
||||
k++;
|
||||
} else if (*c != ':' && *c != '-') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
hex[12] = '\0';
|
||||
return k == 12 && strcmp(hex, mine) == 0;
|
||||
}
|
||||
|
||||
bool in_group(const char *gid)
|
||||
{
|
||||
for (size_t i = 0; i < s_ngroups; i++) {
|
||||
if (strcmp(s_groups[i], gid) == 0) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void save_groups(void)
|
||||
{
|
||||
char buf[kGroupsMax * (kGroupIdMax + 1)] = "";
|
||||
for (size_t i = 0; i < s_ngroups; i++) {
|
||||
if (i) strncat(buf, "\n", sizeof(buf) - strlen(buf) - 1);
|
||||
strncat(buf, s_groups[i], sizeof(buf) - strlen(buf) - 1);
|
||||
}
|
||||
nvs_handle_t h;
|
||||
if (nvs_open("espnow", NVS_READWRITE, &h) == ESP_OK) {
|
||||
nvs_set_str(h, "groups", buf);
|
||||
nvs_commit(h);
|
||||
nvs_close(h);
|
||||
}
|
||||
}
|
||||
|
||||
// GROUPS: [4C][02][count] then per id [len][id]. The list replaces the stored one.
|
||||
void set_groups(const uint8_t *data, size_t len)
|
||||
{
|
||||
if (len < 3) return;
|
||||
size_t count = data[2], off = 3;
|
||||
s_ngroups = 0;
|
||||
for (size_t i = 0; i < count && off < len; i++) {
|
||||
size_t gl = data[off++];
|
||||
if (gl == 0 || off + gl > len) break;
|
||||
if (gl < kGroupIdMax && s_ngroups < kGroupsMax) {
|
||||
cstr(s_groups[s_ngroups], kGroupIdMax, data + off, gl);
|
||||
s_ngroups++;
|
||||
}
|
||||
off += gl;
|
||||
}
|
||||
save_groups();
|
||||
}
|
||||
|
||||
// The device settings in a device_config object, applied to the strip and the saved network settings
|
||||
void apply_device_config(const cJSON *dc)
|
||||
{
|
||||
if (!cJSON_IsObject(dc)) return;
|
||||
const cJSON *v = cJSON_GetObjectItem(dc, "name");
|
||||
if (cJSON_IsString(v) && v->valuestring[0]) {
|
||||
net_cfg_t n;
|
||||
settings_load_net(&n);
|
||||
cstr(n.hostname, sizeof(n.hostname), (const uint8_t *)v->valuestring, strlen(v->valuestring));
|
||||
settings_save_net(&n);
|
||||
}
|
||||
hw_cfg_t hw;
|
||||
settings_load_hw(&hw);
|
||||
bool changed = false;
|
||||
v = cJSON_GetObjectItem(dc, "num_leds");
|
||||
if (cJSON_IsNumber(v) && v->valueint >= 1 && v->valueint <= 512) {
|
||||
hw.num_leds = v->valueint;
|
||||
changed = true;
|
||||
}
|
||||
v = cJSON_GetObjectItem(dc, "max_brightness");
|
||||
if (cJSON_IsNumber(v) && v->valueint >= 0 && v->valueint <= 255) {
|
||||
hw.max_brightness = (uint8_t)v->valueint;
|
||||
changed = true;
|
||||
}
|
||||
v = cJSON_GetObjectItem(dc, "color_order");
|
||||
if (cJSON_IsString(v)) {
|
||||
static const char *orders[] = { "rgb", "rbg", "grb", "gbr", "brg", "bgr" };
|
||||
for (const char *o : orders) {
|
||||
if (strcmp(v->valuestring, o) == 0) {
|
||||
strncpy(hw.order, o, sizeof(hw.order) - 1);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (changed) {
|
||||
settings_save_hw(&hw);
|
||||
patterns_set_hardware(hw);
|
||||
}
|
||||
v = cJSON_GetObjectItem(dc, "startup_mode");
|
||||
if (cJSON_IsString(v) && strcmp(v->valuestring, "off") == 0) {
|
||||
presets_set_startup("");
|
||||
}
|
||||
// "reversed" is accepted and ignored: the C++ driver does not reverse its strip yet
|
||||
}
|
||||
|
||||
void clear_all_presets(void)
|
||||
{
|
||||
while (presets_count() > 0) {
|
||||
char name[32];
|
||||
snprintf(name, sizeof(name), "%s", presets_at(0)->name);
|
||||
presets_delete(name);
|
||||
}
|
||||
}
|
||||
|
||||
// Save each preset by name. Missing fields keep the pattern's defaults.
|
||||
void apply_presets(const cJSON *presets)
|
||||
{
|
||||
const cJSON *rec;
|
||||
cJSON_ArrayForEach(rec, presets)
|
||||
{
|
||||
const cJSON *pat = cJSON_GetObjectItem(rec, "pattern");
|
||||
int idx = cJSON_IsString(pat) ? patterns_index(pat->valuestring) : -1;
|
||||
if (idx < 0) {
|
||||
ESP_LOGW(TAG, "preset %s: unknown pattern", rec->string);
|
||||
continue;
|
||||
}
|
||||
preset_t p;
|
||||
preset_defaults(&p, idx);
|
||||
snprintf(p.name, sizeof(p.name), "%s", rec->string);
|
||||
if (preset_from_json(rec, &p)) {
|
||||
presets_save(&p);
|
||||
}
|
||||
}
|
||||
if (s_pending[0] && presets_select(s_pending) == ESP_OK) {
|
||||
s_pending[0] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
// The select entry for this driver: a device key that names it, else the only entry
|
||||
const cJSON *pick_select(const cJSON *sel)
|
||||
{
|
||||
if (cJSON_IsString(sel)) {
|
||||
return sel;
|
||||
}
|
||||
if (cJSON_IsArray(sel)) {
|
||||
return cJSON_GetArrayItem(sel, 0);
|
||||
}
|
||||
if (!cJSON_IsObject(sel)) {
|
||||
return nullptr;
|
||||
}
|
||||
const cJSON *e;
|
||||
cJSON_ArrayForEach(e, sel)
|
||||
{
|
||||
if (names_me(e->string)) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
return cJSON_GetArraySize(sel) == 1 ? sel->child : nullptr;
|
||||
}
|
||||
|
||||
void apply_select(const cJSON *sel)
|
||||
{
|
||||
const cJSON *e = pick_select(sel);
|
||||
if (!e) return;
|
||||
// an entry is [name, step] or a bare name; step is not used by this driver
|
||||
const cJSON *name = cJSON_IsArray(e) ? cJSON_GetArrayItem(e, 0) : e;
|
||||
if (!cJSON_IsString(name) || !name->valuestring[0]) return;
|
||||
if (presets_select(name->valuestring) == ESP_OK) {
|
||||
return;
|
||||
}
|
||||
// "on" and "off" are the built-in patterns (the controller's identify ends with "off"): select them
|
||||
if (strcmp(name->valuestring, "on") == 0 || strcmp(name->valuestring, "off") == 0) {
|
||||
preset_t p;
|
||||
preset_defaults(&p, patterns_index(name->valuestring));
|
||||
snprintf(p.name, sizeof(p.name), "%s", name->valuestring);
|
||||
patterns_select(p);
|
||||
return;
|
||||
}
|
||||
snprintf(s_pending, sizeof(s_pending), "%s", name->valuestring); // preset not here yet
|
||||
}
|
||||
|
||||
// A default applies when there are no targets or this driver is one of them
|
||||
void apply_default(const cJSON *d)
|
||||
{
|
||||
const cJSON *def = cJSON_GetObjectItem(d, "default");
|
||||
if (!cJSON_IsString(def)) return;
|
||||
const cJSON *targets = cJSON_GetObjectItem(d, "targets");
|
||||
if (cJSON_IsArray(targets) && cJSON_GetArraySize(targets) > 0) {
|
||||
bool hit = false;
|
||||
const cJSON *t;
|
||||
cJSON_ArrayForEach(t, targets)
|
||||
{
|
||||
if (cJSON_IsString(t) && names_me(t->valuestring)) hit = true;
|
||||
}
|
||||
if (!hit) return;
|
||||
}
|
||||
presets_set_startup(def->valuestring);
|
||||
}
|
||||
|
||||
// The controller's JSON path shortens keys (engine.c compact()); these map them back to the long names
|
||||
struct KeyName {
|
||||
const char *shortk;
|
||||
const char *longk;
|
||||
};
|
||||
constexpr KeyName kTopKeys[] = {
|
||||
{"p", "presets"}, {"s", "select"}, {"g", "groups"}, {"sg", "set_groups"},
|
||||
{"sv", "save"}, {"df", "default"}, {"dc", "device_config"}, {"cp", "clear_presets"},
|
||||
{"mf", "manifest"},
|
||||
};
|
||||
constexpr KeyName kPresetKeys[] = {
|
||||
{"p", "pattern"}, {"c", "colors"}, {"d", "delay"}, {"b", "brightness"},
|
||||
{"a", "auto"}, {"bg", "background"}, {"f", "fade"}, {"tb", "beat_time"},
|
||||
};
|
||||
|
||||
const char *long_name(const KeyName *map, size_t n, const char *k)
|
||||
{
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
if (strcmp(map[i].shortk, k) == 0) return map[i].longk;
|
||||
}
|
||||
return k;
|
||||
}
|
||||
|
||||
// A copy of one preset object with long key names
|
||||
cJSON *expand_preset(const cJSON *src)
|
||||
{
|
||||
cJSON *out = cJSON_CreateObject();
|
||||
const cJSON *it;
|
||||
cJSON_ArrayForEach(it, src)
|
||||
{
|
||||
const char *k = long_name(kPresetKeys, sizeof(kPresetKeys) / sizeof(kPresetKeys[0]), it->string);
|
||||
if (!cJSON_HasObjectItem(out, k)) cJSON_AddItemToObject(out, k, cJSON_Duplicate(it, true));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// A copy of a v1 message with long key names (its presets too)
|
||||
cJSON *expand_v1(const cJSON *src)
|
||||
{
|
||||
cJSON *out = cJSON_CreateObject();
|
||||
const cJSON *it;
|
||||
cJSON_ArrayForEach(it, src)
|
||||
{
|
||||
const char *k = long_name(kTopKeys, sizeof(kTopKeys) / sizeof(kTopKeys[0]), it->string);
|
||||
if (cJSON_HasObjectItem(out, k)) continue;
|
||||
if (strcmp(k, "presets") == 0 && cJSON_IsObject(it)) {
|
||||
cJSON *ps = cJSON_AddObjectToObject(out, "presets");
|
||||
const cJSON *p;
|
||||
cJSON_ArrayForEach(p, it) { cJSON_AddItemToObject(ps, p->string, expand_preset(p)); }
|
||||
} else {
|
||||
cJSON_AddItemToObject(out, k, cJSON_Duplicate(it, true));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// A JSON message with groups applies only when this driver is in one of them (no groups: everyone)
|
||||
bool json_for_me(const cJSON *o)
|
||||
{
|
||||
const cJSON *groups = cJSON_GetObjectItem(o, "groups");
|
||||
if (!cJSON_IsArray(groups) || cJSON_GetArraySize(groups) == 0) return true;
|
||||
const cJSON *g;
|
||||
cJSON_ArrayForEach(g, groups)
|
||||
{
|
||||
if (cJSON_IsString(g) && in_group(g->valuestring)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// One v1 object, in the order controller_messages.py applies them
|
||||
void apply_v1(const cJSON *d)
|
||||
{
|
||||
const cJSON *v = cJSON_GetObjectItem(d, "v");
|
||||
if (!cJSON_IsString(v) || strcmp(v->valuestring, "1") != 0) {
|
||||
return;
|
||||
}
|
||||
apply_device_config(cJSON_GetObjectItem(d, "device_config"));
|
||||
v = cJSON_GetObjectItem(d, "b");
|
||||
if (cJSON_IsNumber(v)) {
|
||||
int b = v->valueint < 0 ? 0 : (v->valueint > 255 ? 255 : v->valueint);
|
||||
patterns_set_global((uint8_t)b);
|
||||
}
|
||||
if (cJSON_HasObjectItem(d, "clear_presets")) {
|
||||
clear_all_presets();
|
||||
}
|
||||
const cJSON *presets = cJSON_GetObjectItem(d, "presets");
|
||||
if (cJSON_IsObject(presets)) {
|
||||
apply_presets(presets);
|
||||
}
|
||||
const cJSON *sel = cJSON_GetObjectItem(d, "select");
|
||||
if (sel) {
|
||||
apply_select(sel);
|
||||
}
|
||||
apply_default(d);
|
||||
}
|
||||
|
||||
// A v2/v3 envelope: applied as a v1 object
|
||||
void handle_env(const uint8_t *env, size_t n)
|
||||
{
|
||||
cJSON *o = env_to_json(env, n);
|
||||
if (!o) {
|
||||
ESP_LOGW(TAG, "bad envelope, %u bytes", (unsigned)n);
|
||||
return;
|
||||
}
|
||||
apply_v1(o);
|
||||
cJSON_Delete(o);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void ctrl_init(void)
|
||||
{
|
||||
nvs_handle_t h;
|
||||
if (nvs_open("espnow", NVS_READONLY, &h) != ESP_OK) {
|
||||
return;
|
||||
}
|
||||
char buf[kGroupsMax * (kGroupIdMax + 1)];
|
||||
size_t len = sizeof(buf);
|
||||
if (nvs_get_str(h, "groups", buf, &len) == ESP_OK) {
|
||||
s_ngroups = 0;
|
||||
char *save = nullptr;
|
||||
for (char *tok = strtok_r(buf, "\n", &save); tok && s_ngroups < kGroupsMax; tok = strtok_r(nullptr, "\n", &save)) {
|
||||
snprintf(s_groups[s_ngroups], kGroupIdMax, "%s", tok);
|
||||
s_ngroups++;
|
||||
}
|
||||
}
|
||||
nvs_close(h);
|
||||
}
|
||||
|
||||
void ctrl_handle(const uint8_t mac[6], const uint8_t *data, size_t len)
|
||||
{
|
||||
(void)mac;
|
||||
if (len < 1) return;
|
||||
if (data[0] == '{') {
|
||||
cJSON *raw = cJSON_ParseWithLength((const char *)data, len);
|
||||
if (raw) {
|
||||
cJSON *o = expand_v1(raw);
|
||||
cJSON_Delete(raw);
|
||||
if (json_for_me(o)) apply_v1(o);
|
||||
cJSON_Delete(o);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "bad JSON");
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (len < 2 || data[0] != kMagic) return;
|
||||
switch (data[1]) {
|
||||
case kGroups:
|
||||
set_groups(data, len);
|
||||
break;
|
||||
case kCmd:
|
||||
handle_env(data + 2, len - 2); // a trailing save byte is ignored: settings are saved on every change
|
||||
break;
|
||||
case kGroupCmd: {
|
||||
if (len < 3) return;
|
||||
size_t gl = data[2];
|
||||
if (len < 3 + gl) return;
|
||||
char gid[kGroupIdMax];
|
||||
cstr(gid, sizeof(gid), data + 3, gl);
|
||||
if (!in_group(gid)) return;
|
||||
size_t off = 3 + gl;
|
||||
handle_env(data + off, len - off);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
size_t ctrl_build_announce(uint8_t *out, size_t cap)
|
||||
{
|
||||
net_cfg_t n;
|
||||
settings_load_net(&n);
|
||||
hw_cfg_t hw;
|
||||
settings_load_hw(&hw);
|
||||
size_t nl = strlen(n.hostname);
|
||||
if (nl > 47) nl = 47;
|
||||
static const char *orders[] = { "rgb", "rbg", "grb", "gbr", "brg", "bgr" };
|
||||
int co = 0;
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if (strcmp(hw.order, orders[i]) == 0) co = i;
|
||||
}
|
||||
char cfg[160];
|
||||
snprintf(cfg, sizeof(cfg),
|
||||
"{\"max_brightness\":%u,\"num_leds\":%d,\"color_order\":\"%s\",\"startup_mode\":\"default\",\"reversed\":false}",
|
||||
(unsigned)hw.max_brightness, hw.num_leds, hw.order);
|
||||
size_t cl = strlen(cfg);
|
||||
size_t need = 3 + nl + 6 + 1 + cl;
|
||||
if (need > cap || need > 250) return 0;
|
||||
size_t off = 0;
|
||||
out[off++] = kMagic;
|
||||
out[off++] = 0x01; // ANNOUNCE
|
||||
out[off++] = (uint8_t)nl;
|
||||
memcpy(out + off, n.hostname, nl);
|
||||
off += nl;
|
||||
out[off++] = hw.num_leds & 0xFF;
|
||||
out[off++] = (hw.num_leds >> 8) & 0xFF;
|
||||
out[off++] = (uint8_t)co;
|
||||
out[off++] = 0; // startup mode: default
|
||||
out[off++] = patterns_get_global();
|
||||
out[off++] = 0; // device type: LED strip
|
||||
out[off++] = (uint8_t)cl;
|
||||
memcpy(out + off, cfg, cl);
|
||||
off += cl;
|
||||
return off;
|
||||
}
|
||||
|
||||
cJSON *ctrl_groups_json(void)
|
||||
{
|
||||
cJSON *arr = cJSON_CreateArray();
|
||||
for (size_t i = 0; i < s_ngroups; i++) {
|
||||
cJSON_AddItemToArray(arr, cJSON_CreateString(s_groups[i]));
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Messages from led-controller over ESP-NOW: envelopes (0x4C CMD / GROUP_CMD, v2 and v3 binary), v1 JSON
|
||||
* objects, and the group list. They are applied as the MicroPython driver applies them (controller_messages.py).
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
#include "cJSON.h"
|
||||
|
||||
/** @brief Load the saved group list. Call once, after NVS is up. */
|
||||
void ctrl_init(void);
|
||||
|
||||
/** @brief Handle one controller message from mac. Runs in the ESP-NOW link task. */
|
||||
void ctrl_handle(const uint8_t mac[6], const uint8_t *data, size_t len);
|
||||
|
||||
/** @brief This driver's ANNOUNCE packet (boot hello, with its settings) into out. Returns its length. */
|
||||
size_t ctrl_build_announce(uint8_t *out, size_t cap);
|
||||
|
||||
/** @brief The group ids this driver is in, as a new JSON array. The caller frees it. */
|
||||
cJSON *ctrl_groups_json(void);
|
||||
+4
-2
@@ -78,8 +78,10 @@ static void ddp_task(void *arg)
|
||||
continue;
|
||||
}
|
||||
uint8_t dtype = buf[2];
|
||||
if (dtype != 0x00 && dtype != 0x01) {
|
||||
continue; // only 8-bit RGB
|
||||
// 8-bit RGB: 0x00 (undefined, taken as RGB), 0x01 (the shorthand ddp_test sends) and 0x0B (RGB, 8 bits
|
||||
// per channel, the DDP specification's code, which is what the controller sends)
|
||||
if (dtype != 0x00 && dtype != 0x01 && dtype != 0x0B) {
|
||||
continue;
|
||||
}
|
||||
uint32_t offset = be32(buf + 4);
|
||||
size_t len = be16(buf + 8);
|
||||
|
||||
+4
-2
@@ -92,8 +92,8 @@ int nz(int v, int def); // v when v > 0, else def
|
||||
rgb mk(int r, int g, int b); // clamped
|
||||
rgb ab(rgb c, int level); // c scaled by level/255
|
||||
rgb mix(rgb a, rgb b, int k); // a -> b, k 0..255
|
||||
int sgn(const fx_t &f, int v); // v, negated when reversed
|
||||
int led_at(const fx_t &f, int i); // logical index -> physical (reverse)
|
||||
int sgn(const fx_t &f, int v); // v unchanged; reverse is applied in led_at only
|
||||
int led_at(const fx_t &f, int i); // logical index -> physical (mirrors when reverse)
|
||||
void put(fx_t &f, int phys, rgb c);
|
||||
void putl(fx_t &f, int logical, rgb c);
|
||||
void fill(fx_t &f, rgb c);
|
||||
@@ -121,6 +121,7 @@ bool fx_chase(fx_t &f, st_t &s);
|
||||
bool fx_circle(fx_t &f, st_t &s);
|
||||
bool fx_clock_sweep(fx_t &f, st_t &s);
|
||||
bool fx_colour_cycle(fx_t &f, st_t &s);
|
||||
bool fx_rainbow(fx_t &f, st_t &s);
|
||||
bool fx_flicker(fx_t &f, st_t &s);
|
||||
bool fx_gradient_wipe(fx_t &f, st_t &s);
|
||||
bool fx_hit(fx_t &f, st_t &s);
|
||||
@@ -129,6 +130,7 @@ bool fx_keyframes(fx_t &f, st_t &s);
|
||||
bool fx_orbit(fx_t &f, st_t &s);
|
||||
bool fx_pulse(fx_t &f, st_t &s);
|
||||
bool fx_transition(fx_t &f, st_t &s);
|
||||
bool fx_wipe(fx_t &f, st_t &s);
|
||||
bool fx_strobe_burst(fx_t &f, st_t &s);
|
||||
bool fx_plasma(fx_t &f, st_t &s);
|
||||
|
||||
|
||||
+65
-16
@@ -268,8 +268,7 @@ bool fx_chase(fx_t &f, st_t &s)
|
||||
(void)nc;
|
||||
int on_len = nz(f.pr->n[1], 3);
|
||||
int off_len = nz(f.pr->n[2], 2);
|
||||
int step = abs(sgn(f, nz(f.pr->n[3], 1)));
|
||||
if (step < 1) step = 1;
|
||||
int step = f.pr->n[3] == 0 ? 1 : f.pr->n[3]; // negative steps walk the other way
|
||||
int period = on_len + off_len;
|
||||
if (first_call(s)) {
|
||||
s.last = f.now;
|
||||
@@ -284,7 +283,7 @@ bool fx_chase(fx_t &f, st_t &s)
|
||||
int m = pmod(i + s.phase, period);
|
||||
putl(f, i, m < on_len ? col : bgc);
|
||||
}
|
||||
s.phase = pmod(s.phase + sgn(f, step), period);
|
||||
s.phase = pmod(s.phase + step, period);
|
||||
return !f.pr->autorun;
|
||||
}
|
||||
|
||||
@@ -296,10 +295,9 @@ bool fx_chase(fx_t &f, st_t &s)
|
||||
rgb color1 = ab(nc > 1 ? c[1] : c[0], b);
|
||||
int n1 = nz(f.pr->n[1], 1);
|
||||
int n2 = nz(f.pr->n[2], 1);
|
||||
// Always move forward: a negative n3/n4 would otherwise make the dot bounce back.
|
||||
// sgn() still applies the preset's reverse flag.
|
||||
int n3 = sgn(f, abs(f.pr->n[3]));
|
||||
int n4 = sgn(f, abs(f.pr->n[4]));
|
||||
// Steps may be negative: a negative n3/n4 walks the dot the other way. 0 (unset) means 1.
|
||||
int n3 = f.pr->n[3] == 0 ? 1 : f.pr->n[3];
|
||||
int n4 = f.pr->n[4] == 0 ? 1 : f.pr->n[4];
|
||||
int seg = n1 + n2;
|
||||
int max_pos = nled + seg;
|
||||
auto position = [&](int sc) {
|
||||
@@ -313,7 +311,7 @@ bool fx_chase(fx_t &f, st_t &s)
|
||||
};
|
||||
if (!f.pr->autorun) {
|
||||
chase_draw(f, position(s.sc), n1, n2, n3, n4, color0, color1);
|
||||
s.sc = (s.sc + 1) % 2;
|
||||
s.sc = (s.sc + 1) % (2 * max_pos); // the dot walks the whole cycle; even modulus keeps the parity
|
||||
return true;
|
||||
}
|
||||
uint32_t dur_ms = f.pr->delay < 10 ? 10 : f.pr->delay;
|
||||
@@ -322,7 +320,7 @@ bool fx_chase(fx_t &f, st_t &s)
|
||||
}
|
||||
if ((int32_t)(f.now - s.last) >= (int32_t)dur_ms) {
|
||||
chase_draw(f, position(s.sc), n1, n2, n3, n4, color0, color1);
|
||||
s.sc = (s.sc + 1) % 2;
|
||||
s.sc = (s.sc + 1) % (2 * max_pos); // the dot walks the whole cycle; even modulus keeps the parity
|
||||
s.last += dur_ms;
|
||||
}
|
||||
return false;
|
||||
@@ -495,6 +493,24 @@ bool fx_colour_cycle(fx_t &f, st_t &s)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Rainbow: the spectrum wheel scrolling at n1 steps per frame (the controller's "rainbow" pattern)
|
||||
bool fx_rainbow(fx_t &f, st_t &s)
|
||||
{
|
||||
int step_amount = nz(f.pr->n[1], 1);
|
||||
if (!f.pr->autorun) {
|
||||
rainbow_render(f, s.phase);
|
||||
s.phase = pmod(s.phase + sgn(f, step_amount), 256);
|
||||
return true;
|
||||
}
|
||||
uint32_t d = f.pr->delay < 1 ? 1 : f.pr->delay;
|
||||
if (!due(f.now, s.last, d)) {
|
||||
return false;
|
||||
}
|
||||
rainbow_render(f, s.phase);
|
||||
s.phase = pmod(s.phase + sgn(f, step_amount), 256);
|
||||
return false;
|
||||
}
|
||||
|
||||
// ---- flicker -----------------------------------------------------------------------------------
|
||||
|
||||
bool fx_flicker(fx_t &f, st_t &s)
|
||||
@@ -787,6 +803,41 @@ bool fx_pulse(fx_t &f, st_t &s)
|
||||
|
||||
// ---- transition --------------------------------------------------------------------------------
|
||||
|
||||
// Wipe: the next colour sweeps in from the first LED to the last over `delay` ms, then becomes the
|
||||
// current colour and the one after it starts wiping in.
|
||||
bool fx_wipe(fx_t &f, st_t &s)
|
||||
{
|
||||
rgb c[PATTERN_MAX_COLOURS];
|
||||
int nc = get_colours(f, &WHITE, 1, c);
|
||||
int b = f.pr->brightness;
|
||||
if (nc == 1) {
|
||||
fill(f, ab(c[0], b));
|
||||
return false;
|
||||
}
|
||||
if (first_call(s)) {
|
||||
s.aux = f.now;
|
||||
}
|
||||
int duration = f.pr->delay < 10 ? 10 : (int)f.pr->delay;
|
||||
int elapsed = (int)(f.now - s.aux);
|
||||
if (elapsed >= duration) {
|
||||
if (!f.pr->autorun) {
|
||||
fill(f, ab(c[(s.ci + 1) % nc], b));
|
||||
return true;
|
||||
}
|
||||
s.ci = (s.ci + 1) % nc;
|
||||
s.aux = f.now;
|
||||
elapsed = 0;
|
||||
}
|
||||
rgb cur = c[s.ci % nc];
|
||||
rgb nxt = c[(s.ci + 1) % nc];
|
||||
int front = (elapsed * f.n) / duration; // LEDs already showing the next colour
|
||||
for (int i = 0; i < f.n; i++) {
|
||||
putl(f, i, ab(i < front ? nxt : cur, b));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Transition: the whole bar shows one colour, which fades from each colour to the next over `delay` ms.
|
||||
bool fx_transition(fx_t &f, st_t &s)
|
||||
{
|
||||
rgb c[PATTERN_MAX_COLOURS];
|
||||
@@ -808,14 +859,12 @@ bool fx_transition(fx_t &f, st_t &s)
|
||||
}
|
||||
s.ci = (s.ci + 1) % nc;
|
||||
s.aux = f.now;
|
||||
return false;
|
||||
elapsed = 0;
|
||||
}
|
||||
rgb c1 = c[s.ci % nc];
|
||||
rgb c2 = c[(s.ci + 1) % nc];
|
||||
float factor = (float)elapsed / duration;
|
||||
rgb m = { (int)(c1.r + (c2.r - c1.r) * factor), (int)(c1.g + (c2.g - c1.g) * factor),
|
||||
(int)(c1.b + (c2.b - c1.b) * factor) };
|
||||
fill(f, ab(m, b));
|
||||
rgb cur = c[s.ci % nc];
|
||||
rgb nxt = c[(s.ci + 1) % nc];
|
||||
int k = (elapsed * 255) / duration; // 0 = all current colour, 255 = all next colour
|
||||
fill(f, ab(mix(cur, nxt, k), b));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
+5
-3
@@ -634,9 +634,11 @@ bool fx_radiate(fx_t &f, st_t &s)
|
||||
int nc = get_colours(f, def, 1, c);
|
||||
int b = f.pr->brightness;
|
||||
rgb off_c = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||
int spacing = nz(f.pr->n[1], 1);
|
||||
int outward = nz(f.pr->n[2], 1);
|
||||
int ret = nz(f.pr->n[3], 1);
|
||||
// n1 spacing (LEDs); n2/n3 rise and fall in ms, default half a period each so a ring is seen
|
||||
int half = (int)(period_of(f) / 2) < 1 ? 1 : (int)(period_of(f) / 2);
|
||||
int spacing = nz(f.pr->n[1], 24);
|
||||
int outward = nz(f.pr->n[2], half);
|
||||
int ret = nz(f.pr->n[3], half);
|
||||
int maxd = spacing / 2;
|
||||
int life = outward + ret;
|
||||
int num = f.n;
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <cstring>
|
||||
|
||||
#include "esp_random.h"
|
||||
#include "beat_sync.h"
|
||||
#include "effects.h"
|
||||
|
||||
int clampi(int v, int lo, int hi)
|
||||
@@ -57,9 +58,9 @@ rgb mix(rgb a, rgb b, int k)
|
||||
return o;
|
||||
}
|
||||
|
||||
int sgn(const fx_t &f, int v)
|
||||
int sgn(const fx_t &, int v)
|
||||
{
|
||||
return f.pr->reverse ? -v : v;
|
||||
return v; // reverse mirrors the strip only (led_at), it does not change direction
|
||||
}
|
||||
|
||||
int led_at(const fx_t &f, int i)
|
||||
@@ -95,13 +96,11 @@ bool due(uint32_t now, uint32_t &last, uint32_t d)
|
||||
return false;
|
||||
}
|
||||
|
||||
// The beat is 500 ms here (there is no beat source on this board)
|
||||
uint32_t dur(const fx_t &f, int v)
|
||||
// A preset's timing is always milliseconds. Beats belong to sequences and animations, which the
|
||||
// controller plays, so a preset never follows the tempo.
|
||||
uint32_t dur(const fx_t &, int v)
|
||||
{
|
||||
if (v < 0) {
|
||||
v = 0;
|
||||
}
|
||||
return f.pr->beat_time ? (uint32_t)(v * 500 / 4) : (uint32_t)v;
|
||||
return v < 0 ? 0 : (uint32_t)v;
|
||||
}
|
||||
|
||||
int get_colours(const fx_t &f, const rgb *def, int ndef, rgb *out)
|
||||
|
||||
@@ -0,0 +1,443 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Port of led-driver/src/espnow_rx.py (the MicroPython driver) and the announce and ping parts of
|
||||
* espnow_transport.py. Frames arrive in the ESP-NOW callback; beats are taken there, the rest is queued for
|
||||
* the link task, which does the reliable-link work and calls ctrl_handle.
|
||||
*/
|
||||
#include "espnow_link.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#include "esp_check.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_now.h"
|
||||
#include "esp_random.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#include "beat_sync.h"
|
||||
#include "ctrl_msg.h"
|
||||
#include "net.h"
|
||||
#include "settings.h"
|
||||
|
||||
static const char *TAG = "espnow";
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr uint8_t kMagic = 0x4C;
|
||||
constexpr uint8_t kAnnounce = 0x01;
|
||||
constexpr uint8_t kData = 0x20;
|
||||
constexpr uint8_t kAck = 0x21;
|
||||
constexpr uint8_t kDigest = 0x22;
|
||||
constexpr uint8_t kResync = 0x23;
|
||||
constexpr uint8_t kBeat = 0x25;
|
||||
constexpr uint8_t kPingReq = 0x05;
|
||||
constexpr uint8_t kPingRsp = 0x06;
|
||||
constexpr uint8_t kFlagAck = 1;
|
||||
constexpr uint8_t kFlagCommit = 2;
|
||||
|
||||
constexpr size_t kHdr = 9; // DATA header: [4C][20][flags][seq u16][ver u16][idx][cnt]
|
||||
constexpr size_t kFragMax = 238; // bytes of the inner packet per DATA frame
|
||||
constexpr int kMaxFrags = 32;
|
||||
constexpr int kAsmSlots = 2; // messages being reassembled at once
|
||||
constexpr int kDoneMax = 16; // recent messages kept for duplicate detection
|
||||
constexpr int kInboxLen = 24;
|
||||
constexpr int64_t kAsmTimeoutUs = 3000000;
|
||||
constexpr int64_t kResyncGapUs = 3000000;
|
||||
constexpr int64_t kAnnounceUs[3] = { 300000, 1300000, 2600000 };
|
||||
// After the boot announces, repeat every 10 s: the radio's channel can hop while the client scans for its
|
||||
// network, so one burst can be missed. The controller updates the device it already has, so repeats are harmless.
|
||||
constexpr int64_t kAnnounceEveryUs = 10000000;
|
||||
|
||||
struct Frame {
|
||||
uint8_t mac[6];
|
||||
uint16_t len;
|
||||
uint8_t data[250];
|
||||
};
|
||||
|
||||
struct Asm {
|
||||
bool used;
|
||||
uint8_t mac[6];
|
||||
uint16_t seq;
|
||||
uint8_t cnt;
|
||||
uint8_t flags;
|
||||
uint16_t ver;
|
||||
uint32_t mask;
|
||||
uint16_t lens[kMaxFrags];
|
||||
uint8_t *buf; // cnt * kFragMax
|
||||
int64_t last_us;
|
||||
};
|
||||
|
||||
struct Done {
|
||||
uint8_t mac[6];
|
||||
uint16_t seq;
|
||||
};
|
||||
|
||||
QueueHandle_t s_inbox;
|
||||
bool s_started;
|
||||
espnow_stats_t s_st;
|
||||
Asm s_asm[kAsmSlots];
|
||||
Done s_done[kDoneMax];
|
||||
int s_ndone;
|
||||
uint16_t s_ver; // state version this driver has
|
||||
bool s_resync_pending;
|
||||
int64_t s_resync_at;
|
||||
int64_t s_last_resync;
|
||||
bool s_ping_pending;
|
||||
int64_t s_ping_at;
|
||||
uint8_t s_ping_mac[6];
|
||||
uint32_t s_ping_id;
|
||||
int s_announce_left;
|
||||
int64_t s_announce_base;
|
||||
int64_t s_next_announce;
|
||||
|
||||
// ---- sending ---------------------------------------------------------------------------------
|
||||
|
||||
esp_err_t send_to(const uint8_t mac[6], const uint8_t *data, size_t len)
|
||||
{
|
||||
if (!esp_now_is_peer_exist(mac)) {
|
||||
esp_now_peer_info_t p = {};
|
||||
memcpy(p.peer_addr, mac, 6);
|
||||
p.ifidx = WIFI_IF_STA;
|
||||
p.encrypt = false;
|
||||
esp_err_t e = esp_now_add_peer(&p);
|
||||
if (e != ESP_OK && e != ESP_ERR_ESPNOW_EXIST) {
|
||||
s_st.send_fail++;
|
||||
return e;
|
||||
}
|
||||
}
|
||||
esp_err_t e = esp_now_send(mac, data, len);
|
||||
if (e == ESP_OK) {
|
||||
s_st.sent++;
|
||||
} else {
|
||||
s_st.send_fail++;
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
const uint8_t kBroadcast[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
|
||||
void send_announce(void)
|
||||
{
|
||||
uint8_t pkt[250];
|
||||
size_t n = ctrl_build_announce(pkt, sizeof(pkt));
|
||||
if (n) {
|
||||
send_to(kBroadcast, pkt, n);
|
||||
}
|
||||
}
|
||||
|
||||
void send_ack(const uint8_t mac[6], uint16_t seq, uint8_t cnt, uint32_t mask)
|
||||
{
|
||||
uint8_t p[10] = { kMagic, kAck, 0, (uint8_t)(seq & 0xFF), (uint8_t)(seq >> 8), cnt,
|
||||
(uint8_t)(mask & 0xFF), (uint8_t)(mask >> 8), (uint8_t)(mask >> 16), (uint8_t)(mask >> 24) };
|
||||
send_to(mac, p, sizeof(p));
|
||||
s_st.acks++;
|
||||
}
|
||||
|
||||
void send_resync(void)
|
||||
{
|
||||
if (!s_st.master_seen) return;
|
||||
uint8_t p[4] = { kMagic, kResync, (uint8_t)(s_ver & 0xFF), (uint8_t)(s_ver >> 8) };
|
||||
send_to(s_st.master, p, sizeof(p));
|
||||
s_st.resyncs++;
|
||||
s_last_resync = esp_timer_get_time();
|
||||
}
|
||||
|
||||
void send_ping_rsp(void)
|
||||
{
|
||||
net_cfg_t n;
|
||||
settings_load_net(&n);
|
||||
size_t nl = strlen(n.hostname);
|
||||
uint8_t p[250];
|
||||
p[0] = kMagic;
|
||||
p[1] = kPingRsp;
|
||||
p[2] = s_ping_id & 0xFF;
|
||||
p[3] = (s_ping_id >> 8) & 0xFF;
|
||||
p[4] = (s_ping_id >> 16) & 0xFF;
|
||||
p[5] = (s_ping_id >> 24) & 0xFF;
|
||||
p[6] = (uint8_t)nl;
|
||||
memcpy(p + 7, n.hostname, nl);
|
||||
send_to(s_ping_mac, p, 7 + nl);
|
||||
s_st.pings++;
|
||||
}
|
||||
|
||||
// ---- receiving (link task) --------------------------------------------------------------------
|
||||
|
||||
void asm_free(Asm &a)
|
||||
{
|
||||
free(a.buf);
|
||||
a.buf = nullptr;
|
||||
a.used = false;
|
||||
}
|
||||
|
||||
Asm *asm_find(const uint8_t mac[6], uint16_t seq)
|
||||
{
|
||||
for (auto &a : s_asm) {
|
||||
if (a.used && a.seq == seq && memcmp(a.mac, mac, 6) == 0) return &a;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool done_has(const uint8_t mac[6], uint16_t seq)
|
||||
{
|
||||
for (int i = 0; i < s_ndone; i++) {
|
||||
if (s_done[i].seq == seq && memcmp(s_done[i].mac, mac, 6) == 0) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void done_add(const uint8_t mac[6], uint16_t seq)
|
||||
{
|
||||
if (s_ndone == kDoneMax) {
|
||||
memmove(&s_done[0], &s_done[1], sizeof(Done) * (kDoneMax - 1));
|
||||
s_ndone--;
|
||||
}
|
||||
memcpy(s_done[s_ndone].mac, mac, 6);
|
||||
s_done[s_ndone].seq = seq;
|
||||
s_ndone++;
|
||||
}
|
||||
|
||||
void on_data(const uint8_t mac[6], const uint8_t *data, size_t len, int64_t now)
|
||||
{
|
||||
if (len <= kHdr) return;
|
||||
uint8_t flags = data[2];
|
||||
uint16_t seq = (uint16_t)(data[3] | (data[4] << 8));
|
||||
uint16_t ver = (uint16_t)(data[5] | (data[6] << 8));
|
||||
uint8_t idx = data[7], cnt = data[8];
|
||||
if (cnt == 0 || cnt > kMaxFrags || idx >= cnt) return;
|
||||
memcpy(s_st.master, mac, 6);
|
||||
s_st.master_seen = true;
|
||||
uint32_t full = cnt < 32 ? ((1u << cnt) - 1) : 0xFFFFFFFFu;
|
||||
if (done_has(mac, seq)) {
|
||||
s_st.dups++;
|
||||
if (flags & kFlagAck) send_ack(mac, seq, cnt, full); // our earlier ack was lost
|
||||
return;
|
||||
}
|
||||
Asm *a = asm_find(mac, seq);
|
||||
if (!a) {
|
||||
// a free slot, else the one idle longest
|
||||
Asm *pick = nullptr;
|
||||
for (auto &s : s_asm) {
|
||||
if (!s.used) { pick = &s; break; }
|
||||
}
|
||||
if (!pick) {
|
||||
pick = &s_asm[0];
|
||||
for (auto &s : s_asm) {
|
||||
if (s.last_us < pick->last_us) pick = &s;
|
||||
}
|
||||
asm_free(*pick);
|
||||
}
|
||||
pick->buf = (uint8_t *)malloc((size_t)cnt * kFragMax);
|
||||
if (!pick->buf) return;
|
||||
pick->used = true;
|
||||
memcpy(pick->mac, mac, 6);
|
||||
pick->seq = seq;
|
||||
pick->cnt = cnt;
|
||||
pick->flags = flags;
|
||||
pick->ver = ver;
|
||||
pick->mask = 0;
|
||||
memset(pick->lens, 0, sizeof(pick->lens));
|
||||
a = pick;
|
||||
}
|
||||
size_t frag = len - kHdr;
|
||||
if (frag > kFragMax) frag = kFragMax;
|
||||
memcpy(a->buf + (size_t)idx * kFragMax, data + kHdr, frag);
|
||||
a->lens[idx] = (uint16_t)frag;
|
||||
a->mask |= 1u << idx;
|
||||
a->last_us = now;
|
||||
if (flags & kFlagAck) send_ack(mac, seq, cnt, a->mask);
|
||||
if (a->mask != full) return;
|
||||
|
||||
// complete: the message is the fragments in order
|
||||
size_t total = 0;
|
||||
for (int i = 0; i < cnt; i++) total += a->lens[i];
|
||||
uint8_t *msg = (uint8_t *)malloc(total ? total : 1);
|
||||
if (msg) {
|
||||
size_t off = 0;
|
||||
for (int i = 0; i < cnt; i++) {
|
||||
memcpy(msg + off, a->buf + (size_t)i * kFragMax, a->lens[i]);
|
||||
off += a->lens[i];
|
||||
}
|
||||
done_add(mac, seq);
|
||||
s_st.msgs++;
|
||||
ctrl_handle(mac, msg, total);
|
||||
free(msg);
|
||||
}
|
||||
if ((a->flags & kFlagCommit) && a->ver) {
|
||||
s_ver = a->ver;
|
||||
s_st.ver = a->ver;
|
||||
s_resync_pending = false;
|
||||
}
|
||||
asm_free(*a);
|
||||
}
|
||||
|
||||
void on_digest(const uint8_t mac[6], const uint8_t *data, size_t len, int64_t now)
|
||||
{
|
||||
if (len < 4) return;
|
||||
memcpy(s_st.master, mac, 6);
|
||||
s_st.master_seen = true;
|
||||
uint16_t ver = (uint16_t)(data[2] | (data[3] << 8));
|
||||
if (ver == 0 || ver == s_ver || s_resync_pending) return;
|
||||
if (s_last_resync && now - s_last_resync < kResyncGapUs) return;
|
||||
// spread the drivers' resync requests over a few hundred ms
|
||||
s_resync_at = now + (int64_t)(esp_random() % 400) * 1000;
|
||||
s_resync_pending = true;
|
||||
}
|
||||
|
||||
void handle_frame(const Frame &f, int64_t now)
|
||||
{
|
||||
if (f.len < 2) return;
|
||||
if (f.data[0] == kMagic) {
|
||||
switch (f.data[1]) {
|
||||
case kData:
|
||||
on_data(f.mac, f.data, f.len, now);
|
||||
return;
|
||||
case kDigest:
|
||||
on_digest(f.mac, f.data, f.len, now);
|
||||
return;
|
||||
case kPingReq:
|
||||
if (f.len >= 6) {
|
||||
s_ping_id = f.data[2] | (f.data[3] << 8) | (f.data[4] << 16) | ((uint32_t)f.data[5] << 24);
|
||||
memcpy(s_ping_mac, f.mac, 6);
|
||||
s_ping_at = now + (int64_t)(50 + esp_random() % 451) * 1000; // 50-500 ms, as the other drivers
|
||||
s_ping_pending = true;
|
||||
}
|
||||
return;
|
||||
case kAck:
|
||||
case kResync:
|
||||
case 0x24: // echo: bench only
|
||||
case kPingRsp:
|
||||
case kAnnounce:
|
||||
return; // controller-bound messages, not for a driver
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
ctrl_handle(f.mac, f.data, f.len);
|
||||
}
|
||||
|
||||
// The ESP-NOW receive callback: beats are taken here, anything else is queued
|
||||
void on_recv(const esp_now_recv_info_t *info, const uint8_t *data, int len)
|
||||
{
|
||||
s_st.frames++;
|
||||
if (len < 2 || len > 250) return;
|
||||
if (data[0] == kMagic && data[1] == kBeat) {
|
||||
beat_on_frame(data, (size_t)len);
|
||||
s_st.beats++;
|
||||
return;
|
||||
}
|
||||
Frame f;
|
||||
memcpy(f.mac, info->src_addr, 6);
|
||||
f.len = (uint16_t)len;
|
||||
memcpy(f.data, data, len);
|
||||
if (xQueueSend(s_inbox, &f, 0) != pdTRUE) {
|
||||
s_st.dropped++;
|
||||
}
|
||||
}
|
||||
|
||||
// One pass of the link: up to a burst of received frames (waiting up to wait for the first), then the timed sends
|
||||
// (announces, resync, ping reply) and expired reassemblies
|
||||
void link_pass(TickType_t wait)
|
||||
{
|
||||
Frame f;
|
||||
// at most a burst of frames per pass, then the other tasks run
|
||||
if (xQueueReceive(s_inbox, &f, wait) == pdTRUE) {
|
||||
for (int burst = 0; burst < 8; burst++) {
|
||||
handle_frame(f, esp_timer_get_time());
|
||||
if (xQueueReceive(s_inbox, &f, 0) != pdTRUE) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (wait) {
|
||||
vTaskDelay(1);
|
||||
}
|
||||
int64_t now = esp_timer_get_time();
|
||||
if (s_announce_left > 0 && now >= s_announce_base + kAnnounceUs[3 - s_announce_left]) {
|
||||
s_announce_left--;
|
||||
send_announce();
|
||||
if (s_announce_left == 0) {
|
||||
s_next_announce = now + kAnnounceEveryUs;
|
||||
}
|
||||
} else if (s_announce_left == 0 && now >= s_next_announce) {
|
||||
s_next_announce = now + kAnnounceEveryUs;
|
||||
send_announce();
|
||||
}
|
||||
if (s_resync_pending && now >= s_resync_at) {
|
||||
s_resync_pending = false;
|
||||
send_resync();
|
||||
}
|
||||
if (s_ping_pending && now >= s_ping_at) {
|
||||
s_ping_pending = false;
|
||||
send_ping_rsp();
|
||||
}
|
||||
for (auto &a : s_asm) {
|
||||
if (a.used && now - a.last_us > kAsmTimeoutUs) asm_free(a);
|
||||
}
|
||||
}
|
||||
|
||||
void link_task(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
for (;;) {
|
||||
link_pass(pdMS_TO_TICKS(5));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
esp_err_t espnow_link_start(void)
|
||||
{
|
||||
if (s_started) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
net_cfg_t n;
|
||||
settings_load_net(&n);
|
||||
if (!n.espnow_enabled) {
|
||||
ESP_LOGI(TAG, "ESP-NOW is off in settings");
|
||||
return ESP_OK;
|
||||
}
|
||||
ctrl_init();
|
||||
// ESP-NOW uses the radio's channel: the access point's, or the router's when the client is joined.
|
||||
// It is not set here, since changing the channel while the client connects breaks the association.
|
||||
uint8_t primary = n.espnow_channel;
|
||||
wifi_second_chan_t second;
|
||||
if (esp_wifi_get_channel(&primary, &second) == ESP_OK) {
|
||||
s_st.channel = primary;
|
||||
}
|
||||
ESP_RETURN_ON_ERROR(esp_now_init(), TAG, "esp_now_init");
|
||||
ESP_RETURN_ON_ERROR(esp_now_register_recv_cb(on_recv), TAG, "recv cb");
|
||||
esp_now_peer_info_t bcast = {};
|
||||
memcpy(bcast.peer_addr, kBroadcast, 6);
|
||||
bcast.ifidx = WIFI_IF_STA;
|
||||
esp_now_add_peer(&bcast); // it may already exist
|
||||
|
||||
s_inbox = xQueueCreate(kInboxLen, sizeof(Frame));
|
||||
if (!s_inbox) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
s_announce_left = 3;
|
||||
s_announce_base = esp_timer_get_time();
|
||||
s_st.running = true;
|
||||
s_started = true;
|
||||
xTaskCreate(link_task, "espnow_link", 6144, nullptr, 5, nullptr);
|
||||
ESP_LOGI(TAG, "ESP-NOW on channel %u", n.espnow_channel);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void espnow_link_poll(void)
|
||||
{
|
||||
if (s_started) {
|
||||
link_pass(0);
|
||||
}
|
||||
}
|
||||
|
||||
void espnow_link_stats(espnow_stats_t *out)
|
||||
{
|
||||
*out = s_st;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* ESP-NOW link to led-controller: the 0x4C wire protocol the MicroPython drivers speak. The driver announces
|
||||
* itself at boot, takes the controller's reliable messages (acked, reassembled, deduplicated, resynced by
|
||||
* digest), answers pings, and passes everything else to ctrl_msg. Beats set the tempo (beat_sync).
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
typedef struct {
|
||||
bool running;
|
||||
uint8_t channel;
|
||||
bool master_seen;
|
||||
uint8_t master[6];
|
||||
uint32_t msgs; // reliable messages delivered
|
||||
uint32_t dups; // duplicate fragments of a message already delivered
|
||||
uint32_t acks;
|
||||
uint32_t resyncs;
|
||||
uint32_t pings;
|
||||
uint32_t beats;
|
||||
uint32_t dropped; // frames lost to a full inbox
|
||||
uint32_t frames; // every ESP-NOW frame the radio delivered
|
||||
uint32_t sent;
|
||||
uint32_t send_fail;
|
||||
uint16_t ver; // state version of the last committed message
|
||||
} espnow_stats_t;
|
||||
|
||||
/** @brief Start ESP-NOW and the link task, if enabled in settings. Call after net_start(). */
|
||||
esp_err_t espnow_link_start(void);
|
||||
|
||||
/** @brief One pass of the link without waiting: the received frames, then the timed sends. The link task runs it
|
||||
* in a loop; a build without that task (the browser simulator) calls it from a timer. */
|
||||
void espnow_link_poll(void);
|
||||
|
||||
/** @brief Copy the link's counters and state. */
|
||||
void espnow_link_stats(espnow_stats_t *out);
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "ddp.h"
|
||||
#include "espnow_link.h"
|
||||
#include "net.h"
|
||||
#include "ota.h"
|
||||
#include "patterns.h"
|
||||
@@ -30,6 +31,7 @@ extern "C" void app_main(void)
|
||||
ESP_ERROR_CHECK(presets_init());
|
||||
presets_apply_startup();
|
||||
ESP_ERROR_CHECK(net_start());
|
||||
espnow_link_start(); // ESP-NOW from led-controller, if enabled in settings
|
||||
ESP_ERROR_CHECK(web_start());
|
||||
usb_cfg_start(); // led-tool over USB; the LEDs and Wi-Fi work without it
|
||||
ESP_ERROR_CHECK(ddp_start());
|
||||
|
||||
+55
-4
@@ -8,6 +8,7 @@
|
||||
#include "esp_log.h"
|
||||
#include "esp_mac.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "mdns.h"
|
||||
@@ -15,7 +16,7 @@
|
||||
|
||||
static const char *TAG = "net";
|
||||
|
||||
#define AP_CHANNEL 1
|
||||
#define AP_CHANNEL 1 // the access point's channel when ESP-NOW is off; otherwise the ESP-NOW channel
|
||||
#define AP_MAX_CLIENTS 4
|
||||
#define RETRY_MAX_MS 30000
|
||||
|
||||
@@ -59,7 +60,7 @@ static void apply_ap(bool on)
|
||||
wifi_config_t ap = {};
|
||||
strncpy((char *)ap.ap.ssid, ssid, sizeof(ap.ap.ssid) - 1);
|
||||
ap.ap.ssid_len = strlen(ssid);
|
||||
ap.ap.channel = AP_CHANNEL;
|
||||
ap.ap.channel = n.espnow_enabled ? n.espnow_channel : AP_CHANNEL;
|
||||
ap.ap.max_connection = AP_MAX_CLIENTS;
|
||||
// A password of 8-63 characters is WPA2; anything else is an open network
|
||||
const size_t plen = strlen(n.ap_pass);
|
||||
@@ -96,6 +97,15 @@ static void ap_timer_cb(void *arg)
|
||||
|
||||
static esp_err_t apply_client_ip(const net_cfg_t *c);
|
||||
|
||||
// The Wi-Fi client and ESP-NOW share the radio, and a client that searches for its network hops channels and
|
||||
// breaks ESP-NOW, so only one is on: with ESP-NOW enabled the client stays off (no connect, no retries).
|
||||
static bool client_allowed(void)
|
||||
{
|
||||
net_cfg_t n;
|
||||
settings_load_net(&n);
|
||||
return !n.espnow_enabled;
|
||||
}
|
||||
|
||||
static void reconnect_cb(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
@@ -105,7 +115,9 @@ static void reconnect_cb(void *arg)
|
||||
if (apply_client_ip(&s_pending_net) != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Client address could not be applied");
|
||||
}
|
||||
esp_wifi_connect();
|
||||
if (client_allowed()) {
|
||||
esp_wifi_connect();
|
||||
}
|
||||
}
|
||||
|
||||
static void ap_switch_later(bool on, uint64_t delay_us)
|
||||
@@ -147,6 +159,9 @@ static esp_err_t apply_client_ip(const net_cfg_t *c)
|
||||
static void retry_cb(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
if (!client_allowed()) {
|
||||
return;
|
||||
}
|
||||
esp_wifi_connect();
|
||||
}
|
||||
|
||||
@@ -166,7 +181,11 @@ static void on_wifi_event(void *arg, esp_event_base_t base, int32_t id, void *da
|
||||
n.use_static = false;
|
||||
apply_client_ip(&n);
|
||||
}
|
||||
esp_wifi_connect();
|
||||
if (!n.espnow_enabled) {
|
||||
esp_wifi_connect();
|
||||
} else {
|
||||
ESP_LOGI(TAG, "ESP-NOW is on: the Wi-Fi client stays off");
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -265,6 +284,8 @@ esp_err_t net_start(void)
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
// No modem sleep: the radio stays awake so the AP never holds DDP unicast until a beacon
|
||||
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
|
||||
|
||||
// mDNS: <hostname>.local, with an HTTP service on port 80
|
||||
ESP_ERROR_CHECK(mdns_init());
|
||||
@@ -294,12 +315,42 @@ bool net_sta_status(char *ip, size_t ip_len)
|
||||
return true;
|
||||
}
|
||||
|
||||
// Restart after a Wi-Fi change, so the reply goes out first
|
||||
static void restart_cb(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
esp_restart();
|
||||
}
|
||||
|
||||
esp_err_t net_sta_connect(const wifi_creds_t *c)
|
||||
{
|
||||
esp_err_t err = settings_save_wifi(c);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
net_cfg_t n;
|
||||
settings_load_net(&n);
|
||||
if (n.espnow_enabled) {
|
||||
// Wi-Fi and ESP-NOW share the radio, so Wi-Fi mode means ESP-NOW off. Restart into it: the
|
||||
// saved credentials are joined at boot.
|
||||
n.espnow_enabled = false;
|
||||
err = settings_save_net(&n);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
static esp_timer_handle_t restart_timer;
|
||||
if (!restart_timer) {
|
||||
esp_timer_create_args_t a = {};
|
||||
a.callback = restart_cb;
|
||||
a.name = "wifi_restart";
|
||||
err = esp_timer_create(&a, &restart_timer);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
ESP_LOGI(TAG, "Wi-Fi set: ESP-NOW off, restarting into Wi-Fi mode");
|
||||
return esp_timer_start_once(restart_timer, 1500000);
|
||||
}
|
||||
esp_timer_stop(s_retry_timer);
|
||||
esp_wifi_disconnect();
|
||||
s_retries = 0;
|
||||
|
||||
+50
-39
@@ -59,6 +59,7 @@ static const pattern_def_t kPatterns[] = {
|
||||
{ "pulse", fx_pulse },
|
||||
{ "radiate", fx_radiate },
|
||||
{ "rain_drops", fx_rain_drops },
|
||||
{ "rainbow", fx_rainbow },
|
||||
{ "rime", fx_rime },
|
||||
{ "ripple", fx_ripple },
|
||||
{ "sparkle", fx_sparkle },
|
||||
@@ -67,6 +68,7 @@ static const pattern_def_t kPatterns[] = {
|
||||
{ "sweep", fx_sweep },
|
||||
{ "transition", fx_transition },
|
||||
{ "twinkle", fx_twinkle },
|
||||
{ "wipe", fx_wipe },
|
||||
};
|
||||
#define PATTERN_COUNT ((int)(sizeof(kPatterns) / sizeof(kPatterns[0])))
|
||||
|
||||
@@ -152,9 +154,13 @@ static void sanitize(preset_t *p)
|
||||
if (p->ncol < 0) p->ncol = 0;
|
||||
if (p->ncol > PATTERN_MAX_COLOURS) p->ncol = PATTERN_MAX_COLOURS;
|
||||
if (p->fade > 10000) p->fade = 10000;
|
||||
// Chase steps (n3/n4) may be negative to run the dot backwards; other values stay >= 0.
|
||||
const bool chase = p->pattern == patterns_index("chase");
|
||||
for (int i = 1; i <= 6; i++) {
|
||||
if (p->n[i] < 0) p->n[i] = 0;
|
||||
bool step = chase && (i == 3 || i == 4);
|
||||
if (p->n[i] < 0 && !step) p->n[i] = 0;
|
||||
if (p->n[i] > 100000) p->n[i] = 100000;
|
||||
if (p->n[i] < -100000) p->n[i] = -100000;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -343,48 +349,53 @@ void st_reset(void)
|
||||
memset(next_on, 0, s_cap);
|
||||
}
|
||||
|
||||
// The whole frame (pattern and output) runs under s_lock, so the buffers can't be replaced mid-frame
|
||||
// One frame: the whole frame (pattern and output) runs under s_lock, so the buffers can't be replaced mid-frame
|
||||
void patterns_frame(void)
|
||||
{
|
||||
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||
s_fx.pr = &s_run;
|
||||
bool reset = s_reset_pending;
|
||||
s_reset_pending = false;
|
||||
s_run = s_cur;
|
||||
uint32_t now = now_ms();
|
||||
if (reset) {
|
||||
s_ddp_last_ms = 0; // selecting a pattern takes the strip back from DDP
|
||||
}
|
||||
// Once pixels have arrived the strip keeps the last frame when they stop: the pattern does not
|
||||
// come back until one is selected
|
||||
bool ddp_on = s_ddp_last_ms != 0;
|
||||
if (reset) {
|
||||
if (s_run.fade > 0) {
|
||||
memcpy(s_from, s_shown, s_fx.n * sizeof(rgb));
|
||||
s_fading = true;
|
||||
s_fade_t0 = now;
|
||||
s_fade_ms = s_run.fade;
|
||||
} else {
|
||||
s_fading = false;
|
||||
}
|
||||
st_reset();
|
||||
g_st.t0 = now;
|
||||
}
|
||||
s_fx.now = now;
|
||||
s_fx.t = now - g_st.t0;
|
||||
if (ddp_on) {
|
||||
for (int i = 0; i < s_fx.n; i++) {
|
||||
s_fx.px[i] = { s_ddp_show[3 * i], s_ddp_show[3 * i + 1], s_ddp_show[3 * i + 2] };
|
||||
}
|
||||
} else if (!g_st.done) {
|
||||
if (kPatterns[s_run.pattern].fn(s_fx, g_st)) {
|
||||
g_st.done = true;
|
||||
}
|
||||
}
|
||||
output(now, ddp_on);
|
||||
xSemaphoreGive(s_lock);
|
||||
}
|
||||
|
||||
static void patterns_task(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
s_fx.pr = &s_run;
|
||||
while (true) {
|
||||
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||
bool reset = s_reset_pending;
|
||||
s_reset_pending = false;
|
||||
s_run = s_cur;
|
||||
uint32_t now = now_ms();
|
||||
if (reset) {
|
||||
s_ddp_last_ms = 0; // selecting a pattern takes the strip back from DDP
|
||||
}
|
||||
// Once pixels have arrived the strip keeps the last frame when they stop: the pattern does not
|
||||
// come back until one is selected
|
||||
bool ddp_on = s_ddp_last_ms != 0;
|
||||
if (reset) {
|
||||
if (s_run.fade > 0) {
|
||||
memcpy(s_from, s_shown, s_fx.n * sizeof(rgb));
|
||||
s_fading = true;
|
||||
s_fade_t0 = now;
|
||||
s_fade_ms = s_run.fade;
|
||||
} else {
|
||||
s_fading = false;
|
||||
}
|
||||
st_reset();
|
||||
g_st.t0 = now;
|
||||
}
|
||||
s_fx.now = now;
|
||||
s_fx.t = now - g_st.t0;
|
||||
if (ddp_on) {
|
||||
for (int i = 0; i < s_fx.n; i++) {
|
||||
s_fx.px[i] = { s_ddp_show[3 * i], s_ddp_show[3 * i + 1], s_ddp_show[3 * i + 2] };
|
||||
}
|
||||
} else if (!g_st.done) {
|
||||
if (kPatterns[s_run.pattern].fn(s_fx, g_st)) {
|
||||
g_st.done = true;
|
||||
}
|
||||
}
|
||||
output(now, ddp_on);
|
||||
xSemaphoreGive(s_lock);
|
||||
patterns_frame();
|
||||
vTaskDelay(pdMS_TO_TICKS(FRAME_MS));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,9 @@ void patterns_start(const struct hw_cfg_t &hw);
|
||||
/** @brief Apply new strip settings (count, colour order, brightness ceiling) while running. */
|
||||
esp_err_t patterns_set_hardware(const struct hw_cfg_t &hw);
|
||||
|
||||
/** @brief Run one frame of the pattern engine and show it. The pattern task calls it every 10 ms. */
|
||||
void patterns_frame(void);
|
||||
|
||||
/** @brief Live update of the preset on the strip. A new pattern restarts; other changes keep its place. */
|
||||
void patterns_set(const preset_t &p);
|
||||
|
||||
|
||||
+179
-37
@@ -40,6 +40,19 @@ static cJSON *rgb_json(const uint8_t *c)
|
||||
|
||||
static bool read_rgb(const cJSON *a, uint8_t *out)
|
||||
{
|
||||
// The controller sends colours as "#RRGGBB" text
|
||||
if (cJSON_IsString(a)) {
|
||||
const char *s = a->valuestring[0] == '#' ? a->valuestring + 1 : a->valuestring;
|
||||
char *end = nullptr;
|
||||
unsigned long v = strtoul(s, &end, 16);
|
||||
if (end != s + 6 || s[0] == '+' || s[0] == '-' || s[0] == ' ') {
|
||||
return false;
|
||||
}
|
||||
out[0] = (uint8_t)(v >> 16);
|
||||
out[1] = (uint8_t)((v >> 8) & 0xFF);
|
||||
out[2] = (uint8_t)(v & 0xFF);
|
||||
return true;
|
||||
}
|
||||
if (!cJSON_IsArray(a) || cJSON_GetArraySize(a) != 3) {
|
||||
return false;
|
||||
}
|
||||
@@ -120,8 +133,8 @@ bool preset_from_json(const cJSON *o, preset_t *p)
|
||||
if (cJSON_IsNumber(v)) p->fade = (uint32_t)(v->valueint < 0 ? 0 : v->valueint);
|
||||
v = cJSON_GetObjectItem(o, "beat_time");
|
||||
if (cJSON_IsBool(v)) p->beat_time = cJSON_IsTrue(v);
|
||||
static const char *keys[] = { "n1", "n2", "n3", "n4" };
|
||||
for (int i = 0; i < 4; i++) {
|
||||
static const char *keys[] = { "n1", "n2", "n3", "n4", "n5", "n6" };
|
||||
for (int i = 0; i < 6; i++) {
|
||||
v = cJSON_GetObjectItem(o, keys[i]);
|
||||
if (cJSON_IsNumber(v)) p->n[i + 1] = v->valueint;
|
||||
}
|
||||
@@ -130,27 +143,65 @@ bool preset_from_json(const cJSON *o, preset_t *p)
|
||||
return true;
|
||||
}
|
||||
|
||||
// Write the whole list to NVS as one JSON blob
|
||||
static esp_err_t save_list(void)
|
||||
// One key per preset ("p00".."p63"), so a save writes one small entry instead of the whole list.
|
||||
// NVS_SLOTS is the most keys any build has written; the 24 KB nvs partition holds about 50 presets.
|
||||
#define NVS_SLOTS 64
|
||||
static void entry_key(char *out, size_t n, size_t i)
|
||||
{
|
||||
cJSON *arr = cJSON_CreateArray();
|
||||
for (size_t i = 0; i < s_count; i++) {
|
||||
cJSON_AddItemToArray(arr, preset_to_json(&s_list[i]));
|
||||
}
|
||||
char *s = cJSON_PrintUnformatted(arr);
|
||||
cJSON_Delete(arr);
|
||||
snprintf(out, n, "p%02u", (unsigned)i);
|
||||
}
|
||||
|
||||
static esp_err_t save_entry(nvs_handle_t h, size_t i)
|
||||
{
|
||||
cJSON *o = preset_to_json(&s_list[i]);
|
||||
char *s = cJSON_PrintUnformatted(o);
|
||||
cJSON_Delete(o);
|
||||
if (!s) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
char key[8];
|
||||
entry_key(key, sizeof(key), i);
|
||||
esp_err_t err = nvs_set_str(h, key, s);
|
||||
cJSON_free(s);
|
||||
return err;
|
||||
}
|
||||
|
||||
// Write every preset, the count and the startup name. Erases keys past the new count and the old blob.
|
||||
static esp_err_t save_list(void)
|
||||
{
|
||||
nvs_handle_t h;
|
||||
esp_err_t err = nvs_open(NS, NVS_READWRITE, &h);
|
||||
if (err == ESP_OK) {
|
||||
err = nvs_set_blob(h, "list", s, strlen(s));
|
||||
if (err == ESP_OK) err = nvs_set_str(h, "startup", s_startup);
|
||||
if (err == ESP_OK) err = nvs_commit(h);
|
||||
nvs_close(h);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
cJSON_free(s);
|
||||
for (size_t i = 0; i < s_count && err == ESP_OK; i++) {
|
||||
err = save_entry(h, i);
|
||||
}
|
||||
for (size_t i = s_count; i < NVS_SLOTS; i++) {
|
||||
char key[8];
|
||||
entry_key(key, sizeof(key), i);
|
||||
nvs_erase_key(h, key); // also keys left by an older build with a bigger limit
|
||||
}
|
||||
nvs_erase_key(h, "list"); // the single blob written by older firmware
|
||||
if (err == ESP_OK) err = nvs_set_u32(h, "n", (uint32_t)s_count);
|
||||
if (err == ESP_OK) err = nvs_set_str(h, "startup", s_startup);
|
||||
if (err == ESP_OK) err = nvs_commit(h);
|
||||
nvs_close(h);
|
||||
return err;
|
||||
}
|
||||
|
||||
// Write one preset (slot idx) and the count
|
||||
static esp_err_t save_one(size_t idx)
|
||||
{
|
||||
nvs_handle_t h;
|
||||
esp_err_t err = nvs_open(NS, NVS_READWRITE, &h);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
err = save_entry(h, idx);
|
||||
if (err == ESP_OK) err = nvs_set_u32(h, "n", (uint32_t)s_count);
|
||||
if (err == ESP_OK) err = nvs_commit(h);
|
||||
nvs_close(h);
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -183,6 +234,21 @@ static int build_defaults(preset_t *out, int max)
|
||||
p->delay = 150;
|
||||
p->brightness = 180;
|
||||
}
|
||||
if ((p = add("Chase Marquee", "chase"))) {
|
||||
p->ncol = 1;
|
||||
p->col[0][0] = 255; p->col[0][1] = 200; p->col[0][2] = 80;
|
||||
p->n[1] = 3; p->n[2] = 2; p->n[3] = 1; p->n[6] = 1; // dashes that scroll
|
||||
p->delay = 120;
|
||||
p->brightness = 180;
|
||||
}
|
||||
if ((p = add("Chase Reverse", "chase"))) {
|
||||
p->ncol = 2;
|
||||
p->col[0][0] = 0; p->col[0][1] = 255; p->col[0][2] = 120;
|
||||
p->col[1][0] = 120; p->col[1][1] = 0; p->col[1][2] = 255;
|
||||
p->n[1] = 6; p->n[2] = 6; p->n[3] = -1; p->n[4] = -1; // negative steps run it backwards
|
||||
p->delay = 120;
|
||||
p->brightness = 180;
|
||||
}
|
||||
if ((p = add("Aurora", "aurora"))) {
|
||||
p->n[1] = 3;
|
||||
p->n[2] = 40;
|
||||
@@ -243,6 +309,61 @@ int presets_add_defaults(void)
|
||||
return added;
|
||||
}
|
||||
|
||||
// Read the one JSON list written by older firmware. Returns false if there is none.
|
||||
static bool load_legacy(nvs_handle_t h)
|
||||
{
|
||||
size_t len = 0;
|
||||
if (nvs_get_blob(h, "list", NULL, &len) != ESP_OK || len == 0) {
|
||||
return false;
|
||||
}
|
||||
char *buf = (char *)malloc(len + 1);
|
||||
if (!buf) {
|
||||
return false;
|
||||
}
|
||||
bool ok = false;
|
||||
if (nvs_get_blob(h, "list", buf, &len) == ESP_OK) {
|
||||
buf[len] = '\0';
|
||||
cJSON *arr = cJSON_Parse(buf);
|
||||
cJSON *o;
|
||||
cJSON_ArrayForEach(o, arr) {
|
||||
if (s_count >= PRESETS_MAX) break;
|
||||
preset_t p;
|
||||
preset_defaults(&p, 0);
|
||||
if (preset_from_json(o, &p) && p.name[0]) {
|
||||
s_list[s_count++] = p;
|
||||
}
|
||||
}
|
||||
cJSON_Delete(arr);
|
||||
ok = true;
|
||||
}
|
||||
free(buf);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static void load_entry(nvs_handle_t h, uint32_t i)
|
||||
{
|
||||
char key[8];
|
||||
entry_key(key, sizeof(key), i);
|
||||
size_t len = 0;
|
||||
if (nvs_get_str(h, key, NULL, &len) != ESP_OK || len == 0) {
|
||||
return;
|
||||
}
|
||||
char *buf = (char *)malloc(len);
|
||||
if (!buf) {
|
||||
return;
|
||||
}
|
||||
if (nvs_get_str(h, key, buf, &len) == ESP_OK) {
|
||||
cJSON *o = cJSON_Parse(buf);
|
||||
preset_t p;
|
||||
preset_defaults(&p, 0);
|
||||
if (o && preset_from_json(o, &p) && p.name[0] && s_count < PRESETS_MAX) {
|
||||
s_list[s_count++] = p;
|
||||
}
|
||||
cJSON_Delete(o);
|
||||
}
|
||||
free(buf);
|
||||
}
|
||||
|
||||
esp_err_t presets_init(void)
|
||||
{
|
||||
s_count = 0;
|
||||
@@ -254,35 +375,35 @@ esp_err_t presets_init(void)
|
||||
presets_add_defaults(); // a new board
|
||||
return ESP_OK;
|
||||
}
|
||||
size_t len = 0;
|
||||
bool have_list = nvs_get_blob(h, "list", NULL, &len) == ESP_OK;
|
||||
if (have_list && len > 0) {
|
||||
char *buf = (char *)malloc(len + 1);
|
||||
if (buf && nvs_get_blob(h, "list", buf, &len) == ESP_OK) {
|
||||
buf[len] = '\0';
|
||||
cJSON *arr = cJSON_Parse(buf);
|
||||
cJSON *o;
|
||||
cJSON_ArrayForEach(o, arr) {
|
||||
if (s_count >= PRESETS_MAX) break;
|
||||
preset_t p;
|
||||
preset_defaults(&p, 0);
|
||||
if (preset_from_json(o, &p) && p.name[0]) {
|
||||
s_list[s_count++] = p;
|
||||
}
|
||||
}
|
||||
cJSON_Delete(arr);
|
||||
uint32_t n = 0;
|
||||
bool have_list = nvs_get_u32(h, "n", &n) == ESP_OK;
|
||||
bool legacy = false;
|
||||
if (have_list) {
|
||||
if (n > PRESETS_MAX) {
|
||||
ESP_LOGW(TAG, "%u presets saved, room for %d: the rest are not loaded", (unsigned)n, PRESETS_MAX);
|
||||
}
|
||||
free(buf);
|
||||
for (uint32_t i = 0; i < n && i < PRESETS_MAX; i++) {
|
||||
load_entry(h, i);
|
||||
}
|
||||
} else {
|
||||
legacy = load_legacy(h);
|
||||
have_list = legacy;
|
||||
}
|
||||
len = sizeof(s_startup);
|
||||
size_t len = sizeof(s_startup);
|
||||
if (nvs_get_str(h, "startup", s_startup, &len) != ESP_OK) {
|
||||
s_startup[0] = '\0';
|
||||
}
|
||||
nvs_close(h);
|
||||
if (legacy) {
|
||||
save_list(); // move the old blob to per-preset keys
|
||||
}
|
||||
if (!have_list) {
|
||||
presets_add_defaults(); // nothing saved yet: start with the defaults
|
||||
}
|
||||
ESP_LOGI(TAG, "%u presets loaded", (unsigned)s_count);
|
||||
for (size_t i = 0; i < s_count; i++) {
|
||||
ESP_LOGI(TAG, " [%u] %s", (unsigned)i, s_list[i].name);
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -296,17 +417,36 @@ const preset_t *presets_at(size_t i)
|
||||
return i < s_count ? &s_list[i] : NULL;
|
||||
}
|
||||
|
||||
static uint32_t s_dropped; // saves refused because the store was full
|
||||
|
||||
uint32_t presets_dropped(void)
|
||||
{
|
||||
return s_dropped;
|
||||
}
|
||||
|
||||
esp_err_t presets_save(const preset_t *preset)
|
||||
{
|
||||
int idx = find(preset->name);
|
||||
if (idx < 0) {
|
||||
bool added = idx < 0;
|
||||
if (added) {
|
||||
if (s_count >= PRESETS_MAX) {
|
||||
s_dropped++;
|
||||
ESP_LOGW(TAG, "store full (%d presets): '%s' not saved", PRESETS_MAX, preset->name);
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
idx = (int)s_count++;
|
||||
}
|
||||
s_list[idx] = *preset;
|
||||
return save_list();
|
||||
ESP_LOGI(TAG, "save '%s' (%s), %u presets", preset->name, added ? "new" : "update", (unsigned)s_count);
|
||||
esp_err_t err = save_one((size_t)idx);
|
||||
if (err != ESP_OK) {
|
||||
s_dropped++;
|
||||
ESP_LOGW(TAG, "'%s' not saved to flash: %s", preset->name, esp_err_to_name(err));
|
||||
if (added) {
|
||||
s_count--; // keep RAM and flash the same
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_err_t presets_set_order(const char *const *names, size_t n)
|
||||
@@ -328,6 +468,7 @@ esp_err_t presets_set_order(const char *const *names, size_t n)
|
||||
}
|
||||
}
|
||||
memcpy(s_list, fresh, m * sizeof(preset_t));
|
||||
ESP_LOGI(TAG, "order: %u presets (was %u)", (unsigned)m, (unsigned)s_count);
|
||||
s_count = m;
|
||||
return save_list();
|
||||
}
|
||||
@@ -342,6 +483,7 @@ esp_err_t presets_delete(const char *name)
|
||||
s_list[i] = s_list[i + 1];
|
||||
}
|
||||
s_count--;
|
||||
ESP_LOGI(TAG, "delete '%s', %u presets", name, (unsigned)s_count);
|
||||
if (strcmp(s_startup, name) == 0) {
|
||||
s_startup[0] = '\0';
|
||||
}
|
||||
|
||||
+5
-1
@@ -9,7 +9,8 @@
|
||||
#include "esp_err.h"
|
||||
#include "patterns.h"
|
||||
|
||||
#define PRESETS_MAX 32
|
||||
// Flash holds about 50 presets (the nvs partition is 24 KB); 48 leaves room for garbage collection
|
||||
#define PRESETS_MAX 48
|
||||
|
||||
/** @brief Load the saved presets and the startup name from NVS. Call after NVS is initialised. */
|
||||
esp_err_t presets_init(void);
|
||||
@@ -26,6 +27,9 @@ const preset_t *presets_at(size_t i);
|
||||
/** @brief Create a preset, or update the one with the same name. ESP_ERR_NO_MEM when full. */
|
||||
esp_err_t presets_save(const preset_t *preset);
|
||||
|
||||
/** @brief Saves refused since boot: store full, or flash write failed. */
|
||||
uint32_t presets_dropped(void);
|
||||
|
||||
/** @brief Put the presets in this order. Names not given keep their place at the end. */
|
||||
esp_err_t presets_set_order(const char *const *names, size_t n);
|
||||
|
||||
|
||||
@@ -113,6 +113,8 @@ esp_err_t settings_load_net(net_cfg_t *c)
|
||||
memset(c, 0, sizeof(*c));
|
||||
c->ap_enabled = true;
|
||||
c->ddp_enabled = true;
|
||||
c->espnow_enabled = true;
|
||||
c->espnow_channel = 6;
|
||||
nvs_handle_t h;
|
||||
esp_err_t err = nvs_open(NS, NVS_READONLY, &h);
|
||||
if (err == ESP_ERR_NVS_NOT_FOUND) {
|
||||
@@ -139,6 +141,12 @@ esp_err_t settings_load_net(net_cfg_t *c)
|
||||
char ddp[8];
|
||||
get_str(h, "ddp", ddp, sizeof(ddp), "on");
|
||||
c->ddp_enabled = strcmp(ddp, "off") != 0;
|
||||
char espnow[8];
|
||||
get_str(h, "espnow", espnow, sizeof(espnow), "on");
|
||||
c->espnow_enabled = strcmp(espnow, "off") != 0;
|
||||
int64_t ch = 6;
|
||||
nvs_get_i64(h, "espnow_ch", &ch);
|
||||
c->espnow_channel = (uint8_t)(ch >= 1 && ch <= 13 ? ch : 6);
|
||||
nvs_close(h);
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -160,6 +168,8 @@ esp_err_t settings_save_net(const net_cfg_t *c)
|
||||
if (err == ESP_OK) err = nvs_set_str(h, "ap_ssid", c->ap_ssid);
|
||||
if (err == ESP_OK) err = nvs_set_str(h, "ap_pass", c->ap_pass);
|
||||
if (err == ESP_OK) err = nvs_set_str(h, "ddp", c->ddp_enabled ? "on" : "off");
|
||||
if (err == ESP_OK) err = nvs_set_str(h, "espnow", c->espnow_enabled ? "on" : "off");
|
||||
if (err == ESP_OK) err = nvs_set_i64(h, "espnow_ch", c->espnow_channel);
|
||||
if (err == ESP_OK) err = nvs_commit(h);
|
||||
nvs_close(h);
|
||||
return err;
|
||||
|
||||
@@ -23,6 +23,8 @@ struct net_cfg_t {
|
||||
char ap_ssid[33]; // access point name; empty uses led-XXXXXX from the MAC
|
||||
char ap_pass[65]; // access point password, 8-63 characters (WPA2); empty is an open network
|
||||
bool ddp_enabled; // DDP pixels and frame sync are received; default on
|
||||
bool espnow_enabled; // ESP-NOW link to led-controller (0x4C wire protocol); default on
|
||||
uint8_t espnow_channel; // Wi-Fi channel shared with the controller and its bridge; default 6
|
||||
};
|
||||
|
||||
/** @brief Read saved Wi-Fi client credentials. ESP_ERR_NVS_NOT_FOUND if none are saved. */
|
||||
|
||||
+92
-2
@@ -10,6 +10,7 @@
|
||||
#include "mdns.h"
|
||||
#include "esp_http_server.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_mac.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_timer.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
@@ -19,7 +20,9 @@
|
||||
#include "esp_wifi.h"
|
||||
#include "net.h"
|
||||
#include "ota.h"
|
||||
#include "ctrl_msg.h"
|
||||
#include "ddp.h"
|
||||
#include "espnow_link.h"
|
||||
#include "patterns.h"
|
||||
#include "effects.h"
|
||||
#include "presets.h"
|
||||
@@ -246,6 +249,12 @@ static esp_err_t status_get(api_req_t *req)
|
||||
cJSON_AddStringToObject(o, "ap_ssid", ap);
|
||||
cJSON_AddBoolToObject(o, "ap_on", net_ap_on());
|
||||
cJSON_AddStringToObject(o, "hostname", n.hostname);
|
||||
// The station MAC is the one the board uses on ESP-NOW too, so the controller ties the two records together
|
||||
uint8_t mac[6];
|
||||
esp_read_mac(mac, ESP_MAC_WIFI_STA);
|
||||
char mac_hex[13];
|
||||
snprintf(mac_hex, sizeof(mac_hex), "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
cJSON_AddStringToObject(o, "mac", mac_hex);
|
||||
cJSON_AddBoolToObject(o, "sta_connected", up);
|
||||
cJSON_AddStringToObject(o, "sta_ip", ip);
|
||||
cJSON_AddStringToObject(o, "selected", presets_selected());
|
||||
@@ -268,11 +277,17 @@ static esp_err_t pattern_post(api_req_t *req)
|
||||
preset_t p;
|
||||
patterns_get(&p);
|
||||
bool ok = preset_from_json(in, &p);
|
||||
// "select": the controller's preset select: restart the pattern, and fade in if the preset says so
|
||||
bool select = cJSON_IsTrue(cJSON_GetObjectItem(in, "select"));
|
||||
cJSON_Delete(in);
|
||||
if (!ok) {
|
||||
return send_error(req, "400 Bad Request", "unknown pattern");
|
||||
}
|
||||
patterns_set(p);
|
||||
if (select) {
|
||||
patterns_select(p);
|
||||
} else {
|
||||
patterns_set(p);
|
||||
}
|
||||
return send_json(req, cJSON_CreateObject());
|
||||
}
|
||||
|
||||
@@ -318,13 +333,17 @@ static esp_err_t wifi_post(api_req_t *req)
|
||||
strncpy(c.pass, pw, sizeof(c.pass) - 1);
|
||||
cJSON_Delete(in);
|
||||
|
||||
net_cfg_t before;
|
||||
settings_load_net(&before);
|
||||
esp_err_t err = net_sta_connect(&c);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Connect to %s failed: %s", c.ssid, esp_err_to_name(err));
|
||||
return send_error(req, "500 Internal Server Error", esp_err_to_name(err));
|
||||
}
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(o, "message", "saved; connecting");
|
||||
cJSON_AddStringToObject(o, "message", before.espnow_enabled
|
||||
? "saved; ESP-NOW is off, restarting into Wi-Fi mode"
|
||||
: "saved; connecting");
|
||||
return send_json(req, o);
|
||||
}
|
||||
|
||||
@@ -690,6 +709,7 @@ static esp_err_t presets_get(api_req_t *req)
|
||||
cJSON_AddStringToObject(o, "selected", presets_selected());
|
||||
cJSON_AddStringToObject(o, "startup", presets_startup());
|
||||
cJSON_AddNumberToObject(o, "max", PRESETS_MAX);
|
||||
cJSON_AddNumberToObject(o, "dropped", presets_dropped());
|
||||
return send_json(req, o);
|
||||
}
|
||||
|
||||
@@ -1155,6 +1175,74 @@ typedef struct {
|
||||
api_fn_t fn;
|
||||
} api_route_t;
|
||||
|
||||
// GET /api/espnow: the ESP-NOW link to led-controller: its settings, the controller, the groups, the counters
|
||||
static esp_err_t espnow_get(api_req_t *req)
|
||||
{
|
||||
net_cfg_t n;
|
||||
settings_load_net(&n);
|
||||
espnow_stats_t st;
|
||||
espnow_link_stats(&st);
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddBoolToObject(o, "enabled", n.espnow_enabled);
|
||||
cJSON_AddBoolToObject(o, "running", st.running);
|
||||
cJSON_AddNumberToObject(o, "channel", n.espnow_channel);
|
||||
uint8_t radio = 0;
|
||||
wifi_second_chan_t second;
|
||||
if (esp_wifi_get_channel(&radio, &second) == ESP_OK) {
|
||||
cJSON_AddNumberToObject(o, "radio_channel", radio); // the channel ESP-NOW actually uses
|
||||
}
|
||||
char ctrl[24] = "";
|
||||
if (st.master_seen) {
|
||||
snprintf(ctrl, sizeof(ctrl), "%02x:%02x:%02x:%02x:%02x:%02x", st.master[0], st.master[1], st.master[2],
|
||||
st.master[3], st.master[4], st.master[5]);
|
||||
}
|
||||
cJSON_AddStringToObject(o, "controller", ctrl);
|
||||
cJSON_AddItemToObject(o, "groups", ctrl_groups_json());
|
||||
cJSON *c = cJSON_AddObjectToObject(o, "counters");
|
||||
cJSON_AddNumberToObject(c, "msgs", st.msgs);
|
||||
cJSON_AddNumberToObject(c, "dups", st.dups);
|
||||
cJSON_AddNumberToObject(c, "acks", st.acks);
|
||||
cJSON_AddNumberToObject(c, "resyncs", st.resyncs);
|
||||
cJSON_AddNumberToObject(c, "pings", st.pings);
|
||||
cJSON_AddNumberToObject(c, "beats", st.beats);
|
||||
cJSON_AddNumberToObject(c, "dropped", st.dropped);
|
||||
cJSON_AddNumberToObject(c, "frames", st.frames);
|
||||
cJSON_AddNumberToObject(c, "sent", st.sent);
|
||||
cJSON_AddNumberToObject(c, "send_fail", st.send_fail);
|
||||
cJSON_AddNumberToObject(c, "ver", st.ver);
|
||||
return send_json(req, o);
|
||||
}
|
||||
|
||||
// POST /api/espnow {"enabled": bool, "channel": 1-13}. Saved now; the link starts or moves channel on restart.
|
||||
static esp_err_t espnow_post(api_req_t *req)
|
||||
{
|
||||
cJSON *in = read_json(req);
|
||||
if (!in) {
|
||||
return send_error(req, "400 Bad Request", "empty, too large or invalid JSON");
|
||||
}
|
||||
net_cfg_t n;
|
||||
settings_load_net(&n);
|
||||
cJSON *v = cJSON_GetObjectItem(in, "enabled");
|
||||
if (cJSON_IsBool(v)) {
|
||||
n.espnow_enabled = cJSON_IsTrue(v);
|
||||
}
|
||||
v = cJSON_GetObjectItem(in, "channel");
|
||||
if (cJSON_IsNumber(v)) {
|
||||
if (v->valueint < 1 || v->valueint > 13) {
|
||||
cJSON_Delete(in);
|
||||
return send_error(req, "400 Bad Request", "channel must be 1-13");
|
||||
}
|
||||
n.espnow_channel = (uint8_t)v->valueint;
|
||||
}
|
||||
cJSON_Delete(in);
|
||||
if (settings_save_net(&n) != ESP_OK) {
|
||||
return send_error(req, "500 Internal Server Error", "could not save");
|
||||
}
|
||||
cJSON *o = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(o, "message", "saved, restart to apply");
|
||||
return send_json(req, o);
|
||||
}
|
||||
|
||||
static const api_route_t s_api[] = {
|
||||
{"/api/status", HTTP_GET, status_get},
|
||||
{"/api/pattern", HTTP_POST, pattern_post},
|
||||
@@ -1164,6 +1252,8 @@ static const api_route_t s_api[] = {
|
||||
{"/api/network", HTTP_GET, network_get},
|
||||
{"/api/network", HTTP_POST, network_post},
|
||||
{"/api/ddp", HTTP_GET, ddp_get},
|
||||
{"/api/espnow", HTTP_GET, espnow_get},
|
||||
{"/api/espnow", HTTP_POST, espnow_post},
|
||||
{"/api/hardware", HTTP_GET, hardware_get},
|
||||
{"/api/hardware", HTTP_POST, hardware_post},
|
||||
{"/api/presets", HTTP_GET, presets_get},
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
#define WEB_API_MSG_MAX 2048 // a request message: {"method","path","body","host"}
|
||||
#define WEB_API_REPLY_MAX 8192 // a reply envelope: {"status","body"}
|
||||
#define WEB_API_REPLY_MAX 16384 // a reply envelope: {"status","body"}
|
||||
|
||||
/** @brief Start the HTTP server on port 80 (UI and JSON API). */
|
||||
esp_err_t web_start(void);
|
||||
|
||||
Reference in new issue
Block a user