From c9838d1cbec293675c7390d82d1e83232eb45378 Mon Sep 17 00:00:00 2001 From: jimmy Date: Sat, 21 Nov 2026 01:18:55 +1300 Subject: [PATCH] led-driver: channel default 11, bare ESP-NOW build, settings and USB config updates Default ESP-NOW channel is now 11. Adds the bare ESP-NOW-only build option (CONFIG_LED_ESPNOW_ONLY) with its sdkconfig fragments, a default hostname from the Wi-Fi MAC, and the related net, link, web and settings changes. Co-Authored-By: Claude Haiku 5.5 --- Makefile | 27 +++++++++++++-- main/Kconfig.projbuild | 25 ++++++++++++++ main/ctrl_msg.cpp | 42 +++++++++++++++++++++-- main/espnow_link.cpp | 8 ++++- main/main.cpp | 22 ++++++++++-- main/net.cpp | 78 +++++++++++++++++++++++++++++++++++++++--- main/net.h | 5 +++ main/patterns.cpp | 24 ++++++++++--- main/patterns.h | 5 +++ main/presets.cpp | 5 ++- main/presets.h | 2 +- main/settings.cpp | 23 ++++++++++--- main/settings.h | 2 +- main/web.cpp | 15 ++++++-- main/web.h | 3 +- sdkconfig.espnow | 8 +++++ sdkconfig.timing | 3 ++ web/index.html | 2 +- 18 files changed, 268 insertions(+), 31 deletions(-) create mode 100644 main/Kconfig.projbuild create mode 100644 sdkconfig.espnow create mode 100644 sdkconfig.timing diff --git a/Makefile b/Makefile index 75709eb..a964c65 100644 --- a/Makefile +++ b/Makefile @@ -38,20 +38,40 @@ define idf endef endif -.PHONY: all build flash monitor flash-monitor menuconfig clean fullclean help +# A running led-controller (desktop or `make rig`) probes every serial port while it looks for a bridge +# board, and a probe in the middle of a flash breaks the write (the erase still gets through). The flash +# and erase targets stop while one runs; FORCE=1 flashes anyway. Not checked on Windows. +APP_PROCS := led-controller|led_controller_ + +.PHONY: all no-app build flash erase monitor flash-monitor menuconfig clean fullclean help all: build +ifeq ($(HOST_OS),windows) +no-app: +else +no-app: + @pids=$$(pgrep -x '$(APP_PROCS)' | xargs); \ + if [ -n "$$pids" ] && [ -z "$(FORCE)" ]; then \ + echo "led-controller is running (pid $$pids). It probes serial ports and breaks flashing: quit it first, or add FORCE=1."; \ + exit 1; \ + fi +endif + build: $(call idf,build) -flash: build +flash: no-app build $(call idf,-p $(PORT) flash) +# Erase the whole flash on PORT (firmware and stored settings); make flash afterwards +erase: no-app + $(call idf,-p $(PORT) erase-flash) + monitor: $(call idf,-p $(PORT) monitor) -flash-monitor: build +flash-monitor: no-app build $(call idf,-p $(PORT) flash monitor) menuconfig: @@ -67,6 +87,7 @@ help: @echo Targets: @echo build Build the firmware @echo flash Build, then flash to the board on PORT + @echo erase Erase the whole flash on PORT, stored settings included @echo monitor Open the serial monitor on PORT @echo flash-monitor Build, flash, then open the serial monitor @echo menuconfig Open the project configuration menu diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild new file mode 100644 index 0000000..587eb87 --- /dev/null +++ b/main/Kconfig.projbuild @@ -0,0 +1,25 @@ +menu "LED driver" + + config LED_ESPNOW_ONLY + bool "Bare ESP-NOW driver (no Wi-Fi, web, USB config, mDNS, DDP or OTA)" + default n + help + Only the LEDs and the ESP-NOW link run: the radio is a station that never + joins a network, on the saved ESP-NOW channel, with no access point. The + controller changes the settings over ESP-NOW (device_config: name, LED + count, data pin, colour order, brightness, startup mode, ESP-NOW channel). + Nothing else can: there is no web interface and no USB config port, so + set the channel before flashing this (led-tool/espnow_ap_off.py does), or + flash it again over USB (the chip's ROM loader always works). + sdkconfig.espnow turns it on; build it in build-espnow. + + config LED_TIMING_LOG + bool "Log each sequence step's select with a timestamp (timing test)" + default n + help + Prints one USB log line per step select, with the chip's microsecond clock, so a + controller's step cadence can be checked against the driver's own timing. It goes + out on the USB port, so only build it for a bench test with the port being read. + sdkconfig.timing turns it on; build it in build-timing. + +endmenu diff --git a/main/ctrl_msg.cpp b/main/ctrl_msg.cpp index a3d639a..4a4b15f 100644 --- a/main/ctrl_msg.cpp +++ b/main/ctrl_msg.cpp @@ -9,6 +9,11 @@ #include "esp_log.h" #include "esp_mac.h" +#include "esp_timer.h" +#include "sdkconfig.h" +#include "esp_system.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" #include "nvs.h" #include "nvs_flash.h" @@ -347,6 +352,12 @@ void apply_device_config(const cJSON *dc) } } } + // The strip's data pin: 0-10 or 20-21 (11-19 are the flash and USB), as /api/hardware allows + v = cJSON_GetObjectItem(dc, "pin"); + if (cJSON_IsNumber(v) && ((v->valueint >= 0 && v->valueint <= 10) || v->valueint == 20 || v->valueint == 21)) { + hw.pin = v->valueint; + changed = true; + } if (changed) { settings_save_hw(&hw); patterns_set_hardware(hw); @@ -355,6 +366,25 @@ void apply_device_config(const cJSON *dc) if (cJSON_IsString(v) && strcmp(v->valuestring, "off") == 0) { presets_set_startup(""); } + // The ESP-NOW channel: saved, then the driver restarts onto it (after the ack has gone). Move the + // controller's bridge to the same channel, or the driver is out of reach. + v = cJSON_GetObjectItem(dc, "espnow_channel"); + if (cJSON_IsNumber(v) && v->valueint >= 1 && v->valueint <= 13) { + net_cfg_t n; + settings_load_net(&n); + if (n.espnow_channel != v->valueint) { + n.espnow_channel = (uint8_t)v->valueint; + settings_save_net(&n); + ESP_LOGI(TAG, "ESP-NOW channel %u saved: restarting onto it", n.espnow_channel); + // A task, not an esp_timer: the fixture driver's host build (fixture-driver/) has no esp_timer + static bool restarting; + if (!restarting) { + restarting = true; + xTaskCreate([](void *) { vTaskDelay(pdMS_TO_TICKS(1000)); esp_restart(); }, "ch_restart", 2048, NULL, 1, + NULL); + } + } + } // "reversed" is accepted and ignored: the C++ driver does not reverse its strip yet } @@ -420,6 +450,10 @@ void apply_select(const cJSON *sel) // an entry is [name, step] or a bare name; step is not used by this driver const cJSON *name = cJSON_IsArray(e) ? cJSON_GetArrayItem(e, 0) : e; if (!cJSON_IsString(name) || !name->valuestring[0]) return; +#ifdef CONFIG_LED_TIMING_LOG + // Timing test: when each step's select arrives, on this chip's clock (see main/Kconfig.projbuild) + ESP_LOGI(TAG, "step select %s t=%lld us", name->valuestring, (long long)esp_timer_get_time()); +#endif if (presets_select(name->valuestring) == ESP_OK) { return; } @@ -634,10 +668,12 @@ size_t ctrl_build_announce(uint8_t *out, size_t cap) for (int i = 0; i < 6; i++) { if (strcmp(hw.order, orders[i]) == 0) co = i; } - char cfg[160]; + // The settings the controller can change over ESP-NOW (device_config), so it shows what the driver has + char cfg[200]; snprintf(cfg, sizeof(cfg), - "{\"max_brightness\":%u,\"num_leds\":%d,\"color_order\":\"%s\",\"startup_mode\":\"default\",\"reversed\":false}", - (unsigned)hw.max_brightness, hw.num_leds, hw.order); + "{\"max_brightness\":%u,\"num_leds\":%d,\"color_order\":\"%s\",\"startup_mode\":\"default\",\"reversed\":false," + "\"pin\":%d,\"espnow_channel\":%u}", + (unsigned)hw.max_brightness, hw.num_leds, hw.order, hw.pin, (unsigned)n.espnow_channel); size_t cl = strlen(cfg); size_t need = 3 + nl + 6 + 1 + cl; if (need > cap || need > 250) return 0; diff --git a/main/espnow_link.cpp b/main/espnow_link.cpp index e545939..eb5999e 100644 --- a/main/espnow_link.cpp +++ b/main/espnow_link.cpp @@ -17,6 +17,7 @@ #include "esp_timer.h" #include "esp_wifi.h" #include "freertos/FreeRTOS.h" +#include "sdkconfig.h" #include "freertos/queue.h" #include "freertos/task.h" @@ -384,8 +385,11 @@ void link_pass(TickType_t wait) void link_task(void *arg) { (void)arg; + // pdMS_TO_TICKS(5) is 0 at CONFIG_FREERTOS_HZ=100: the pass would never block or yield, and the idle task + // would starve (task watchdog reset). Wait at least one tick. + constexpr TickType_t kWait = pdMS_TO_TICKS(5) > 0 ? pdMS_TO_TICKS(5) : 1; for (;;) { - link_pass(pdMS_TO_TICKS(5)); + link_pass(kWait); } } @@ -398,10 +402,12 @@ esp_err_t espnow_link_start(void) } net_cfg_t n; settings_load_net(&n); +#if !CONFIG_LED_ESPNOW_ONLY // the bare build is ESP-NOW whatever the setting says if (!n.espnow_enabled) { ESP_LOGI(TAG, "ESP-NOW is off in settings"); return ESP_OK; } +#endif ctrl_init(); // ESP-NOW uses the radio's channel: the access point's, or the router's when the client is joined. // It is not set here, since changing the channel while the client connects breaks the association. diff --git a/main/main.cpp b/main/main.cpp index 0692dc6..dcb203d 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -3,6 +3,7 @@ */ #include "esp_log.h" #include "nvs_flash.h" +#include "sdkconfig.h" #include "ddp.h" #include "espnow_link.h" @@ -31,13 +32,28 @@ extern "C" void app_main(void) ESP_ERROR_CHECK(presets_init()); presets_apply_startup(); ESP_ERROR_CHECK(net_start()); - espnow_link_start(); // ESP-NOW from led-controller, if enabled in settings - ESP_ERROR_CHECK(web_start()); + espnow_link_start(); // ESP-NOW from led-controller (always, in the bare build) +#if CONFIG_LED_ESPNOW_ONLY + // Bare ESP-NOW driver: nothing else runs, and nothing else is linked in (no calls to it). The settings + // come from the controller over ESP-NOW. + ota_confirm_later(); // a freshly flashed image is marked good (app rollback is on) + ESP_LOGI(TAG, "Ready: bare ESP-NOW driver (settings from the controller over ESP-NOW)"); +#else usb_cfg_start(); // led-tool over USB; the LEDs and Wi-Fi work without it - ESP_ERROR_CHECK(ddp_start()); + // ESP-NOW only (access point off, Wi-Fi client off): nothing reaches the driver over IP, so no web + // interface or DDP. Turning the access point on starts the web interface (net.cpp). + if (!net_espnow_only()) { + ESP_ERROR_CHECK(web_start()); + ESP_ERROR_CHECK(ddp_start()); + } ota_confirm_later(); + if (net_espnow_only()) { + ESP_LOGI(TAG, "Ready: ESP-NOW only (set up over USB with led-cli)"); + return; + } char ssid[16]; net_ap_ssid(ssid, sizeof(ssid)); ESP_LOGI(TAG, "Ready: join Wi-Fi %s and open http://192.168.4.1/, or http://led.local/ on your network", ssid); +#endif } diff --git a/main/net.cpp b/main/net.cpp index 9e64db8..edb523d 100644 --- a/main/net.cpp +++ b/main/net.cpp @@ -13,6 +13,8 @@ #include "esp_wifi.h" #include "mdns.h" #include "net.h" +#include "sdkconfig.h" +#include "web.h" static const char *TAG = "net"; @@ -31,6 +33,8 @@ static int s_retries; // then stays down for about a minute (AP_FALLBACK_RETRIES reconnect attempts), the access point comes back. #define AP_FALLBACK_RETRIES 6 static volatile bool s_ap_on; +static bool s_net_up; // net_start has finished +static bool s_mdns_on; // mDNS was started (not in ESP-NOW-only mode) static bool s_ap_want; // the state ap_timer switches to static esp_timer_handle_t s_ap_timer; @@ -48,6 +52,22 @@ static void apply_sta_config(const wifi_creds_t *c) esp_wifi_set_config(WIFI_IF_STA, &cfg); } +// ESP-NOW uses the radio's channel, which the access point (on the ESP-NOW channel) or a joined network sets. +// With ESP-NOW on the client stays off, so with the access point off too nothing sets it: set it here. +// Not while a client is joined (it would break the association). Before esp_wifi_start this fails +// harmlessly; net_start calls it again after the start. +static void pin_espnow_channel(void) +{ + net_cfg_t n; + settings_load_net(&n); + if (n.espnow_enabled && !s_sta_up) { + esp_err_t err = esp_wifi_set_channel(n.espnow_channel, WIFI_SECOND_CHAN_NONE); + if (err == ESP_OK) { + ESP_LOGI(TAG, "Radio on ESP-NOW channel %u", n.espnow_channel); + } + } +} + // Turn the access point on (its saved name and password, or led-XXXXXX and open) or off. // The client keeps running either way. static void apply_ap(bool on) @@ -73,8 +93,14 @@ static void apply_ap(bool on) esp_wifi_set_config(WIFI_IF_AP, &ap); esp_wifi_set_mode(WIFI_MODE_APSTA); ESP_LOGI(TAG, "Access point %s on (%s)", ssid, plen >= 8 ? "WPA2" : "open"); +#if !CONFIG_LED_ESPNOW_ONLY + if (s_net_up) { + web_start(); // turned on later (USB, or the client fallback): its page needs the web interface + } +#endif } else { esp_wifi_set_mode(WIFI_MODE_STA); + pin_espnow_channel(); ESP_LOGI(TAG, "Access point off"); } s_ap_on = on; @@ -232,6 +258,27 @@ void net_ap_ssid(char *buf, size_t len) snprintf(buf, len, "led-%02X%02X%02X", mac[3], mac[4], mac[5]); } +#if CONFIG_LED_ESPNOW_ONLY +// The bare ESP-NOW driver: the radio only, a station that never joins a network, held on the saved ESP-NOW +// channel. No network interfaces, access point, client, mDNS or timers. +esp_err_t net_start(void) +{ + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + wifi_init_config_t init_cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&init_cfg)); + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); // nothing of the client's is used or saved + 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)); // the radio stays awake for every frame + net_cfg_t n; + settings_load_net(&n); + ESP_ERROR_CHECK(esp_wifi_set_channel(n.espnow_channel, WIFI_SECOND_CHAN_NONE)); + s_net_up = true; + ESP_LOGI(TAG, "Bare ESP-NOW: radio on channel %u, no Wi-Fi client or access point", n.espnow_channel); + return ESP_OK; +} +#else esp_err_t net_start(void) { ESP_ERROR_CHECK(esp_netif_init()); @@ -272,21 +319,29 @@ esp_err_t net_start(void) ESP_ERROR_CHECK(esp_timer_create(&reconnect_args, &s_reconnect_timer)); // The access point is on unless turned off. With no client Wi-Fi saved it stays on, so the device - // can always be reached and set up. + // can always be reached and set up, except with ESP-NOW on: then the controller and USB reach it. net_cfg_t n; settings_load_net(&n); wifi_creds_t c; const bool has_creds = settings_load_wifi(&c) == ESP_OK && c.ssid[0]; - apply_ap(n.ap_enabled || !has_creds); + apply_ap(n.ap_enabled || (!has_creds && !n.espnow_enabled)); if (has_creds) { apply_sta_config(&c); } ESP_ERROR_CHECK(esp_wifi_start()); + if (!s_ap_on) { + pin_espnow_channel(); // no access point to set the channel (see pin_espnow_channel) + } // No modem sleep: the radio stays awake so the AP never holds DDP unicast until a beacon ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE)); + s_net_up = true; + if (net_espnow_only()) { + ESP_LOGI(TAG, "ESP-NOW only: no web interface, mDNS or DDP (USB and ESP-NOW still work)"); + return ESP_OK; + } // mDNS: .local, with an HTTP service on port 80 ESP_ERROR_CHECK(mdns_init()); ESP_ERROR_CHECK(mdns_hostname_set(n.hostname)); @@ -294,15 +349,27 @@ esp_err_t net_start(void) // app=led-strip lets other drivers' discovery (GET /api/devices) pick out LED drivers from other HTTP devices static mdns_txt_item_t txt[] = { { "app", "led-strip" } }; ESP_ERROR_CHECK(mdns_service_add(NULL, "_http", "_tcp", 80, txt, 1)); + s_mdns_on = true; ESP_LOGI(TAG, "mDNS: %s.local", n.hostname); return ESP_OK; } +#endif bool net_ap_on(void) { return s_ap_on; } +bool net_espnow_only(void) +{ +#if CONFIG_LED_ESPNOW_ONLY + return true; +#endif + net_cfg_t n; + settings_load_net(&n); + return n.espnow_enabled && !s_ap_on; +} + bool net_sta_status(char *ip, size_t ip_len) { if (ip_len) { @@ -360,17 +427,18 @@ esp_err_t net_sta_connect(const wifi_creds_t *c) esp_err_t net_save_network(const net_cfg_t *c) { - // Turning the access point off needs a connected client, or the device could not be reached again + // Turning the access point off needs a connected client or ESP-NOW (with USB, the controller reaches it), + // or the device could not be reached again net_cfg_t old; settings_load_net(&old); - if (!c->ap_enabled && old.ap_enabled && !s_sta_up) { + if (!c->ap_enabled && old.ap_enabled && !s_sta_up && !c->espnow_enabled) { return ESP_ERR_INVALID_STATE; } esp_err_t err = settings_save_net(c); if (err != ESP_OK) { return err; } - err = mdns_hostname_set(c->hostname); + err = s_mdns_on ? mdns_hostname_set(c->hostname) : ESP_OK; // ESP-NOW only: no mDNS, the name is just saved if (err != ESP_OK) { return err; } diff --git a/main/net.h b/main/net.h index 03ab733..a4d633b 100644 --- a/main/net.h +++ b/main/net.h @@ -19,6 +19,11 @@ void net_ap_ssid(char *buf, size_t len); /** @brief True while the access point is running. */ bool net_ap_on(void); +/** @brief ESP-NOW only: ESP-NOW is on (so the Wi-Fi client stays off) and the access point is off. Nothing + * can reach the driver over IP, so the web interface, mDNS and DDP are not started: only the LEDs, the + * ESP-NOW link and the USB config port run. Turning the access point on starts the web interface. */ +bool net_espnow_only(void); + /** @brief Client link state and its IPv4 address ("" if not connected). */ bool net_sta_status(char *ip, size_t ip_len); diff --git a/main/patterns.cpp b/main/patterns.cpp index 7dfffac..b9b21d9 100644 --- a/main/patterns.cpp +++ b/main/patterns.cpp @@ -107,6 +107,8 @@ static uint32_t s_ddp_shown; // frames shown static uint8_t s_max_brightness = 255; static uint8_t s_global = 255; static uint8_t s_master = 255; +static bool s_dimmer_out; // dimmer mode: colours unscaled; the scale is read as the dimmer level +static volatile uint8_t s_out_level = 255; // the scale of the last output (device x master x ceiling) static uint32_t s_fade_t0; static uint32_t s_fade_ms; @@ -223,6 +225,18 @@ uint8_t patterns_get_master(void) return v; } +void patterns_set_dimmer_out(bool on) +{ + xSemaphoreTake(s_lock, portMAX_DELAY); + s_dimmer_out = on; + xSemaphoreGive(s_lock); +} + +uint8_t patterns_output_level(void) +{ + return s_out_level; +} + void patterns_get(preset_t *out) { xSemaphoreTake(s_lock, portMAX_DELAY); @@ -248,6 +262,11 @@ static void output(uint32_t now, bool full_brightness) // Pattern output: device brightness x master scale, under the max brightness ceiling. DDP frames // skip the device and master brightness (the sender sets the colours) but still respect the ceiling. int s_scale = full_brightness ? s_max_brightness : s_global * s_master / 255 * s_max_brightness / 255; + s_out_level = (uint8_t)s_scale; + // Dimmer mode: the scale goes to the fixture's dimmer channel instead, so the colours are not scaled + if (s_dimmer_out) { + s_scale = 255; + } int k = -1; if (s_fading) { uint32_t el = now - s_fade_t0; @@ -413,10 +432,7 @@ void patterns_start(const hw_cfg_t &hw) s_lock = xSemaphoreCreateMutex(); preset_defaults(&s_cur, 0); preset_defaults(&s_run, 0); - s_cur.pattern = patterns_index("chase"); - s_cur.delay = 1000; - s_cur.brightness = 16; - s_cur.col[0][0] = 255; + s_cur.pattern = patterns_index("off"); // LEDs stay dark until a startup preset or a controller selects one s_reset_pending = true; int n = clamp_leds(hw.num_leds); diff --git a/main/patterns.h b/main/patterns.h index 5ae1604..63b062f 100644 --- a/main/patterns.h +++ b/main/patterns.h @@ -71,6 +71,11 @@ uint8_t patterns_get_global(void); void patterns_set_master(uint8_t level); uint8_t patterns_get_master(void); +/** @brief Dimmer mode (DMX fixtures): colours are not scaled; the scale is sent as the fixture's dimmer level. */ +void patterns_set_dimmer_out(bool on); +/** @brief The scale of the last output, 0-255: device x master x max brightness ceiling. */ +uint8_t patterns_output_level(void); + /** @brief Copy of the preset being shown. */ void patterns_get(preset_t *out); diff --git a/main/presets.cpp b/main/presets.cpp index e63edc8..0c57f22 100644 --- a/main/presets.cpp +++ b/main/presets.cpp @@ -17,6 +17,8 @@ static const char *NS = "presets"; static preset_t s_list[PRESETS_MAX]; static size_t s_count; static char s_startup[32]; +// The startup preset until one is chosen (one of the defaults built below) +static const char DEFAULT_STARTUP[] = "Off"; static char s_selected[32]; static int find(const char *name) @@ -365,6 +367,7 @@ esp_err_t presets_init(void) nvs_handle_t h; if (nvs_open(NS, NVS_READONLY, &h) != ESP_OK) { + strcpy(s_startup, DEFAULT_STARTUP); presets_add_defaults(); // a new board return ESP_OK; } @@ -384,7 +387,7 @@ esp_err_t presets_init(void) } size_t len = sizeof(s_startup); if (nvs_get_str(h, "startup", s_startup, &len) != ESP_OK) { - s_startup[0] = '\0'; + strcpy(s_startup, DEFAULT_STARTUP); // never set: start dark } nvs_close(h); if (legacy) { diff --git a/main/presets.h b/main/presets.h index 7ef9d7a..5842e1e 100644 --- a/main/presets.h +++ b/main/presets.h @@ -45,7 +45,7 @@ const char *presets_selected(void); /** @brief Set the preset applied at boot. An empty name clears it. */ esp_err_t presets_set_startup(const char *name); -/** @brief Name of the startup preset, or "". */ +/** @brief Name of the startup preset ("Off" until one is chosen), or "" if cleared. */ const char *presets_startup(void); /** @brief Apply the startup preset, if one is set and still exists. */ diff --git a/main/settings.cpp b/main/settings.cpp index ff51ba8..e205a72 100644 --- a/main/settings.cpp +++ b/main/settings.cpp @@ -1,8 +1,11 @@ /* * SPDX-License-Identifier: Apache-2.0 */ +#include #include +#include "esp_mac.h" + #include "nvs.h" #include "settings.h" @@ -108,24 +111,36 @@ static void get_str(nvs_handle_t h, const char *key, char *buf, size_t len, cons } } +// A driver that was never named is led-, so boards are told apart from the start. +// "led" was the old default and is saved by any network settings change, so it counts as not named. +static void default_hostname(char *buf, size_t len) +{ + uint8_t mac[6] = {}; + esp_read_mac(mac, ESP_MAC_WIFI_STA); + snprintf(buf, len, "led-%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); +} + esp_err_t settings_load_net(net_cfg_t *c) { memset(c, 0, sizeof(*c)); c->ap_enabled = true; c->ddp_enabled = true; c->espnow_enabled = true; - c->espnow_channel = 6; + c->espnow_channel = 11; nvs_handle_t h; esp_err_t err = nvs_open(NS, NVS_READONLY, &h); if (err == ESP_ERR_NVS_NOT_FOUND) { // Nothing saved yet: defaults - strncpy(c->hostname, "led", sizeof(c->hostname) - 1); + default_hostname(c->hostname, sizeof(c->hostname)); return ESP_OK; } if (err != ESP_OK) { return err; } - get_str(h, "hostname", c->hostname, sizeof(c->hostname), "led"); + get_str(h, "hostname", c->hostname, sizeof(c->hostname), ""); + if (!c->hostname[0] || strcmp(c->hostname, "led") == 0) { + default_hostname(c->hostname, sizeof(c->hostname)); + } char mode[8]; get_str(h, "ip_mode", mode, sizeof(mode), "dhcp"); c->use_static = strcmp(mode, "static") == 0; @@ -146,7 +161,7 @@ esp_err_t settings_load_net(net_cfg_t *c) c->espnow_enabled = strcmp(espnow, "off") != 0; int64_t ch = 6; nvs_get_i64(h, "espnow_ch", &ch); - c->espnow_channel = (uint8_t)(ch >= 1 && ch <= 13 ? ch : 6); + c->espnow_channel = (uint8_t)(ch >= 1 && ch <= 13 ? ch : 11); nvs_close(h); return ESP_OK; } diff --git a/main/settings.h b/main/settings.h index e024162..cc1425e 100644 --- a/main/settings.h +++ b/main/settings.h @@ -24,7 +24,7 @@ struct net_cfg_t { char ap_pass[65]; // access point password, 8-63 characters (WPA2); empty is an open network bool ddp_enabled; // DDP pixels and frame sync are received; default on bool espnow_enabled; // ESP-NOW link to led-controller (0x4C wire protocol); default on - uint8_t espnow_channel; // Wi-Fi channel shared with the controller and its bridge; default 6 + uint8_t espnow_channel; // Wi-Fi channel shared with the controller and its bridge; default 11 }; /** @brief Read saved Wi-Fi client credentials. ESP_ERR_NVS_NOT_FOUND if none are saved. */ diff --git a/main/web.cpp b/main/web.cpp index c272ffc..4c40046 100644 --- a/main/web.cpp +++ b/main/web.cpp @@ -509,7 +509,7 @@ static esp_err_t network_post(api_req_t *req) esp_err_t err = net_save_network(&n); if (err == ESP_ERR_INVALID_STATE) { - return send_error(req, "400 Bad Request", "connect to a Wi-Fi network before turning the access point off"); + return send_error(req, "400 Bad Request", "connect to a Wi-Fi network or turn ESP-NOW on before turning the access point off"); } if (err != ESP_OK) { ESP_LOGW(TAG, "Network apply failed: %s", esp_err_to_name(err)); @@ -1368,9 +1368,15 @@ static void api_call_work(void *arg) void web_api_call(const char *msg, char *out, size_t cap) { + if (!s_server) { + // No web interface (Wi-Fi client and access point off): no HTTP or WebSocket handler can run, so + // the call runs here, on the USB task + ws_dispatch(msg, out, cap); + return; + } api_call_t c = { msg, out, cap, xSemaphoreCreateBinary() }; - if (!s_server || !c.done) { - envelope(out, cap, 503, "{\"error\":\"web server not running\"}"); + if (!c.done) { + envelope(out, cap, 503, "{\"error\":\"out of memory\"}"); } else if (httpd_queue_work(s_server, api_call_work, &c) != ESP_OK) { envelope(out, cap, 503, "{\"error\":\"server busy\"}"); } else { @@ -1439,6 +1445,9 @@ static esp_err_t ws_handler(httpd_req_t *req) esp_err_t web_start(void) { + if (s_server) { + return ESP_OK; // already running (net starts it when the access point comes on) + } httpd_config_t cfg = HTTPD_DEFAULT_CONFIG(); cfg.max_uri_handlers = 30; cfg.stack_size = 8192; diff --git a/main/web.h b/main/web.h index 6492a3d..1e2636e 100644 --- a/main/web.h +++ b/main/web.h @@ -13,7 +13,8 @@ extern "C" { #define WEB_API_MSG_MAX 2048 // a request message: {"method","path","body","host"} #define WEB_API_REPLY_MAX 16384 // a reply envelope: {"status","body"} -/** @brief Start the HTTP server on port 80 (UI and JSON API). */ +/** @brief Start the HTTP server on port 80 (UI and JSON API). Does nothing if it is running already. + * Without it the USB config port still works (web_api_call runs the call itself). */ esp_err_t web_start(void); /** diff --git a/sdkconfig.espnow b/sdkconfig.espnow new file mode 100644 index 0000000..8e660d4 --- /dev/null +++ b/sdkconfig.espnow @@ -0,0 +1,8 @@ +# The bare ESP-NOW image: the project's sdkconfig with these changes. Build it in its own folder, so the +# normal build (Wi-Fi, web, USB config: for setting boards up) is untouched: +# idf.py -B build-espnow -D SDKCONFIG=build-espnow/sdkconfig -D "SDKCONFIG_DEFAULTS=sdkconfig;sdkconfig.espnow" build +# Set the board's ESP-NOW channel first (led-tool/espnow_ap_off.py does), then flash this. After that the +# controller changes its settings over ESP-NOW (see main/Kconfig.projbuild). + +# Only the LEDs and ESP-NOW: no Wi-Fi client, access point, web interface, USB config, mDNS, DDP or OTA +CONFIG_LED_ESPNOW_ONLY=y diff --git a/sdkconfig.timing b/sdkconfig.timing new file mode 100644 index 0000000..70d0fe7 --- /dev/null +++ b/sdkconfig.timing @@ -0,0 +1,3 @@ +# Timing test build: the project sdkconfig plus the per-step timing log. Build it in its own folder: +# idf.py -B build-timing -D SDKCONFIG=build-timing/sdkconfig -D "SDKCONFIG_DEFAULTS=sdkconfig;sdkconfig.timing" build +CONFIG_LED_TIMING_LOG=y diff --git a/web/index.html b/web/index.html index 2a44fb1..53f78cb 100644 --- a/web/index.html +++ b/web/index.html @@ -170,7 +170,7 @@ -

Turn it off to use only your Wi-Fi network. Turning it off needs a connected Wi-Fi network. If the network stays down for about a minute, the access point comes back on by itself.

+

Turn it off to use only your Wi-Fi network. Turning it off needs a connected Wi-Fi network, or ESP-NOW on (the controller and USB still reach the driver). If the Wi-Fi network stays down for about a minute, the access point comes back on by itself.