void PBStorage::ls(uint8_t flags) { SoftwareSerial btSerial(2, 4); btSerial.begin(38400); if (!mFile.openRoot(&mVolume)) btSerial.println("openRoot failed"); //mFile.ls(flags); dir_t p; mFile.rewind(); while (mFile.readDir(p) > 0) { // 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 file name with possible blank fill //root.printDirName(*p, flags & (LS_DATE | LS_SIZE) ? 14 : 0); for (uint8_t i = 0; i < 11; i++) { if (p.name[i] == ' ') continue; if (i == 8) { btSerial.print('.'); } btSerial.print( char(p.name[i]) ); } if (DIR_IS_SUBDIR(&p)) { btSerial.print('/'); } // print modify date/time if requested if (flags & LS_DATE) { mFile.printFatDate(p.lastWriteDate); btSerial.print(' '); mFile.printFatTime(p.lastWriteTime); } // print size if requested if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) { btSerial.print(' '); btSerial.print(p.fileSize); } btSerial.println(""); } }
/** * Dive into a folder and recurse depth-first to perform a pre-set operation lsAction: * LS_Count - Add +1 to nrFiles for every file within the parent * LS_GetFilename - Get the filename of the file indexed by nrFiles */ void CardReader::lsDive(SdBaseFile parent, const char* const match/*=NULL*/) { dir_t* p; uint8_t cnt = 0; // Read the next entry from a directory while ((p = parent.getLongFilename(p, fullName, 0, NULL)) != NULL) { char pn0 = p->name[0]; if (pn0 == DIR_NAME_FREE) break; if (pn0 == DIR_NAME_DELETED || pn0 == '.') continue; if (fullName[0] == '.') continue; if (!DIR_IS_FILE_OR_SUBDIR(p)) continue; filenameIsDir = DIR_IS_SUBDIR(p); if (!filenameIsDir && (p->name[8] != 'G' || p->name[9] == '~')) continue; switch (lsAction) { case LS_Count: nrFiles++; break; case LS_GetFilename: if (match != NULL) { if (strcasecmp(match, fullName) == 0) return; } else if (cnt == nrFiles) return; cnt++; break; } } // while readDir }
int Jukebox2::fileCount(FatReader &dir){ //Serial.print("WTF!"); int count = 0; FatReader tempFile; dir_t dirBuf; // buffer for directory reads dir.rewind(); while (dir.readDir(dirBuf) > 0) { // Read every file in the directory one at a time //Serial.print("QQQ!"); // 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; } if (!tempFile.open(vol, dirBuf)) { // open the file in the directory Serial.print("file.open failed"); // something went wrong } if (tempFile.isDir()) { // check if we opened a new directory } else{ count++; } } dir.rewind(); Serial.print("Count was:"); Serial.println(count); return count; }
void ListFiles(Client client, uint8_t flags) { // This code is just copied from SdFile.cpp in the SDFat library // and tweaked to print to the client output in html! dir_t p; root.rewind(); while (root.readDir(p) > 0) { // 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 file name with possible blank fill //root.printDirName(*p, flags & (LS_DATE | LS_SIZE) ? 14 : 0); for (uint8_t i = 0; i < 11; i++) { if (p.name[i] == ' ') continue; if (i == 8) { client.print('.'); } client.print(p.name[i]); } if (DIR_IS_SUBDIR(&p)) { client.print('/'); } // print modify date/time if requested if (flags & LS_DATE) { root.printFatDate(p.lastWriteDate); client.print(' '); root.printFatTime(p.lastWriteTime); } // print size if requested if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) { client.print(' '); client.print(p.fileSize); } client.println("<br>"); } }
/* * 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(' '); // this is for prettyprinting, put spaces in front } if (!file.open(vol, dirBuf)) { // open the file in the directory error("file.open failed"); // something went wrong } if (file.isDir()) { // check if we opened a new directory putstring("Subdir: "); printEntryName(dirBuf); Serial.println(); 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 "); printEntryName(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! uint8_t n = 0; while (wave.isplaying) {// playing occurs in interrupts, so we print dots in realtime putstring("."); if (!(++n % 32))Serial.println(); delay(100); } sdErrorCheck(); // everything OK? // if (wave.errors)Serial.println(wave.errors); // wave decoding errors } } } }
void Jukebox2::PlayRandomSound(FatReader &dir){ //Serial.print("OMG!"); int count = 0; FatReader tempFile; dir_t dirBuf; // buffer for directory reads int r = random(1,fileCount(dir)); dir.rewind(); while (dir.readDir(dirBuf) > 0) { // Read every file in the directory one at a time //Serial.print("LOL!"); //Serial.println(r); // 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; } if (!tempFile.open(vol, dirBuf)) { // open the file in the directory Serial.print("file.open failed"); // something went wrong } if (tempFile.isDir()) { // check if we opened a new directory } else { count++; if(count == r){ Serial.println("found a sound!"); PlaySound(dirBuf); dir.rewind(); return; } } } Serial.println("Never found a sound!"); Serial.print("Rand was:"); Serial.println(r); }
void CardReader::lsDive(const char *prepend,SdFile parent) { dir_t p; uint8_t cnt=0; while (parent.readDir(p, longFilename) > 0) { if( DIR_IS_SUBDIR(&p) && lsAction!=LS_Count && lsAction!=LS_GetFilename) // hence LS_SerialPrint { char path[13*2]; char lfilename[13]; createFilename(lfilename,p); path[0]=0; if(strlen(prepend)==0) //avoid leading / if already in prepend { strcat(path,"/"); } strcat(path,prepend); strcat(path,lfilename); strcat(path,"/"); //Serial.print(path); SdFile dir; if(!dir.open(parent,lfilename, O_READ)) { if(lsAction==LS_SerialPrint) { SERIAL_ECHO_START; SERIAL_ECHOLN(MSG_SD_CANT_OPEN_SUBDIR); SERIAL_ECHOLN(lfilename); } } lsDive(path,dir); //close done automatically by destructor of SdFile } else { if (p.name[0] == DIR_NAME_FREE) break; if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue; if ( p.name[0] == '.') { if ( p.name[1] != '.') continue; } if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue; filenameIsDir=DIR_IS_SUBDIR(&p); if(!filenameIsDir) { if(p.name[8]!='G') continue; if(p.name[9]=='~') continue; } //if(cnt++!=nr) continue; createFilename(filename,p); if(lsAction==LS_SerialPrint) { SERIAL_PROTOCOL(prepend); SERIAL_PROTOCOLLN(filename); } else if(lsAction==LS_Count) { nrFiles++; } else if(lsAction==LS_GetFilename) { if(cnt==nrFiles) return; cnt++; } } } }
void listFiles(const char* path, SdBaseFile* file, Client* client, uint8_t flags) { // This code is just copied from SdFile.cpp in the SDFat library // and tweaked to print to the client output in html! dir_t p; if(strcmp(path, "/") != 0) { int idx = strrchr( path, '/' ) - path; char parent[idx + 1]; strncpy(parent, path, idx); parent[idx] = 0; client->print(F("<a href=\"")); client->print(F("http://")); client->print(Ethernet.localIP()); client->print(parent); client->println(F("\">[To Parent Directory]</a>\n")); } file->rewind(); while (file->readDir(&p) > 0) { // 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 modify date/time if requested if (flags & LS_DATE) { printFatDate(client, p.lastWriteDate); client->print(' '); printFatTime(client, p.lastWriteTime); } // print size if requested if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE)) { client->print(' '); client->print(p.fileSize); } if (DIR_IS_SUBDIR(&p)) { client->print(F(" <dir>")); } // print file name with possible blank fill char name[13]; file->dirName(p, name); client->print(F(" <a href=\"")); client->print(F("http://")); client->print(Ethernet.localIP()); client->print(path); if(strcmp(path, "/") != 0) client->print('/'); client->print(name); client->print("\">"); client->print(name); client->println("</a>"); } }
/** * Dive into a folder and recurse depth-first to perform a pre-set operation lsAction: * LS_Count - Add +1 to nrFiles for every file within the parent * LS_GetFilename - Get the filename of the file indexed by nrFiles * LS_SerialPrint - Print the full path of each file to serial output */ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) { dir_t p; uint8_t cnt = 0; // Read the next entry from a directory while (parent.readDir(p, longFilename) > 0) { // If the entry is a directory and the action is LS_SerialPrint if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) { // Get the short name for the item, which we know is a folder char lfilename[FILENAME_LENGTH]; createFilename(lfilename, p); // Allocate enough stack space for the full path to a folder, trailing slash, and nul boolean prepend_is_empty = (prepend[0] == '\0'); int len = (prepend_is_empty ? 1 : strlen(prepend)) + strlen(lfilename) + 1 + 1; char path[len]; // Append the FOLDERNAME12/ to the passed string. // It contains the full path to the "parent" argument. // We now have the full path to the item in this folder. strcpy(path, prepend_is_empty ? "/" : prepend); // root slash if prepend is empty strcat(path, lfilename); // FILENAME_LENGTH-1 characters maximum strcat(path, "/"); // 1 character // Serial.print(path); // Get a new directory object using the full path // and dive recursively into it. SdFile dir; if (!dir.open(parent, lfilename, O_READ)) { if (lsAction == LS_SerialPrint) { ECHO_LMV(ER, MSG_SD_CANT_OPEN_SUBDIR, lfilename); } } lsDive(path, dir); // close() is done automatically by destructor of SdFile } else { char pn0 = p.name[0]; if (pn0 == DIR_NAME_FREE) break; if (pn0 == DIR_NAME_DELETED || pn0 == '.') continue; if (longFilename[0] == '.') continue; if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue; filenameIsDir = DIR_IS_SUBDIR(&p); if (!filenameIsDir && (p.name[8] != 'G' || p.name[9] == '~')) continue; switch (lsAction) { case LS_Count: nrFiles++; break; case LS_SerialPrint: createFilename(filename, p); ECHO_V(prepend); ECHO_EV(filename); break; case LS_GetFilename: createFilename(filename, p); if (match != NULL) { if (strcasecmp(match, filename) == 0) return; } else if (cnt == nrFiles) return; cnt++; break; } } } // while readDir }