From fcc4188b15d53446612ef70c4e6a3d1b6287638a Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sat, 20 Nov 2010 11:45:05 -0500 Subject: [PATCH] SD File object implements Stream. Added peak() and available() using a single byte buffer. Added flush(). --- hardware/arduino/cores/arduino/Stream.h | 1 + libraries/SD/File.cpp | 21 +++++++++++++++++++ libraries/SD/SD.cpp | 1 + libraries/SD/SD.h | 28 ++++++++++++++----------- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/hardware/arduino/cores/arduino/Stream.h b/hardware/arduino/cores/arduino/Stream.h index c98759b31..93d8275dc 100644 --- a/hardware/arduino/cores/arduino/Stream.h +++ b/hardware/arduino/cores/arduino/Stream.h @@ -28,6 +28,7 @@ class Stream : public Print public: virtual int available() = 0; virtual int read() = 0; + virtual int peek() = 0; virtual void flush() = 0; }; diff --git a/libraries/SD/File.cpp b/libraries/SD/File.cpp index 91aaa557b..d09bd8f85 100644 --- a/libraries/SD/File.cpp +++ b/libraries/SD/File.cpp @@ -26,10 +26,31 @@ void File::write(const uint8_t *buf, size_t size) { SD.file.write(buf, size); } +int File::peek() { + if (SD.c != -1) return SD.c; + SD.c = SD.file.read(); + return SD.c; +} + int File::read() { + if (SD.c != -1) { + int tmp = SD.c; + SD.c = -1; + return tmp; + } return SD.file.read(); } +int File::available() { + if (SD.c != -1) return 1; + SD.c = SD.file.read(); + return SD.c != -1; +} + +void File::flush() { + SD.file.sync(); +} + void File::close() { SD.file.close(); } diff --git a/libraries/SD/SD.cpp b/libraries/SD/SD.cpp index 8c9e63dd4..ec6c7fc7b 100644 --- a/libraries/SD/SD.cpp +++ b/libraries/SD/SD.cpp @@ -297,6 +297,7 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent, SDClass *p_MemoryCard = static_cast(object); p_MemoryCard->file.open(parentDir, filePathComponent, p_MemoryCard->fileOpenMode); + p_MemoryCard->c = -1; // TODO: Return file open result? return false; } diff --git a/libraries/SD/SD.h b/libraries/SD/SD.h index 704d0655d..f7c9fe4f6 100644 --- a/libraries/SD/SD.h +++ b/libraries/SD/SD.h @@ -23,26 +23,28 @@ // Use this to configure the chip select pin of the SD card. #define SD_CARD_CHIP_SELECT_PIN 4 // For use with Arduino Ethernet Shield -class File : public Print { - public: - virtual void write(uint8_t); - virtual void write(const char *str); - virtual void write(const uint8_t *buf, size_t size); - int read(); - void close(); - operator bool(); +class File : public Stream { +public: + virtual void write(uint8_t); + virtual void write(const char *str); + virtual void write(const uint8_t *buf, size_t size); + virtual int read(); + virtual int peek(); + virtual int available(); + virtual void flush(); + void close(); + operator bool(); }; class SDClass { - private: +private: // These are required for initialisation and use of sdfatlib Sd2Card card; SdVolume volume; SdFile root; - - public: +public: // This needs to be called to set up the connection to the SD card // before other methods are used. void begin(uint8_t csPin = SD_CARD_CHIP_SELECT_PIN); @@ -59,7 +61,7 @@ class SDClass { // do not exist they will be created. boolean makeDir(char *filepath); - private: +private: SdFile file; // This is used to determine the mode used to open a file @@ -69,6 +71,8 @@ class SDClass { // It shouldn't be set directly--it is set via the parameters to `open`. int fileOpenMode; + int c; + friend class File; friend boolean callback_openPath(SdFile&, char *, boolean, void *); };