Public Access
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:
1 parent
cefb76c1ad
commit
683a47c055
29 files changed
+1289
-755
No files matched your search
@@ -0,0 +1,118 @@
|
||||
#include "logic/display_controller.h"
|
||||
|
||||
#include "config.h"
|
||||
#include "logic/schedule.h"
|
||||
|
||||
DisplayController::DisplayController(DisplayPower &power, Frames &frames) : _power(power), _frames(frames)
|
||||
{
|
||||
}
|
||||
|
||||
void DisplayController::setClockEnabled(bool enabled)
|
||||
{
|
||||
_clock_enabled = enabled;
|
||||
}
|
||||
|
||||
void DisplayController::setPowerEnabled(bool enabled)
|
||||
{
|
||||
_power_enabled = enabled;
|
||||
}
|
||||
|
||||
bool DisplayController::clockEnabled() const
|
||||
{
|
||||
return _clock_enabled;
|
||||
}
|
||||
|
||||
bool DisplayController::powerEnabled() const
|
||||
{
|
||||
return _power_enabled;
|
||||
}
|
||||
|
||||
void DisplayController::update(uint32_t now, bool time_valid, int hour, int minute)
|
||||
{
|
||||
// Switched off elsewhere (power timer, Nano flashing): the Nano lost its frame
|
||||
if (!_power.isOn())
|
||||
_screen = Screen::Unknown;
|
||||
|
||||
if (_off_timer.expired(now))
|
||||
{
|
||||
_power.off();
|
||||
_screen = Screen::Unknown;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_power_enabled)
|
||||
{
|
||||
if (_power.isOn())
|
||||
{
|
||||
_showBlank();
|
||||
_off_timer.request(now, CLEAR_BEFORE_OFF_TIME);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Keep the boot screen until the time is known
|
||||
if (!time_valid)
|
||||
return;
|
||||
|
||||
if (!_clock_enabled)
|
||||
{
|
||||
// The logo is only shown while the display is on; it never switches the power on
|
||||
if (_power.isOn())
|
||||
{
|
||||
_showLogo();
|
||||
_off_timer.request(now, POWER_OFF_DELAY);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
switch (scheduleFor(hour, minute))
|
||||
{
|
||||
case ScheduleMode::Night:
|
||||
if (_power.isOn())
|
||||
{
|
||||
_showBlank();
|
||||
_off_timer.request(now, POWER_OFF_DELAY);
|
||||
}
|
||||
break;
|
||||
case ScheduleMode::Clock:
|
||||
_off_timer.cancel();
|
||||
if (!_power.isOn())
|
||||
{
|
||||
_power.on();
|
||||
_screen = Screen::Unknown;
|
||||
}
|
||||
_showClock(hour, minute);
|
||||
break;
|
||||
case ScheduleMode::Standby:
|
||||
// Flipdots keep their image without power, so the last time stays visible
|
||||
if (_power.isOn())
|
||||
_off_timer.request(now, POWER_OFF_DELAY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void DisplayController::_showClock(int hour, int minute)
|
||||
{
|
||||
if (_screen == Screen::Clock && _shown_hour == hour && _shown_minute == minute)
|
||||
return;
|
||||
_frames.clock(hour, minute);
|
||||
_screen = Screen::Clock;
|
||||
_shown_hour = hour;
|
||||
_shown_minute = minute;
|
||||
}
|
||||
|
||||
void DisplayController::_showLogo()
|
||||
{
|
||||
if (_screen == Screen::Logo)
|
||||
return;
|
||||
_frames.logo();
|
||||
_screen = Screen::Logo;
|
||||
}
|
||||
|
||||
void DisplayController::_showBlank()
|
||||
{
|
||||
if (_screen == Screen::Blank)
|
||||
return;
|
||||
_frames.blank();
|
||||
_screen = Screen::Blank;
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "logic/power_timer.h"
|
||||
|
||||
// Power for the panels and the Nano (implemented by NanoLink)
|
||||
class DisplayPower
|
||||
{
|
||||
public:
|
||||
virtual bool isOn() = 0;
|
||||
// Switch on and wait until the Nano is ready
|
||||
virtual void on() = 0;
|
||||
virtual void off() = 0;
|
||||
};
|
||||
|
||||
// The screens the display can show (implemented by FlipdotFrames)
|
||||
class Frames
|
||||
{
|
||||
public:
|
||||
virtual void clock(int hour, int minute) = 0;
|
||||
virtual void logo() = 0;
|
||||
virtual void blank() = 0;
|
||||
};
|
||||
|
||||
// Decides what the display shows and when it is powered, based on the time and the two
|
||||
// Home Assistant switches. A frame is only sent when it changes. Pure logic, tested in
|
||||
// test/test_esp32.
|
||||
class DisplayController
|
||||
{
|
||||
public:
|
||||
DisplayController(DisplayPower &power, Frames &frames);
|
||||
|
||||
// Home Assistant "FlipDot Display Clock" switch: off shows the logo instead of the clock
|
||||
void setClockEnabled(bool enabled);
|
||||
// Home Assistant "FlipDot Display Power" switch: off clears the display and powers it off
|
||||
void setPowerEnabled(bool enabled);
|
||||
|
||||
bool clockEnabled() const;
|
||||
bool powerEnabled() const;
|
||||
|
||||
// Call often (every loop). time_valid is false until the time is synced over NTP.
|
||||
void update(uint32_t now, bool time_valid, int hour, int minute);
|
||||
|
||||
private:
|
||||
enum class Screen
|
||||
{
|
||||
Unknown, // nothing sent since the last power on
|
||||
Clock,
|
||||
Logo,
|
||||
Blank,
|
||||
};
|
||||
|
||||
DisplayPower &_power;
|
||||
Frames &_frames;
|
||||
PowerTimer _off_timer;
|
||||
|
||||
bool _clock_enabled = true;
|
||||
bool _power_enabled = true;
|
||||
|
||||
Screen _screen = Screen::Unknown;
|
||||
int _shown_hour = -1;
|
||||
int _shown_minute = -1;
|
||||
|
||||
void _showClock(int hour, int minute);
|
||||
void _showLogo();
|
||||
void _showBlank();
|
||||
};
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "logic/power_timer.h"
|
||||
|
||||
// a is before b, also when millis() overflowed in between
|
||||
static bool before(uint32_t a, uint32_t b)
|
||||
{
|
||||
return (int32_t)(a - b) < 0;
|
||||
}
|
||||
|
||||
void PowerTimer::request(uint32_t now, uint32_t delay)
|
||||
{
|
||||
uint32_t deadline = now + delay;
|
||||
if (!_pending || before(deadline, _deadline))
|
||||
_deadline = deadline;
|
||||
_pending = true;
|
||||
}
|
||||
|
||||
void PowerTimer::cancel()
|
||||
{
|
||||
_pending = false;
|
||||
}
|
||||
|
||||
bool PowerTimer::pending() const
|
||||
{
|
||||
return _pending;
|
||||
}
|
||||
|
||||
bool PowerTimer::expired(uint32_t now)
|
||||
{
|
||||
if (!_pending || before(now, _deadline))
|
||||
return false;
|
||||
_pending = false;
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// Countdown to switching the display off. Works across millis() overflow.
|
||||
class PowerTimer
|
||||
{
|
||||
public:
|
||||
// Switch off `delay` ms from `now`. An earlier pending deadline is kept.
|
||||
void request(uint32_t now, uint32_t delay);
|
||||
|
||||
void cancel();
|
||||
|
||||
bool pending() const;
|
||||
|
||||
// True once when the deadline has passed (the timer is then cleared)
|
||||
bool expired(uint32_t now);
|
||||
|
||||
private:
|
||||
bool _pending = false;
|
||||
uint32_t _deadline = 0;
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "logic/schedule.h"
|
||||
|
||||
static const int DAY_START_HOUR = 8;
|
||||
static const int LATE_START_HOUR = 22;
|
||||
static const int NIGHT_START_HOUR = 23;
|
||||
static const int NIGHT_START_MINUTE = 31;
|
||||
static const int EVENING_START_HOUR = 17;
|
||||
|
||||
ScheduleMode scheduleFor(int hour, int minute)
|
||||
{
|
||||
if (hour < DAY_START_HOUR || (hour >= NIGHT_START_HOUR && minute >= NIGHT_START_MINUTE))
|
||||
return ScheduleMode::Night;
|
||||
if (hour < LATE_START_HOUR || minute % 15 == 0)
|
||||
return ScheduleMode::Clock;
|
||||
return ScheduleMode::Standby;
|
||||
}
|
||||
|
||||
bool isEvening(int hour)
|
||||
{
|
||||
return hour >= EVENING_START_HOUR;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
// When the clock is shown. Pure logic, tested in test/test_esp32.
|
||||
//
|
||||
// 00:00 - 07:59 Night screen cleared, power off
|
||||
// 08:00 - 21:59 Clock clock updated every minute
|
||||
// 22:00 - 23:30 Clock only during the minutes :00, :15, :30 and :45
|
||||
// Standby in between: power off, the last time stays on the dots
|
||||
// 23:31 - 23:59 Night
|
||||
|
||||
enum class ScheduleMode
|
||||
{
|
||||
Night,
|
||||
Clock,
|
||||
Standby,
|
||||
};
|
||||
|
||||
ScheduleMode scheduleFor(int hour, int minute);
|
||||
|
||||
// Evening: the yawning Pikachu is shown
|
||||
bool isEvening(int hour);
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// Built-in LED: off while the display is off, slow blink (1 s) when connected to WiFi,
|
||||
// fast blink (200 ms) without WiFi. Pure logic, tested in test/test_esp32.
|
||||
inline bool statusLedLevel(uint32_t now, bool wifi_connected, bool display_on)
|
||||
{
|
||||
if (!display_on)
|
||||
return false;
|
||||
uint32_t period = wifi_connected ? 1000 : 200;
|
||||
return now % period < period / 2;
|
||||
}
|
||||
Reference in new issue
Block a user