SD.h library


hi,

just starting use sd.h library access sd card. tried example printdirectory code gives directory listing not work. there no way leave while(true) loop. added break clause when reaches last file, , function works now, sort of.

if try calling function repeatedly works once. it's if there persistent data corrupting when called second time. tried adding rewinddirectory() start of function make sure starts beginning each time called doesn't help. notice function opens instance of file object while initial file object still open. printdirectory code seems logical there problem library?

here code, can see printdirectory() function called 3 times.

code: [select]
#include <sd.h>

file root;

void setup()
{
  serial.begin(9600);
  pinmode(10, output);

  sd.begin(4);

  root = sd.open("/");

  printdirectory(root, 0);
  root.rewinddirectory();
  printdirectory(root, 0);
  root.rewinddirectory();
  printdirectory(root, 0);
  root.rewinddirectory();
 
  serial.println("done!");
}

void loop()
{
  // nothing happens after setup finishes.
}

void printdirectory(file dir, int numtabs) {
   while(true) {

     file entry =  dir.opennextfile();
     if (! entry) {
       // no more files
       serial.println("**nomorefiles**");
       entry.rewinddirectory();
       entry.close();
       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 not
       serial.print("\t\t");
       serial.println(entry.size(), dec);
     }
   }
}


my sd card has 9 files on , 1 subdirectory same 9 files in that. output of code :

code: [select]
001.txt 33
002.txt 15
003.txt 15
004.txt 13
005.txt 15
006.txt 15
007.txt 15
008.txt 15
009.txt 185
newfol~2/
006.txt 15
007.txt 15
008.txt 15
009.txt 185
001.txt 33
002.txt 15
003.txt 15
004.txt 13
005.txt 15
**nomorefiles**
**nomorefiles**
001.txt 33
002.txt 15
003.txt 15
**nomorefiles**
**nomorefiles**
done!


you can see first time function called files in root listed correctly. second time called prints nothing, third time prints first 3 files ....

any suggestions?

how sdfat library compare ships arduino?

thanks.

sd.h old , buggy.  it wrapper old version of sdfat wrote in 2009.

you added bug example caused memory leak broken.

code: [select]

      serial.println(entry.size(), dec);
    }
    entry.close();  <-------- don't remove this, frees memory.
  }


the example stopped listing files when free memory exhausted.

you don't need these won't hurt.
code: [select]

      // no more files
      serial.println("**nomorefiles**");
      entry.rewinddirectory();  <--------------- not needed
      entry.close();  <-------------------------  not needed, file didn't open there no memory.
      break;


this listed on first call.  files not sorted correct.
quote

001.txt      33
002.txt      15
003.txt      15
004.txt      13
005.txt      15
006.txt      15
007.txt      15
008.txt      15
009.txt      185
newfol~2/
   006.txt      15
   007.txt      15
   008.txt      15
   009.txt      185
   001.txt      33
   002.txt      15
   003.txt      15
   004.txt      13
   005.txt      15
**nomorefiles**
**nomorefiles**


here ran out of memory on second call.
quote

001.txt      33
002.txt      15
003.txt      15
**nomorefiles**  <------------------ memory gone


here third call, no memory no list.
quote

**nomorefiles**


sdfat not use dynamic memory.  happen due poor design of sd.h wrapper allocates memory when open file , frees when close file.

i posted new version of sdfat http://forum.arduino.cc/index.php?topic=259317.0.  has ls() function don't need write list files function.  download , @ examples.  it's far more complex sd.h, that's why arduino company wrote sd.h wrapper reduce number of api functions.


Arduino Forum > Using Arduino > Storage > SD.h library


arduino

Comments

Popular posts from this blog

Connecting Raspberry Pi 2 to P10(1R)-V706 LED Dot Matrix - Raspberry Pi Forums

TypeError: <unknown> is not a numpy array - Raspberry Pi Forums

datso and removing imagetitle - Joomla! Forum - community, help and support