From daec1f3e35517471f3935fb5290900db44e7ba79 Mon Sep 17 00:00:00 2001 From: Vincent Pelletier Date: Tue, 25 Oct 2022 15:09:08 +0000 Subject: [PATCH] Card_Reader.ino: Optimise get_line for speed Do fewer calls to readfile->read, especially if the buffer is large enough to fit an entire line, resulting in less call/return overhead. --- Cart_Reader/Cart_Reader.ino | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Cart_Reader/Cart_Reader.ino b/Cart_Reader/Cart_Reader.ino index 0ad5bb4..aa21e2c 100644 --- a/Cart_Reader/Cart_Reader.ino +++ b/Cart_Reader/Cart_Reader.ino @@ -590,28 +590,29 @@ void skip_line(FsFile* readfile) { //Get line from file void get_line(char* str_buf, FsFile* readfile, uint8_t maxi) { + int read_len; + // Status LED on statusLED(true); - int i = 0; - - while (readfile->available()) { - //If line size is more than maximum array, limit it. - if (i >= maxi) { - i = maxi - 1; - } - - //Read 1 byte from file - str_buf[i] = readfile->read(); + read_len = readfile->read(str_buf, maxi - 1); + for (int i = 0; i < read_len; i++) { //if end of file or newline found, execute command if (str_buf[i] == '\r') { - str_buf[i] = '\0'; - readfile->read(); //dispose \n because \r\n + str_buf[i] = 0; + readfile->seekCur(i - read_len + 2); // +2 to skip over \n because \r\n + return; + } + } + str_buf[maxi - 1] = 0; + // EOL was not found, keep looking (slower) + while (readfile->available()) { + if (readfile->read() == '\r') { + readfile->read(); // read \n because \r\n break; } - i++; - } //End while + } } // Calculate CRC32 if needed and compare it to CRC read from database