Question

I am trying to write a program that allows for uses to traverse the contents of a SD card with buttons on a touch screen (assume there is only one level of files; aka no folders). However, I am finding it impossible to get a "list" of all the files on the SD card:

  • I can't just create an array of stringsor char* because I don't know the number of files on the card. Besides, I want the container to be dynamic if possible.

  • I can't create a vector because Arduino doesn't recognize std::vector or vector even when I have C++ for Arduino.

  • Searching google produces new does not exist in Arduino's C++

I could use malloc (or new), but that would involve me creating my own container class. As interesting as that may be, the goal of what I am doing is not to implement a dynamic container class.

Have I missed something major in my search for such a function?

Was it helpful?

Solution

I recommend you look at my example MP3 File Player and the Web Player.

There are TWO issues:

1) You need to approach this from the point of view appreciating the Arduino does not have enough resources (SRAM) to hold a list of entire SdFAT's directories. Hence my approach was to use the users console for retaining the list. It dumps the directories contents to the console, along with a corresponding number. From which the user could select the number they wish to enter. Similarly the Web Player does the same thing, but when generating the HTML, it generates a link pointing to the corresponding listed item. Hence the list is stored on the console being either the Browser or Serial Monitor.

2) The default provided SD library is not sufficient to do what you want. Recently Arduino incorporated Bill Greiman’s SdFatLib as the under the hood class. But limited it. Where using Bill’s native SdFat library allows you the use of additional methods to access individual objects, such as getFilename(), not available in SD. This is necessary when going through the directory. The sd.ls(LS_DATE | LS_SIZE) will only dump directly to serial. Where you need to use access the individual files themselves. As show below or in actual code

SdFile file;
char filename[13];
sd.chdir("/",true);
uint16_t count = 1;
while (file.openNext(sd.vwd(),O_READ))
{
  file.getFilename(filename);
  Serial.print(count);
  Serial.print(F(": "));
  Serial.println(filename);
  count++;
}
file.close();

Additionally there are buried public methods accessible by references as show in WebPlayer’s ListFiles() function, to get more discrete handling of the files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top