Problem writing filenames read from SD to TFT

Hi everybody, I have just recently downloaded the SdRaw and SdFat library and trying to use it.
I am using 5 inch tft & Mega with CTE shield. I have the example OpenNext.ino working with my SD
card. The problem I am having is I want to list the SD files on the TFT.
The example writes to Serial, how do I get the filenames into a character array (string) and write to
the TFT.

Trevor.

Hi,

I saw your other post asking the same thing… And I couldn’t easily figure it out so I didn’t respond, but since you are now asking again, I came up with something that may help you in the right direction.

int maxfiles = 300;
int maxfilenamelength = 20;

#include <SPI.h>
#include <SdFat.h>

// SD chip select pin
const uint8_t chipSelect = 52;

// file system object
SdFat sd;

SdFile file;
//------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  while (!Serial) {} // wait for Leonardo
  delay(1000);
  Serial.println();

  // 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();
  }
  char filenames[maxfiles][maxfilenamelength];
  int fileptr = 0;
  // open next file in root.  The volume working directory, vwd, is root
  while (file.openNext(sd.vwd(), O_READ)) {
    if (fileptr >= maxfiles) continue;
    file.getName(filenames[fileptr++], maxfilenamelength);
    file.close();
  }
  for (int a = 0; a < fileptr; a++)
    Serial.println(filenames[a]);
  Serial.print("Total files found :");
  Serial.println(fileptr);
}
//------------------------------------------------------------------------------
void loop() {}

You just need to decide how you will display them on your TFT screen, but all the filenames are stored in the array ‘filenames’.

Regards,

Graham

Hi Graham, thanks for your reply, the solution you gave worked as expected.
I had tried "file.getName" before, but got "no member" error.
I didn't know it had 2 parameters. As a matter of interest where is (getName) defined as I couldn't find it.

Trevor

PS thanks again for your help.

It is defined in FatFile.h, FatFileSFN.cpp and FatFileLFN.cpp.

Regards,

Graham