Power-cycle-friendly Nano boot and ESP relay handling

Nano / driver:
- No full on/off flip at boot; the first display() after boot is a forced
  full redraw instead, since the physical dot state is unknown
- Send READY when booted

ESP32:
- relayOn() only switches and waits when the relay was off, and waits for
  the Nano's READY (3 s timeout) instead of a fixed 3 s on every frame
- relayOff() ends Serial and releases the TX pin so the idle-high line no
  longer back-powers the Nano through its RX pin
- No display commands are sent while the relay is off
- Switching the relay on cancels a pending power-off countdown

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-10-13 00:12:28 +02:00
1 parent f7ffcf4ab3
commit 6e4d5e152b
8 files changed
+158 -68

No files matched your search

+7 -1
View File
@@ -26,7 +26,13 @@ BROSE9323 driver is built with `-D ESPSERIAL`, so every GFX call is sent over `S
- `FlipDotDisplayClock`: clock on, or the Nintendo logo when off
- `FlipDotDisplayPower`: turns the display off through the relay
- **Relay on GPIO 13** switches the power for both the panels and the Nano. `LOW` turns it
on. After switching on, the ESP waits 3 s so the Nano can boot before frames are sent.
on.
- **On:** the ESP waits for the Nano's `READY` message, at most 3 s
(`NANO_BOOT_TIMEOUT`), before it sends frames. The relay stays on while it is needed;
the ESP only waits when it actually switches the relay on.
- **Off:** the ESP stops sending and releases its TX pin (`Serial.end()` and the pin set
to input). An idle UART line is high and would otherwise power the Nano through its RX
pin.
- **Status LED** (built-in) blinks fast when WiFi is down and slowly when connected. It is
off at night or when the display is powered off.
+5 -1
View File
@@ -44,7 +44,11 @@ HASwitch powerSwitch("FlipDotDisplayPower");
bool power = true;
const int led1 = BUILTIN_LED; // Pin of the LED
const int relay = 13; // Pin of the relay
const int relay = 13; // Pin of the relay, powers the panels and the Nano (LOW = on)
const int nano_tx = 1; // UART0 TX, wired to the Nano RX
bool relay_on = false;
// Max wait for the Nano's READY after power on (also the wait when Nano TX is not wired to our RX)
const uint32_t NANO_BOOT_TIMEOUT = 3000;
int hour = 0;
int minute = 0;
+115 -54
View File
@@ -5,6 +5,60 @@ void toggleLED(void *parameter);
uint32_t power_off_delay;
int triggered_power_off = false;
int turn_hassio_power_off = false;
// Wait until the Nano reports READY after booting, or until NANO_BOOT_TIMEOUT
void waitForNano()
{
char line[16];
uint8_t len = 0;
uint32_t start = millis();
while (millis() - start < NANO_BOOT_TIMEOUT)
{
while (Serial.available())
{
char c = Serial.read();
if (c == '\n')
{
line[len] = '\0';
if (strstr(line, "READY"))
return;
len = 0;
}
else if (len < sizeof(line) - 1)
line[len++] = c;
}
delay(10);
}
}
void relayOn()
{
// cancel a pending power off countdown
triggered_power_off = false;
if (relay_on)
return;
Serial.begin(115200);
// drop old data (e.g. ERR replies) so only a new READY counts
while (Serial.available())
Serial.read();
digitalWrite(relay, LOW);
waitForNano();
relay_on = true;
}
void relayOff()
{
digitalWrite(relay, HIGH);
relay_on = false;
// An idle UART TX line is high and would power the Nano through its RX pin
Serial.flush();
Serial.end();
pinMode(nano_tx, INPUT);
}
void turnPowerOff(uint32_t delay = 30000, bool _turn_hassio_power_off = false)
{
if(triggered_power_off == false)
@@ -25,7 +79,7 @@ void updatePower()
triggered_power_off = false;
// OFF
digitalWrite(relay, HIGH);
relayOff();
if(turn_hassio_power_off)
{
@@ -46,10 +100,13 @@ void onPowerSwitchStateChanged(bool state, HASwitch *s)
if(power == false)
{
display.fillScreen(0);
display.display();
if (relay_on)
{
display.fillScreen(0);
display.display();
delay(1000);
delay(1000);
}
turnPowerOff(0);
@@ -230,9 +287,7 @@ void setup()
otaPanicMode = false;
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW);
delay(4000);
relayOn();
display.begin(&Serial);
// display.setTiming(200);
@@ -379,11 +434,7 @@ void drawRotatedBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, i
void turnPowerOn(bool overide = false)
{
if(power == true || overide == true)
{
// ON
digitalWrite(relay, LOW);
delay(3000);
}
relayOn();
}
void loop()
@@ -434,8 +485,11 @@ void loop()
if(hour <= 7 || (hour >= 23 && minute > 30))
{
display.fillScreen(0);
display.display();
if (relay_on)
{
display.fillScreen(0);
display.display();
}
turnPowerOff();
}
@@ -446,28 +500,32 @@ void loop()
{
turnPowerOn();
int xoffset = 3;
// power can be off (Home Assistant power switch): then nothing is sent
if (relay_on)
{
int xoffset = 3;
display.fillScreen(0);
// display.setTextSize(2);
display.setTextSize(1);
// display.setCursor(7, 3);
display.setCursor(xoffset + 0, 19 - 2);
display.setTextWrap(false);
// display.print(t);
display.fillScreen(0);
// display.setTextSize(2);
display.setTextSize(1);
// display.setCursor(7, 3);
display.setCursor(xoffset + 0, 19 - 2);
display.setTextWrap(false);
// display.print(t);
// if(minute % 2 == 0)
if(hour >= 17 || hour <= 6)
display.drawBitmap(xoffset + 0, 0, yawning_pikachu_bitmap_yawning_pikachu_black_border, 35, 19, 1);
else
display.drawBitmap(xoffset + 0, 0, pikachu_bitmap_pikachu_black_border, 35, 19, 1);
// if(minute % 2 == 0)
if(hour >= 17 || hour <= 6)
display.drawBitmap(xoffset + 0, 0, yawning_pikachu_bitmap_yawning_pikachu_black_border, 35, 19, 1);
else
display.drawBitmap(xoffset + 0, 0, pikachu_bitmap_pikachu_black_border, 35, 19, 1);
display.drawBitmap(xoffset + 35, 0, clock_thought_bubble_bitmap_clock_thought_bubble, 46, 19, 1);
display.drawBitmap(xoffset + 35, 0, clock_thought_bubble_bitmap_clock_thought_bubble, 46, 19, 1);
display.setCursor(xoffset + 43, (19/2) - 5);
display.print(timeStringBuff);
display.setCursor(xoffset + 43, (19/2) - 5);
display.print(timeStringBuff);
display.display();
display.display();
}
}
else
{
@@ -476,34 +534,37 @@ void loop()
}
else
{
display.fillScreen(0);
display.drawBitmap(0, 0, nintendo_bitmap_Nintendo_new, 84, 19, 1);
if (relay_on)
{
display.fillScreen(0);
display.drawBitmap(0, 0, nintendo_bitmap_Nintendo_new, 84, 19, 1);
// display.drawBitmap(0, 0, nintendo_bitmap_panda_inverted, 17, 18, 1);
// display.drawBitmap(18, 0, nintendo_bitmap_panda_inverted, 17, 18, 1);
// display.drawBitmap(18*2, 0, nintendo_bitmap_panda_inverted, 17, 18, 1);
// display.drawBitmap(18*3, 0, nintendo_bitmap_panda_inverted, 17, 18, 1);
// display.drawBitmap(18*4, 0, nintendo_bitmap_panda_inverted, 17, 18, 1);
// display.drawBitmap(0, 0, nintendo_bitmap_panda_inverted, 17, 18, 1);
// display.drawBitmap(18, 0, nintendo_bitmap_panda_inverted, 17, 18, 1);
// display.drawBitmap(18*2, 0, nintendo_bitmap_panda_inverted, 17, 18, 1);
// display.drawBitmap(18*3, 0, nintendo_bitmap_panda_inverted, 17, 18, 1);
// display.drawBitmap(18*4, 0, nintendo_bitmap_panda_inverted, 17, 18, 1);
// drawRotatedBitmap(0, 0, nintendo_bitmap_Nintendo_new, 17, 18, 1, 90);
// drawRotatedBitmap(0, 0, nintendo_bitmap_Nintendo_new, 17, 18, 1, 90);
// Text inside border.
// display.drawBitmap(0, 0, nintendo_bitmap_nintendo_border, 84, 19, 1);
// display.setFont();
// display.setTextSize(1);
// display.setTextWrap(false);
// display.setCursor(5, 4);
// display.print("Nintendo");
// Text inside border.
// display.drawBitmap(0, 0, nintendo_bitmap_nintendo_border, 84, 19, 1);
// display.setFont();
// display.setTextSize(1);
// display.setTextWrap(false);
// display.setCursor(5, 4);
// display.print("Nintendo");
// display.setFont();
// // display.setTextSize(2);
// display.setTextSize(1);
// display.setCursor(0, 0);
// display.setTextWrap(true);
// // display.print(t);
// display.print("Carla & Jan Kragt");
// display.setFont();
// // display.setTextSize(2);
// display.setTextSize(1);
// display.setCursor(0, 0);
// display.setTextWrap(true);
// // display.print(t);
// display.print("Carla & Jan Kragt");
display.display();
display.display();
}
turnPowerOff();
}
+9 -3
View File
@@ -8,8 +8,11 @@ logic.
## Behaviour
- **Boot:** the Nano loads the flip time from EEPROM and flips the whole display on and
then off.
- **Boot:** the Nano loads the flip time from EEPROM, sets up the pins and sends `READY`.
It doesn't flip any dots at boot. The relay powers the Nano, so it boots every time the
display is switched on.
- **First `display()` after boot** redraws every dot, because the Nano doesn't know which
state the dots were left in.
- **`display()`:** compares the new buffer with the last one that was shown and strobes
only the dots that changed.
- **Every 2 minutes** (`REFRESH_TIME`) it does a full redraw to fix dots that got stuck.
@@ -47,7 +50,10 @@ skipped and the next line starts clean. Coordinates are 2-digit **hex**. Color i
| `R` | `R` | Print the buffer as ASCII art on the serial port |
| `T` | `Tnnn` | Set the flip pulse length in µs (decimal, 50–2000), stored in EEPROM |
Invalid commands get a reply that starts with `ERR:` on the Nano's TX pin.
Replies on the Nano's TX pin:
- `READY` after booting. The ESP waits for it before sending the first frame.
- `ERR: ...` for invalid commands.
Example (what the ESP32 sends for one pixel and a refresh):
+3
View File
@@ -265,6 +265,9 @@ void setup()
last_refresh = millis();
last_rx = millis();
// Tells the ESP that frames can be sent (the relay powers the Nano, so this happens at every power on)
Serial.println(F("READY"));
}
void loop()