P4-Nano: controller port with the C6 as ESP-NOW radio, and the simulator
The controller now builds for the Waveshare ESP32-P4-Nano only (plus the linux sim and the desktop); the S3 stays for the USB/network bridge in bridge/. - radio-c6/: C6 firmware, esp-hosted plus ESP-NOW over peer data. The P4 loads it from the slave_fw partition when the C6 runs another version (c6_update.c) - bridge_hosted.c: the C6 as the always-on "c6" bridge, and the P4 as a network bridge on UDP 8766 for the desktop - Ethernet on the P4's EMAC, ES8311 mic; default address 192.168.50.20. The W5500, INMP441 and direct ESP-NOW code is gone - The simulated rig at /sim/ is built into the board image, with an always-on "browser" bridge. Its frames go through a queue so a slow page cannot hold up the radio, and frames for simulated bars are not sent over the C6. Receiver buffers are on the heap; a bar's flash file is only written when it changes - HTTP server: more handlers, sockets and stack for the rig - docs/P4_NANO.md, README and Makefile updated; graphify output Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ba8e160aee
commit
4ab0c7c6d7
101 files changed
+3284
-971
No files matched your search
@@ -5,9 +5,12 @@
|
||||
#include "sim_net.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/select.h>
|
||||
#include <unistd.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_timer.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
@@ -43,8 +46,10 @@ typedef struct {
|
||||
} ddp_t;
|
||||
|
||||
static SemaphoreHandle_t s_mu;
|
||||
static universe_t s_uni[MAX_UNIVERSES];
|
||||
static ddp_t s_ddp[SIM_NET_MAX_DDP_PORTS];
|
||||
static universe_t *s_uni; // [MAX_UNIVERSES]. These are 130 KB together: on the heap, so in PSRAM on the board
|
||||
static ddp_t *s_ddp; // [SIM_NET_MAX_DDP_PORTS]
|
||||
static uint8_t *s_frame; // flush's frame to the page
|
||||
#define FRAME_CAP (1 + 6 + SIM_NET_DDP_PIXELS * 3)
|
||||
static uint16_t s_want[SIM_NET_MAX_DDP_PORTS]; // the ports the room asks for
|
||||
static int s_nwant;
|
||||
static bool s_want_changed;
|
||||
@@ -237,7 +242,7 @@ static void sync_ddp(void)
|
||||
// Send what changed since the last tick to the page
|
||||
static void flush(void)
|
||||
{
|
||||
static uint8_t frame[1 + 6 + SIM_NET_DDP_PIXELS * 3];
|
||||
uint8_t *frame = s_frame;
|
||||
for (int i = 0; i < MAX_UNIVERSES; i++) {
|
||||
xSemaphoreTake(s_mu, portMAX_DELAY);
|
||||
bool send = s_uni[i].uni >= 0 && s_uni[i].dirty;
|
||||
@@ -330,13 +335,21 @@ void sim_net_start(void)
|
||||
if (s_mu) {
|
||||
return;
|
||||
}
|
||||
s_mu = xSemaphoreCreateMutex();
|
||||
s_uni = calloc(MAX_UNIVERSES, sizeof(*s_uni));
|
||||
s_ddp = calloc(SIM_NET_MAX_DDP_PORTS, sizeof(*s_ddp));
|
||||
s_frame = malloc(FRAME_CAP);
|
||||
SemaphoreHandle_t mu = xSemaphoreCreateMutex();
|
||||
if (!s_uni || !s_ddp || !s_frame || !mu) {
|
||||
ESP_LOGE(TAG, "no memory: Art-Net and DDP fixtures stay dark");
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < MAX_UNIVERSES; i++) {
|
||||
s_uni[i].uni = -1;
|
||||
}
|
||||
for (int i = 0; i < SIM_NET_MAX_DDP_PORTS; i++) {
|
||||
s_ddp[i].fd = -1;
|
||||
}
|
||||
s_mu = mu;
|
||||
const char *env = getenv("LED_SIM_ARTNET_PORT");
|
||||
if (env && env[0]) {
|
||||
s_art_port = atoi(env);
|
||||
|
||||
@@ -46,6 +46,12 @@
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
#if !CONFIG_IDF_TARGET_LINUX
|
||||
#include "freertos/idf_additions.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/task.h"
|
||||
#include "lwip/sockets.h"
|
||||
#endif
|
||||
#include "sim_net.h"
|
||||
#include "store.h"
|
||||
|
||||
@@ -126,7 +132,7 @@ static esp_err_t send_file(httpd_req_t *req, const char *path)
|
||||
if (!find_file("index.html", &data, &len)) {
|
||||
httpd_resp_set_type(req, "text/plain");
|
||||
return httpd_resp_sendstr(req, "The simulated rig is not in this build: run `make sim-wasm`, then rebuild "
|
||||
"(make sim-web, or make desktop).");
|
||||
"(make build, make sim-web or make desktop).");
|
||||
}
|
||||
return httpd_resp_send_404(req);
|
||||
}
|
||||
@@ -669,7 +675,10 @@ static cJSON *room_names(void)
|
||||
{
|
||||
char dir[300];
|
||||
snprintf(dir, sizeof(dir), "%s/sim/rooms", db_root());
|
||||
char names[64][41];
|
||||
char (*names)[41] = malloc(64 * sizeof(*names)); // 2.6 KB: too much for the board's task stacks
|
||||
if (!names) {
|
||||
return cJSON_CreateArray();
|
||||
}
|
||||
int n = 0;
|
||||
DIR *dp = opendir(dir);
|
||||
struct dirent *e;
|
||||
@@ -691,6 +700,7 @@ static cJSON *room_names(void)
|
||||
for (int i = 0; i < n; i++) {
|
||||
cJSON_AddItemToArray(a, cJSON_CreateString(names[i]));
|
||||
}
|
||||
free(names);
|
||||
return a;
|
||||
}
|
||||
|
||||
@@ -802,7 +812,7 @@ static const cJSON *fixture_by_mac(const cJSON *room, const char *mac12)
|
||||
/* ------------------------------------------------------------------ the page's link */
|
||||
|
||||
static httpd_handle_t s_hd;
|
||||
static int s_page_fd = -1;
|
||||
static volatile int s_page_fd = -1;
|
||||
static SemaphoreHandle_t s_tx_mu; // one frame at a time to the page
|
||||
|
||||
static bool page_open_locked(void)
|
||||
@@ -815,13 +825,9 @@ static bool page_open_locked(void)
|
||||
|
||||
bool sim_rig_page_open(void)
|
||||
{
|
||||
if (!s_tx_mu) {
|
||||
return false;
|
||||
}
|
||||
xSemaphoreTake(s_tx_mu, portMAX_DELAY);
|
||||
bool open = page_open_locked();
|
||||
xSemaphoreGive(s_tx_mu);
|
||||
return open;
|
||||
// Without s_tx_mu: a page that stopped reading holds it for seconds, and bridge.c asks with its own lock held
|
||||
int fd = s_page_fd;
|
||||
return s_hd && fd >= 0 && httpd_ws_get_fd_info(s_hd, fd) == HTTPD_WS_CLIENT_WEBSOCKET;
|
||||
}
|
||||
|
||||
static bool send_frame(int fd, httpd_ws_type_t type, const uint8_t *data, size_t len)
|
||||
@@ -845,9 +851,40 @@ bool sim_rig_page_send(const uint8_t *frame, size_t len)
|
||||
return ok;
|
||||
}
|
||||
|
||||
#if !CONFIG_IDF_TARGET_LINUX
|
||||
// On the board the C6 radio shares bridge_send with the page, so a slow page must not hold it up: the frames go
|
||||
// through a queue, and a full queue loses them, as on air
|
||||
#define TX_QUEUE_LEN 64
|
||||
typedef struct {
|
||||
uint16_t len;
|
||||
uint8_t data[1 + 6 + 250];
|
||||
} tx_frame_t;
|
||||
static QueueHandle_t s_txq;
|
||||
|
||||
static void tx_task(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
tx_frame_t f;
|
||||
for (;;) {
|
||||
xQueueReceive(s_txq, &f, portMAX_DELAY);
|
||||
sim_rig_page_send(f.data, f.len);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Called by bridge.c for the "browser" bridge: dst_mac(6) + packet, sent to the page behind a 0 byte
|
||||
bool sim_rig_tx(const uint8_t *frame, size_t len)
|
||||
{
|
||||
#if !CONFIG_IDF_TARGET_LINUX
|
||||
tx_frame_t f;
|
||||
if (!s_txq || s_page_fd < 0 || len + 1 > sizeof(f.data)) {
|
||||
return false;
|
||||
}
|
||||
f.len = (uint16_t)(len + 1);
|
||||
f.data[0] = 0;
|
||||
memcpy(f.data + 1, frame, len);
|
||||
return xQueueSend(s_txq, &f, 0) == pdTRUE;
|
||||
#else
|
||||
uint8_t buf[1 + 6 + 256];
|
||||
if (len + 1 > sizeof(buf)) {
|
||||
return false;
|
||||
@@ -855,6 +892,7 @@ bool sim_rig_tx(const uint8_t *frame, size_t len)
|
||||
buf[0] = 0;
|
||||
memcpy(buf + 1, frame, len);
|
||||
return sim_rig_page_send(buf, len + 1);
|
||||
#endif
|
||||
}
|
||||
|
||||
// The receiver listens on the DDP ports the room's DDP fixtures use
|
||||
@@ -889,6 +927,10 @@ static esp_err_t ws_open(httpd_req_t *req)
|
||||
return ESP_FAIL;
|
||||
}
|
||||
int fd = httpd_req_to_sockfd(req);
|
||||
#if !CONFIG_IDF_TARGET_LINUX
|
||||
int one = 1; // small frames: do not let Nagle hold them for the page's delayed ACK
|
||||
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
|
||||
#endif
|
||||
xSemaphoreTake(s_tx_mu, portMAX_DELAY);
|
||||
int old = page_open_locked() ? s_page_fd : -1;
|
||||
s_page_fd = fd;
|
||||
@@ -999,8 +1041,10 @@ static esp_err_t flash_get(httpd_req_t *req, const char *mac_in)
|
||||
sim_path(p, sizeof(p), "flash", h, ".json");
|
||||
cJSON *room = load_room(s_room, err, sizeof(err));
|
||||
const cJSON *f = room ? fixture_by_mac(room, h) : NULL;
|
||||
cJSON *nvs = read_json(p);
|
||||
if (!nvs) {
|
||||
char *before = read_text(p, FLASH_MAX);
|
||||
cJSON *nvs = before ? cJSON_Parse(before) : NULL;
|
||||
if (!cJSON_IsObject(nvs)) {
|
||||
cJSON_Delete(nvs);
|
||||
nvs = cJSON_CreateObject();
|
||||
}
|
||||
cJSON *led = cJSON_GetObjectItem(nvs, "led");
|
||||
@@ -1027,9 +1071,14 @@ static esp_err_t flash_get(httpd_req_t *req, const char *mac_in)
|
||||
NVS_SET("espnow_ch", "i64", cJSON_CreateNumber(WIFI_CHANNEL));
|
||||
NVS_SET("espnow", "str", cJSON_CreateString("on"));
|
||||
#undef NVS_SET
|
||||
xSemaphoreTake(s_mu, portMAX_DELAY);
|
||||
write_json(p, nvs, false);
|
||||
xSemaphoreGive(s_mu);
|
||||
char *after = cJSON_PrintUnformatted(nvs);
|
||||
if (after && (!before || strcmp(before, after))) { // only a change is written: every page load asks for every bar
|
||||
xSemaphoreTake(s_mu, portMAX_DELAY);
|
||||
write_text(p, after);
|
||||
xSemaphoreGive(s_mu);
|
||||
}
|
||||
free(before);
|
||||
free(after);
|
||||
cJSON_Delete(room);
|
||||
return api_json(req, "200 OK", nvs, true);
|
||||
}
|
||||
@@ -1250,6 +1299,12 @@ esp_err_t sim_rig_register(httpd_handle_t server)
|
||||
if (!s_mu || !s_tx_mu) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
#if !CONFIG_IDF_TARGET_LINUX
|
||||
s_txq = xQueueCreateWithCaps(TX_QUEUE_LEN, sizeof(tx_frame_t), MALLOC_CAP_SPIRAM);
|
||||
if (!s_txq || xTaskCreate(tx_task, "sim_tx", 4096, NULL, 4, NULL) != pdPASS) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
#endif
|
||||
load_active();
|
||||
sim_net_start();
|
||||
active_room_ports();
|
||||
|
||||
Reference in new issue
Block a user