feat: browser simulator served by the controller, desktop build, browser bridge

- led-simulator: the rig runs the C++ led-driver as WebAssembly (sim-driver/),
  one single-threaded worker per bar, no cross-origin isolation needed. Rooms
  (one JSON per room) with an on-page editor, and a pattern gallery. The
  Python server, Pyodide boot and ring buffer are removed.
- sim-controller sim_rig.c: serves /sim/ from an embedded pack, the page's
  WebSocket and the rooms/flash API. The desktop build embeds it too.
- bridge.c: a "browser" bridge kind that carries ESP-NOW frames to the page;
  devices announced through it are marked sim.
- UI: Simulator in the Menu, split view with resize and pop-out, swipe
  between controller and rig on phones; SIM/REAL badges in Devices.
- Fades: preset pushes and Wi-Fi driver bodies always carry fade and
  beat_time, so "off" no longer inherits an earlier fade.
- Also the sequences-v2 work in progress: desktop port, bridge firmware,
  serial bridge, Wi-Fi drivers, sequence timeline, audio and API changes.
- led-driver: ESP-NOW link, beat sync, control messages, sim hooks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Opus 5.5 committed 2026-11-16 19:32:46 +13:00
1 parent 698c1463c0
commit 581b751904
506 files changed
+42336 -3599

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
idf_component_register(SRCS "bridge_main.c" "bridge_eth.c"
INCLUDE_DIRS "."
REQUIRES esp_wifi esp_netif esp_event nvs_flash esp_driver_usb_serial_jtag esp_timer esp_hw_support
esp_driver_i2s esp_driver_gpio esp_driver_spi esp_eth lwip
w5500)
+85
View File
@@ -0,0 +1,85 @@
menu "Bridge Ethernet (W5500)"
config BRIDGE_ETH
bool "W5500 Ethernet with a UDP bridge"
default y if IDF_TARGET_ESP32S3
default n
help
The board has a W5500 on SPI, wired to the network. It takes UDP datagrams on BRIDGE_ETH_PORT
(dst_mac + packet, sent on ESP-NOW) and sends frames it hears back to the last sender (src_mac + packet).
Its address is logged over the USB link once DHCP answers.
config BRIDGE_ETH_PORT
int "UDP port"
depends on BRIDGE_ETH
default 8766
config BRIDGE_ETH_AUDIO_PORT
int "UDP control port (microphone audio and HELLO)"
depends on BRIDGE_ETH
default 8767
config BRIDGE_ETH_SPI_CLOCK_MHZ
int "SPI clock (MHz)"
depends on BRIDGE_ETH
range 5 80
default 20
config BRIDGE_ETH_SCLK_GPIO
int "SCLK GPIO (XIAO D8)"
depends on BRIDGE_ETH
default 7
config BRIDGE_ETH_MISO_GPIO
int "MISO GPIO (XIAO D9)"
depends on BRIDGE_ETH
default 8
config BRIDGE_ETH_MOSI_GPIO
int "MOSI GPIO (XIAO D10)"
depends on BRIDGE_ETH
default 9
config BRIDGE_ETH_CS_GPIO
int "CS GPIO (XIAO D1)"
depends on BRIDGE_ETH
default 2
config BRIDGE_ETH_INT_GPIO
int "INT GPIO (XIAO D2), -1 to poll"
depends on BRIDGE_ETH
default 3
config BRIDGE_ETH_RST_GPIO
int "RST GPIO (XIAO D0), -1 if not wired"
depends on BRIDGE_ETH
default 1
endmenu
menu "Bridge microphone (I2S)"
config BRIDGE_MIC
bool "Stream an I2S microphone to the host"
default y if IDF_TARGET_ESP32S3
default n
help
The host (led-controller on the desktop) can record from an I2S mic on this board
(INMP441 wiring, as on the controller's XIAO ESP32-S3). Off on boards without one.
config BRIDGE_MIC_SCK_GPIO
int "I2S SCK (bit clock) GPIO"
depends on BRIDGE_MIC
default 5
config BRIDGE_MIC_WS_GPIO
int "I2S WS (word select) GPIO"
depends on BRIDGE_MIC
default 6
config BRIDGE_MIC_SD_GPIO
int "I2S SD (data) GPIO"
depends on BRIDGE_MIC
default 43
endmenu
+123
View File
@@ -0,0 +1,123 @@
#include "bridge_eth.h"
#include <string.h>
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "esp_eth.h"
#include "esp_eth_mac_w5500.h"
#include "esp_eth_phy_w5500.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "esp_netif.h"
#include "sdkconfig.h"
static const char *TAG = "bridge_eth";
static volatile bool s_up;
static uint8_t s_ip[4];
static void on_got_ip(void *arg, esp_event_base_t base, int32_t id, void *data)
{
(void)arg;
(void)base;
(void)id;
const ip_event_got_ip_t *e = data;
s_up = true;
esp_ip4_addr_t a = e->ip_info.ip;
s_ip[0] = esp_ip4_addr1_16(&a);
s_ip[1] = esp_ip4_addr2_16(&a);
s_ip[2] = esp_ip4_addr3_16(&a);
s_ip[3] = esp_ip4_addr4_16(&a);
ESP_LOGI(TAG, "Ethernet address " IPSTR "/" IPSTR, IP2STR(&e->ip_info.ip), IP2STR(&e->ip_info.netmask));
}
static void on_eth_down(void *arg, esp_event_base_t base, int32_t id, void *data)
{
(void)arg;
(void)base;
(void)id;
(void)data;
s_up = false;
}
bool bridge_eth_up(void)
{
return s_up;
}
bool bridge_eth_ip(uint8_t ip[4])
{
if (!s_up) {
return false;
}
memcpy(ip, s_ip, 4);
return true;
}
esp_err_t bridge_eth_start(void)
{
// The INT pin uses a GPIO interrupt; the service may already be installed by another driver
esp_err_t isr = gpio_install_isr_service(0);
if (isr != ESP_OK && isr != ESP_ERR_INVALID_STATE) {
ESP_LOGE(TAG, "GPIO ISR service: %s", esp_err_to_name(isr));
return ESP_FAIL;
}
const spi_host_device_t host = SPI2_HOST;
spi_bus_config_t buscfg = {
.miso_io_num = CONFIG_BRIDGE_ETH_MISO_GPIO,
.mosi_io_num = CONFIG_BRIDGE_ETH_MOSI_GPIO,
.sclk_io_num = CONFIG_BRIDGE_ETH_SCLK_GPIO,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
};
if (spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO) != ESP_OK) {
ESP_LOGE(TAG, "SPI bus init failed");
return ESP_FAIL;
}
spi_device_interface_config_t devcfg = {
.mode = 0,
.clock_speed_hz = CONFIG_BRIDGE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
.queue_size = 20,
.spics_io_num = CONFIG_BRIDGE_ETH_CS_GPIO,
};
eth_w5500_config_t wcfg = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
wcfg.base.int_gpio_num = CONFIG_BRIDGE_ETH_INT_GPIO;
if (CONFIG_BRIDGE_ETH_INT_GPIO < 0) {
wcfg.base.poll_period_ms = 10; // no INT wire: poll the chip
}
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
phy_config.reset_gpio_num = CONFIG_BRIDGE_ETH_RST_GPIO;
esp_eth_mac_t *mac = esp_eth_mac_new_w5500(&wcfg, &mac_config);
esp_eth_phy_t *phy = esp_eth_phy_new_w5500(&phy_config);
if (!mac || !phy) {
ESP_LOGE(TAG, "W5500 MAC/PHY create failed");
return ESP_FAIL;
}
esp_eth_config_t ecfg = ETH_DEFAULT_CONFIG(mac, phy);
esp_eth_handle_t eth = NULL;
if (esp_eth_driver_install(&ecfg, &eth) != ESP_OK) {
ESP_LOGE(TAG, "W5500 not found: check its wiring and power");
spi_bus_free(host);
return ESP_FAIL;
}
// The W5500 has no factory address: use the board's Ethernet MAC from eFuse
uint8_t eth_mac[6];
esp_read_mac(eth_mac, ESP_MAC_ETH);
esp_eth_ioctl(eth, ETH_CMD_S_MAC_ADDR, eth_mac);
esp_netif_config_t ncfg = ESP_NETIF_DEFAULT_ETH();
esp_netif_t *netif = esp_netif_new(&ncfg);
if (!netif || esp_netif_attach(netif, esp_eth_new_netif_glue(eth)) != ESP_OK) {
ESP_LOGE(TAG, "Ethernet netif failed");
return ESP_FAIL;
}
esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, on_got_ip, NULL);
esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, on_eth_down, NULL);
if (esp_eth_start(eth) != ESP_OK) {
ESP_LOGE(TAG, "Ethernet start failed");
return ESP_FAIL;
}
ESP_LOGI(TAG, "W5500 on SPI, waiting for DHCP");
return ESP_OK;
}
+15
View File
@@ -0,0 +1,15 @@
#pragma once
// Ethernet for the bridge board: a W5500 on SPI, DHCP. The UDP bridge (bridge_main.c) runs on it: a host sends
// dst_mac + packet to this board's port and gets src_mac + packet back, the same as the simulator's bridge.
#include <stdbool.h>
#include <stdint.h>
#include "esp_err.h"
// Start the W5500 (pins from Kconfig). Its address is logged once DHCP answers. ESP_FAIL when there is no
// W5500 on the board; the USB bridge works without it.
esp_err_t bridge_eth_start(void);
bool bridge_eth_up(void);
// The board's IPv4 address (a.b.c.d) once DHCP has answered; false until then
bool bridge_eth_ip(uint8_t ip[4]);
+597
View File
@@ -0,0 +1,597 @@
/*
* USB ESP-NOW bridge. A computer talks to it over the USB-Serial-JTAG port in SLIP frames
* (bridge_proto.h); it sends each PKT's dst_mac + packet on ESP-NOW and returns every received frame
* as src_mac + packet. ESP-NOW runs on one Wi-Fi channel with no access point and no router.
*/
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "driver/gpio.h"
#include "driver/i2s_std.h"
#include "driver/usb_serial_jtag.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "esp_netif.h"
#include "esp_event.h"
#include "esp_now.h"
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "esp_timer.h"
#include "freertos/task.h"
#include "lwip/sockets.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "bridge_proto.h"
#include "sdkconfig.h"
#if CONFIG_BRIDGE_ETH
#include "bridge_eth.h"
#endif
static const char *TAG = "bridge";
#define DEFAULT_CHANNEL 6 // the controller's CONFIG_ESPNOW_CHANNEL
#define PEERS_MAX 19 // ESP-NOW allows 20 peers: 19 unicast and the broadcast one
#define TX_QUEUE_LEN 32
#define RX_QUEUE_LEN 32
#define SEND_WAIT_MS 100
typedef struct {
uint8_t mac[6];
uint8_t len;
uint8_t data[BRIDGE_PKT_MAX];
} pkt_t;
static const uint8_t BROADCAST[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
static QueueHandle_t s_txq;
static QueueHandle_t s_rxq;
static SemaphoreHandle_t s_sent; // given by the ESP-NOW send callback
static SemaphoreHandle_t s_usb_lock;
static volatile esp_now_send_status_t s_last_status;
static volatile bool s_usb_ready;
static uint8_t s_mac[6];
static uint8_t s_channel = DEFAULT_CHANNEL;
static uint8_t s_peers[PEERS_MAX][6]; // most recently used first
static int s_npeers;
static uint32_t s_tx, s_tx_fail, s_rx, s_drop;
static volatile bool s_mic_on; // the host asked for microphone audio (SET_MIC)
#if CONFIG_BRIDGE_ETH
// The UDP bridge: the host that sent the last datagram gets every frame the radio hears
static int s_udp = -1;
static SemaphoreHandle_t s_udp_lock;
static struct sockaddr_in s_udp_peer;
static bool s_udp_have_peer;
// The control port (BRIDGE_ETH_AUDIO_PORT): HELLO gets INFO back; SET_MIC 1 from a host makes this board send it
// microphone audio (AUDIO datagrams) until SET_MIC 1 stops arriving for MIC_UDP_TIMEOUT_MS
#define MIC_UDP_TIMEOUT_MS 2000
static int s_ctl = -1;
static struct sockaddr_in s_ctl_peer;
static bool s_ctl_mic;
static int64_t s_ctl_mic_ms;
#endif
/* ---- USB link ---------------------------------------------------------------------------------- */
// One whole frame per call, under the lock so frames from different tasks never interleave
#define USB_WRITE_PIECE 64
static void usb_send_frame(uint8_t type, const uint8_t *body, size_t n)
{
static uint8_t out[BRIDGE_ENC_MAX];
if (!s_usb_ready) {
return;
}
size_t len = bridge_encode(out, sizeof(out), type, body, n);
if (!len) {
return;
}
xSemaphoreTake(s_usb_lock, portMAX_DELAY);
size_t off = 0;
while (off < len) {
// The driver's TX ring takes a write only as a whole, so a frame longer than its ring is written in pieces
size_t piece = len - off < USB_WRITE_PIECE ? len - off : USB_WRITE_PIECE;
int w = usb_serial_jtag_write_bytes(out + off, piece, pdMS_TO_TICKS(200));
if (w <= 0) {
break; // host not reading: drop the rest rather than stall the radio
}
off += (size_t)w;
}
xSemaphoreGive(s_usb_lock);
}
// Log lines travel as LOG frames, so nothing but frames ever reaches the USB port
static int log_hook(const char *fmt, va_list args)
{
char line[200];
int n = vsnprintf(line, sizeof(line), fmt, args);
if (n > 0) {
size_t len = (size_t)n < sizeof(line) ? (size_t)n : sizeof(line) - 1;
usb_send_frame(BRIDGE_MSG_LOG, (const uint8_t *)line, len);
}
return n;
}
#if CONFIG_BRIDGE_MIC
#define BRIDGE_CAP_MIC_BOARD BRIDGE_CAP_MIC
#else
#define BRIDGE_CAP_MIC_BOARD 0
#endif
static void put_u32(uint8_t *p, uint32_t v)
{
p[0] = v & 0xFF;
p[1] = (v >> 8) & 0xFF;
p[2] = (v >> 16) & 0xFF;
p[3] = (v >> 24) & 0xFF;
}
// The INFO body (see bridge_proto.h), the same over USB and over the UDP control port
static void build_info(uint8_t *b)
{
b[0] = BRIDGE_PROTO_VERSION;
memcpy(b + 1, s_mac, 6);
b[7] = s_channel;
put_u32(b + 8, s_tx);
put_u32(b + 12, s_tx_fail);
put_u32(b + 16, s_rx);
put_u32(b + 20, s_drop);
uint8_t ip[4] = { 0, 0, 0, 0 };
uint8_t eth_cap = 0;
#if CONFIG_BRIDGE_ETH
if (bridge_eth_ip(ip)) {
eth_cap = BRIDGE_CAP_ETH;
}
#endif
b[24] = BRIDGE_CAP_MIC_BOARD | eth_cap;
memcpy(b + 25, ip, 4);
}
static void send_info(void)
{
uint8_t b[BRIDGE_INFO_LEN];
build_info(b);
usb_send_frame(BRIDGE_MSG_INFO, b, sizeof(b));
}
/* ---- ESP-NOW ----------------------------------------------------------------------------------- */
static void on_sent(const esp_now_send_info_t *info, esp_now_send_status_t status)
{
(void)info;
s_last_status = status;
xSemaphoreGive(s_sent);
}
static void on_recv(const esp_now_recv_info_t *info, const uint8_t *data, int len)
{
if (len <= 0 || len > BRIDGE_PKT_MAX) {
return;
}
pkt_t p;
memcpy(p.mac, info->src_addr, 6);
p.len = (uint8_t)len;
memcpy(p.data, data, len);
if (xQueueSend(s_rxq, &p, 0) == pdTRUE) {
s_rx++;
} else {
s_drop++;
}
}
// Keep the peer table within the ESP-NOW limit, least recently used dropped first
static void ensure_peer(const uint8_t *mac)
{
if (memcmp(mac, BROADCAST, 6) == 0) {
if (!esp_now_is_peer_exist(BROADCAST)) {
esp_now_peer_info_t p = { .channel = s_channel, .ifidx = WIFI_IF_STA, .encrypt = false };
memcpy(p.peer_addr, BROADCAST, 6);
esp_now_add_peer(&p);
}
return;
}
for (int i = 0; i < s_npeers; i++) {
if (memcmp(s_peers[i], mac, 6) == 0) {
uint8_t hit[6];
memcpy(hit, mac, 6);
memmove(s_peers[1], s_peers[0], i * 6);
memcpy(s_peers[0], hit, 6);
return;
}
}
if (s_npeers == PEERS_MAX) {
esp_now_del_peer(s_peers[PEERS_MAX - 1]);
s_npeers--;
}
esp_now_peer_info_t p = { .channel = s_channel, .ifidx = WIFI_IF_STA, .encrypt = false };
memcpy(p.peer_addr, mac, 6);
if (esp_now_add_peer(&p) != ESP_OK) {
return;
}
memmove(s_peers[1], s_peers[0], s_npeers * 6);
memcpy(s_peers[0], mac, 6);
s_npeers++;
}
static void drop_unicast_peers(void)
{
for (int i = 0; i < s_npeers; i++) {
esp_now_del_peer(s_peers[i]);
}
s_npeers = 0;
}
static void set_channel(uint8_t ch)
{
drop_unicast_peers(); // peers were added on the old channel
s_channel = ch;
esp_wifi_set_channel(ch, WIFI_SECOND_CHAN_NONE);
nvs_handle_t h;
if (nvs_open("bridge", NVS_READWRITE, &h) == ESP_OK) {
nvs_set_u8(h, "channel", ch);
nvs_commit(h);
nvs_close(h);
}
ESP_LOGI(TAG, "ESP-NOW channel %u", ch);
}
// One frame at a time, waiting for each send callback, so the radio is never overrun
static void tx_task(void *arg)
{
(void)arg;
pkt_t p;
for (;;) {
if (xQueueReceive(s_txq, &p, portMAX_DELAY) != pdTRUE) {
continue;
}
ensure_peer(p.mac);
if (esp_now_send(p.mac, p.data, p.len) != ESP_OK) {
s_tx_fail++;
continue;
}
if (xSemaphoreTake(s_sent, pdMS_TO_TICKS(SEND_WAIT_MS)) == pdTRUE && s_last_status == ESP_NOW_SEND_SUCCESS) {
s_tx++;
} else {
s_tx_fail++;
}
}
}
#if CONFIG_BRIDGE_ETH
static void udp_send_up(const uint8_t *body, size_t n);
#endif
// Received frames go to the host as src_mac + packet
static void rx_task(void *arg)
{
(void)arg;
pkt_t p;
uint8_t body[6 + BRIDGE_PKT_MAX];
for (;;) {
if (xQueueReceive(s_rxq, &p, portMAX_DELAY) != pdTRUE) {
continue;
}
memcpy(body, p.mac, 6);
memcpy(body + 6, p.data, p.len);
usb_send_frame(BRIDGE_MSG_PKT, body, 6 + p.len);
#if CONFIG_BRIDGE_ETH
udp_send_up(body, 6 + p.len);
#endif
}
}
#if CONFIG_BRIDGE_ETH
// Frames heard on ESP-NOW go to the last UDP sender as src_mac + packet
static void udp_send_up(const uint8_t *body, size_t n)
{
xSemaphoreTake(s_udp_lock, portMAX_DELAY);
if (s_udp_have_peer && s_udp >= 0) {
sendto(s_udp, body, n, 0, (const struct sockaddr *)&s_udp_peer, sizeof(s_udp_peer));
}
xSemaphoreGive(s_udp_lock);
}
// Datagrams from a host are dst_mac + packet, queued like a USB PKT frame. The sender becomes the peer.
static void udp_task(void *arg)
{
(void)arg;
uint8_t buf[6 + BRIDGE_PKT_MAX];
for (;;) {
struct sockaddr_in from;
socklen_t fl = sizeof(from);
int n = recvfrom(s_udp, buf, sizeof(buf), 0, (struct sockaddr *)&from, &fl);
if (n <= 6) {
continue;
}
xSemaphoreTake(s_udp_lock, portMAX_DELAY);
s_udp_peer = from;
s_udp_have_peer = true;
xSemaphoreGive(s_udp_lock);
pkt_t p;
memcpy(p.mac, buf, 6);
p.len = (uint8_t)(n - 6);
memcpy(p.data, buf + 6, p.len);
if (xQueueSend(s_txq, &p, 0) != pdTRUE) {
s_drop++;
}
}
}
#if CONFIG_BRIDGE_ETH
static void ctl_task(void *arg)
{
(void)arg;
uint8_t buf[32];
for (;;) {
struct sockaddr_in from;
socklen_t fl = sizeof(from);
int n = recvfrom(s_ctl, buf, sizeof(buf), 0, (struct sockaddr *)&from, &fl);
if (n < 1) {
vTaskDelay(pdMS_TO_TICKS(10));
continue;
}
if (buf[0] == BRIDGE_MSG_HELLO) {
uint8_t out[1 + BRIDGE_INFO_LEN];
out[0] = BRIDGE_MSG_INFO;
build_info(out + 1);
sendto(s_ctl, out, sizeof(out), 0, (struct sockaddr *)&from, fl);
} else if (buf[0] == BRIDGE_MSG_SET_MIC && n >= 2) {
xSemaphoreTake(s_udp_lock, portMAX_DELAY);
s_ctl_peer = from;
s_ctl_mic = buf[1] != 0;
s_ctl_mic_ms = esp_timer_get_time() / 1000;
xSemaphoreGive(s_udp_lock);
}
}
}
static void ctl_open(void)
{
s_ctl = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in a = { .sin_family = AF_INET, .sin_port = htons(CONFIG_BRIDGE_ETH_AUDIO_PORT), .sin_addr.s_addr = htonl(INADDR_ANY) };
if (s_ctl < 0 || bind(s_ctl, (struct sockaddr *)&a, sizeof(a)) != 0) {
ESP_LOGE(TAG, "UDP control port %d not open", CONFIG_BRIDGE_ETH_AUDIO_PORT);
return;
}
xTaskCreate(ctl_task, "udp_ctl", 4096, NULL, 5, NULL);
ESP_LOGI(TAG, "UDP control on port %d", CONFIG_BRIDGE_ETH_AUDIO_PORT);
}
#endif
static void udp_open(void)
{
ESP_LOGI(TAG, "udp: opening");
s_udp = socket(AF_INET, SOCK_DGRAM, 0);
ESP_LOGI(TAG, "udp: socket %d", s_udp);
struct sockaddr_in a = { .sin_family = AF_INET, .sin_port = htons(CONFIG_BRIDGE_ETH_PORT), .sin_addr.s_addr = htonl(INADDR_ANY) };
if (s_udp < 0 || bind(s_udp, (struct sockaddr *)&a, sizeof(a)) != 0) {
ESP_LOGE(TAG, "UDP port %d not open", CONFIG_BRIDGE_ETH_PORT);
return;
}
xTaskCreate(udp_task, "udp_rx", 4096, NULL, 5, NULL);
ESP_LOGI(TAG, "UDP bridge on port %d", CONFIG_BRIDGE_ETH_PORT);
ctl_open();
}
#endif
/* ---- host commands ----------------------------------------------------------------------------- */
static void handle_frame(const uint8_t *msg, size_t len)
{
if (len < 1) {
return;
}
const uint8_t *body = msg + 1;
size_t n = len - 1;
switch (msg[0]) {
case BRIDGE_MSG_PKT: {
if (n < 7 || n > 6 + BRIDGE_PKT_MAX) {
return;
}
pkt_t p;
memcpy(p.mac, body, 6);
p.len = (uint8_t)(n - 6);
memcpy(p.data, body + 6, p.len);
if (xQueueSend(s_txq, &p, 0) != pdTRUE) {
s_drop++;
}
break;
}
case BRIDGE_MSG_HELLO:
send_info();
break;
case BRIDGE_MSG_SET_CHANNEL:
if (n >= 1 && body[0] >= 1 && body[0] <= 13) {
set_channel(body[0]);
}
send_info();
break;
case BRIDGE_MSG_SET_MIC:
if (n >= 1) {
s_mic_on = body[0] != 0;
ESP_LOGI(TAG, "microphone %s", s_mic_on ? "on" : "off");
}
break;
default:
break;
}
}
static void host_rx_task(void *arg)
{
(void)arg;
uint8_t buf[64];
bridge_decoder_t dec = { 0 };
for (;;) {
int n = usb_serial_jtag_read_bytes(buf, sizeof(buf), pdMS_TO_TICKS(50));
for (int i = 0; i < n; i++) {
if (bridge_decode_feed(&dec, buf[i])) {
handle_frame(dec.buf, dec.frame_len);
}
}
}
}
/* ---- microphone (boards with BRIDGE_MIC) ------------------------------------------------------- */
#if CONFIG_BRIDGE_MIC
#define MIC_RATE 16000
static i2s_chan_handle_t s_mic_chan;
// Reads the I2S mic all the time (so the bus never stalls) and sends 10 ms of 16-bit mono while the host wants it.
// The mic drives one of the two slots and the other reads 0: the busier slot is the one kept.
static void mic_task(void *arg)
{
(void)arg;
static int32_t raw[BRIDGE_AUDIO_SAMPLES * 2];
uint8_t out[BRIDGE_AUDIO_BYTES];
uint8_t dg[1 + BRIDGE_AUDIO_BYTES]; // a UDP audio datagram: AUDIO, then the samples
for (;;) {
size_t got = 0;
esp_err_t err = i2s_channel_read(s_mic_chan, raw, sizeof(raw), &got, portMAX_DELAY);
if (err != ESP_OK || got == 0) {
continue;
}
size_t frames = got / (2 * sizeof(int32_t));
size_t nonzero[2] = { 0, 0 };
for (size_t i = 0; i < frames; i++) {
nonzero[0] += raw[2 * i] != 0;
nonzero[1] += raw[2 * i + 1] != 0;
}
int ch = nonzero[1] > nonzero[0] ? 1 : 0;
bool want_usb = s_mic_on;
bool want_udp = false;
struct sockaddr_in peer;
#if CONFIG_BRIDGE_ETH
xSemaphoreTake(s_udp_lock, portMAX_DELAY);
want_udp = s_ctl_mic && esp_timer_get_time() / 1000 - s_ctl_mic_ms < MIC_UDP_TIMEOUT_MS;
peer = s_ctl_peer;
xSemaphoreGive(s_udp_lock);
#endif
if (!want_usb && !want_udp) {
continue;
}
for (size_t i = 0; i < frames; i++) {
int16_t s = (int16_t)(raw[2 * i + ch] >> 16); // 24-bit sample in the top of the 32-bit slot
out[2 * i] = (uint8_t)(s & 0xFF);
out[2 * i + 1] = (uint8_t)((uint16_t)s >> 8);
}
if (want_usb) {
usb_send_frame(BRIDGE_MSG_AUDIO, out, 2 * frames);
}
#if CONFIG_BRIDGE_ETH
if (want_udp) {
dg[0] = BRIDGE_MSG_AUDIO;
memcpy(dg + 1, out, 2 * frames);
sendto(s_ctl, dg, 1 + 2 * frames, 0, (struct sockaddr *)&peer, sizeof(peer));
}
#endif
}
}
static void mic_start(void)
{
i2s_chan_config_t chan = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_0, I2S_ROLE_MASTER);
if (i2s_new_channel(&chan, NULL, &s_mic_chan) != ESP_OK) {
ESP_LOGE(TAG, "I2S channel create failed: no microphone");
return;
}
i2s_std_config_t std = {
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(MIC_RATE),
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_STEREO),
.gpio_cfg = {
.mclk = I2S_GPIO_UNUSED,
.bclk = CONFIG_BRIDGE_MIC_SCK_GPIO,
.ws = CONFIG_BRIDGE_MIC_WS_GPIO,
.dout = I2S_GPIO_UNUSED,
.din = CONFIG_BRIDGE_MIC_SD_GPIO,
},
};
if (i2s_channel_init_std_mode(s_mic_chan, &std) != ESP_OK || i2s_channel_enable(s_mic_chan) != ESP_OK) {
ESP_LOGE(TAG, "I2S mic init failed: no microphone");
return;
}
gpio_pulldown_en(CONFIG_BRIDGE_MIC_SD_GPIO); // SD tri-states outside the mic's slot
xTaskCreate(mic_task, "mic", 4096, NULL, 5, NULL);
ESP_LOGI(TAG, "I2S mic on SCK %d WS %d SD %d at %d Hz",
CONFIG_BRIDGE_MIC_SCK_GPIO, CONFIG_BRIDGE_MIC_WS_GPIO, CONFIG_BRIDGE_MIC_SD_GPIO, MIC_RATE);
}
#else
static void mic_start(void)
{
}
#endif
/* ---- start-up ---------------------------------------------------------------------------------- */
static void load_channel(void)
{
nvs_handle_t h;
if (nvs_open("bridge", NVS_READONLY, &h) == ESP_OK) {
uint8_t ch = 0;
if (nvs_get_u8(h, "channel", &ch) == ESP_OK && ch >= 1 && ch <= 13) {
s_channel = ch;
}
nvs_close(h);
}
}
void app_main(void)
{
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
nvs_flash_erase();
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
load_channel();
s_txq = xQueueCreate(TX_QUEUE_LEN, sizeof(pkt_t));
s_rxq = xQueueCreate(RX_QUEUE_LEN, sizeof(pkt_t));
s_sent = xSemaphoreCreateBinary();
s_usb_lock = xSemaphoreCreateMutex();
#if CONFIG_BRIDGE_ETH
s_udp_lock = xSemaphoreCreateMutex(); // the microphone task uses it from the start
#endif
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t wcfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&wcfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
ESP_ERROR_CHECK(esp_wifi_set_channel(s_channel, WIFI_SECOND_CHAN_NONE));
ESP_ERROR_CHECK(esp_wifi_get_mac(WIFI_IF_STA, s_mac));
ESP_ERROR_CHECK(esp_now_init());
ESP_ERROR_CHECK(esp_now_register_send_cb(on_sent));
ESP_ERROR_CHECK(esp_now_register_recv_cb(on_recv));
ensure_peer(BROADCAST);
usb_serial_jtag_driver_config_t ucfg = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT();
ESP_ERROR_CHECK(usb_serial_jtag_driver_install(&ucfg));
s_usb_ready = true;
esp_log_set_vprintf(log_hook);
xTaskCreate(tx_task, "espnow_tx", 4096, NULL, 6, NULL);
xTaskCreate(rx_task, "espnow_rx", 4096, NULL, 6, NULL);
xTaskCreate(host_rx_task, "usb_rx", 4096, NULL, 5, NULL);
mic_start();
#if CONFIG_BRIDGE_ETH
ESP_LOGI(TAG, "eth: starting");
esp_err_t eth_err = bridge_eth_start();
ESP_LOGI(TAG, "eth: %s", esp_err_to_name(eth_err));
if (eth_err == ESP_OK) {
udp_open();
}
#endif
ESP_LOGI(TAG, "bridge %02x:%02x:%02x:%02x:%02x:%02x on ESP-NOW channel %u",
s_mac[0], s_mac[1], s_mac[2], s_mac[3], s_mac[4], s_mac[5], s_channel);
}
+149
View File
@@ -0,0 +1,149 @@
/*
* USB ESP-NOW bridge framing, shared by bridge/ (ESP32-C3 firmware) and main/bridge_serial.c (host).
*
* Each message is one SLIP frame (END-delimited, ESC-escaped) holding [type][body][crc16 LE], where the
* CRC-16/CCITT-FALSE covers type and body. Bytes outside frames are ignored by the host.
*
* 00 PKT host -> bridge dst_mac(6) + packet sent on ESP-NOW to dst_mac
* 00 PKT bridge -> host src_mac(6) + packet every frame received on ESP-NOW
* 10 HELLO host -> bridge (empty) answered with INFO
* 11 INFO bridge -> host see bridge_info below
* 12 SET_CHANNEL host -> bridge channel(1) sets the ESP-NOW channel, answered with INFO
* 13 SET_MIC host -> bridge on(1) start (1) or stop (0) streaming the microphone
* 40 AUDIO bridge -> host BRIDGE_AUDIO_SAMPLES 16-bit LE mono samples at 16 kHz, while the mic is on
* 30 LOG bridge -> host text the bridge's log lines
*
* INFO: byte 24 is capabilities (bit 0 = a microphone, BRIDGE_CAP_MIC; bit 1 = Ethernet up, BRIDGE_CAP_ETH),
* bytes 25-28 the board's Ethernet IPv4 address (0.0.0.0 while it has none).
*/
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define BRIDGE_PROTO_VERSION 1
#define BRIDGE_PKT_MAX 250 // ESP-NOW payload limit
#define BRIDGE_AUDIO_SAMPLES 160 // 10 ms at 16 kHz
#define BRIDGE_AUDIO_BYTES (2 * BRIDGE_AUDIO_SAMPLES)
#define BRIDGE_PKT_BODY (6 + BRIDGE_PKT_MAX)
#define BRIDGE_BODY_MAX (BRIDGE_PKT_BODY > BRIDGE_AUDIO_BYTES ? BRIDGE_PKT_BODY : BRIDGE_AUDIO_BYTES)
#define BRIDGE_FRAME_MAX (BRIDGE_BODY_MAX + 3) // type + body + crc
#define BRIDGE_ENC_MAX (2 * BRIDGE_FRAME_MAX + 2)
#define BRIDGE_INFO_LEN 29
#define BRIDGE_CAP_MIC 0x01
#define BRIDGE_CAP_ETH 0x02
enum {
BRIDGE_MSG_PKT = 0x00,
BRIDGE_MSG_HELLO = 0x10,
BRIDGE_MSG_INFO = 0x11,
BRIDGE_MSG_SET_CHANNEL = 0x12,
BRIDGE_MSG_SET_MIC = 0x13,
BRIDGE_MSG_AUDIO = 0x40,
BRIDGE_MSG_LOG = 0x30,
};
#define SLIP_END 0xC0
#define SLIP_ESC 0xDB
#define SLIP_ESC_END 0xDC
#define SLIP_ESC_ESC 0xDD
static inline uint16_t bridge_crc16_update(uint16_t crc, uint8_t b)
{
crc ^= (uint16_t)b << 8;
for (int i = 0; i < 8; i++) {
crc = (crc & 0x8000) ? (uint16_t)((crc << 1) ^ 0x1021) : (uint16_t)(crc << 1);
}
return crc;
}
/* Encode one message into out (capacity cap). Returns the frame length, or 0 if it does not fit. */
static inline size_t bridge_encode(uint8_t *out, size_t cap, uint8_t type, const uint8_t *body, size_t n)
{
size_t off = 0;
uint16_t crc = 0xFFFF;
if (cap < 2) {
return 0;
}
out[off++] = SLIP_END;
/* the type and body bytes, then the CRC, each escaped as they go out */
for (size_t i = 0; i < n + 3; i++) {
uint8_t b;
if (i == 0) {
b = type;
crc = bridge_crc16_update(crc, b);
} else if (i <= n) {
b = body[i - 1];
crc = bridge_crc16_update(crc, b);
} else {
b = (i == n + 1) ? (uint8_t)(crc & 0xFF) : (uint8_t)(crc >> 8);
}
if (b == SLIP_END) {
if (off + 2 >= cap) return 0;
out[off++] = SLIP_ESC;
out[off++] = SLIP_ESC_END;
} else if (b == SLIP_ESC) {
if (off + 2 >= cap) return 0;
out[off++] = SLIP_ESC;
out[off++] = SLIP_ESC_ESC;
} else {
if (off + 1 >= cap) return 0;
out[off++] = b;
}
}
if (off + 1 > cap) return 0;
out[off++] = SLIP_END;
return off;
}
/* Incremental decoder. Feed bytes; when bridge_decode_feed returns true, d->buf holds [type][body] of
* length d->frame_len, valid until the next call. */
typedef struct {
uint8_t buf[BRIDGE_FRAME_MAX];
size_t len;
size_t frame_len;
bool esc;
bool overflow;
} bridge_decoder_t;
static inline bool bridge_decode_feed(bridge_decoder_t *d, uint8_t b)
{
if (b == SLIP_END) {
bool ok = false;
if (!d->overflow && d->len >= 3) {
uint16_t crc = 0xFFFF;
for (size_t i = 0; i < d->len - 2; i++) {
crc = bridge_crc16_update(crc, d->buf[i]);
}
uint16_t got = (uint16_t)(d->buf[d->len - 2] | (d->buf[d->len - 1] << 8));
ok = crc == got;
if (ok) {
d->frame_len = d->len - 2;
}
}
d->len = 0;
d->esc = false;
d->overflow = false;
return ok;
}
if (d->esc) {
d->esc = false;
if (b == SLIP_ESC_END) {
b = SLIP_END;
} else if (b == SLIP_ESC_ESC) {
b = SLIP_ESC;
} else {
d->overflow = true; // not valid SLIP: drop the frame
}
} else if (b == SLIP_ESC) {
d->esc = true;
return false;
}
if (d->len < sizeof(d->buf)) {
d->buf[d->len++] = b;
} else {
d->overflow = true;
}
return false;
}
+3
View File
@@ -0,0 +1,3 @@
dependencies:
espressif/w5500:
version: "^2.0.1"