Skip to content

Commit 47c6e0a

Browse files
James Fosterjgfoster
andauthored
Mock for SD (#11)
* We now have an in-memory mock (test double) for the file system! * Add GitHub Action (fix #8) * Update format. * Format examples. * Add include for `assert()`. * Use proper delete to avoid AddressSanitizer: alloc-dealloc-mismatch (operator new [] vs operator delete). * Do proper garbage collection when removing all files. * Update format. Co-authored-by: James Foster <[email protected]>
1 parent 06ee57a commit 47c6e0a

File tree

27 files changed

+1860
-2157
lines changed

27 files changed

+1860
-2157
lines changed

.github/workflows/arduino_ci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
name: Arduino CI
3+
4+
on: [push, pull_request]
5+
6+
jobs:
7+
arduino_ci:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: Arduino-CI/[email protected]

.github/workflows/format.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: test-clang-format
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: DoozyX/[email protected]
12+
with:
13+
source: './'
14+
extensions: 'c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx,ino'
15+
clangFormatVersion: 9

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ Gemfile.lock
22
.bundle
33
vendor
44
*.cpp.bin*
5+
.vscode

examples/CardInfo/CardInfo.ino

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,23 @@
33
44
This example shows how use the utility libraries on which the'
55
SD library is based in order to get info about your SD card.
6-
Very useful for testing a card when you're not sure whether its working or not.
7-
Pin numbers reflect the default SPI pins for Uno and Nano models
8-
The circuit:
9-
SD card attached to SPI bus as follows:
6+
Very useful for testing a card when you're not sure whether its working or
7+
not. Pin numbers reflect the default SPI pins for Uno and Nano models The
8+
circuit: SD card attached to SPI bus as follows:
109
** SDO - pin 11 on Arduino Uno/Duemilanove/Diecimila
1110
** SDI - pin 12 on Arduino Uno/Duemilanove/Diecimila
1211
** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
1312
** CS - depends on your SD card shield or module.
14-
Pin 10 used here for consistency with other Arduino examples
13+
Pin 10 used here for consistency with other Arduino examples
1514
1615
created 28 Mar 2011
1716
by Limor Fried
1817
modified 24 July 2020
1918
by Tom Igoe
2019
*/
2120
// include the SD library:
22-
#include <SPI.h>
2321
#include <SD.h>
22+
#include <SPI.h>
2423

2524
// set up variables using the SD utility library functions:
2625
Sd2Card card;
@@ -42,7 +41,6 @@ void setup() {
4241
; // wait for serial port to connect. Needed for native USB port only
4342
}
4443

45-
4644
Serial.print("\nInitializing SD card...");
4745

4846
// we'll use the initialization code from the utility libraries
@@ -51,8 +49,10 @@ void setup() {
5149
Serial.println("initialization failed. Things to check:");
5250
Serial.println("* is a card inserted?");
5351
Serial.println("* is your wiring correct?");
54-
Serial.println("* did you change the chipSelect pin to match your shield or module?");
55-
while (1);
52+
Serial.println(
53+
"* did you change the chipSelect pin to match your shield or module?");
54+
while (1)
55+
;
5656
} else {
5757
Serial.println("Wiring is correct and a card is present.");
5858
}
@@ -61,23 +61,26 @@ void setup() {
6161
Serial.println();
6262
Serial.print("Card type: ");
6363
switch (card.type()) {
64-
case SD_CARD_TYPE_SD1:
65-
Serial.println("SD1");
66-
break;
67-
case SD_CARD_TYPE_SD2:
68-
Serial.println("SD2");
69-
break;
70-
case SD_CARD_TYPE_SDHC:
71-
Serial.println("SDHC");
72-
break;
73-
default:
74-
Serial.println("Unknown");
64+
case SD_CARD_TYPE_SD1:
65+
Serial.println("SD1");
66+
break;
67+
case SD_CARD_TYPE_SD2:
68+
Serial.println("SD2");
69+
break;
70+
case SD_CARD_TYPE_SDHC:
71+
Serial.println("SDHC");
72+
break;
73+
default:
74+
Serial.println("Unknown");
7575
}
7676

77-
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
77+
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or
78+
// FAT32
7879
if (!volume.init(card)) {
79-
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
80-
while (1);
80+
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've "
81+
"formatted the card");
82+
while (1)
83+
;
8184
}
8285

8386
Serial.print("Clusters: ");
@@ -94,9 +97,9 @@ void setup() {
9497
Serial.print("Volume type is: FAT");
9598
Serial.println(volume.fatType(), DEC);
9699

97-
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
98-
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
99-
volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1KB)
100+
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
101+
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
102+
volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1KB)
100103
Serial.print("Volume size (Kb): ");
101104
Serial.println(volumesize);
102105
Serial.print("Volume size (Mb): ");
@@ -113,5 +116,4 @@ void setup() {
113116
root.close();
114117
}
115118

116-
void loop(void) {
117-
}
119+
void loop(void) {}

examples/Datalogger/Datalogger.ino

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
** SDI - pin 12
1313
** CLK - pin 13
1414
** CS - depends on your SD card shield or module.
15-
Pin 10 used here for consistency with other Arduino examples
15+
Pin 10 used here for consistency with other Arduino examples
1616
(for MKRZero SD: SDCARD_SS_PIN)
1717
1818
created 24 Nov 2010
@@ -23,26 +23,30 @@
2323
2424
*/
2525

26-
#include <SPI.h>
2726
#include <SD.h>
27+
#include <SPI.h>
2828

2929
const int chipSelect = 10;
3030

3131
void setup() {
3232
// Open serial communications and wait for port to open:
3333
Serial.begin(9600);
3434
// wait for Serial Monitor to connect. Needed for native USB port boards only:
35-
while (!Serial);
35+
while (!Serial)
36+
;
3637

3738
Serial.print("Initializing SD card...");
3839

3940
if (!SD.begin(chipSelect)) {
4041
Serial.println("initialization failed. Things to check:");
4142
Serial.println("1. is a card inserted?");
4243
Serial.println("2. is your wiring correct?");
43-
Serial.println("3. did you change the chipSelect pin to match your shield or module?");
44-
Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");
45-
while (true);
44+
Serial.println(
45+
"3. did you change the chipSelect pin to match your shield or module?");
46+
Serial.println("Note: press reset or reopen this serial monitor after "
47+
"fixing your issue!");
48+
while (true)
49+
;
4650
}
4751

4852
Serial.println("initialization done.");

examples/DumpFile/DumpFile.ino

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
** SDI - pin 12
1212
** CLK - pin 13
1313
** CS - depends on your SD card shield or module.
14-
Pin 10 used here for consistency with other Arduino examples
14+
Pin 10 used here for consistency with other Arduino examples
1515
(for MKRZero SD: SDCARD_SS_PIN)
1616
1717
created 22 December 2010
@@ -29,17 +29,21 @@ void setup() {
2929
// Open serial communications and wait for port to open:
3030
Serial.begin(9600);
3131
// wait for Serial Monitor to connect. Needed for native USB port boards only:
32-
while (!Serial);
32+
while (!Serial)
33+
;
3334

3435
Serial.print("Initializing SD card...");
3536

3637
if (!SD.begin(chipSelect)) {
3738
Serial.println("initialization failed. Things to check:");
3839
Serial.println("1. is a card inserted?");
3940
Serial.println("2. is your wiring correct?");
40-
Serial.println("3. did you change the chipSelect pin to match your shield or module?");
41-
Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");
42-
while (true);
41+
Serial.println(
42+
"3. did you change the chipSelect pin to match your shield or module?");
43+
Serial.println("Note: press reset or reopen this serial monitor after "
44+
"fixing your issue!");
45+
while (true)
46+
;
4347
}
4448

4549
Serial.println("initialization done.");
@@ -61,5 +65,4 @@ void setup() {
6165
}
6266
}
6367

64-
void loop() {
65-
}
68+
void loop() {}

examples/Files/Files.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
** SDI - pin 12
1010
** CLK - pin 13
1111
** CS - depends on your SD card shield or module.
12-
Pin 10 used here for consistency with other Arduino examples
12+
Pin 10 used here for consistency with other Arduino examples
1313
(for MKRZero SD: SDCARD_SS_PIN)
1414
1515
created Nov 2010
@@ -28,13 +28,15 @@ void setup() {
2828
// Open serial communications and wait for port to open:
2929
Serial.begin(9600);
3030
// wait for Serial Monitor to connect. Needed for native USB port boards only:
31-
while (!Serial);
31+
while (!Serial)
32+
;
3233

3334
Serial.print("Initializing SD card...");
3435

3536
if (!SD.begin(chipSelect)) {
3637
Serial.println("initialization failed!");
37-
while (1);
38+
while (1)
39+
;
3840
}
3941
Serial.println("initialization done.");
4042

examples/NonBlockingWrite/NonBlockingWrite.ino

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,21 @@ void setup() {
5555
pinMode(LED_BUILTIN, OUTPUT);
5656

5757
// wait for Serial Monitor to connect. Needed for native USB port boards only:
58-
while (!Serial);
58+
while (!Serial)
59+
;
5960

6061
Serial.print("Initializing SD card...");
6162

6263
if (!SD.begin(chipSelect)) {
6364
Serial.println("initialization failed. Things to check:");
6465
Serial.println("1. is a card inserted?");
6566
Serial.println("2. is your wiring correct?");
66-
Serial.println("3. did you change the chipSelect pin to match your shield or module?");
67-
Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");
68-
while (true);
67+
Serial.println(
68+
"3. did you change the chipSelect pin to match your shield or module?");
69+
Serial.println("Note: press reset or reopen this serial monitor after "
70+
"fixing your issue!");
71+
while (true)
72+
;
6973
}
7074

7175
Serial.println("initialization done.");
@@ -79,7 +83,8 @@ void setup() {
7983
if (!myFile) {
8084
Serial.print("error opening ");
8185
Serial.println(filename);
82-
while (true);
86+
while (true)
87+
;
8388
}
8489

8590
// add some new lines to start

examples/ReadWrite/ReadWrite.ino

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,21 @@ void setup() {
2727
// Open serial communications and wait for port to open:
2828
Serial.begin(9600);
2929
// wait for Serial Monitor to connect. Needed for native USB port boards only:
30-
while (!Serial);
30+
while (!Serial)
31+
;
3132

3233
Serial.print("Initializing SD card...");
3334

3435
if (!SD.begin(chipSelect)) {
3536
Serial.println("initialization failed. Things to check:");
3637
Serial.println("1. is a card inserted?");
3738
Serial.println("2. is your wiring correct?");
38-
Serial.println("3. did you change the chipSelect pin to match your shield or module?");
39-
Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");
40-
while (true);
39+
Serial.println(
40+
"3. did you change the chipSelect pin to match your shield or module?");
41+
Serial.println("Note: press reset or reopen this serial monitor after "
42+
"fixing your issue!");
43+
while (true)
44+
;
4145
}
4246

4347
Serial.println("initialization done.");

examples/listfiles/listfiles.ino

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
** SDI - pin 12
1212
** CLK - pin 13
1313
** CS - depends on your SD card shield or module.
14-
Pin 10 used here for consistency with other Arduino examples
14+
Pin 10 used here for consistency with other Arduino examples
1515
(for MKRZero SD: SDCARD_SS_PIN)
1616
1717
created Nov 2010
@@ -22,7 +22,7 @@
2222
by Scott Fitzgerald
2323
modified 24 July 2020
2424
by Tom Igoe
25-
25+
2626
This example code is in the public domain.
2727
2828
*/
@@ -32,20 +32,24 @@ const int chipSelect = 10;
3232
File root;
3333

3434
void setup() {
35-
// Open serial communications and wait for port to open:
35+
// Open serial communications and wait for port to open:
3636
Serial.begin(9600);
3737
// wait for Serial Monitor to connect. Needed for native USB port boards only:
38-
while (!Serial);
38+
while (!Serial)
39+
;
3940

4041
Serial.print("Initializing SD card...");
4142

4243
if (!SD.begin(chipSelect)) {
4344
Serial.println("initialization failed. Things to check:");
4445
Serial.println("1. is a card inserted?");
4546
Serial.println("2. is your wiring correct?");
46-
Serial.println("3. did you change the chipSelect pin to match your shield or module?");
47-
Serial.println("Note: press reset or reopen this serial monitor after fixing your issue!");
48-
while (true);
47+
Serial.println(
48+
"3. did you change the chipSelect pin to match your shield or module?");
49+
Serial.println("Note: press reset or reopen this serial monitor after "
50+
"fixing your issue!");
51+
while (true)
52+
;
4953
}
5054

5155
Serial.println("initialization done.");
@@ -64,8 +68,8 @@ void loop() {
6468
void printDirectory(File dir, int numTabs) {
6569
while (true) {
6670

67-
File entry = dir.openNextFile();
68-
if (! entry) {
71+
File entry = dir.openNextFile();
72+
if (!entry) {
6973
// no more files
7074
break;
7175
}

0 commit comments

Comments
 (0)