-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlistfiles.ino
82 lines (70 loc) · 1.76 KB
/
listfiles.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Listfiles
This example shows how print out the files in a
directory on a SD card
The circuit:
* SD card attached
This example code is in the public domain.
*/
#include <STM32SD.h>
// If SD card slot has no detect pin then define it as SD_DETECT_NONE
// to ignore it. One other option is to call 'SD.begin()' without parameter.
#ifndef SD_DETECT_PIN
#define SD_DETECT_PIN SD_DETECT_NONE
#endif
File root;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for Serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
while (!SD.begin(SD_DETECT_PIN)) {
delay(10);
}
Serial.println("initialization done.");
root = SD.open("/");
if (root) {
printDirectory(root, 0);
delay(2000);
Serial.println();
Serial.println("Rewinding, and repeating below:");
Serial.println();
delay(2000);
root.rewindDirectory();
printDirectory(root, 0);
root.close();
} else {
Serial.println("Could not open root");
}
if (!SD.end()) {
Serial.println("Failed to properly end the SD.");
}
Serial.println("###### End of the SD tests ######");
}
void loop() {
// nothing happens after setup finishes.
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (!entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}