Restructure ESP32 firmware into modules, fix bugs, add tests

Structure (src/esp32/):
- logic/: DisplayController, schedule, power timer, LED pattern; pure C++
- display/: FlipdotFrames and bitmaps
- nano_link/: NanoLink (relay, serial, READY, bootloader) and flash bridge
- net/: WiFi/OTA/panic window, Home Assistant
- config.h with all settings; libs.h (globals in a header) removed

Bug fixes:
- Home Assistant switches now report their state back and start as ON
- loop() no longer blocks: WiFi.waitForConnectResult() (up to 60 s
  without WiFi) and getLocalTime() (5 s before NTP sync) were called
  every iteration
- Power switch off now powers off after 1 s even when a 30 s countdown
  is already running
- millis() overflow safe power-off timer; no frame catch-up bursts
- No garbage time string before NTP sync; 'Connecting...' stays instead
- Frames are only sent when the screen changes instead of every second
- Reset reasons are logged once instead of every 100 ms
- Removed dead code (unused fonts, WiFiMulti, rotated bitmap, ...)

Tests: new test_esp32 environment (26 tests); native renamed to test_nano

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-10-13 00:56:32 +02:00
1 parent cefb76c1ad
commit 683a47c055
29 files changed
+1289 -755

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
#include "net/home_assistant.h"
#include <ArduinoHA.h>
#include <WiFi.h>
#include "config.h"
#include "credentials.h"
static WiFiClient client;
static HADevice device(HA_DEVICE_ID, sizeof(HA_DEVICE_ID));
static HAMqtt mqtt(client, device);
// Unique ids: do not change, Home Assistant's entities are linked to them
static HASwitch clockSwitch("FlipDotDisplayClock");
static HASwitch powerSwitch("FlipDotDisplayPower");
static DisplayController *displayController = nullptr;
// The switches are not optimistic: the new state must be reported back to Home Assistant
static void onClockCommand(bool state, HASwitch *sender)
{
displayController->setClockEnabled(state);
sender->setState(state);
}
static void onPowerCommand(bool state, HASwitch *sender)
{
displayController->setPowerEnabled(state);
sender->setState(state);
}
void homeAssistantBegin(DisplayController &controller)
{
displayController = &controller;
device.setName(HA_DEVICE_NAME);
device.setSoftwareVersion(HA_SOFTWARE_VERSION);
clockSwitch.setName("FlipDot Display Clock");
clockSwitch.setIcon("mdi:lightbulb");
clockSwitch.setCurrentState(controller.clockEnabled());
clockSwitch.onCommand(onClockCommand);
powerSwitch.setName("FlipDot Display Power");
powerSwitch.setIcon("mdi:lightbulb");
powerSwitch.setCurrentState(controller.powerEnabled());
powerSwitch.onCommand(onPowerCommand);
mqtt.begin(BROKER_ADDR, MQTT_USER, MQTT_PASSWORD);
}
void homeAssistantLoop()
{
mqtt.loop();
}
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include "logic/display_controller.h"
// Home Assistant integration over MQTT (with discovery). Two switches:
// FlipDot Display Clock on = clock, off = Nintendo logo
// FlipDot Display Power off = clear the display and switch it off
void homeAssistantBegin(DisplayController &controller);
void homeAssistantLoop();
+70
View File
@@ -0,0 +1,70 @@
#include "net/wifi_ota.h"
#include <ArduinoOTA.h>
#include <WiFi.h>
#include <rom/rtc.h>
#include "config.h"
#include "credentials.h"
void wifiBegin()
{
WiFi.setHostname(HOSTNAME);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.println("Connecting to WiFi");
if (WiFi.waitForConnectResult() != WL_CONNECTED)
{
Serial.println("WiFi connection failed, restarting");
delay(5000);
ESP.restart();
}
// Later disconnects are handled by the WiFi driver's auto reconnect
Serial.print("Connected, IP ");
Serial.println(WiFi.localIP());
}
bool wifiConnected()
{
return WiFi.status() == WL_CONNECTED;
}
void otaBegin()
{
ArduinoOTA.setHostname(HOSTNAME);
ArduinoOTA.onStart([]() { Serial.println("OTA update started"); });
ArduinoOTA.onEnd([]() { Serial.println("OTA update done"); });
ArduinoOTA.onError([](ota_error_t error) { Serial.printf("OTA error %u\n", error); });
ArduinoOTA.begin();
}
void otaLoop()
{
ArduinoOTA.handle();
}
// A normal start is a power on of CPU 0 or a start of CPU 1 by CPU 0
static bool unexpectedReset()
{
RESET_REASON cpu0 = rtc_get_reset_reason(0);
RESET_REASON cpu1 = rtc_get_reset_reason(1);
Serial.printf("Reset reason: CPU0 %d, CPU1 %d\n", cpu0, cpu1);
return cpu0 != POWERON_RESET && cpu1 != EXT_CPU_RESET;
}
void otaPanicWindow(int led_pin)
{
if (!unexpectedReset())
return;
Serial.println("Unexpected reset: waiting for an OTA update");
uint32_t start = millis();
while (millis() - start < OTA_PANIC_WINDOW)
{
ArduinoOTA.handle();
// fast blink
digitalWrite(led_pin, (millis() / 50) % 2);
delay(10);
}
digitalWrite(led_pin, LOW);
}
+14
View File
@@ -0,0 +1,14 @@
#pragma once
// Connect to WiFi. Restarts the ESP32 when the first connection fails.
void wifiBegin();
bool wifiConnected();
// Over-the-air updates of the ESP32 firmware (pio run -e esp32 -t upload)
void otaBegin();
void otaLoop();
// After a crash or unexpected reset, run only OTA for OTA_PANIC_WINDOW, so a firmware that
// crashes at boot can still be replaced. Does nothing after a normal power on.
void otaPanicWindow(int led_pin);