@@ -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 () {
137141 // If a transfer is interrupted and a file is left open, abort it after TIMEOUT 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 ();
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);
@@ -193,8 +198,6 @@ class SDFileTransferProtocol {
193198 break ;
194199 }
195200 }
196-
197- static const uint16_t VERSION_MAJOR = 0 , VERSION_MINOR = 1 , VERSION_PATCH = 0 , TIMEOUT = 10000 , IDLE_PERIOD = 1000 ;
198201};
199202
200203class BinaryStream {
@@ -206,6 +209,8 @@ class BinaryStream {
206209 enum class StreamState : uint8_t { PACKET_RESET, PACKET_WAIT, PACKET_HEADER, PACKET_DATA, PACKET_FOOTER,
207210 PACKET_PROCESS, PACKET_RESEND, PACKET_TIMEOUT, PACKET_ERROR };
208211
212+ static const uint16_t PACKET_MAX_WAIT = 500 , RX_TIMESLICE = 20 , MAX_RETRIES = 0 , VERSION_MAJOR = 0 , VERSION_MINOR = 1 , VERSION_PATCH = 0 ;
213+
209214 struct Packet { // 10 byte protocol overhead, ascii with checksum and line number has a minimum of 7 increasing with line
210215
211216 union Header {
@@ -236,7 +241,7 @@ class BinaryStream {
236241 Footer footer;
237242 uint32_t bytes_received;
238243 uint16_t checksum, header_checksum;
239- millis_t timeout ;
244+ TDelay<PACKET_MAX_WAIT> max_wait ;
240245 char * buffer;
241246
242247 void reset () {
@@ -245,7 +250,7 @@ class BinaryStream {
245250 bytes_received = 0 ;
246251 checksum = 0 ;
247252 header_checksum = 0 ;
248- timeout = millis () + PACKET_MAX_WAIT ;
253+ max_wait. prime () ;
249254 buffer = nullptr ;
250255 }
251256 } packet{};
@@ -258,28 +263,28 @@ class BinaryStream {
258263
259264 // fletchers 16 checksum
260265 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;
266+ const uint16_t cs_low = (((cs & 0xFF ) + value) % 255 );
267+ return ((((cs >> 8 ) + cs_low) % 255 ) << 8 ) | cs_low;
263268 }
264269
265270 // read the next byte from the data stream keeping track of
266271 // whether the stream times out from data starvation
267272 // takes the data variable by reference in order to return status
268273 bool stream_read (uint8_t & data) {
269- if (stream_state != StreamState::PACKET_WAIT && ELAPSED ( millis (), packet.timeout )) {
274+ if (stream_state != StreamState::PACKET_WAIT && packet.max_wait . pending ( )) {
270275 stream_state = StreamState::PACKET_TIMEOUT;
271276 return false ;
272277 }
273278 if (!bs_serial_data_available (card.transfer_port_index )) return false ;
274279 data = bs_read_serial (card.transfer_port_index );
275- packet.timeout = millis () + PACKET_MAX_WAIT ;
280+ packet.max_wait . prime () ;
276281 return true ;
277282 }
278283
279284 template <const size_t buffer_size>
280285 void receive (char (&buffer)[buffer_size]) {
281286 uint8_t data = 0 ;
282- millis_t transfer_window = millis () + RX_TIMESLICE;
287+ const TDelay< RX_TIMESLICE> transfer_window ;
283288
284289 #if HAS_MEDIA
285290 PORT_REDIRECT (SERIAL_PORTMASK (card.transfer_port_index ));
@@ -288,7 +293,7 @@ class BinaryStream {
288293 #pragma GCC diagnostic push
289294 #pragma GCC diagnostic ignored "-Warray-bounds"
290295
291- while (PENDING ( millis (), transfer_window)) {
296+ while (transfer_window. pending ( )) {
292297 switch (stream_state) {
293298 /* *
294299 * Data stream packet handling
@@ -446,7 +451,6 @@ class BinaryStream {
446451 SDFileTransferProtocol::idle ();
447452 }
448453
449- static const uint16_t PACKET_MAX_WAIT = 500 , RX_TIMESLICE = 20 , MAX_RETRIES = 0 , VERSION_MAJOR = 0 , VERSION_MINOR = 1 , VERSION_PATCH = 0 ;
450454 uint8_t packet_retries, sync;
451455 uint16_t buffer_next_index;
452456 uint32_t bytes_received;
0 commit comments