I started using the SDfat lib after having some trouble with the built in SD lib. It works wonderfully. I'm trying to make a four button navigation system: NextDir, PreviousDir, NextFile, PreviousFile. NextDir and NextFile were easy thanks to the openNext() function. And using rewind() it's easy to wrap around to the start of the directory when you hit the end. Opening files backwards is proving much tricker. I need to implement an openPrevious() function similar to openNext(), but in reverse.
Any ideas how to accomplish this? I tried working with file indexes (like in this post: SdFat file index questions - Storage - Arduino Forum) but the indexes aren't always incremented by one and they start at different positions depending on the folder/files. For instance with the following code and 2 directories with two files each, the indexes will read (dir1: 6, 7 dir2: 3, 5)
void SeekToNextFile(SdFile &folder, SdFile &file) {
do {
file.close();
if(!file.openNext(&folder, O_READ)) {
folder.rewind();
do {
file.close();
file.openNext(&folder, O_READ);
}
while(file.isDir());
return;
}
}
while(file.isDir());
}
void NextFile() {
SeekToNextFile(folder, file);
int index = folder.curPosition()/32;
Serial.print("index: ");
Serial.println(index);
}
A thought of an approach whereby you could openNext until you hit the end of the directory, caching all the indexes in an array, it's just that RAM is tight so I want to avoid making that big int array if possible. Any ideas?
Use curPosition() to get the position of the folder file. Use open by index to open file.
This is not debugged code - just the basic idea.
bool openPrevious(SdBaseFile& folder, SdFile &file, uint8_t oflag) {
// dir size is 32.
uint16_t index = folder.curPosition()/32;
if (index < 2) return false;
// position to possible previous file location.
index -= 2;
do {
if (file.open(&folder, index, oflag)) return true;
} while (index-- > 0);
return false;
}
Thank You! Very cool method!
But when I give acces to file by index, method getName() give me short file name. Maybe there is way to give short and long filenames from index? It could help me to create list of files which takes up little memory. In RAM I create 8.3 format filenames list, sorting it, but when user want to acces to any file, Arduino returns long file name.
And if there is way to give long filename from index, I no need save names, I only save number indexes in array, and returns long name from indexes.
Sorry, for my bad English
fat16lib:
I assume previous means before last opened file.
Use curPosition() to get the position of the folder file. Use open by index to open file.
// dir size is 32.
uint16_t index = folder.curPosition()/32;
if (index < 2) return false;
// position to possible previous file location.
index -= 2;
do {
if (file.open(&folder, index, oflag)) return true;
} while (index-- > 0);
return false;
}