Modifications

Sauter à la navigation Sauter à la recherche
6 543 octets ajoutés ,  13 mars 2012 à 12:39
aucun résumé de modification
Ligne 103 : Ligne 103 :  
   // Print out all of the files in all the directories.
 
   // Print out all of the files in all the directories.
 
   lsR(root);
 
   lsR(root);
 +
}
 +
</nowiki>
 +
 +
== Looking for files in a directory ==
 +
 +
OK now that you've initialized the card, we perform a recursive list of all files found. This is useful for debugging and ALSO shows how you can navigate the file system
 +
 +
To start, pass a directory object (like '''root''') to '''lsR()''' which will do the following:
 +
# Read a file from the directory. The files are read in the order they are copied into the directory, '''not alphabetical order'''!
 +
# If the directories are the special links "." (current directory) or ".." (upper directory) it ignores them and goes to step 1 again.
 +
# It prints out spaces to create a nicely formatted output. Each level of directory gets 2 spaces
 +
# It prints out the name of the file in 8.3 format
 +
# If it is a subdirectory, it makes a new object and opens up the subdirectory. Then it prints out all of the files in that new directory
 +
# It continues to step 1 until there are no more files to be read
 +
 +
<nowiki>/*
 +
* list recursively - possible stack overflow if subdirectories too nested
 +
*/
 +
void lsR(FatReader &d)
 +
{
 +
  int8_t r;                    // indicates the level of recursion
 +
 
 +
  while ((r = d.readDir(dirBuf)) > 0) {    // read the next file in the directory
 +
    // skip subdirs . and ..
 +
    if (dirBuf.name[0] == '.')
 +
      continue;
 +
   
 +
    for (uint8_t i = 0; i < dirLevel; i++)
 +
      Serial.print(' ');        // this is for prettyprinting, put spaces in front
 +
    printName(dirBuf);          // print the name of the file we just found
 +
    Serial.println();          // and a new line
 +
   
 +
    if (DIR_IS_SUBDIR(dirBuf)) {  // we will recurse on any direcory
 +
      FatReader s;                // make a new directory object to hold information
 +
      dirLevel += 2;              // indent 2 spaces for future prints
 +
      if (s.open(vol, dirBuf))
 +
        lsR(s);                    // list all the files in this directory now!
 +
      dirLevel -=2;                // remove the extra indentation
 +
    }
 +
  }
 +
  sdErrorCheck();                  // are we doign OK?
 +
}
 +
</nowiki>
 +
 +
There is also a helper called '''printName''' which prints out the file in a nice format. Files are named in '''8.3''' format, an older and simpler way of addressing files. Its a little less pretty than "Long Name Format" so watch out to see what your files are renamed as. For example "Bird song.wav" may be renamed to "BIRDSONG.WAV" or "BIRDSO~1.WAV" !
 +
 +
<nowiki>/*
 +
* print dir_t name field. The output is 8.3 format, so like SOUND.WAV or FILENAME.DAT
 +
*/
 +
void printName(dir_t &dir)
 +
{
 +
  for (uint8_t i = 0; i < 11; i++) {    // 8.3 format has 8+3 = 11 letters in it
 +
    if (dir.name[i] == ' ')
 +
        continue;        // dont print any spaces in the name
 +
    if (i == 8)
 +
        Serial.print('.');          // after the 8th letter, place a dot
 +
    Serial.print(dir.name[i]);      // print the n'th digit
 +
  }
 +
  if (DIR_IS_SUBDIR(dir))
 +
    Serial.print('/');      // directories get a / at the end
 +
}
 +
</nowiki>
 +
One thing that appears in '''loop()''' is '''dir.rewind()'''. The reason we rewind a directory is that our Arduino code is very simple. It can go through the files in a directory but only 'forward', not backward (FAT format is kinda like that). So if you skipped a file and want to go back, or you've gone through the directory, you will need to call '''rewind()''' to set it back to the beginning!
 +
 +
== Playing all the files ==
 +
 +
The digital audio player plays all files in the card. To do that it recursively looks in every directory, just like lsR() above so the code looks somewhat similar. The big difference is we call the play() routine to play a file!
 +
 +
To start, pass a directory object (like root) to '''lsR()''' which will do the following:
 +
# Read a file from the directory. The files are read in the order they are copied into the directory, '''not alphabetical order'''!
 +
# If the directories are the special links "." (current directory) or ".." (upper directory) it ignores them and goes to step 1 again.
 +
# It prints out spaces to create a nicely formatted output. Each level of directory gets 2 spaces
 +
# It prints out the name of the file in 8.3 format
 +
# If it is a subdirectory, it makes a new object and opens up the subdirectory. Then it plays all of the wave files in that new directory
 +
# If it isn't a subdirectory, it will try to play the file by opening it as a Wave object. That requires looking through the file and trying to find a Wave header, etc. If it doesnt succeed it will print out that its not valid and skip to the next file
 +
# If the wave file is valid, it will finally start the file by calling '''play()''' on the Wave object
 +
# While the wave sound file is playing, it prints out a dot every 100 ms or so.
 +
# It continues to step 1 until there are no more files to be read
 +
 +
<nowiki>/*
 +
* play recursively - possible stack overflow if subdirectories too nested
 +
*/
 +
void play(FatReader &dir)
 +
{
 +
  FatReader file;
 +
  while (dir.readDir(dirBuf) > 0) {    // Read every file in the directory one at a time
 +
    // skip . and .. directories
 +
    if (dirBuf.name[0] == '.')
 +
      continue;
 +
   
 +
    Serial.println();            // clear out a new line
 +
   
 +
    for (uint8_t i = 0; i < dirLevel; i++)
 +
      Serial.print(' ');      // this is for prettyprinting, put spaces in front
 +
 +
    if (!file.open(vol, dirBuf)) {      // open the file in the directory
 +
      Serial.println("file.open failed");  // something went wrong :(
 +
      while(1);                            // halt
 +
    }
 +
   
 +
    if (file.isDir()) {                    // check if we opened a new directory
 +
      putstring("Subdir: ");
 +
      printName(dirBuf);
 +
      dirLevel += 2;                      // add more spaces
 +
      // play files in subdirectory
 +
      play(file);                        // recursive!
 +
      dirLevel -= 2;   
 +
    }
 +
    else {
 +
      // Aha! we found a file that isnt a directory
 +
      putstring("Playing "); printName(dirBuf);      // print it out
 +
      if (!wave.create(file)) {            // Figure out, is it a WAV proper?
 +
        putstring(" Not a valid WAV");    // ok skip it
 +
      } else {
 +
        Serial.println();                  // Hooray it IS a WAV proper!
 +
        wave.play();                      // make some noise!
 +
     
 +
        while (wave.isplaying) {          // playing occurs in interrupts, so we print dots in realtime
 +
          putstring(".");
 +
          delay(100);
 +
        }
 +
        sdErrorCheck();                    // everything OK?
 +
//        if (wave.errors)Serial.println(wave.errors);    // wave decoding errors
 +
      }
 +
    }
 +
  }
 
}
 
}
 
</nowiki>
 
</nowiki>
29 917

modifications

Menu de navigation