Files
led-sync-studio/firmware/src/main.cpp
Claude 230be9994e feat(01-02): wifi_server.h/.cpp — AsyncUDP listener with FreeRTOS queue
- RawPacket struct carries raw bytes, IP, port from AsyncUDP callback
- xQueueSendFromISR in callback (ISR-safe) — zero JSON parsing in callback
- parseTask runs in separate FreeRTOS task, drains rawQueue, parses JSON
- STATUS command handled immediately in parseTask with UDP response to sender
- commandQueue declared extern for animation engine (Plan 03) to consume
- main.cpp: include wifi_server.h, call startUdpServer() after WIFI_PS_NONE
2026-04-03 13:25:15 +02:00

120 lines
4.2 KiB
C++

#include <Arduino.h>
#include <WiFi.h>
#include <esp_wifi.h>
#include "config.h"
#include "led_driver.h"
#include "credentials.h"
#include "wifi_server.h"
// ---------------------------------------------------------------------------
// Color constants for startup indicators
// ---------------------------------------------------------------------------
static const RgbwColor COLOR_STARTUP_GREEN = RgbwColor(0, 30, 0, 0); // Dim green: WiFi connecting
static const RgbwColor COLOR_WIFI_READY = RgbwColor(0, 0, 0, 20); // Dim white: WiFi connected
static const RgbwColor COLOR_LOOP_RED = RgbwColor(15, 0, 0, 0); // Dim red: loop toggle A
static const RgbwColor COLOR_LOOP_BLUE = RgbwColor(0, 0, 15, 0); // Dim blue: loop toggle B
// ---------------------------------------------------------------------------
// State
// ---------------------------------------------------------------------------
static uint32_t lastLoopTick = 0;
static bool loopColorToggle = false;
// ---------------------------------------------------------------------------
// WiFi connect with timeout
// ---------------------------------------------------------------------------
static bool connectWiFi() {
WiFi.begin(WIFI_SSID, WIFI_PASS);
uint32_t startMs = millis();
Serial.print("WiFi connecting");
while (WiFi.status() != WL_CONNECTED) {
if (millis() - startMs > 30000) {
Serial.println();
Serial.println("WiFi timeout — check credentials.h");
return false;
}
delay(500);
Serial.print(".");
}
Serial.println();
return true;
}
// ---------------------------------------------------------------------------
// setup()
// ---------------------------------------------------------------------------
void setup() {
// 1. Serial — wait up to 2s for monitor to attach (USB CDC)
Serial.begin(115200);
uint32_t t0 = millis();
while (!Serial && millis() - t0 < 2000) { delay(10); }
Serial.println("=== LED Sync Studio — ESP32-C3 SuperMini ===");
// 2. Initialize LED strips (clears both, prints driver info)
initStrips();
// 3. Startup indicator: dim green = WiFi connecting
setAllWand(COLOR_STARTUP_GREEN);
showBothStrips();
// 4. Connect to WiFi
if (!connectWiFi()) {
// Stay in error state — strip stays green, serial shows timeout
// Developer must check credentials.h and reflash
return;
}
// 5. CRITICAL: Disable WiFi power save immediately after connect
// Must be called before any LED output to prevent RMT interrupt corruption
esp_wifi_set_ps(WIFI_PS_NONE);
Serial.println("WIFI_PS_NONE set — RMT coexistence mode active");
// 6. Start UDP server (must be after WiFi connect and WIFI_PS_NONE)
startUdpServer();
// 7. Print connection info
Serial.print("WiFi connected: ");
Serial.println(WiFi.localIP().toString());
// 8. WiFi connected indicator: dim white
setAllWand(COLOR_WIFI_READY);
showBothStrips();
lastLoopTick = millis();
}
// ---------------------------------------------------------------------------
// loop() — static color test: toggle strip every 5s to confirm responsiveness
// ---------------------------------------------------------------------------
void loop() {
uint32_t now = millis();
if (now - lastLoopTick >= 5000) {
lastLoopTick = now;
if (loopColorToggle) {
setAllWand(COLOR_LOOP_RED);
} else {
setAllWand(COLOR_LOOP_BLUE);
}
showBothStrips();
loopColorToggle = !loopColorToggle;
Serial.println("loop tick");
}
}
// ---------------------------------------------------------------------------
// COEXISTENCE TEST PROCEDURE (run on day 1 before Plan 02):
// 1. Flash firmware, open serial monitor at 115200
// 2. Confirm: "WIFI_PS_NONE set" + IP address printed
// 3. From Pi/laptop: run `for i in $(seq 1 300); do echo '{"v":1,"cmd":"test"}' | nc -u <ESP_IP> 4210; sleep 0.1; done`
// 4. Observe SK6812 strip for 30 seconds during UDP burst
// 5. PASS: Zero visible flicker throughout
// 6. FAIL: Any flicker -> check DMA buffer size, confirm WIFI_PS_NONE, try core 2.0.17 fallback
// ---------------------------------------------------------------------------