Hello!
How can I read the volume label from a fat32 fs on an sd card?
I have almost got it, in a way, but I'm hoping there is a better way.
I'm using SdFat, and I temporarily hacked DIR_IS_FILE_OR_SUBDIR() in FatStructs.h like so:
static inline uint8_t DIR_IS_FILE_OR_SUBDIR(const dir_t* dir) {
//return (dir->attributes & DIR_ATT_VOLUME_ID) == 0; // return anything but volume label?
return dir->attributes & DIR_ATT_VOLUME_ID; // return only volume label?
}
This totally breaks openNext() and openNextFile() for normal use, but it causes openNextFile() to return the entries it used to exlude.
That obviously borken hack, plus this sketch:
#include <SdFat.h>
SdFatSdioEX sd;
File root;
File entry;
void setup() {
Serial.begin(9600);
while(!Serial);
while(!initCard());
printVolumeLabel();
}
void loop() {
}
bool initCard () {
Serial.print("sd.open(): ");
if (sd.begin()) {
Serial.print("OK\r\n");
} else {
Serial.print("FAILED\r\n");
delay (500);
return false;
}
sd.chvol();
return true;
}
void printVolumeLabel() {
char fileName[32] = "";
Serial.print("---[ begin / ]---\r\n");
Serial.print("fileName\t\tfileAttr\r\n");
root = sd.open("/");
while (true) {
entry = root.openNextFile();
if (!entry) break;
entry.getName(fileName,32);
Serial.print("\"");
Serial.print(fileName);
Serial.print("\"\t\t");
Serial.print(entry.fileAttr(),HEX);
Serial.print("\r\n");
entry.close();
}
root.close();
Serial.print("---[ end / ]---\r\n");
}
Produces:
sd.open(): OK
---[ begin / ]---
fileName fileAttr
"M_100" 8
"AG" F
---[ end / ]---
That "M_100" is the volume label.
I don't know what that "AG" is, perhaps a deleted entry? I'm not worried about that. I'm sure I can filter it later by looking at fileAttr() or something.
I took a clean new card, formatted fat32 with no label, and assigned a label of "VOLUMELABEL" (which happens to exactly fill all the characters allowed)
That card produces:
sd.open(): OK
---[ begin / ]---
fileName fileAttr
"VOLUMELA.BEL" 8
---[ end / ]---
OK! So, clearly it is physically attainable!
However the way I got it is terrible. Is there some better way?
I'm ok with ignoring FAT12, FAT16, and exFAT.
At this point I'm looking at I guess copying openNextFile() and openNext() to make openNextFile_r() and openNext_r() "raw" versions that don't exlude anything, so that you can do your own filtering for the type of entry you want, but it seems kind of ungainly, and I suspect there might already be a way to get the info using interfaces that already exist, just not normally used, like fileAttr() etc.
Thank you