// 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);
    }
}
예제 #3
0
uint32_t SD_GetFileSize(void)
{
    uint32_t retVal = 0;
    //TEST.TXT
    sdf.chdir();	// Change directory to root
    // OPEN the file for reading:
    if (!file.open("TEST.TXT", O_READ)) {
        sdf.errorHalt("opening FILE for read failed");
    }
    else
    {
        retVal = file.fileSize();
        file.close();

    }

    return retVal;
}
예제 #4
0
boolean SD_ReadFile(const char *filename)
{
    uint32_t t_arrival = millis();
    boolean retVal = false;
    sdf.chdir();
    if (!file.open(filename, O_READ)) {
        sdf.errorHalt("opening FILE for read failed");
    }
    else
    {
        uint32_t len = file.fileSize();
        uint8_t index = 0;
        for(uint32_t i=0; i< len; i++)
        {
            int intData = file.read();
            char byteData;
            int base = 10u;
            //itoa(intData,&byteData,base);	// Convert to ASCII
            buffer[index] = (uint8_t)intData;//byteData;
            if(index < 63u) {
                index++;
            }
            else {
                usb_rawhid_send(buffer, 100u);
                index = 0;
            }

        }
        file.close();
        uint32_t t_exit = millis() - t_arrival;
        buffer[62] = (uint8_t)((t_exit & 0x000000FF)>>8);
        buffer[63] = (uint8_t)t_exit;
        usb_rawhid_send(buffer,100u);

    }
}
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");
}