Skip to content

Commit fcc4188

Browse files
committed
SD File object implements Stream.
Added peak() and available() using a single byte buffer. Added flush().
1 parent 6788eea commit fcc4188

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

hardware/arduino/cores/arduino/Stream.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Stream : public Print
2828
public:
2929
virtual int available() = 0;
3030
virtual int read() = 0;
31+
virtual int peek() = 0;
3132
virtual void flush() = 0;
3233
};
3334

libraries/SD/File.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,31 @@ void File::write(const uint8_t *buf, size_t size) {
2626
SD.file.write(buf, size);
2727
}
2828

29+
int File::peek() {
30+
if (SD.c != -1) return SD.c;
31+
SD.c = SD.file.read();
32+
return SD.c;
33+
}
34+
2935
int File::read() {
36+
if (SD.c != -1) {
37+
int tmp = SD.c;
38+
SD.c = -1;
39+
return tmp;
40+
}
3041
return SD.file.read();
3142
}
3243

44+
int File::available() {
45+
if (SD.c != -1) return 1;
46+
SD.c = SD.file.read();
47+
return SD.c != -1;
48+
}
49+
50+
void File::flush() {
51+
SD.file.sync();
52+
}
53+
3354
void File::close() {
3455
SD.file.close();
3556
}

libraries/SD/SD.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
297297
SDClass *p_MemoryCard = static_cast<SDClass*>(object);
298298
p_MemoryCard->file.open(parentDir, filePathComponent,
299299
p_MemoryCard->fileOpenMode);
300+
p_MemoryCard->c = -1;
300301
// TODO: Return file open result?
301302
return false;
302303
}

libraries/SD/SD.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,28 @@
2323
// Use this to configure the chip select pin of the SD card.
2424
#define SD_CARD_CHIP_SELECT_PIN 4 // For use with Arduino Ethernet Shield
2525

26-
class File : public Print {
27-
public:
28-
virtual void write(uint8_t);
29-
virtual void write(const char *str);
30-
virtual void write(const uint8_t *buf, size_t size);
31-
int read();
32-
void close();
33-
operator bool();
26+
class File : public Stream {
27+
public:
28+
virtual void write(uint8_t);
29+
virtual void write(const char *str);
30+
virtual void write(const uint8_t *buf, size_t size);
31+
virtual int read();
32+
virtual int peek();
33+
virtual int available();
34+
virtual void flush();
35+
void close();
36+
operator bool();
3437
};
3538

3639
class SDClass {
3740

38-
private:
41+
private:
3942
// These are required for initialisation and use of sdfatlib
4043
Sd2Card card;
4144
SdVolume volume;
4245
SdFile root;
4346

44-
45-
public:
47+
public:
4648
// This needs to be called to set up the connection to the SD card
4749
// before other methods are used.
4850
void begin(uint8_t csPin = SD_CARD_CHIP_SELECT_PIN);
@@ -59,7 +61,7 @@ class SDClass {
5961
// do not exist they will be created.
6062
boolean makeDir(char *filepath);
6163

62-
private:
64+
private:
6365
SdFile file;
6466

6567
// This is used to determine the mode used to open a file
@@ -69,6 +71,8 @@ class SDClass {
6971
// It shouldn't be set directly--it is set via the parameters to `open`.
7072
int fileOpenMode;
7173

74+
int c;
75+
7276
friend class File;
7377
friend boolean callback_openPath(SdFile&, char *, boolean, void *);
7478
};

0 commit comments

Comments
 (0)