//============================================================================
// SdFat version 2.0.0 BETA ONLY
//============================================================================
#include <SdFat.h> // SdFat V "2.0.0"
const char *SDFAT_ver = SD_FAT_VERSION;
// Use built-in SD for SPI modes on Teensy 3.5/3.6.
// Teensy 4.0 use first SPI port.
// SDCARD_SS_PIN is defined for the built-in SD on some boards.
#ifndef SDCARD_SS_PIN
const uint8_t SD_CS_PIN = SS;
#else // SDCARD_SS_PIN
// Assume built-in SD is used.
const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
#endif // SDCARD_SS_PIN
// SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
// 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
#define SD_FAT_TYPE 3
SdFat sd; // SD
// SdFat SD;
// The file to be parsed.
File m_pSource;
const int DIR_max = 150; // [# of Directory - max]
const int max_DIR_name_lenght = 100;
char CURDIRname[max_DIR_name_lenght] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Elements with missing values will be initialized to 0;
uint32_t DIRcount = 1; // COUNT of all DIRs on SD CARD
char DIR_list_array[DIR_max][max_DIR_name_lenght]; // [# of DIRs max][max DIR name lenght];
// uint32_t TOTAL_PMF_FILE_counter = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(38400); // Teensy ignores this and use full USB speed: T3.x USB 1.1/Full-Speed: 12 Mbps, T4.x Hi-Speed: 480 Mbps.
byte i = 0;
while (!Serial && millis() < 5000) { // wait for Arduino Serial Monitor
}
if (!sd.begin(SdioConfig(FIFO_SDIO))) { // SdFat.h Ver 2.0.0 Beta
Serial.println("m.SD initialization failed!");
} else {
Serial.println("m.SD initialization OK");
LISTDIRs();
m_pSource = sd.open("/"); // ROOT
PrintFiles(m_pSource);
m_pSource.close();
}
#if defined(SdFat_h)
if (!strcmp("2.0.0", SDFAT_ver)) { // ver 2.0.0
Serial.print( "SdFat version "), Serial.println( SD_FAT_VERSION );
} else if ((SDFAT_ver[0] == '2') && (SDFAT_ver[1] == '.')) { // ver 2.
Serial.println("Warning: MTP Library was only tested on SdFat Version 2.0.0 beta");
Serial.print("SdFat Version currently installed "), Serial.println( SD_FAT_VERSION );
} else { // ver 1
Serial.print("Wrong SdFat Version "), Serial.println( SD_FAT_VERSION );
}
#endif
}
void loop() {
// put your main code here, to run repeatedly:
}
// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
//============================================================================
//------------ LIST ALL DIRs ON SD CARD & SAVE IN DIR_list_array -------------
//============================================================================
void LISTDIRs() {
Serial.println("\t**List Directories**");
m_pSource = sd.open("/"); // ROOT
char dirname[max_DIR_name_lenght]; // SDFAT // max_DIR_name_lenght
if (m_pSource.isDirectory()) {
strcpy(DIR_list_array[DIRcount], "/"); // array
Serial.print("ROOT# ");
Serial.print(DIRcount);
Serial.print("\t");
Serial.println(DIR_list_array[DIRcount]); // SDFAT LIB
DIRcount++;
}
while (true) {
File DIR = m_pSource.openNextFile();
if (! DIR) {
// no more files
Serial.println("\t **no more Directories** ");
DIR.close();
break;
}
DIR.getName(dirname, sizeof(dirname));
if (DIR.isDirectory()) {
//DIRcount++;
strcpy(DIR_list_array[DIRcount], dirname); // array
strcat(DIR_list_array[DIRcount], "/");
Serial.print("DIR# ");
Serial.print(DIRcount);
Serial.print("\t");
Serial.println(DIR_list_array[DIRcount]); // SDFAT LIB
DIRcount++;
}
DIR.close();
}
DIRcount --;
Serial.print("\t **Directory count ");
Serial.print(DIRcount), Serial.println(" **"), Serial.println();
m_pSource.close();
}
//----------------------------------------------------------------------------
//============================================================================
// Print Files in Cur. Directory
//============================================================================
void PrintFiles(File dir) {
Serial.println("\t**List Files in Cur. Directory**");
char filename[max_DIR_name_lenght]; // SDFAT
int len = strlen(filename);
char *extension = filename + len - 4;
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
Serial.println("\t **no more files**");
entry.close();
// Serial.print("\t TOTAL PMF_FILE_counter: "), Serial.println(TOTAL_PMF_FILE_counter), Serial.println();
break;
}
entry.getName(filename, sizeof(filename));
if (entry.isDirectory()) {
// skip
} else {
Serial.print(filename); // SDFAT LIB
// files have sizes, directories do not
Serial.print(" \t");
Serial.print(entry.size(), DEC);
Serial.print("\t dirIndex "), Serial.println(entry.dirIndex());
// PMF File Format counter
/*
len = strlen(filename);
extension = filename + len - 4;
if (!strcmp(".pmf", extension) || !strcmp(".PMF", extension)) {
TOTAL_PMF_FILE_counter++;
}
*/
}
entry.close();
}
}
//----------------------------------------------------------------------------