/* * led-controller on the host (ESP-IDF linux target, and the desktop build), with the simulated rig. * * The same firmware sources as the board, minus the hardware: no Ethernet, Wi-Fi, mic or OTA. * ESP-NOW frames go to the bridges in settings instead of the radio. With none set, that is the "browser" * bridge: the rig page this program serves at /sim/ (sim_rig.c), where every bar is the C++ led-driver. * * Environment: * LED_SIM_BRIDGE one bridge address, overriding settings "bridges": "browser", host:port or /dev/... * LED_SIM_HTTP_PORT web UI / API port, default 8080 * The store is the /db directory (mount a volume there to keep it). */ #include #include #include #include #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 "wifi_drv.h" #include "mic.h" #include "ota.h" #include "rel.h" #include "seqplay.h" #include "sim_rig.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[3072]; char first_bridge[BRIDGE_ADDR_MAX] = ""; char bridge_addrs[BRIDGE_MAX_LINKS][BRIDGE_ADDR_MAX]; if (bridge_list(bridge_addrs, BRIDGE_MAX_LINKS) > 0) { snprintf(first_bridge, sizeof(first_bridge), "%s", bridge_addrs[0]); } char bridges_json[1024]; bridge_status_json(bridges_json, sizeof(bridges_json)); beat_get_info(&bi); espnow_tx_stats(&es); rel_stats(&rs); snprintf(buf, sizeof(buf), "{\"sim\":true,\"bridge\":\"%s\",\"bridges\":%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}," "\"bridge_links\":%s}", first_bridge, bridges_json, (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, bridges_json); 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 = 96; config.stack_size = 32768; // in StackType_t words on linux (8 bytes); the depth type is 16-bit config.lru_purge_enable = true; // The board's limit (lwIP) does not apply here: the rig page alone opens several connections per bar while // its drivers load (the worker scripts, the wasm), on top of the controller's own UI. Under CONFIG_LWIP_MAX_SOCKETS - 3. config.max_open_sockets = 48; 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); sim_rig_register(server); 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/, the simulated rig on http://localhost:%d/sim/", config.server_port, 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()); char bridges[BRIDGE_MAX_LINKS][BRIDGE_ADDR_MAX]; size_t nb = 0; const char *env = getenv("LED_SIM_BRIDGE"); if (env && env[0]) { snprintf(bridges[0], sizeof(bridges[0]), "%s", env); nb = 1; } else { nb = store_bridges(bridges, BRIDGE_MAX_LINKS); } if (nb == 0) { snprintf(bridges[0], sizeof(bridges[0]), "%s", BRIDGE_BROWSER); // the rig page at /sim/ nb = 1; } espnow_tx_set_bridges(bridges, nb); ESP_ERROR_CHECK(espnow_tx_init(NULL, NULL)); start_webserver(); wifi_drv_start_discovery(); // desktop: finds Wi-Fi drivers over mDNS; a no-op on the simulator ESP_ERROR_CHECK(rel_init()); ESP_ERROR_CHECK(engine_init()); ESP_ERROR_CHECK(mic_start()); ESP_ERROR_CHECK(seqplay_init()); #ifdef LED_DESKTOP_ALSA audio_api_resume_mic(); #endif for (;;) { vTaskDelay(pdMS_TO_TICKS(1000)); } }