feat: DDP frame sync over UDP broadcast, DDP acks, DDP enable setting
- Driver acks each completed DDP frame; the test waits for the acks before broadcasting the sync. - Sync listener on UDP 4049: a frame is held until the next sync, then shown. - DDP pixels are shown at full brightness, ignoring the device brightness settings. - "DDP and sync receive on" setting (NVS, network API, web UI); off ignores DDP and sync packets. - ddp_test.py: finds devices on the subnet, drives them in lockstep at a fixed rate, runs animations. - /api/ddp reports sync counters for diagnosis. - Includes in-progress WebSocket, multi-driver web UI and Wi-Fi work from the same tree. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
47f4eba0d6
commit
4cd3bc7973
294 files changed
+48954
-301
No files matched your search
+105
-25
@@ -26,6 +26,17 @@ static volatile bool s_sta_up;
|
||||
static esp_ip4_addr_t s_sta_ip;
|
||||
static int s_retries;
|
||||
|
||||
// The access point. It can be turned off in settings, as long as the client is connected. If the client
|
||||
// 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_ap_want; // the state ap_timer switches to
|
||||
static esp_timer_handle_t s_ap_timer;
|
||||
|
||||
// A saved network change reconnects the client a moment later, so the reply to the request still gets out
|
||||
static esp_timer_handle_t s_reconnect_timer;
|
||||
static net_cfg_t s_pending_net;
|
||||
|
||||
// Set the STA config from credentials. Empty password means an open network.
|
||||
static void apply_sta_config(const wifi_creds_t *c)
|
||||
{
|
||||
@@ -36,6 +47,58 @@ static void apply_sta_config(const wifi_creds_t *c)
|
||||
esp_wifi_set_config(WIFI_IF_STA, &cfg);
|
||||
}
|
||||
|
||||
// Turn the access point on (with its led-XXXXXX name, open) or off. The client keeps running either way.
|
||||
static void apply_ap(bool on)
|
||||
{
|
||||
if (on) {
|
||||
char ssid[16];
|
||||
net_ap_ssid(ssid, sizeof(ssid));
|
||||
wifi_config_t ap = {};
|
||||
strncpy((char *)ap.ap.ssid, ssid, sizeof(ap.ap.ssid) - 1);
|
||||
ap.ap.ssid_len = strlen(ssid);
|
||||
ap.ap.channel = AP_CHANNEL;
|
||||
ap.ap.max_connection = AP_MAX_CLIENTS;
|
||||
ap.ap.authmode = WIFI_AUTH_OPEN;
|
||||
esp_wifi_set_config(WIFI_IF_AP, &ap);
|
||||
esp_wifi_set_mode(WIFI_MODE_APSTA);
|
||||
ESP_LOGI(TAG, "Access point %s on", ssid);
|
||||
} else {
|
||||
esp_wifi_set_mode(WIFI_MODE_STA);
|
||||
ESP_LOGI(TAG, "Access point off");
|
||||
}
|
||||
s_ap_on = on;
|
||||
}
|
||||
|
||||
// Switch the access point later, from a timer task (not inside a Wi-Fi event)
|
||||
static void ap_timer_cb(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
if (s_ap_want != s_ap_on) {
|
||||
apply_ap(s_ap_want);
|
||||
}
|
||||
}
|
||||
|
||||
static esp_err_t apply_client_ip(const net_cfg_t *c);
|
||||
|
||||
static void reconnect_cb(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
esp_timer_stop(s_retry_timer);
|
||||
esp_wifi_disconnect();
|
||||
s_retries = 0;
|
||||
if (apply_client_ip(&s_pending_net) != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Client address could not be applied");
|
||||
}
|
||||
esp_wifi_connect();
|
||||
}
|
||||
|
||||
static void ap_switch_later(bool on, uint64_t delay_us)
|
||||
{
|
||||
s_ap_want = on;
|
||||
esp_timer_stop(s_ap_timer); // not running is fine
|
||||
esp_timer_start_once(s_ap_timer, delay_us);
|
||||
}
|
||||
|
||||
// Client address: DHCP, or the static address from settings. Called before the client connects.
|
||||
static esp_err_t apply_client_ip(const net_cfg_t *c)
|
||||
{
|
||||
@@ -99,6 +162,9 @@ static void on_wifi_event(void *arg, esp_event_base_t base, int32_t id, void *da
|
||||
s_retries++;
|
||||
esp_timer_stop(s_retry_timer);
|
||||
esp_timer_start_once(s_retry_timer, (uint64_t)delay_ms * 1000);
|
||||
if (!s_ap_on && s_retries >= AP_FALLBACK_RETRIES) {
|
||||
ap_switch_later(true, 1000000); // the client keeps failing: bring the access point back
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -146,31 +212,39 @@ esp_err_t net_start(void)
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_timer_create(&timer_args, &s_retry_timer));
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
|
||||
const esp_timer_create_args_t ap_args = {
|
||||
.callback = ap_timer_cb,
|
||||
.arg = NULL,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "ap_switch",
|
||||
.skip_unhandled_events = false,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_timer_create(&ap_args, &s_ap_timer));
|
||||
|
||||
// Open access point named led-XXXXXX
|
||||
char ssid[16];
|
||||
net_ap_ssid(ssid, sizeof(ssid));
|
||||
wifi_config_t ap = {};
|
||||
strncpy((char *)ap.ap.ssid, ssid, sizeof(ap.ap.ssid) - 1);
|
||||
ap.ap.ssid_len = strlen(ssid);
|
||||
ap.ap.channel = AP_CHANNEL;
|
||||
ap.ap.max_connection = AP_MAX_CLIENTS;
|
||||
ap.ap.authmode = WIFI_AUTH_OPEN;
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap));
|
||||
const esp_timer_create_args_t reconnect_args = {
|
||||
.callback = reconnect_cb,
|
||||
.arg = NULL,
|
||||
.dispatch_method = ESP_TIMER_TASK,
|
||||
.name = "wifi_reconnect",
|
||||
.skip_unhandled_events = false,
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_timer_create(&reconnect_args, &s_reconnect_timer));
|
||||
|
||||
// Client credentials, if any were saved
|
||||
// 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.
|
||||
net_cfg_t n;
|
||||
settings_load_net(&n);
|
||||
wifi_creds_t c;
|
||||
if (settings_load_wifi(&c) == ESP_OK && c.ssid[0]) {
|
||||
const bool has_creds = settings_load_wifi(&c) == ESP_OK && c.ssid[0];
|
||||
apply_ap(n.ap_enabled || !has_creds);
|
||||
|
||||
if (has_creds) {
|
||||
apply_sta_config(&c);
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
ESP_LOGI(TAG, "AP %s started", ssid);
|
||||
|
||||
// mDNS: <hostname>.local, with an HTTP service on port 80
|
||||
net_cfg_t n;
|
||||
settings_load_net(&n);
|
||||
ESP_ERROR_CHECK(mdns_init());
|
||||
ESP_ERROR_CHECK(mdns_hostname_set(n.hostname));
|
||||
ESP_ERROR_CHECK(mdns_instance_name_set("LED controller"));
|
||||
@@ -208,6 +282,12 @@ 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
|
||||
net_cfg_t old;
|
||||
settings_load_net(&old);
|
||||
if (!c->ap_enabled && old.ap_enabled && !s_sta_up) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
esp_err_t err = settings_save_net(c);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
@@ -216,19 +296,19 @@ esp_err_t net_save_network(const net_cfg_t *c)
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
// Reconnect with the new address settings if a client is configured
|
||||
if (c->ap_enabled != s_ap_on) {
|
||||
// After the reply has gone out, so the page that asked still gets it
|
||||
ap_switch_later(c->ap_enabled, c->ap_enabled ? 1000000 : 1500000);
|
||||
}
|
||||
// Reconnect with the new address settings if a client is configured (after the reply, see reconnect_cb)
|
||||
wifi_creds_t w;
|
||||
if (settings_load_wifi(&w) != ESP_OK || !w.ssid[0]) {
|
||||
return ESP_OK;
|
||||
}
|
||||
esp_timer_stop(s_retry_timer);
|
||||
esp_wifi_disconnect();
|
||||
s_retries = 0;
|
||||
err = apply_client_ip(c);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
return esp_wifi_connect();
|
||||
s_pending_net = *c;
|
||||
esp_timer_stop(s_reconnect_timer);
|
||||
esp_timer_start_once(s_reconnect_timer, 1000000);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t net_scan(wifi_ap_record_t *recs, uint16_t *count)
|
||||
|
||||
Reference in new issue
Block a user