From 9bc8ea1b58b2a370e1f5911e345661f0ad8bbff8 Mon Sep 17 00:00:00 2001 From: Konstantin Ryabinin Date: Sat, 6 Oct 2018 01:44:55 +0500 Subject: [PATCH] SD: Implement readBytes (#4931) This speeds up the ESP8266WebServer::streamFile more than 3 times. Tested on streaming the 800+ Kb file from SD (FAT32), average time without a fix was 9000 ms, with the fix is 2600 ms (maximal possible SPI speed used), which is as fast as streaming the same file from internal SPIFFS. Hardware: WeMos D1 mini. --- libraries/SD/src/File.cpp | 5 +++++ libraries/SD/src/SD.h | 1 + 2 files changed, 6 insertions(+) diff --git a/libraries/SD/src/File.cpp b/libraries/SD/src/File.cpp index 380298d1f..2002f8512 100644 --- a/libraries/SD/src/File.cpp +++ b/libraries/SD/src/File.cpp @@ -95,6 +95,11 @@ int File::read(void *buf, uint16_t nbyte) { return 0; } +size_t File::readBytes(char *buffer, size_t length) { + int result = read(buffer, (uint16_t)length); + return result < 0 ? 0 : (size_t)result; +} + int File::available() { if (! _file) return 0; diff --git a/libraries/SD/src/SD.h b/libraries/SD/src/SD.h index 38ad79359..9697d1292 100644 --- a/libraries/SD/src/SD.h +++ b/libraries/SD/src/SD.h @@ -34,6 +34,7 @@ public: virtual size_t write(uint8_t); virtual size_t write(const uint8_t *buf, size_t size); virtual int read(); + virtual size_t readBytes(char *buffer, size_t length); virtual int peek(); virtual int available(); virtual void flush();