feat(v2): ESP32-S3 controller with Ethernet, I2S mic beat detection and ESP-NOW
New ESP-IDF project in v2/ that replaces the Pi and the UART bridge: - W5500 Ethernet, INMP441 beat/tempo detector (from esp32-eth), ESP-NOW master - LittleFS data store and a JSON API over the Pi's db/*.json collections, with /db import and export; web UI minified and gzipped at build time - Zone play/stop as 0x4C GROUP_CMD, preset push/send as CMD (binary v2 envelope, byte-identical to src/util/binary_envelope.py), sequencer and scenes, driver registration from the boot hello - Reliable bulk link: acks with retransmit, fragments, state-version digest and resync; beat frames sent twice with a lead so drivers fire together - Bench tooling: lossy-link mode, synthetic beats with echo measurement, USB and logic-analyzer latency scripts, and docs/BENCH_TEST.md Needs the matching led-driver changes (p2p branch) to receive the link and beat frames. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7336c7fb22
commit
a94d6a7de5
37 files changed
+5109
No files matched your search
+163
@@ -0,0 +1,163 @@
|
||||
#include "store.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "esp_check.h"
|
||||
#include "esp_littlefs.h"
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
static const char *TAG = "store";
|
||||
|
||||
static const char *const NAMES[] = {
|
||||
"device", "zone", "preset", "profile", "palette", "pattern",
|
||||
"sequence", "scene", "group", "output", "settings",
|
||||
};
|
||||
#define N_COLL (sizeof(NAMES) / sizeof(NAMES[0]))
|
||||
|
||||
static cJSON *s_coll[N_COLL];
|
||||
static SemaphoreHandle_t s_lock;
|
||||
|
||||
static int index_of(const char *name)
|
||||
{
|
||||
for (size_t i = 0; i < N_COLL; i++) {
|
||||
if (strcmp(NAMES[i], name) == 0) {
|
||||
return (int)i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static esp_err_t mount(const char *label, const char *base)
|
||||
{
|
||||
esp_vfs_littlefs_conf_t conf = {
|
||||
.base_path = base,
|
||||
.partition_label = label,
|
||||
.format_if_mount_failed = true,
|
||||
};
|
||||
return esp_vfs_littlefs_register(&conf);
|
||||
}
|
||||
|
||||
static cJSON *load_file(const char *name)
|
||||
{
|
||||
char path[48];
|
||||
snprintf(path, sizeof(path), "/db/%s.json", name);
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) {
|
||||
return cJSON_CreateObject();
|
||||
}
|
||||
fseek(f, 0, SEEK_END);
|
||||
long n = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
char *buf = malloc(n + 1);
|
||||
cJSON *obj = NULL;
|
||||
if (buf) {
|
||||
buf[fread(buf, 1, n, f)] = 0;
|
||||
obj = cJSON_Parse(buf);
|
||||
free(buf);
|
||||
}
|
||||
fclose(f);
|
||||
if (!cJSON_IsObject(obj)) {
|
||||
ESP_LOGW(TAG, "%s: unreadable, starting empty", path);
|
||||
cJSON_Delete(obj);
|
||||
obj = cJSON_CreateObject();
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
esp_err_t store_init(void)
|
||||
{
|
||||
s_lock = xSemaphoreCreateRecursiveMutex();
|
||||
ESP_RETURN_ON_FALSE(s_lock, ESP_ERR_NO_MEM, TAG, "mutex");
|
||||
ESP_RETURN_ON_ERROR(mount("www", "/www"), TAG, "mount www");
|
||||
ESP_RETURN_ON_ERROR(mount("db", "/db"), 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]));
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
bool store_known(const char *name)
|
||||
{
|
||||
return index_of(name) >= 0;
|
||||
}
|
||||
|
||||
cJSON *store_coll(const char *name)
|
||||
{
|
||||
int i = index_of(name);
|
||||
return i < 0 ? NULL : s_coll[i];
|
||||
}
|
||||
|
||||
esp_err_t store_save(const char *name)
|
||||
{
|
||||
int i = index_of(name);
|
||||
ESP_RETURN_ON_FALSE(i >= 0, ESP_ERR_NOT_FOUND, TAG, "unknown collection");
|
||||
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);
|
||||
FILE *f = fopen(tmp, "wb");
|
||||
esp_err_t ret = ESP_FAIL;
|
||||
if (f) {
|
||||
size_t len = strlen(json);
|
||||
bool ok = fwrite(json, 1, len, f) == len;
|
||||
ok = fclose(f) == 0 && ok;
|
||||
if (ok) {
|
||||
remove(path); // littlefs rename-over is not guaranteed on every version
|
||||
ok = rename(tmp, path) == 0;
|
||||
}
|
||||
ret = ok ? ESP_OK : ESP_FAIL;
|
||||
}
|
||||
free(json);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "saving %s failed", name);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t store_replace(const char *name, cJSON *obj)
|
||||
{
|
||||
int i = index_of(name);
|
||||
if (i < 0 || !cJSON_IsObject(obj)) {
|
||||
cJSON_Delete(obj);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
cJSON_Delete(s_coll[i]);
|
||||
s_coll[i] = obj;
|
||||
return store_save(name);
|
||||
}
|
||||
|
||||
void store_next_id(const char *name, char *buf, size_t len)
|
||||
{
|
||||
long max = 0;
|
||||
cJSON *c = store_coll(name), *it;
|
||||
cJSON_ArrayForEach(it, c)
|
||||
{
|
||||
char *end;
|
||||
long v = strtol(it->string, &end, 10);
|
||||
if (*end == 0 && v > max) {
|
||||
max = v;
|
||||
}
|
||||
}
|
||||
snprintf(buf, len, "%ld", max + 1);
|
||||
}
|
||||
|
||||
cJSON *store_settings(void)
|
||||
{
|
||||
return store_coll("settings");
|
||||
}
|
||||
|
||||
void store_lock(void)
|
||||
{
|
||||
xSemaphoreTakeRecursive(s_lock, portMAX_DELAY);
|
||||
}
|
||||
|
||||
void store_unlock(void)
|
||||
{
|
||||
xSemaphoreGiveRecursive(s_lock);
|
||||
}
|
||||
Reference in new issue
Block a user