Skip to content

Commit 9bc8ea1

Browse files
icosaederearlephilhower
authored andcommitted
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.
1 parent 9c46a81 commit 9bc8ea1

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

libraries/SD/src/File.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ int File::read(void *buf, uint16_t nbyte) {
9595
return 0;
9696
}
9797

98+
size_t File::readBytes(char *buffer, size_t length) {
99+
int result = read(buffer, (uint16_t)length);
100+
return result < 0 ? 0 : (size_t)result;
101+
}
102+
98103
int File::available() {
99104
if (! _file) return 0;
100105

libraries/SD/src/SD.h

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class File : public Stream {
3434
virtual size_t write(uint8_t);
3535
virtual size_t write(const uint8_t *buf, size_t size);
3636
virtual int read();
37+
virtual size_t readBytes(char *buffer, size_t length);
3738
virtual int peek();
3839
virtual int available();
3940
virtual void flush();

0 commit comments

Comments
 (0)