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).
|
|
|
|
|
//
|
2026-10-13 00:44:35 +02:00
|
|
|
// 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:
|
2026-10-13 00:44:35 +02:00
|
|
|
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:
|
2026-10-13 00:44:35 +02:00
|
|
|
// 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;
|
2026-10-13 00:44:35 +02:00
|
|
|
void (*_onStart)(uint32_t baud);
|
2026-10-13 00:35:28 +02:00
|
|
|
void (*_onEnd)();
|
|
|
|
|
bool _active = false;
|
|
|
|
|
uint32_t _last_activity = 0;
|
|
|
|
|
|
2026-10-13 00:44:35 +02:00
|
|
|
uint32_t _readHeader(WiFiClient &client);
|
2026-10-13 00:35:28 +02:00
|
|
|
void _end();
|
|
|
|
|
};
|