chipKIT® Development Platform

Inspired by Arduino™

SD card file list

Created Wed, 18 Jul 2012 13:44:16 +0000 by Paulus


Paulus

Wed, 18 Jul 2012 13:44:16 +0000

Hi all, This is my first post so be gentle! I'm using a UNO32 with an SD card attached. All the SD example files work however I want search the SD card for *.bmp files and create an array of these file names sort them and then run a slide show of the images. Can anyone help with the first part of searching foe *.bmp files and creating an array of names?

Thanks in advance


rasmadrak

Thu, 19 Jul 2012 09:20:50 +0000

Hi there!

There is a example program that lists the files on the SD card and prints out a list to the serial output. You should be able to adapt it to your liking and needs. :) I can't recall the name of it at the moment thou, but it's in the SD library "folder" in the examples-menu.


mikes

Thu, 19 Jul 2012 13:35:05 +0000

rasmadrak is referring to CardInfo. But that sketch uses a single call to a library function to print the files

root.ls(LS_R | LS_DATE | LS_SIZE);

You would have to dig into the library to find out how it works.


rasmadrak

Thu, 19 Jul 2012 13:55:19 +0000

void SdFile::ls(uint8_t flags, uint8_t indent) {
  dir_t* p;

  rewind();
  while ((p = readDirCache())) {
    // done if past last used entry
    if (p->name[0] == DIR_NAME_FREE) break;

    // skip deleted entry and entries for . and  ..
    if (p->name[0] == DIR_NAME_DELETED || p->name[0] == '.') continue;

    // only list subdirectories and files
    if (!DIR_IS_FILE_OR_SUBDIR(p)) continue;

    // print any indent spaces
    for (int8_t i = 0; i < indent; i++) Serial.print(' ');

    // print file name with possible blank fill
    printDirName(*p, flags & (LS_DATE | LS_SIZE) ? 14 : 0);

    // print modify date/time if requested
    if (flags & LS_DATE) {
       printFatDate(p->lastWriteDate);
       Serial.print(' ');
       printFatTime(p->lastWriteTime);
    }
    // print size if requested
    if (!DIR_IS_SUBDIR(p) && (flags & LS_SIZE)) {
      Serial.print(' ');
      Serial.print(p->fileSize);
    }
    Serial.println();

    // list subdirectory content if requested
    if ((flags & LS_R) && DIR_IS_SUBDIR(p)) {
      uint16_t index = curPosition()/32 - 1;
      SdFile s;
      if (s.open(this, index, O_READ)) s.ls(flags, indent + 2);
      seekSet(32 * (index + 1));
    }
  }
}

That's the code (in SD/utility/SdFile.cpp).

Should be simple enough to add a function to the SD library that returns an array of the files instead of Serial.print'ing.


mikes

Sat, 21 Jul 2012 01:44:34 +0000

I had to initialize the SD two different ways to get File and and volume information commands to work in the same Sketch. Is this the way it is supposed to work?

if (!SD.begin(6)) { //makes files work
    Serial.println("initialization failed!");
    return;
  }
  if (!card.init(SPI_HALF_SPEED, 6)) { //makes root.ls and other things work
    Serial.println("initialization failed!");
    return;
  }

The first way is used in the Files example and the second way is used in the CardInfo example. I seems strange to me that I would have to use two different kinds of inits to get full functionality. It seems to be working but it would be nice to know if I am doing it correctly.


rasmadrak

Sun, 22 Jul 2012 17:07:08 +0000

The SD.begin() starts the SD card including the "built-in" card.init() function. What you would have to do is to add the wrapper function for root.ls() in the SD card library so that you may call it via SD.ls() or similar.

What you've done above is basically initializing two different SD card "systems" on the same SD card.


mikes

Mon, 23 Jul 2012 17:58:34 +0000

From SD.h

private:
  // These are required for initialisation and use of sdfatlib
  Sd2Card card;
  SdVolume volume;
  SdFile root;

What about making these public so you can call it directly

SD.root.ls