1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-29 10:24:12 +01:00

Trying to fix seek() / peek() interactions.

This commit is contained in:
David A. Mellis 2010-12-22 16:37:48 -06:00
parent 4742739d6e
commit a7a8f3fa10
4 changed files with 169 additions and 11 deletions

View File

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

View File

@ -0,0 +1,91 @@
// Tests writing to and reading from a file, in particular the
// the Stream implementation (e.g. read() and peek()).
#include <SD.h>
#include <ArduinoTestSuite.h>
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() {}

View File

@ -15,14 +15,17 @@
#include <SD.h>
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();
}

View File

@ -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 <SD.h>
// 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()
{
}