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:
jimmyandClaude Sonnet 5.5 committed 2026-11-10 17:47:27 +13:00
1 parent d664026ee5
commit 2543673bd8
68 files changed
+5761 -460

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
build/
sdkconfig
sdkconfig.old
data/
dependencies.lock
+6
View File
@@ -0,0 +1,6 @@
# led-controller firmware built for the ESP-IDF linux target, for led-simulator. See README.md.
cmake_minimum_required(VERSION 3.16)
set(EXTRA_COMPONENT_DIRS ${CMAKE_CURRENT_LIST_DIR}/../main ${CMAKE_CURRENT_LIST_DIR}/components/sim)
set(COMPONENTS main sim)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(led_controller_sim)
+24
View File
@@ -0,0 +1,24 @@
# sim-controller
The led-controller firmware (`main/`) built for ESP-IDF's **linux** target, so the whole rig runs on a
computer with no hardware:
```
sim-controller (http://localhost:8080) ──UDP 8766──▶ led-simulator ──▶ 16 × led-driver/src
◀── src_mac + packet ── ◀──
```
```bash
make sim # headless drivers (CPython), plus this controller
make sim-web # drivers in the browser at http://localhost:8765/, plus this controller
make test-sim # test/api against it, including test_rig_sim.py
```
- Same sources as the board, minus Ethernet, Wi-Fi, mic and firmware OTA (`components/sim`: `app_main`,
`esp_timer`, mic/OTA/littlefs stubs, `driver/gpio.h` and lwip socket headers). The beat clock at
`audio_simulated_bpm` stands in for the mic.
- ESP-NOW goes through `main/bridge.c`: `dst_mac + packet` to the simulator, `src_mac + packet` back.
On the board the same bridge runs when `settings.bridge` is set.
- The store is a directory: `LED_SIM_DB` (default `sim-controller/data` under `make sim`).
- `LED_SIM_HTTP_PORT` (default 8080), `LED_SIM_BRIDGE` (default `127.0.0.1:8766`).
- The binary runs on the host, not in Docker: rootless Docker's `--network host` is its own namespace.
@@ -0,0 +1,6 @@
idf_component_register(SRCS "sim_main.c" "sim_stubs.c" "sim_timer.c"
INCLUDE_DIRS "include"
REQUIRES esp_http_server esp_timer esp_event json main freertos)
target_link_libraries(${COMPONENT_LIB} INTERFACE m)
# main's objects need esp_timer and libm, which come after it on the link line: link sim (and libm) last
target_link_libraries(__idf_main INTERFACE ${COMPONENT_LIB} m)
@@ -0,0 +1,21 @@
#pragma once
// Host build: no pins. Calls succeed and do nothing.
#include <stdint.h>
#include "esp_err.h"
typedef int gpio_num_t;
typedef enum { GPIO_MODE_DISABLE = 0, GPIO_MODE_INPUT = 1, GPIO_MODE_OUTPUT = 2, GPIO_MODE_INPUT_OUTPUT = 3 } gpio_mode_t;
typedef enum { GPIO_PULLUP_DISABLE = 0, GPIO_PULLUP_ENABLE = 1 } gpio_pullup_t;
typedef enum { GPIO_PULLDOWN_DISABLE = 0, GPIO_PULLDOWN_ENABLE = 1 } gpio_pulldown_t;
typedef enum { GPIO_INTR_DISABLE = 0 } gpio_int_type_t;
typedef struct {
uint64_t pin_bit_mask;
gpio_mode_t mode;
gpio_pullup_t pull_up_en;
gpio_pulldown_t pull_down_en;
gpio_int_type_t intr_type;
} gpio_config_t;
static inline esp_err_t gpio_config(const gpio_config_t *c) { (void)c; return ESP_OK; }
static inline esp_err_t gpio_set_direction(int pin, gpio_mode_t m) { (void)pin; (void)m; return ESP_OK; }
static inline esp_err_t gpio_set_level(int pin, uint32_t l) { (void)pin; (void)l; return ESP_OK; }
static inline int gpio_get_level(int pin) { (void)pin; return 1; }
static inline esp_err_t gpio_reset_pin(int pin) { (void)pin; return ESP_OK; }
@@ -0,0 +1,11 @@
#pragma once
// Host build: the store's /db is a real directory (docker volume), so mounting is a no-op
#include <stdbool.h>
#include "esp_err.h"
typedef struct {
const char *base_path;
const char *partition_label;
bool format_if_mount_failed;
bool grow_on_mount;
} esp_vfs_littlefs_conf_t;
esp_err_t esp_vfs_littlefs_register(const esp_vfs_littlefs_conf_t *conf);
@@ -0,0 +1,2 @@
#pragma once
#include <netdb.h>
@@ -0,0 +1,9 @@
#pragma once
// Host build: lwip's socket API is the host's
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
+126
View File
@@ -0,0 +1,126 @@
/*
* led-controller on the host (ESP-IDF linux target), for led-simulator.
*
* The same firmware sources as the board, minus the hardware: no Ethernet, Wi-Fi, mic or OTA.
* ESP-NOW frames go to the UDP bridge (led-simulator/bridge_sim.py) instead of the radio.
*
* Environment:
* LED_SIM_BRIDGE simulator address, default 127.0.0.1:8766 (overrides settings "bridge")
* LED_SIM_HTTP_PORT web UI / API port, default 8080
* The store is the /db directory (mount a volume there to keep it).
*/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_event.h"
#include "esp_http_server.h"
#include "esp_timer.h"
#include "cJSON.h"
#include "api.h"
#include "audio.h"
#include "audio_api.h"
#include "beat.h"
#include "drv_ota.h"
#include "engine.h"
#include "espnow_tx.h"
#include "bridge.h"
#include "mic.h"
#include "ota.h"
#include "rel.h"
#include "seqplay.h"
#include "store.h"
#include "web.h"
#include "ws.h"
static const char *TAG = "sim";
static esp_err_t status_get_handler(httpd_req_t *req)
{
beat_info_t bi;
espnow_stats_t es;
rel_stats_t rs;
char buf[1024];
beat_get_info(&bi);
espnow_tx_stats(&es);
rel_stats(&rs);
const char *br = bridge_addr();
snprintf(buf, sizeof(buf),
"{\"sim\":true,\"bridge\":\"%s\",\"ap\":false,\"ip\":\"127.0.0.1\",\"mac\":\"02:51:4d:00:00:00\",\"uptime_s\":%lld,"
"\"reset_reason\":\"power_on\",\"free_heap\":0,\"free_internal\":0,"
"\"mic_rms_dbfs\":-120.0,\"mic_peak_dbfs\":-120.0,\"mic_spl_db\":0.0,"
"\"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}}",
br ? br : "", (long long)(esp_timer_get_time() / 1000000),
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",
(unsigned long)bi.downbeat_count,
(unsigned long)es.beats_sent, (unsigned long)es.beat_send_fail, (unsigned long)es.bulk_sent,
(unsigned long)es.bulk_retries, (unsigned long)es.bulk_failed, (unsigned long)es.sim_dropped, rel_ver(),
(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);
}
static void start_webserver(void)
{
static httpd_handle_t server;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
const char *port = getenv("LED_SIM_HTTP_PORT");
config.server_port = port ? atoi(port) : 8080;
config.ctrl_port = config.server_port + 10000;
config.uri_match_fn = httpd_uri_match_wildcard;
config.max_uri_handlers = 80;
config.stack_size = 32768; // in StackType_t words on linux (8 bytes); the depth type is 16-bit
config.lru_purge_enable = true;
config.max_open_sockets = 12;
config.backlog_conn = 10;
if (httpd_start(&server, &config) != ESP_OK) {
ESP_LOGE(TAG, "HTTP server start failed on port %d", config.server_port);
abort();
}
const httpd_uri_t status_uri = { .uri = "/status", .method = HTTP_GET, .handler = status_get_handler };
httpd_register_uri_handler(server, &status_uri);
web_register(server);
audio_register(server);
audio_api_register(server);
ota_register(server);
drv_ota_register(server);
api_register(server);
ws_register(server);
ESP_LOGI(TAG, "web UI and API on http://localhost:%d/", config.server_port);
}
static const char *settings_str(const char *key)
{
cJSON *v = cJSON_GetObjectItem(store_settings(), key);
return cJSON_IsString(v) && v->valuestring[0] ? v->valuestring : NULL;
}
void app_main(void)
{
signal(SIGPIPE, SIG_IGN); // a browser that hangs up mid-reply is an error return, as on lwip, not a kill
ESP_ERROR_CHECK(store_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
const char *br = getenv("LED_SIM_BRIDGE");
if (!br || !br[0]) {
br = settings_str("bridge");
}
espnow_tx_set_bridge(br ? br : "127.0.0.1:8766");
ESP_ERROR_CHECK(espnow_tx_init(NULL, NULL));
start_webserver();
ESP_ERROR_CHECK(rel_init());
ESP_ERROR_CHECK(engine_init());
ESP_ERROR_CHECK(mic_start());
ESP_ERROR_CHECK(seqplay_init());
for (;;) {
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
+61
View File
@@ -0,0 +1,61 @@
// Host build: what the board's hardware files (mic.c, ota.c, littlefs) provide, without hardware
#include <string.h>
#include <sys/stat.h>
#include "esp_littlefs.h"
#include "esp_log.h"
#include "beat.h"
#include "mic.h"
#include "ota.h"
esp_err_t esp_vfs_littlefs_register(const esp_vfs_littlefs_conf_t *conf)
{
mkdir(conf->base_path, 0755);
return ESP_OK;
}
// No microphone: audio stays stopped, so the simulated-BPM clock times sequences
esp_err_t mic_start(void)
{
return beat_start(MIC_SAMPLE_RATE); // never fed: no beats detected, the BPM clock stands in
}
void mic_get_level(mic_level_t *out)
{
out->rms_dbfs = -120;
out->peak_dbfs = -120;
out->spl_db = 0;
}
esp_err_t mic_stream_open(void)
{
return ESP_ERR_NOT_SUPPORTED;
}
size_t mic_stream_read(void *buf, size_t len, TickType_t timeout)
{
(void)buf;
(void)len;
vTaskDelay(timeout ? timeout : 1);
return 0;
}
void mic_stream_close(void)
{
}
void ota_confirm_later(void)
{
}
static esp_err_t ota_unsupported(httpd_req_t *req)
{
httpd_resp_set_status(req, "501 Not Implemented");
httpd_resp_set_type(req, "application/json");
return httpd_resp_sendstr(req, "{\"error\":\"firmware OTA is not available in the simulator\"}");
}
esp_err_t ota_register(httpd_handle_t server)
{
const httpd_uri_t u = { .uri = "/ota", .method = HTTP_POST, .handler = ota_unsupported };
return httpd_register_uri_handler(server, &u);
}
+95
View File
@@ -0,0 +1,95 @@
// Host build: esp_timer (the linux target ships only its header). One FreeRTOS task per timer.
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
struct esp_timer {
esp_timer_cb_t cb;
void *arg;
volatile int64_t due_us; // 0 = stopped
volatile int64_t period_us;
SemaphoreHandle_t wake;
};
int64_t esp_timer_get_time(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
}
static void timer_task(void *arg)
{
struct esp_timer *t = arg;
for (;;) {
int64_t due = t->due_us;
if (!due) {
xSemaphoreTake(t->wake, portMAX_DELAY);
continue;
}
int64_t wait = due - esp_timer_get_time();
if (wait > 0) {
TickType_t ticks = (TickType_t)((wait + 999) / 1000 / portTICK_PERIOD_MS);
if (xSemaphoreTake(t->wake, ticks ? ticks : 1) == pdTRUE) {
continue; // restarted or stopped: look again
}
if (t->due_us != due || esp_timer_get_time() < due) {
continue;
}
}
t->due_us = t->period_us ? due + t->period_us : 0;
t->cb(t->arg);
}
}
esp_err_t esp_timer_create(const esp_timer_create_args_t *args, esp_timer_handle_t *out)
{
struct esp_timer *t = calloc(1, sizeof(*t));
if (!t) {
return ESP_ERR_NO_MEM;
}
t->cb = args->callback;
t->arg = args->arg;
t->wake = xSemaphoreCreateBinary();
xTaskCreate(timer_task, args->name ? args->name : "esp_timer", 16384, t, 22, NULL);
*out = t;
return ESP_OK;
}
static esp_err_t arm(esp_timer_handle_t t, uint64_t us, uint64_t period)
{
t->period_us = period;
t->due_us = esp_timer_get_time() + (int64_t)us;
if (!t->due_us) {
t->due_us = 1;
}
xSemaphoreGive(t->wake);
return ESP_OK;
}
esp_err_t esp_timer_start_once(esp_timer_handle_t t, uint64_t timeout_us)
{
return arm(t, timeout_us, 0);
}
esp_err_t esp_timer_start_periodic(esp_timer_handle_t t, uint64_t period)
{
return arm(t, period, period);
}
esp_err_t esp_timer_stop(esp_timer_handle_t t)
{
bool was = t->due_us != 0;
t->due_us = 0;
xSemaphoreGive(t->wake);
return was ? ESP_OK : ESP_ERR_INVALID_STATE;
}
bool esp_timer_is_active(esp_timer_handle_t t)
{
return t->due_us != 0;
}
+7
View File
@@ -0,0 +1,7 @@
CONFIG_IDF_TARGET="linux"
CONFIG_FREERTOS_HZ=1000
CONFIG_ESPNOW_CHANNEL=5
CONFIG_HTTPD_WS_SUPPORT=y
CONFIG_HTTPD_WS_POST_HANDSHAKE_CB_SUPPORT=y
CONFIG_HTTPD_MAX_REQ_HDR_LEN=1024
CONFIG_LWIP_MAX_SOCKETS=16