@@ -40,6 +40,9 @@ inline int bs_read_serial(const serial_index_t index) {
4040}
4141
4242class SDFileTransferProtocol {
43+ public:
44+ static const uint16_t VERSION_MAJOR = 0 , VERSION_MINOR = 1 , VERSION_PATCH = 0 , TIMEOUT = 10000 , IDLE_PERIOD = 1000 ;
45+
4346private:
4447 struct Packet {
4548 struct [[gnu::packed]] Open {
@@ -128,22 +131,24 @@ class SDFileTransferProtocol {
128131
129132 enum class FileTransfer : uint8_t { QUERY, OPEN, CLOSE, WRITE, ABORT };
130133
131- static size_t data_waiting, transfer_timeout, idle_timeout;
134+ static size_t data_waiting;
135+ static TDelay<TIMEOUT> transfer_timeout;
132136 static bool transfer_active, dummy_transfer, compression;
133137
134138public:
135139
136140 static void idle () {
137- // If a transfer is interrupted and a file is left open, abort it after 'idle_period' ms
138- const millis_t ms = millis ();
139- if (transfer_active && ELAPSED (ms, idle_timeout)) {
140- idle_timeout = ms + idle_period;
141- if (ELAPSED (ms, transfer_timeout)) transfer_abort ();
141+ // If a transfer is interrupted and a file is left open, abort it after TIMEOUT ms
142+ static TDelay<IDLE_PERIOD> idle_timeout;
143+ const uint16_t ms = millis16 ();
144+ if (transfer_active && idle_timeout.elapsed (ms)) {
145+ idle_timeout.prime (ms);
146+ if (transfer_timeout.elapsed (ms)) transfer_abort ();
142147 }
143148 }
144149
145150 static void process (uint8_t packet_type, char *buffer, const uint16_t length) {
146- transfer_timeout = millis () + timeout ;
151+ transfer_timeout. prime () ;
147152 switch (static_cast <FileTransfer>(packet_type)) {
148153 case FileTransfer::QUERY:
149154 SERIAL_ECHO (F (" PFT:version:" ), version_major, C (' .' ), version_minor, C (' .' ), version_patch);
@@ -206,6 +211,8 @@ class BinaryStream {
206211 enum class StreamState : uint8_t { PACKET_RESET, PACKET_WAIT, PACKET_HEADER, PACKET_DATA, PACKET_FOOTER,
207212 PACKET_PROCESS, PACKET_RESEND, PACKET_TIMEOUT, PACKET_ERROR };
208213
214+ static const uint16_t PACKET_MAX_WAIT = 500 , RX_TIMESLICE = 20 , MAX_RETRIES = 0 , VERSION_MAJOR = 0 , VERSION_MINOR = 1 , VERSION_PATCH = 0 ;
215+
209216 struct Packet { // 10 byte protocol overhead, ascii with checksum and line number has a minimum of 7 increasing with line
210217
211218 union Header {
@@ -236,7 +243,7 @@ class BinaryStream {
236243 Footer footer;
237244 uint32_t bytes_received;
238245 uint16_t checksum, header_checksum;
239- millis_t timeout ;
246+ TDelay<PACKET_MAX_WAIT> max_wait ;
240247 char * buffer;
241248
242249 void reset () {
@@ -245,7 +252,7 @@ class BinaryStream {
245252 bytes_received = 0 ;
246253 checksum = 0 ;
247254 header_checksum = 0 ;
248- timeout = millis () + packet_max_wait ;
255+ max_wait. prime () ;
249256 buffer = nullptr ;
250257 }
251258 } packet{};
@@ -258,28 +265,28 @@ class BinaryStream {
258265
259266 // fletchers 16 checksum
260267 uint32_t checksum (uint32_t cs, uint8_t value) {
261- uint16_t cs_low = (((cs & 0xFF ) + value) % 255 );
262- return ((((cs >> 8 ) + cs_low) % 255 ) << 8 ) | cs_low;
268+ const uint16_t cs_low = (((cs & 0xFF ) + value) % 255 );
269+ return ((((cs >> 8 ) + cs_low) % 255 ) << 8 ) | cs_low;
263270 }
264271
265272 // read the next byte from the data stream keeping track of
266273 // whether the stream times out from data starvation
267274 // takes the data variable by reference in order to return status
268275 bool stream_read (uint8_t & data) {
269- if (stream_state != StreamState::PACKET_WAIT && ELAPSED ( millis (), packet.timeout )) {
276+ if (stream_state != StreamState::PACKET_WAIT && packet.max_wait . pending ( )) {
270277 stream_state = StreamState::PACKET_TIMEOUT;
271278 return false ;
272279 }
273280 if (!bs_serial_data_available (card.transfer_port_index )) return false ;
274281 data = bs_read_serial (card.transfer_port_index );
275- packet.timeout = millis () + packet_max_wait ;
282+ packet.max_wait . prime () ;
276283 return true ;
277284 }
278285
279286 template <const size_t buffer_size>
280287 void receive (char (&buffer)[buffer_size]) {
281288 uint8_t data = 0 ;
282- millis_t transfer_window = millis () + rx_timeslice;
289+ const TDelay< rx_timeslice> transfer_window ;
283290
284291 #if HAS_MEDIA
285292 PORT_REDIRECT (SERIAL_PORTMASK (card.transfer_port_index ));
@@ -288,7 +295,7 @@ class BinaryStream {
288295 #pragma GCC diagnostic push
289296 #pragma GCC diagnostic ignored "-Warray-bounds"
290297
291- while (PENDING ( millis (), transfer_window)) {
298+ while (transfer_window. pending ( )) {
292299 switch (stream_state) {
293300 /* *
294301 * Data stream packet handling
0 commit comments