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:
1 parent
d664026ee5
commit
2543673bd8
68 files changed
+5761
-460
No files matched your search
+106
-9
@@ -4,13 +4,18 @@
|
||||
#include "esp_check.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_now.h"
|
||||
#include "esp_rom_sys.h"
|
||||
#include "esp_random.h"
|
||||
#include "esp_timer.h"
|
||||
#include "bench.h"
|
||||
#include "bridge.h"
|
||||
#if !CONFIG_IDF_TARGET_LINUX
|
||||
#include "esp_netif.h"
|
||||
#include "esp_now.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_mac.h"
|
||||
#include "esp_wifi_ap_get_sta_list.h"
|
||||
#endif
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/semphr.h"
|
||||
@@ -32,7 +37,7 @@ typedef struct {
|
||||
|
||||
static QueueHandle_t s_bulk_q;
|
||||
static SemaphoreHandle_t s_bulk_done;
|
||||
static volatile esp_now_send_status_t s_bulk_status;
|
||||
static volatile bool s_bulk_ok;
|
||||
static volatile bool s_bulk_inflight;
|
||||
static volatile int64_t s_last_beat_us;
|
||||
static esp_timer_handle_t s_copy_timer;
|
||||
@@ -44,6 +49,26 @@ static bool s_have_peer;
|
||||
static espnow_stats_t s_stats;
|
||||
static volatile int s_loss_pct;
|
||||
static volatile bool s_loss_beats = true;
|
||||
static espnow_rx_cb_t s_rx_cb;
|
||||
static char s_bridge[80];
|
||||
|
||||
void espnow_tx_set_bridge(const char *addr)
|
||||
{
|
||||
snprintf(s_bridge, sizeof(s_bridge), "%s", addr ? addr : "");
|
||||
}
|
||||
|
||||
// One frame onto the air, or to the UDP bridge while one is set
|
||||
static esp_err_t radio_send(const uint8_t *dst, const void *data, size_t len)
|
||||
{
|
||||
if (bridge_active()) {
|
||||
return bridge_send(dst, data, len);
|
||||
}
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
#else
|
||||
return esp_now_send(dst, data, len);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool espnow_tx_drop(void)
|
||||
{
|
||||
@@ -64,17 +89,39 @@ int espnow_tx_loss(bool *include_beats)
|
||||
return s_loss_pct;
|
||||
}
|
||||
|
||||
#if !CONFIG_IDF_TARGET_LINUX
|
||||
static void IRAM_ATTR send_cb(const esp_now_send_info_t *info, esp_now_send_status_t status)
|
||||
{
|
||||
(void)info;
|
||||
if (s_bulk_inflight) {
|
||||
s_bulk_status = status;
|
||||
s_bulk_ok = status == ESP_NOW_SEND_SUCCESS;
|
||||
BaseType_t woken = pdFALSE;
|
||||
xSemaphoreGiveFromISR(s_bulk_done, &woken);
|
||||
portYIELD_FROM_ISR(woken);
|
||||
}
|
||||
}
|
||||
|
||||
static void recv_cb(const esp_now_recv_info_t *info, const uint8_t *data, int len)
|
||||
{
|
||||
if (s_rx_cb) {
|
||||
s_rx_cb(info->src_addr, data, len);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void bridge_rx(const uint8_t *src, const uint8_t *data, int len)
|
||||
{
|
||||
if (s_rx_cb) {
|
||||
s_rx_cb(src, data, len);
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t espnow_tx_set_rx_cb(espnow_rx_cb_t cb)
|
||||
{
|
||||
s_rx_cb = cb;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// Each copy carries a fire_in_ms reduced by the time already spent, so every
|
||||
// copy points at the same instant.
|
||||
static void send_beat_copy(void *arg)
|
||||
@@ -87,7 +134,7 @@ static void send_beat_copy(void *arg)
|
||||
|
||||
if (s_loss_beats && espnow_tx_drop()) {
|
||||
s_stats.sim_dropped++; // lossy-link test: this copy never reaches the radio
|
||||
} else if (esp_now_send(BROADCAST, (const uint8_t *)&s_pending, sizeof(s_pending)) != ESP_OK) {
|
||||
} else if (radio_send(BROADCAST, &s_pending, sizeof(s_pending)) != ESP_OK) {
|
||||
s_stats.beat_send_fail++;
|
||||
}
|
||||
s_last_beat_us = esp_timer_get_time();
|
||||
@@ -134,6 +181,12 @@ static void on_beat(const beat_info_t *info, float bpm, bool downbeat)
|
||||
|
||||
static esp_err_t select_peer(const uint8_t *mac)
|
||||
{
|
||||
if (bridge_active()) {
|
||||
return ESP_OK; // no peer table behind the bridge
|
||||
}
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
#else
|
||||
if (s_have_peer && memcmp(s_cur_peer, mac, 6) == 0) {
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -149,6 +202,7 @@ static esp_err_t select_peer(const uint8_t *mac)
|
||||
memcpy(s_cur_peer, mac, 6);
|
||||
s_have_peer = true;
|
||||
return ESP_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void bulk_task(void *arg)
|
||||
@@ -180,9 +234,11 @@ static void bulk_task(void *arg)
|
||||
if (espnow_tx_drop()) {
|
||||
s_stats.sim_dropped++; // lossy-link test: pretend the radio sent it
|
||||
ok = true;
|
||||
} else if (esp_now_send(dst, b.data, b.len) == ESP_OK &&
|
||||
} else if (bridge_active()) {
|
||||
ok = bridge_send(dst, b.data, b.len) == ESP_OK; // no MAC-layer ack: rel.c's acks retry
|
||||
} else if (radio_send(dst, b.data, b.len) == ESP_OK &&
|
||||
xSemaphoreTake(s_bulk_done, pdMS_TO_TICKS(30)) == pdTRUE) {
|
||||
ok = s_bulk_status == ESP_NOW_SEND_SUCCESS || b.broadcast;
|
||||
ok = s_bulk_ok || b.broadcast;
|
||||
}
|
||||
s_bulk_inflight = false;
|
||||
}
|
||||
@@ -210,6 +266,7 @@ void espnow_tx_stats(espnow_stats_t *out)
|
||||
*out = s_stats;
|
||||
}
|
||||
|
||||
#if !CONFIG_IDF_TARGET_LINUX
|
||||
// A link-local IPv6 address on the AP lets the mDNS responder answer the AAAA half of a browser's
|
||||
// lookup; without it the browser waits about a second for an answer that never comes.
|
||||
static esp_netif_t *s_ap_netif;
|
||||
@@ -220,7 +277,22 @@ static void ap_event(void *arg, esp_event_base_t base, int32_t id, void *data)
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t espnow_tx_init(const char *ap_ssid, const char *ap_password)
|
||||
int espnow_tx_ap_clients(char *out, size_t n)
|
||||
{
|
||||
int len = snprintf(out, n, "[");
|
||||
wifi_sta_list_t sl;
|
||||
wifi_sta_mac_ip_list_t ml;
|
||||
if (s_ap_netif && esp_wifi_ap_get_sta_list(&sl) == ESP_OK && esp_wifi_ap_get_sta_list_with_ip(&sl, &ml) == ESP_OK) {
|
||||
for (int i = 0; i < ml.num && len < (int)n - 64; i++) {
|
||||
len += snprintf(out + len, n - len, "%s{\"mac\":\"" MACSTR "\",\"ip\":\"" IPSTR "\"}", i ? "," : "",
|
||||
MAC2STR(ml.sta[i].mac), IP2STR(&ml.sta[i].ip));
|
||||
}
|
||||
}
|
||||
len += snprintf(out + len, n - len, "]");
|
||||
return len;
|
||||
}
|
||||
|
||||
static esp_err_t radio_init(const char *ap_ssid, const char *ap_password)
|
||||
{
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_RETURN_ON_ERROR(esp_wifi_init(&cfg), TAG, "wifi init");
|
||||
@@ -264,6 +336,32 @@ esp_err_t espnow_tx_init(const char *ap_ssid, const char *ap_password)
|
||||
esp_now_peer_info_t bc = { .channel = 0, .ifidx = WIFI_IF_STA, .encrypt = false };
|
||||
memcpy(bc.peer_addr, BROADCAST, 6);
|
||||
ESP_RETURN_ON_ERROR(esp_now_add_peer(&bc), TAG, "broadcast peer");
|
||||
ESP_RETURN_ON_ERROR(esp_now_register_recv_cb(recv_cb), TAG, "recv cb");
|
||||
ESP_LOGI(TAG, "ESP-NOW up on channel %d, %d beat copies", CONFIG_ESPNOW_CHANNEL, CONFIG_BEAT_TX_COPIES);
|
||||
return ESP_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
int espnow_tx_ap_clients(char *out, size_t n)
|
||||
{
|
||||
return snprintf(out, n, "[]");
|
||||
}
|
||||
#endif
|
||||
|
||||
esp_err_t espnow_tx_init(const char *ap_ssid, const char *ap_password)
|
||||
{
|
||||
#if CONFIG_IDF_TARGET_LINUX
|
||||
(void)ap_ssid;
|
||||
(void)ap_password;
|
||||
ESP_RETURN_ON_FALSE(s_bridge[0], ESP_ERR_INVALID_STATE, TAG, "the host build needs a bridge address");
|
||||
#else
|
||||
// The radio still comes up behind a bridge: the access point serves the web UI
|
||||
ESP_RETURN_ON_ERROR(radio_init(ap_ssid, ap_password), TAG, "radio");
|
||||
#endif
|
||||
if (s_bridge[0]) {
|
||||
ESP_RETURN_ON_ERROR(bridge_start(s_bridge, bridge_rx), TAG, "bridge");
|
||||
}
|
||||
|
||||
s_bulk_q = xQueueCreate(BULK_QUEUE_LEN, sizeof(bulk_t));
|
||||
s_bulk_done = xSemaphoreCreateBinary();
|
||||
@@ -274,6 +372,5 @@ esp_err_t espnow_tx_init(const char *ap_ssid, const char *ap_password)
|
||||
xTaskCreate(bulk_task, "espnow_bulk", 4096, NULL, 5, NULL);
|
||||
|
||||
beat_set_callback(on_beat);
|
||||
ESP_LOGI(TAG, "ESP-NOW up on channel %d, %d beat copies", CONFIG_ESPNOW_CHANNEL, CONFIG_BEAT_TX_COPIES);
|
||||
return ESP_OK;
|
||||
}
|
||||
Reference in new issue
Block a user