From a7a8f3fa1047201b097bbda280c6bb4952addd7e Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Wed, 22 Dec 2010 16:37:48 -0600 Subject: [PATCH] Trying to fix seek() / peek() interactions. --- .../examples/ATS_SD_File/ATS_SD_File.pde | 20 ++-- .../examples/ATS_SD_Seek/ATS_SD_Seek.pde | 91 +++++++++++++++++++ libraries/SD/File.cpp | 5 + libraries/SD/examples/DumpFile/DumpFile.pde | 64 +++++++++++++ 4 files changed, 169 insertions(+), 11 deletions(-) create mode 100644 libraries/ArduinoTestSuite/examples/ATS_SD_Seek/ATS_SD_Seek.pde create mode 100644 libraries/SD/examples/DumpFile/DumpFile.pde diff --git a/libraries/ArduinoTestSuite/examples/ATS_SD_File/ATS_SD_File.pde b/libraries/ArduinoTestSuite/examples/ATS_SD_File/ATS_SD_File.pde index 43cc454e9..fefd6b07d 100644 --- a/libraries/ArduinoTestSuite/examples/ATS_SD_File/ATS_SD_File.pde +++ b/libraries/ArduinoTestSuite/examples/ATS_SD_File/ATS_SD_File.pde @@ -15,21 +15,17 @@ void setup() ATS_PrintTestStatus("SD.begin()", b = SD.begin(4)); if (!b) goto done; - f = SD.open("test.txt", FILE_TRUNCATE); + SD.remove("test.txt"); + + f = SD.open("test.txt", FILE_WRITE); ATS_PrintTestStatus("SD.open()", f); if (!f) goto done; - f.print("1234"); + f.print("abc"); + f.print("de"); f.close(); - f = SD.open("test.txt", FILE_TRUNCATE); - ATS_PrintTestStatus("SD.open()", f); - if (!f) goto done; - - f.print("abcde"); - f.close(); - - f = SD.open("test.txt", FILE_APPEND); + f = SD.open("test.txt", FILE_WRITE); ATS_PrintTestStatus("SD.open()", f); if (!f) goto done; @@ -70,8 +66,10 @@ void setup() ATS_PrintTestStatus("read()", f.read() == -1); f.close(); + + SD.remove("test2.txt"); - f = SD.open("test2.txt", FILE_TRUNCATE); + f = SD.open("test2.txt", FILE_WRITE); ATS_PrintTestStatus("SD.open()", f); if (!f) goto done; diff --git a/libraries/ArduinoTestSuite/examples/ATS_SD_Seek/ATS_SD_Seek.pde b/libraries/ArduinoTestSuite/examples/ATS_SD_Seek/ATS_SD_Seek.pde new file mode 100644 index 000000000..27c7cc545 --- /dev/null +++ b/libraries/ArduinoTestSuite/examples/ATS_SD_Seek/ATS_SD_Seek.pde @@ -0,0 +1,91 @@ +// Tests writing to and reading from a file, in particular the +// the Stream implementation (e.g. read() and peek()). + +#include +#include + +void setup() +{ + int startMemoryUsage = ATS_GetFreeMemory(); + boolean b; + File f; + + ATS_begin("Arduino", "SD Test"); + + ATS_PrintTestStatus("SD.begin()", b = SD.begin(4)); + if (!b) goto done; + + SD.remove("test.txt"); + + f = SD.open("test.txt", FILE_WRITE); + ATS_PrintTestStatus("SD.open()", f); + if (!f) goto done; + + ATS_PrintTestStatus("initial position", f.position() == 0); + ATS_PrintTestStatus("initial size", f.size() == 0); + + f.print("0123456789"); + + ATS_PrintTestStatus("position after writing", f.position() == 10); + ATS_PrintTestStatus("size after writing", f.size() == 10); + + f.seek(0); + + ATS_PrintTestStatus("size after seek", f.size() == 10); + ATS_PrintTestStatus("position after seek", f.position() == 0); + + f.seek(7); + + ATS_PrintTestStatus("position after seek", f.position() == 7); + ATS_PrintTestStatus("reading after seek", f.read() == '7'); + ATS_PrintTestStatus("position after reading after seeking", f.position() == 8); + ATS_PrintTestStatus("reading after reading after seeking", f.read() == '8'); + + f.seek(3); + + ATS_PrintTestStatus("position after seeking", f.position() == 3); + ATS_PrintTestStatus("peeking after seeking", f.peek() == '3'); + ATS_PrintTestStatus("position after peeking after seeking", f.position() == 3); + ATS_PrintTestStatus("peeking after peeking after seeking", f.peek() == '3'); + ATS_PrintTestStatus("position after peeking after seeking", f.position() == 3); + ATS_PrintTestStatus("peeking after peeking after seeking", f.read() == '3'); + ATS_PrintTestStatus("position after peeking after seeking", f.position() == 4); + + f.seek(1); + + ATS_PrintTestStatus("position after seeking", f.position() == 1); + ATS_PrintTestStatus("peeking after seeking", f.peek() == '1'); + + f.seek(4); + + ATS_PrintTestStatus("position after seeking", f.position() == 4); + ATS_PrintTestStatus("peeking after seeking", f.peek() == '4'); + + f.seek(7); + + ATS_PrintTestStatus("position()", f.position() == 7); + ATS_PrintTestStatus("read()", f.read() == '7'); + + f.seek(0); + f.peek(); + f.print("AB"); + + ATS_PrintTestStatus("position()", f.position() == 2); + ATS_PrintTestStatus("size()", f.size() == 10); + ATS_PrintTestStatus("read()", f.read() == '2'); + + f.seek(0); + + ATS_PrintTestStatus("read()", f.read() == 'A'); + ATS_PrintTestStatus("read()", f.read() == 'B'); + ATS_PrintTestStatus("read()", f.read() == '2'); + + f.close(); + +done: + ATS_ReportMemoryUsage(startMemoryUsage); + ATS_end(); + +} + +void loop() {} diff --git a/libraries/SD/File.cpp b/libraries/SD/File.cpp index d6fb6417f..3a2419312 100644 --- a/libraries/SD/File.cpp +++ b/libraries/SD/File.cpp @@ -15,14 +15,17 @@ #include void File::write(uint8_t val) { + SD.c = -1; SD.file.write(val); } void File::write(const char *str) { + SD.c = -1; SD.file.write(str); } void File::write(const uint8_t *buf, size_t size) { + SD.c = -1; SD.file.write(buf, size); } @@ -52,10 +55,12 @@ void File::flush() { } boolean File::seek(uint32_t pos) { + SD.c = -1; return SD.file.seekSet(pos); } uint32_t File::position() { + if (SD.c != -1) return SD.file.curPosition() - 1; return SD.file.curPosition(); } diff --git a/libraries/SD/examples/DumpFile/DumpFile.pde b/libraries/SD/examples/DumpFile/DumpFile.pde new file mode 100644 index 000000000..961717fc9 --- /dev/null +++ b/libraries/SD/examples/DumpFile/DumpFile.pde @@ -0,0 +1,64 @@ +/* + SD card file dump + + This example shows how to read a file from the SD card using the + SD library and send it over the serial port. + + The circuit: + * SD card attached to SPI bus as follows: + ** MOSI - pin 11 + ** MISO - pin 12 + ** CLK - pin 13 + ** CS - pin 4 + + created 22 December 2010 + + This example code is in the public domain. + + */ + +#include + +// On the Ethernet Shield, CS is pin 4. Note that even if it's not +// used as the CS pin, the hardware CS pin (10 on most Arduino boards, +// 53 on the Mega) must be left as an output or the SD library +// functions will not work. +const int chipSelect = 4; + +void setup() +{ + Serial.begin(9600); + Serial.print("Initializing SD card..."); + // make sure that the default chip select pin is set to + // output, even if you don't use it: + pinMode(10, OUTPUT); + + // see if the card is present and can be initialized: + if (!SD.begin(chipSelect)) { + Serial.println("Card failed, or not present"); + // don't do anything more: + return; + } + Serial.println("card initialized."); + + // open the file. note that only one file can be open at a time, + // so you have to close this one before opening another. + File dataFile = SD.open("datalog.txt"); + + // if the file is available, write to it: + if (dataFile) { + while (dataFile.available()) { + Serial.write(dataFile.read()); + } + dataFile.close(); + } + // if the file isn't open, pop up an error: + else { + Serial.println("error opening datalog.txt"); + } +} + +void loop() +{ +} +