Skip to content

Commit a7a8f3f

Browse files
committed
Trying to fix seek() / peek() interactions.
1 parent 4742739 commit a7a8f3f

File tree

4 files changed

+168
-10
lines changed

4 files changed

+168
-10
lines changed

libraries/ArduinoTestSuite/examples/ATS_SD_File/ATS_SD_File.pde

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

18-
f = SD.open("test.txt", FILE_TRUNCATE);
19-
ATS_PrintTestStatus("SD.open()", f);
20-
if (!f) goto done;
18+
SD.remove("test.txt");
2119

22-
f.print("1234");
23-
f.close();
24-
25-
f = SD.open("test.txt", FILE_TRUNCATE);
20+
f = SD.open("test.txt", FILE_WRITE);
2621
ATS_PrintTestStatus("SD.open()", f);
2722
if (!f) goto done;
2823

29-
f.print("abcde");
24+
f.print("abc");
25+
f.print("de");
3026
f.close();
3127

32-
f = SD.open("test.txt", FILE_APPEND);
28+
f = SD.open("test.txt", FILE_WRITE);
3329
ATS_PrintTestStatus("SD.open()", f);
3430
if (!f) goto done;
3531

@@ -70,8 +66,10 @@ void setup()
7066
ATS_PrintTestStatus("read()", f.read() == -1);
7167

7268
f.close();
69+
70+
SD.remove("test2.txt");
7371

74-
f = SD.open("test2.txt", FILE_TRUNCATE);
72+
f = SD.open("test2.txt", FILE_WRITE);
7573
ATS_PrintTestStatus("SD.open()", f);
7674
if (!f) goto done;
7775

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Tests writing to and reading from a file, in particular the
2+
// the Stream implementation (e.g. read() and peek()).
3+
4+
#include <SD.h>
5+
#include <ArduinoTestSuite.h>
6+
7+
void setup()
8+
{
9+
int startMemoryUsage = ATS_GetFreeMemory();
10+
boolean b;
11+
File f;
12+
13+
ATS_begin("Arduino", "SD Test");
14+
15+
ATS_PrintTestStatus("SD.begin()", b = SD.begin(4));
16+
if (!b) goto done;
17+
18+
SD.remove("test.txt");
19+
20+
f = SD.open("test.txt", FILE_WRITE);
21+
ATS_PrintTestStatus("SD.open()", f);
22+
if (!f) goto done;
23+
24+
ATS_PrintTestStatus("initial position", f.position() == 0);
25+
ATS_PrintTestStatus("initial size", f.size() == 0);
26+
27+
f.print("0123456789");
28+
29+
ATS_PrintTestStatus("position after writing", f.position() == 10);
30+
ATS_PrintTestStatus("size after writing", f.size() == 10);
31+
32+
f.seek(0);
33+
34+
ATS_PrintTestStatus("size after seek", f.size() == 10);
35+
ATS_PrintTestStatus("position after seek", f.position() == 0);
36+
37+
f.seek(7);
38+
39+
ATS_PrintTestStatus("position after seek", f.position() == 7);
40+
ATS_PrintTestStatus("reading after seek", f.read() == '7');
41+
ATS_PrintTestStatus("position after reading after seeking", f.position() == 8);
42+
ATS_PrintTestStatus("reading after reading after seeking", f.read() == '8');
43+
44+
f.seek(3);
45+
46+
ATS_PrintTestStatus("position after seeking", f.position() == 3);
47+
ATS_PrintTestStatus("peeking after seeking", f.peek() == '3');
48+
ATS_PrintTestStatus("position after peeking after seeking", f.position() == 3);
49+
ATS_PrintTestStatus("peeking after peeking after seeking", f.peek() == '3');
50+
ATS_PrintTestStatus("position after peeking after seeking", f.position() == 3);
51+
ATS_PrintTestStatus("peeking after peeking after seeking", f.read() == '3');
52+
ATS_PrintTestStatus("position after peeking after seeking", f.position() == 4);
53+
54+
f.seek(1);
55+
56+
ATS_PrintTestStatus("position after seeking", f.position() == 1);
57+
ATS_PrintTestStatus("peeking after seeking", f.peek() == '1');
58+
59+
f.seek(4);
60+
61+
ATS_PrintTestStatus("position after seeking", f.position() == 4);
62+
ATS_PrintTestStatus("peeking after seeking", f.peek() == '4');
63+
64+
f.seek(7);
65+
66+
ATS_PrintTestStatus("position()", f.position() == 7);
67+
ATS_PrintTestStatus("read()", f.read() == '7');
68+
69+
f.seek(0);
70+
f.peek();
71+
f.print("AB");
72+
73+
ATS_PrintTestStatus("position()", f.position() == 2);
74+
ATS_PrintTestStatus("size()", f.size() == 10);
75+
ATS_PrintTestStatus("read()", f.read() == '2');
76+
77+
f.seek(0);
78+
79+
ATS_PrintTestStatus("read()", f.read() == 'A');
80+
ATS_PrintTestStatus("read()", f.read() == 'B');
81+
ATS_PrintTestStatus("read()", f.read() == '2');
82+
83+
f.close();
84+
85+
done:
86+
ATS_ReportMemoryUsage(startMemoryUsage);
87+
ATS_end();
88+
89+
}
90+
91+
void loop() {}

