mirror of https://github.com/noisymime/Arduino.git
SD File object implements Stream.
Added peak() and available() using a single byte buffer. Added flush().
This commit is contained in:
parent
6788eea3b8
commit
fcc4188b15
|
@ -28,6 +28,7 @@ class Stream : public Print
|
||||||
public:
|
public:
|
||||||
virtual int available() = 0;
|
virtual int available() = 0;
|
||||||
virtual int read() = 0;
|
virtual int read() = 0;
|
||||||
|
virtual int peek() = 0;
|
||||||
virtual void flush() = 0;
|
virtual void flush() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -26,10 +26,31 @@ void File::write(const uint8_t *buf, size_t size) {
|
||||||
SD.file.write(buf, 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() {
|
int File::read() {
|
||||||
|
if (SD.c != -1) {
|
||||||
|
int tmp = SD.c;
|
||||||
|
SD.c = -1;
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
return SD.file.read();
|
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() {
|
void File::close() {
|
||||||
SD.file.close();
|
SD.file.close();
|
||||||
}
|
}
|
||||||
|
|
|
@ -297,6 +297,7 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
|
||||||
SDClass *p_MemoryCard = static_cast<SDClass*>(object);
|
SDClass *p_MemoryCard = static_cast<SDClass*>(object);
|
||||||
p_MemoryCard->file.open(parentDir, filePathComponent,
|
p_MemoryCard->file.open(parentDir, filePathComponent,
|
||||||
p_MemoryCard->fileOpenMode);
|
p_MemoryCard->fileOpenMode);
|
||||||
|
p_MemoryCard->c = -1;
|
||||||
// TODO: Return file open result?
|
// TODO: Return file open result?
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,26 +23,28 @@
|
||||||
// Use this to configure the chip select pin of the SD card.
|
// 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
|
#define SD_CARD_CHIP_SELECT_PIN 4 // For use with Arduino Ethernet Shield
|
||||||
|
|
||||||
class File : public Print {
|
class File : public Stream {
|
||||||
public:
|
public:
|
||||||
virtual void write(uint8_t);
|
virtual void write(uint8_t);
|
||||||
virtual void write(const char *str);
|
virtual void write(const char *str);
|
||||||
virtual void write(const uint8_t *buf, size_t size);
|
virtual void write(const uint8_t *buf, size_t size);
|
||||||
int read();
|
virtual int read();
|
||||||
void close();
|
virtual int peek();
|
||||||
operator bool();
|
virtual int available();
|
||||||
|
virtual void flush();
|
||||||
|
void close();
|
||||||
|
operator bool();
|
||||||
};
|
};
|
||||||
|
|
||||||
class SDClass {
|
class SDClass {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// These are required for initialisation and use of sdfatlib
|
// These are required for initialisation and use of sdfatlib
|
||||||
Sd2Card card;
|
Sd2Card card;
|
||||||
SdVolume volume;
|
SdVolume volume;
|
||||||
SdFile root;
|
SdFile root;
|
||||||
|
|
||||||
|
public:
|
||||||
public:
|
|
||||||
// This needs to be called to set up the connection to the SD card
|
// This needs to be called to set up the connection to the SD card
|
||||||
// before other methods are used.
|
// before other methods are used.
|
||||||
void begin(uint8_t csPin = SD_CARD_CHIP_SELECT_PIN);
|
void begin(uint8_t csPin = SD_CARD_CHIP_SELECT_PIN);
|
||||||
|
@ -59,7 +61,7 @@ class SDClass {
|
||||||
// do not exist they will be created.
|
// do not exist they will be created.
|
||||||
boolean makeDir(char *filepath);
|
boolean makeDir(char *filepath);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SdFile file;
|
SdFile file;
|
||||||
|
|
||||||
// This is used to determine the mode used to open a 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`.
|
// It shouldn't be set directly--it is set via the parameters to `open`.
|
||||||
int fileOpenMode;
|
int fileOpenMode;
|
||||||
|
|
||||||
|
int c;
|
||||||
|
|
||||||
friend class File;
|
friend class File;
|
||||||
friend boolean callback_openPath(SdFile&, char *, boolean, void *);
|
friend boolean callback_openPath(SdFile&, char *, boolean, void *);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue