Tip:
Highlight text to annotate it
X
#include #include
SdReader card; FatVolume vol;
FatReader root; WaveHC wave;
uint8_t dirLevel; dir_t dirBuf;
uint8_t debounce = 0;
#define error(msg) error_P(PSTR(msg));
void play(FatReader &dir);
//////////////////////////////////// SETUP void setup() {
Serial.begin(9600); putstring_nl("\nWave test!");
putstring("Free RAM: "); Serial.println(FreeRam());
// if (!card.init(true)) { if (!card.init()) {
error("Card init. failed!"); }
// enable optimize read - some cards may timeout. Disable if you're having problems
card.partialBlockRead(true); digitalWrite(A0, HIGH);
// Now we will look for a FAT partition! uint8_t part;
for (part = 0; part < 5; part++) { if (vol.init(card, part))
break; }
if (part == 5) { error("No valid FAT partition!");
}
putstring("Using partition "); Serial.print(part, DEC);
putstring(", type is FAT"); Serial.println(vol.fatType(), DEC); // FAT16
or FAT32?
// Try to open the root directory if (!root.openRoot(vol)) {
error("Can't open root dir!"); }
putstring_nl("Files found (* = fragmented):");
// Print out all of the files in all the directories. root.ls(LS_R | LS_FLAG_FRAGMENTED);
}
//////////////////////////////////// LOOP void loop() {
if(digitalRead(A0) == HIGH) { debounce = 0; // Reset debounce counter
return; // and nothing else }
if(++debounce >= 20) { // Debounced button press
uint8_t n; char name[20];
root.rewind(); play(root);
} }
/////////////////////////////////// HELPERS /*
* print error message and halt */
void error_P(const char *str) { PgmPrint("Error: ");
SerialPrint_P(str); sdErrorCheck();
while(1); }
void sdErrorCheck(void) { if (!card.errorCode()) return;
PgmPrint("\r\nSD I/O error: "); Serial.print(card.errorCode(), HEX);
PgmPrint(", "); Serial.println(card.errorData(), HEX);
while(1); }
/* * 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 it if not a subdirectory and not a .WAV file
if (!DIR_IS_SUBDIR(dirBuf) && strncmp_P((char *)&dirBuf.name[8], PSTR("WAV"),
3)) { continue;
}
Serial.println(); // clear out a new line
for (uint8_t i = 0; i < dirLevel; i++) { Serial.write(' ');
} if (!file.open(vol, dirBuf)) { // open the
file in the directory error("file.open failed"); // something went
wrong }
if (file.isDir()) { putstring("Subdir: ");
printEntryName(dirBuf); Serial.println();
dirLevel += 2; // add more spaces // play files in subdirectory
play(file); dirLevel -= 2;
} else {
// Aha! we found a file that isnt a directory putstring("Playing ");
printEntryName(dirBuf); if (!wave.create(file)) {
putstring(" Not a valid WAV"); }
else { Serial.println();
wave.play();
uint8_t n = 0; while (wave.isplaying)
{// playing occurs in interrupts, so we print dots in realtime
if(digitalRead(A0) == HIGH) // Button not pressed
{ return; // exit early if the button is not
pressed anymore. }
putstring("."); if (!(++n % 32))Serial.println();
delay(100); }
sdErrorCheck(); // everything OK? // if (wave.errors)Serial.println(wave.errors);
// wave decoding errors }
} }
}