libraries/SD/File.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
#include <SD.h>
1616

1717
void File::write(uint8_t val) {
18+
SD.c = -1;
1819
SD.file.write(val);
1920
}
2021

2122
void File::write(const char *str) {
23+
SD.c = -1;
2224
SD.file.write(str);
2325
}
2426

2527
void File::write(const uint8_t *buf, size_t size) {
28+
SD.c = -1;
2629
SD.file.write(buf, size);
2730
}
2831

@@ -52,10 +55,12 @@ void File::flush() {
5255
}
5356

5457
boolean File::seek(uint32_t pos) {
58+
SD.c = -1;
5559
return SD.file.seekSet(pos);
5660
}
5761

5862
uint32_t File::position() {
63+
if (SD.c != -1) return SD.file.curPosition() - 1;
5964
return SD.file.curPosition();
6065
}
6166

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
SD card file dump
3+
4+
This example shows how to read a file from the SD card using the
5+
SD library and send it over the serial port.
6+
7+
The circuit:
8+
* SD card attached to SPI bus as follows:
9+
** MOSI - pin 11
10+
** MISO - pin 12
11+
** CLK - pin 13
12+
** CS - pin 4
13+
14+
created 22 December 2010
15+
16+
This example code is in the public domain.
17+
18+
*/
19+
20+
#include <SD.h>
21+
22+
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
23+
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
24+
// 53 on the Mega) must be left as an output or the SD library
25+
// functions will not work.
26+
const int chipSelect = 4;
27+
28+
void setup()
29+
{
30+
Serial.begin(9600);
31+
Serial.print("Initializing SD card...");
32+
// make sure that the default chip select pin is set to
33+
// output, even if you don't use it:
34+
pinMode(10, OUTPUT);
35+
36+
// see if the card is present and can be initialized:
37+
if (!SD.begin(chipSelect)) {
38+
Serial.println("Card failed, or not present");
39+
// don't do anything more:
40+
return;
41+
}
42+
Serial.println("card initialized.");
43+
44+
// open the file. note that only one file can be open at a time,
45+
// so you have to close this one before opening another.
46+
File dataFile = SD.open("datalog.txt");
47+
48+
// if the file is available, write to it:
49+
if (dataFile) {
50+
while (dataFile.available()) {
51+
Serial.write(dataFile.read());
52+
}
53+
dataFile.close();
54+
}
55+
// if the file isn't open, pop up an error:
56+
else {
57+
Serial.println("error opening datalog.txt");
58+
}
59+
}
60+
61+
void loop()
62+
{
63+
}
64+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy