Ultimately I want to count the files on an SD card.
I have taken an example from the Arduino SD library and adapted it for the Wio Terminal.
#include <SPI.h>
#include <Seeed_FS.h>
#include "SD/Seeed_SD.h"
#include "TFT_eSPI.h"
void setup() {
// Try to open USB serial communications
Serial.begin(115200);
String build = String(__DATE__) + " " + String(__TIME__);
//Wait up to 2 seconds for for USB serial port to open
unsigned long endtime = millis() + 2000;
while ((millis() < endtime) && !Serial) {}
bool USBOK = Serial;
// Flush serial output buffers
if (USBOK) Serial.flush();
if (USBOK) {
Serial.print("\r\n\nSD Test Build: ");
Serial.println(build);
}
delay(1000);
if (USBOK) Serial.println("Initializing SD card");
if (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI)) {
if (USBOK) Serial.println("SD init failed!");
while (1);
}
if (USBOK) Serial.println("SD card OK");
if (USBOK) {
File testFile = SD.open("testfile.cfg", FILE_WRITE); // open new file
Serial.print("testFile Open Status: ");
Serial.println(testFile);
if (!testFile) {
Serial.println("testfile.cfg could not be opened");
}
testFile.write('%'); // write a character to the file.
testFile.close();
// Now try to open the root directory to list the files present (from Arduino sample code)
File root = SD.open("/");
Serial.print("root Open Status: ");
Serial.println(root);
printDirectory(root, 0);
Serial.println();
Serial.println("PRINT AGAIN");
Serial.println("-----------");
root.rewindDirectory(); // Return to the first file in the directory
printDirectory(root, 0);
Serial.println("Done!");
}
}
//Start Readings
void loop() { // run over and over//
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
Serial.print("entry Open Status: ");
Serial.println(entry);
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);
}
}
}
When this is run, the testfile is successfully created but the call to SD.open("/") fails.
The output looks like this:
SD Test Build: Feb 22 2024 13:07:25
Initializing SD card
SD card OK
testFile Open Status: 1
root Open Status: 0
entry Open Status: 0
PRINT AGAIN
-----------
entry Open Status: 0
Done!
How can I open the root directory on the SD card so that I can use the openNextFile() function to count files?
Thanks