Files
led-controller/sim-controller/components/sim/sim_main.c
T
jimmyandClaude Haiku 5.5 50ca46fee6 feat: sequences v2, driver frame sync and submodule updates
- Sequences v2, bridges and sim-controller changes from the working tree.
- Records the led-driver, led-tool and led-simulator commits above.
- Includes dmx-driver/ and graphify-out/.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-11-11 14:10:56 +13:00

125 lines
4.7 KiB
C

/*
* 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 "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);
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));
}
}