// Enumerate and possibly display the animated GIF filenames in GIFS directory int enumerateGIFFiles(const char *directoryName, boolean displayFilenames) { numberOfFiles = 0; // Set the current working directory if (! sd.chdir(directoryName, true)) { sd.errorHalt("Could not change to gifs directory"); } sd.vwd()->rewind(); char fn[13]; while (file.openNext(sd.vwd(), O_READ)) { file.getName(fn, 13); // If filename not deleted, count it if (fn[0] != '_') { numberOfFiles++; if (displayFilenames) { Serial.println(fn); delay(20); } } file.close(); } // Set the current working directory if (! sd.chdir("/", true)) { sd.errorHalt("Could not change to root directory"); } return numberOfFiles; }
// Get the full path/filename of the GIF file with specified index void getGIFFilenameByIndex(const char *directoryName, int index, char *pnBuffer) { char filename[13]; // Make sure index is in range if ((index >= 0) && (index < numberOfFiles)) { // Set the current working directory if (! sd.chdir(directoryName, true)) { sd.errorHalt("Could not change to gifs directory"); } // Make sure file is closed before starting file.close(); // Rewind the directory to the beginning sd.vwd()->rewind(); while ((file.openNext(sd.vwd(), O_READ)) && (index >= 0)) { file.getName(filename, 13); // If filename is not marked as deleted, count it if ((filename[0] != '_') && (filename[0] != '~')) { index--; } file.close(); } // Set the current working directory back to root if (! sd.chdir("/", true)) { sd.errorHalt("Could not change to root directory"); } // Copy the directory name into the pathname buffer strcpy(pnBuffer, directoryName); // Append the filename to the pathname strcat(pnBuffer, filename); } }
uint16_t SD_NumberOfFiles(void) { uint16_t fileCount = 0; buffer[0] = 0x01; usb_rawhid_send(buffer, 100); while(file.openNext(sdf.vwd(), O_READ)) { fileCount++; buffer[0]++; usb_rawhid_send(buffer, 100); } file.close(); return fileCount; }
void SD_ListFiles(void) { Serial.begin(9600); while (!Serial) {} delay(1000); Serial.println(); while (file.openNext(sdf.vwd(), O_READ)) { file.printFileSize(&Serial); Serial.write(' '); file.printModifyDateTime(&Serial); Serial.write(' '); file.printName(&Serial); if (file.isDir()) { // Indicate a directory. Serial.write('/'); } Serial.println(); file.close(); } }
int Tune::listFiles() { nb_track = 0; char filename[13] = ""; char firstfilename[13] = ""; // Save the name of the first file to rewind the directory later track.openNext(sd.vwd(), O_READ); track.getName(firstfilename, 13); if (isMP3(firstfilename)) nb_track++; track.close(); // open next file in root. The volume working directory, vwd, is root while (track.openNext(sd.vwd(), O_READ)) { track.getName(filename, 13); if (isMP3(filename)) nb_track++; track.close(); } // now that we know how many available tracks we have // we can itinialize the 2D-array with the correct dimensions tracklist = (char**) malloc (nb_track*sizeof(char*)); for (int k=0; k<nb_track; k++) { tracklist[k] = (char*) malloc(13 * sizeof(char)); } nb_track = 0; // reset for rerun // rewind for first run then loop again track.open(firstfilename); track.getName(filename, 13); if (isMP3(filename)) { for (int j=0; j<13; j++) { tracklist[nb_track][j] = filename[j]; // actually save the filename } nb_track++; } track.close(); // save as above... while (track.openNext(sd.vwd(), O_READ)) { track.getName(filename, 13); if (isMP3(filename)) { for (int j=0; j<13; j++) { tracklist[nb_track][j] = filename[j]; } nb_track++; } track.close(); } return nb_track; }
void startBinLogger(void (*dateTime)(uint16_t *date, uint16_t *time)){ #ifdef LOGGER_DEBUG Serial.print("Size of Struct: "); Serial.println(sizeof(salus_data_t)); Serial.print("Data_DIM: "); Serial.println(DATA_DIM); Serial.print("FILL_DIM: "); Serial.println(FILL_DIM); Serial.print("Sizeof Block: "); Serial.println(sizeof(block_t)); Serial.println(); #endif if (!sd.begin(SD_CHIPSELECT, SD_SPI_SPEED)) { sd.initErrorHalt(); } int number = 0; char sName[80]; // Find a filename that hasn't been used already do { sprintf(sName, "Salus_Results_%d.bin", number++); } while (sd.exists(sName)); binFile.close(); binFile.dateTimeCallback(dateTime); if (!binFile.createContiguous(sd.vwd(), sName, 512 * FILE_BLOCK_COUNT)){ error("createContiguous failed"); } if (!binFile.contiguousRange(&bgnBlock, &endBlock)){ error("contiguousRange failed"); } // Use SdFat's internal buffer ( ???? ) uint8_t* cache = (uint8_t*)sd.vol()->cacheClear(); if (cache == 0) { error("cacheClear failed"); } binFile.dateTimeCallbackCancel(); uint32_t bgnErase = bgnBlock; uint32_t endErase; while (bgnErase < endBlock) { endErase = bgnErase + ERASE_SIZE; if (endErase > endBlock) { endErase = endBlock; } if (!sd.card()->erase(bgnErase, endErase)) { error("erase failed"); } bgnErase = endErase + 1; } // Start a multiple block write. if (!sd.card()->writeStart(bgnBlock, FILE_BLOCK_COUNT)) { error("writeBegin failed"); } }
void MP3Player::PlayTrack(const char* dirName,int track_no,const char* track_name) { if(!PLAY) { finishSearch = false; int count=0; //if(track_no>0) //counter = track_no; if(!isPlayAll) { char temp[20]; isPlayAll = true; sd.chdir();delayMicroseconds(100000); sd.chdir(dirName,true); sd.vwd()->rewind(); sd.vwd()->getName(temp,20); currentDir = temp; counter = 0; } while(true) { if(myFile.openNext(myFile.cwd(), O_READ)) { char FileName[80]; myFile.getName(FileName,80); #if DEBUG //myFile.printName(&Serial); Serial.print(FileName); if (myFile.isDir()) { // Indicate a directory. Serial.write('/'); } Serial.println(); #endif if (!(myFile.isDir()||(String(FileName).indexOf(".mp3")==-1&&String(FileName).indexOf(".MP3")==-1))) { counter++; if(ls_flag) { Serial.print(counter); Serial.print(". "); Serial.print(FileName); Serial.print(" "); Serial.print(myFile.fileSize()); Serial.println(" bytes"); myFile.close(); continue; } //Play(FileName); if(!String(track_name).equals("")) { //counter++; if(String(track_name).equals(FileName)) { Play(FileName); //name = track_name; isPlayAll = false; break; } } else if(track_no==0) { //counter++; Play(FileName); break; } else if(track_no>0) { count++; if(count==track_no) { counter = track_no; Play(FileName); isPlayAll = false; break; } } } } else { isPlayAll = false; finishSearch = true; ls_flag = false; break; } //myFile.getFilename(FileName); myFile.close(); } } }
//------------------------------------------------------------------------------ void setup() { Serial.begin(9600); // Wait for USB Serial while (!Serial) { SysCall::yield(); } delay(1000); cout << F("Type any character to start\n"); // Wait for input line and discard. cin.readline(); cout << endl; // Initialize the SD card at SPI_HALF_SPEED to avoid bus errors with // breadboards. use SPI_FULL_SPEED for better performance. if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) { sd.initErrorHalt(); } if (sd.exists("Folder1") || sd.exists("Folder1/file1.txt") || sd.exists("Folder1/File2.txt")) { error("Please remove existing Folder1, file1.txt, and File2.txt"); } int rootFileCount = 0; sd.vwd()->rewind(); while (file.openNext(sd.vwd(), O_READ)) { if (!file.isHidden()) { rootFileCount++; } file.close(); if (rootFileCount > 10) { error("Too many files in root. Please use an empty SD."); } } if (rootFileCount) { cout << F("\nPlease use an empty SD for best results.\n\n"); delay(1000); } // Create a new folder. if (!sd.mkdir("Folder1")) { error("Create Folder1 failed"); } cout << F("Created Folder1\n"); // Create a file in Folder1 using a path. if (!file.open("Folder1/file1.txt", O_CREAT | O_WRITE)) { error("create Folder1/file1.txt failed"); } file.close(); cout << F("Created Folder1/file1.txt\n"); // Change volume working directory to Folder1. if (!sd.chdir("Folder1")) { error("chdir failed for Folder1.\n"); } cout << F("chdir to Folder1\n"); // Create File2.txt in current directory. if (!file.open("File2.txt", O_CREAT | O_WRITE)) { error("create File2.txt failed"); } file.close(); cout << F("Created File2.txt in current directory\n"); cout << F("\nList of files on the SD.\n"); sd.ls("/", LS_R); // Remove files from current directory. if (!sd.remove("file1.txt") || !sd.remove("File2.txt")) { error("remove failed"); } cout << F("\nfile1.txt and File2.txt removed.\n"); // Change current directory to root. if (!sd.chdir()) { error("chdir to root failed.\n"); } cout << F("\nList of files on the SD.\n"); sd.ls(LS_R); // Remove Folder1. if (!sd.rmdir("Folder1")) { error("rmdir for Folder1 failed\n"); } cout << F("\nFolder1 removed.\n"); cout << F("\nList of files on the SD.\n"); sd.ls(LS_R); cout << F("Done!\n"); }