#pragma once #include #include // 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 \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 // 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)()); 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; // 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); void (*_onEnd)(); bool _active = false; uint32_t _last_activity = 0; uint32_t _readHeader(WiFiClient &client); void _end(); };