Skip to content

Commit ff89afb

Browse files
committed
Replacing boolean parameters to SD.open() with SD_MODE constants.
1 parent 00a9f99 commit ff89afb

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

libraries/ArduinoTestSuite/examples/ATS_SD_File/ATS_SD_File.pde

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,25 @@ void setup()
1515
ATS_PrintTestStatus("SD.begin()", b = SD.begin(4));
1616
if (!b) goto done;
1717

18-
f = SD.open("test.txt", true, false);
18+
f = SD.open("test.txt", SD_TRUNCATE);
1919
ATS_PrintTestStatus("SD.open()", f);
2020
if (!f) goto done;
2121

22-
f.print("abcdefgh");
22+
f.print("1234");
23+
f.close();
24+
25+
f = SD.open("test.txt", SD_TRUNCATE);
26+
ATS_PrintTestStatus("SD.open()", f);
27+
if (!f) goto done;
28+
29+
f.print("abcde");
30+
f.close();
31+
32+
f = SD.open("test.txt", SD_APPEND);
33+
ATS_PrintTestStatus("SD.open()", f);
34+
if (!f) goto done;
35+
36+
f.print("fgh");
2337
f.close();
2438

2539
f = SD.open("test.txt");
@@ -57,7 +71,7 @@ void setup()
5771

5872
f.close();
5973

60-
f = SD.open("test2.txt", true, false);
74+
f = SD.open("test2.txt", SD_TRUNCATE);
6175
ATS_PrintTestStatus("SD.open()", f);
6276
if (!f) goto done;
6377

libraries/ArduinoTestSuite/examples/ATS_SD_Files/ATS_SD_Files.pde

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void setup()
1313
if (!b) goto done;
1414

1515
ATS_PrintTestStatus("!SD.exists()", !SD.exists("asdf.txt"));
16-
ATS_PrintTestStatus("SD.open()", f = SD.open("asdf.txt", true)); f.close();
16+
ATS_PrintTestStatus("SD.open()", f = SD.open("asdf.txt", SD_TRUNCATE)); f.close();
1717
ATS_PrintTestStatus("SD.exists()", SD.exists("asdf.txt"));
1818
ATS_PrintTestStatus("SD.exists()", SD.exists("/asdf.txt"));
1919
ATS_PrintTestStatus("SD.remove()", SD.remove("asdf.txt"));
@@ -48,13 +48,13 @@ void setup()
4848
ATS_PrintTestStatus("!SD.exists()", !SD.exists("x/y"));
4949
ATS_PrintTestStatus("!SD.exists()", !SD.exists("x/y/z"));
5050

51-
ATS_PrintTestStatus("!SD.open()", !(f = SD.open("asdf/asdf.txt", true))); f.close();
51+
ATS_PrintTestStatus("!SD.open()", !(f = SD.open("asdf/asdf.txt", SD_TRUNCATE))); f.close();
5252
ATS_PrintTestStatus("!SD.exists()", !SD.exists("asdf"));
5353
ATS_PrintTestStatus("!SD.exists()", !SD.exists("asdf.txt"));
5454
ATS_PrintTestStatus("!SD.exists()", !SD.exists("asdf/asdf.txt"));
5555
ATS_PrintTestStatus("SD.mkdir()", SD.mkdir("asdf"));
5656
ATS_PrintTestStatus("SD.exists()", SD.exists("asdf"));
57-
ATS_PrintTestStatus("SD.open()", f = SD.open("asdf/asdf.txt", true)); f.close();
57+
ATS_PrintTestStatus("SD.open()", f = SD.open("asdf/asdf.txt", SD_TRUNCATE)); f.close();
5858
ATS_PrintTestStatus("SD.exists()", SD.exists("asdf/asdf.txt"));
5959
ATS_PrintTestStatus("!SD.rmdir()", !SD.rmdir("asdf"));
6060
ATS_PrintTestStatus("SD.exists()", SD.exists("asdf"));

libraries/SD/SD.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ boolean SDClass::begin(uint8_t csPin) {
343343
}
344344

345345

346-
File SDClass::open(char *filepath, boolean write, boolean append) {
346+
File SDClass::open(char *filepath, uint8_t mode) {
347347
/*
348348
349349
Open the supplied file path for reading or writing.
@@ -369,18 +369,7 @@ File SDClass::open(char *filepath, boolean write, boolean append) {
369369

370370
// TODO: Allow for read&write? (Possibly not, as it requires seek.)
371371

372-
uint8_t oflag = O_RDONLY;
373-
374-
if (write) {
375-
oflag = O_CREAT | O_WRITE;
376-
if (append) {
377-
oflag |= O_APPEND;
378-
} else {
379-
oflag |= O_TRUNC;
380-
}
381-
}
382-
383-
fileOpenMode = oflag;
372+
fileOpenMode = mode;
384373
walkPath(filepath, root, callback_openPath, this);
385374

386375
return File();

libraries/SD/SD.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
#include <utility/SdFat.h>
2121
#include <utility/SdFatUtil.h>
2222

23+
#define SD_READ O_READ
24+
#define SD_TRUNCATE (O_WRITE | O_CREAT | O_TRUNC)
25+
#define SD_APPEND (O_WRITE | O_CREAT | O_APPEND)
26+
2327
class File : public Stream {
2428
public:
2529
virtual void write(uint8_t);
@@ -49,7 +53,7 @@ class SDClass {
4953
// Open the specified file/directory with the supplied mode (e.g. read or
5054
// write, etc). Returns a File object for interacting with the file.
5155
// Note that currently only one file can be open at a time.
52-
File open(char *filename, boolean write = false, boolean append = true);
56+
File open(char *filename, uint8_t mode = SD_READ);
5357

5458
// Methods to determine if the requested file path exists.
5559
boolean exists(char *filepath);

0 commit comments

Comments
 (0)