SD File object implements Stream.

Added peak() and available() using a single byte buffer.
Added flush().
This commit is contained in:
David A. Mellis 2010-11-20 11:45:05 -05:00
parent 6788eea3b8
commit fcc4188b15
4 changed files with 39 additions and 12 deletions

View File

@ -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;
};

View File

@ -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();
}

View File

@ -297,6 +297,7 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
SDClass *p_MemoryCard = static_cast<SDClass*>(object);
p_MemoryCard->file.open(parentDir, filePathComponent,
p_MemoryCard->fileOpenMode);
p_MemoryCard->c = -1;
// TODO: Return file open result?
return false;
}

View File

@ -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 *);
};