Files
Flipdot-display/src/esp32/nano_flash_bridge.h
T

41 lines
1.3 KiB
C++
Raw Normal View History

2026-10-13 00:35:28 +02:00
#pragma once
#include <Arduino.h>
#include <WiFi.h>
// WiFi to serial bridge for flashing the Nano from the PC (pio run -e nano_ota -t upload).
//
// The uploader (tools/nano_net_upload.py) connects and first sends one line:
// FLASH <baud>\n
// with the baud rate of the Nano's bootloader (57600 old bootloader, 115200 Optiboot).
// onStart(baud) then restarts the Nano into its bootloader, the bridge answers "OK\n",
// and from then on bytes are copied between the TCP connection and the Nano's serial port
2026-10-13 00:35:28 +02:00
// until the client disconnects or is idle too long. onEnd() is then called.
class NanoFlashBridge
{
public:
NanoFlashBridge(uint16_t port, HardwareSerial &serial, void (*onStart)(uint32_t baud), void (*onEnd)());
2026-10-13 00:35:28 +02:00
void begin();
// Call from loop(). Returns true while a flash session is running; the caller must not
// send anything to the Nano then.
bool handle();
private:
// Max wait for the FLASH line after connecting
static const uint32_t HEADER_TIMEOUT = 2000;
2026-10-13 00:35:28 +02:00
// Give up when no data moves in either direction for this long
static const uint32_t IDLE_TIMEOUT = 10000;
WiFiServer _server;
WiFiClient _client;
HardwareSerial &_serial;
void (*_onStart)(uint32_t baud);
2026-10-13 00:35:28 +02:00
void (*_onEnd)();
bool _active = false;
uint32_t _last_activity = 0;
uint32_t _readHeader(WiFiClient &client);
2026-10-13 00:35:28 +02:00
void _end();
};