Files
desklock/firmware/main/net.c
T
jpmschweitzerandClaude Fable 5 ed0e8221f1
Test, Build and Push / test-gateway (push) Successful in 10s
Test, Build and Push / release (push) Skipped
Test, Build and Push / build-gateway (push) Skipped
Phase 2: full LVGL face + Wi-Fi + gateway WebSocket + touch-to-talk
The device now runs the complete face contract on glass: seven states
with glyph expressions (custom 140px face font), matrix rain as pooled
label streams (22px DejaVu+Noto katakana font), rage kaomoji frames
(64px), orbit arc + elapsed counter wait cues, blink/talk/thought
animations, idle clock (SNTP, Europe/Amsterdam), and the power ladder
(active/ambient 35%/dormant 5% with rain parked).

Voice path: ES7210 mic capture task streams 16k PCM over
esp_websocket_client to the gateway; reply PCM buffers to PSRAM and
plays via the audio task; touch-to-talk (tap to speak, tap to send).
Protocol mapping per docs: thinking->effort, audio->speaking,
error event->rage (auto-composes after 2 loops), WS loss->error face.

Bring-up fixes: 8MB factory partition (fonts overflowed 1.5M),
bsp_display_lock(0) is try-lock in this adapter (use UINT32_MAX),
gong synth yields to feed IDLE0, wifi scan diagnostics (found SSID
case mismatch), esp_websocket_client pinned ~1.3 (1.4 needs IDF>5.5).

Verified on hardware: boots, connects to Wi-Fi and holds an open
WebSocket to the gateway. Sauron (iMac TTY endpoint + ops console)
planned in architecture.md.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-14 21:42:51 +02:00

84 lines
2.9 KiB
C

/* Wi-Fi STA via ESP-Hosted (C6 radio) + SNTP, then start the gateway link. */
#include <string.h>
#include "esp_event.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_netif_sntp.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include "desklock.h"
#include "secrets.h"
static const char *TAG = "net";
static void scan_then_connect(void *arg)
{
(void)arg;
ESP_LOGI(TAG, "scanning for APs…");
if (esp_wifi_scan_start(NULL, true) == ESP_OK) {
uint16_t n = 12;
wifi_ap_record_t recs[12];
if (esp_wifi_scan_get_ap_records(&n, recs) == ESP_OK) {
for (int i = 0; i < n; i++) {
ESP_LOGI(TAG, " ap: '%s' rssi=%d ch=%d auth=%d",
(const char *)recs[i].ssid, recs[i].rssi,
recs[i].primary, recs[i].authmode);
}
}
}
esp_wifi_connect();
vTaskDelete(NULL);
}
static void wifi_event(void *arg, esp_event_base_t base, int32_t id, void *data)
{
(void)arg;
(void)data;
if (base == WIFI_EVENT && id == WIFI_EVENT_STA_START) {
xTaskCreate(scan_then_connect, "scan", 4096, NULL, 4, NULL);
} else if (base == WIFI_EVENT && id == WIFI_EVENT_STA_DISCONNECTED) {
wifi_event_sta_disconnected_t *dc = (wifi_event_sta_disconnected_t *)data;
ESP_LOGW(TAG, "wifi disconnected (reason %d), retrying", dc ? dc->reason : -1);
face_set(FACE_ERROR);
vTaskDelay(pdMS_TO_TICKS(3000));
esp_wifi_connect();
} else if (base == IP_EVENT && id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t *ev = (ip_event_got_ip_t *)data;
ESP_LOGI(TAG, "got ip " IPSTR, IP2STR(&ev->ip_info.ip));
setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1);
tzset();
esp_sntp_config_t sntp = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org");
esp_netif_sntp_init(&sntp);
gw_start();
}
}
void net_start(void)
{
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ESP_ERROR_CHECK(nvs_flash_init());
}
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event, NULL));
wifi_config_t wc = { 0 };
wc.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
strlcpy((char *)wc.sta.ssid, WIFI_SSID, sizeof(wc.sta.ssid));
strlcpy((char *)wc.sta.password, WIFI_PASS, sizeof(wc.sta.password));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wc));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "wifi starting, ssid=%s", WIFI_SSID);
}