Hello,
I am working with the following SD card library:
https://code.google.com/p/sdfatlib/
I'm needing to obtain the names of the files on the card and store them into an array that will be used to display the names on a screen as to allow selection of the file that wanted to play.
There is an example code file that obtains the file names and prints them to the serial monitor. The code is as follows:
"OpenNext" Example
/*
* Open all files in the root dir and print their filename and modify date/time
*/
#include <SdFat.h>
// SD chip select pin
const uint8_t chipSelect = SS;
// file system object
SdFat sd;
SdFile file;
// define a serial output stream
ArduinoOutStream cout(Serial);
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
while (!Serial) {} // wait for Leonardo
delay(1000);
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
}
//------------------------------------------------------------------------------
int i = 0;
void loop() {
// open next file in root. The volume working directory, vwd, is root
while (file.openNext(sd.vwd(), O_READ)) { //openNext() in SDbaseFile.cpp
file.printName(&Serial);
cout << ' ';
file.printModifyDateTime(&Serial);
cout << endl;
file.close();
}
}
Even with some sleuthing, I am not understanding how the program is gathering the files. Here's my research:
I looked up the definition of the printName() function:
bool SdBaseFile::printName(Print* pr) {
char name[13];
if (!getFilename(name)) {
DBG_FAIL_MACRO;
goto fail;
}
return pr->print(name) > 0;
fail:
return false;
}
That leads me to the print() member function of the Print object "pr":
"print.cpp"
I discovered many print() functions indicating overloading. I found the one of interest:
size_t Print::print(const char str[])
{
return write(str);
}
Which led me to the function write(), also located in print.cpp:
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
n += write(*buffer++);
}
return n;
}
That's where the trail ends.
I'm not understanding two things:
- I don't see where the code is printing the file names to the serial monitor. If I can discover how it acquires the names and cycles through them, I can access and save them as it iterates through.
- In the write() function, there are two input parameters but when the print() function calls write(), it only provides one parameter (str).
I would appreciate insight from someone who mind lending their time.
-P


