From d7ec1d455865a382c7b5d1ace7a73f0a28eff139 Mon Sep 17 00:00:00 2001 From: hasenradball Date: Mon, 18 Nov 2024 17:17:58 +0100 Subject: [PATCH 01/11] add new example for enhanced file listing --- .../listFilesEnhanced/listfilesEnhanced.ino | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino diff --git a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino new file mode 100644 index 0000000000..858ec146ec --- /dev/null +++ b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino @@ -0,0 +1,137 @@ +/* + Listfiles Enhanced + + This example shows how to list files in SDcard in the following way: + 1) collect all directories + 2) build full path of directories and keep in mind + 3) then print all files with the help of the directorie pathes + + The circuit: + SD card attached to SPI bus as follows: + ** MOSI - pin 11 + ** MISO - pin 12 + ** CLK - pin 13 + ** CS - pin 4 + + created: + + 18. Nov 2024 + by Frank Häfele + + This example code is in the public domain. + +*/ +#include +#include +#include + +#define SD_CS_PIN 2 + + +void dir(String path) { + std::vector directories; + collectDirectories(path, directories); + for (auto directory:directories) { + printDirectoryName(directory.c_str(), 1); + File fs = SD.open(directory); + printFilesInDirectory(fs); + Serial.println("\n==============="); + fs.close(); + } +} + +void setup() { + // Open serial communications and wait for port to open: + Serial.begin(115200); + + Serial.print("\n\n==== List Directory ====\n\n"); + listDirectory(); + + Serial.println("done!"); +} + +void loop() { + // nothing happens after setup finishes. +} + +void listDirectory() { + Serial.print("\n\nInitializing SD card..."); + if (!SD.begin(SD_CS_PIN)) { + Serial.println("initialization failed!"); + return; + } + Serial.print("initialization successful.\n"); + Serial.print("List Files:\n"); + dir("/"); +} + + +void printDirectoryName(const char* name, uint8_t level) { + for (uint8_t i = 0; i < level; ++i) { + Serial.print(" "); + } + Serial.println(name); +} + + + +// helper function: combine path +String joinPath(const String &base, const String &name) { + if (base.endsWith("/")) { + return base + name; + } + return base + "/" + name; +} + +// recusive function to collect directory names +void collectDirectories(const String &dirname, std::vector &directories) { + File root = SD.open(dirname); + if (!root || !root.isDirectory()) { + Serial.printf("Error: Verzeichnis %s konnte nicht geöffnet werden\n", dirname.c_str()); + return; + } + directories.push_back(dirname); // Verzeichnis speichern + + File file = root.openNextFile(); + while (file) { + if (file.isDirectory()) { + String fullPath = joinPath(dirname, file.name()); // Vollständigen Pfad erstellen + collectDirectories(fullPath, directories); // Rekursiver Aufruf + } + file = root.openNextFile(); + } + root.close(); +} + +// print filenames +void printFileName(File file) { + Serial.print("\t"); + Serial.printf("%30s", file.name()); + // files have sizes, directories do not + Serial.print(" - "); + Serial.print(file.size(), DEC); + time_t cr = file.getCreationTime(); + time_t lw = file.getLastWrite(); + struct tm* tmstruct = localtime(&cr); + Serial.printf("\tCREATION: %d-%02d-%02d %02d:%02d:%02d", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec); + tmstruct = localtime(&lw); + Serial.printf("\tLAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec); +} + + +// print files in directories +void printFilesInDirectory(File dir) { + while (true) { + auto file = dir.openNextFile(); + if (!file) { + // no more files + break; + } + if (file.isDirectory()) { + continue; + } + else { + printFileName(file); + } + } +} \ No newline at end of file From 033daf663d58f9bf92417d595fa47b7341888206 Mon Sep 17 00:00:00 2001 From: hasenradball Date: Mon, 18 Nov 2024 17:18:55 +0100 Subject: [PATCH 02/11] adapt Pin --- libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino index 858ec146ec..728189cbf3 100644 --- a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino +++ b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino @@ -25,7 +25,7 @@ #include #include -#define SD_CS_PIN 2 +#define SD_CS_PIN 4 void dir(String path) { From 4e430d69e2d46efca860bb5344127789ad001d3e Mon Sep 17 00:00:00 2001 From: hasenradball Date: Mon, 18 Nov 2024 21:07:38 +0100 Subject: [PATCH 03/11] adapt due to clanf format check in line 14 --- .../listFilesEnhanced/listfilesEnhanced.ino | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino index 728189cbf3..ae85954984 100644 --- a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino +++ b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino @@ -1,22 +1,20 @@ /* Listfiles Enhanced - This example shows how to list files in SDcard in the following way: + This example demonstrates how to list files on an SDcard in the following way: 1) collect all directories 2) build full path of directories and keep in mind 3) then print all files with the help of the directorie pathes - The circuit: - SD card attached to SPI bus as follows: - ** MOSI - pin 11 - ** MISO - pin 12 - ** CLK - pin 13 - ** CS - pin 4 - - created: - - 18. Nov 2024 - by Frank Häfele + Wiring: + SDcard attached to SPI bus as follows: + - MOSI: pin 11 + - MISO: pin 12 + - CLK : pin 13 + - CS : pin 4 + + Created: + 18. Nov 2024 by Frank Häfele This example code is in the public domain. From 300af919eadb4f637523d9b2c0f3f38ea35f52ae Mon Sep 17 00:00:00 2001 From: hasenradball Date: Mon, 18 Nov 2024 21:19:45 +0100 Subject: [PATCH 04/11] Adapt further clang findings --- .../listFilesEnhanced/listfilesEnhanced.ino | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino index ae85954984..5d8ceb5bd0 100644 --- a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino +++ b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino @@ -29,7 +29,7 @@ void dir(String path) { std::vector directories; collectDirectories(path, directories); - for (auto directory:directories) { + for (auto directory : directories) { printDirectoryName(directory.c_str(), 1); File fs = SD.open(directory); printFilesInDirectory(fs); @@ -75,30 +75,30 @@ void printDirectoryName(const char* name, uint8_t level) { // helper function: combine path String joinPath(const String &base, const String &name) { - if (base.endsWith("/")) { - return base + name; - } - return base + "/" + name; + if (base.endsWith("/")) { + return base + name; + } + return base + "/" + name; } // recusive function to collect directory names void collectDirectories(const String &dirname, std::vector &directories) { - File root = SD.open(dirname); - if (!root || !root.isDirectory()) { - Serial.printf("Error: Verzeichnis %s konnte nicht geöffnet werden\n", dirname.c_str()); - return; - } - directories.push_back(dirname); // Verzeichnis speichern - - File file = root.openNextFile(); - while (file) { - if (file.isDirectory()) { - String fullPath = joinPath(dirname, file.name()); // Vollständigen Pfad erstellen - collectDirectories(fullPath, directories); // Rekursiver Aufruf - } - file = root.openNextFile(); - } - root.close(); + File root = SD.open(dirname); + if (!root || !root.isDirectory()) { + Serial.printf("Error: Verzeichnis %s konnte nicht geöffnet werden\n", dirname.c_str()); + return; + } + directories.push_back(dirname); // Verzeichnis speichern + + File file = root.openNextFile(); + while (file) { + if (file.isDirectory()) { + String fullPath = joinPath(dirname, file.name()); // Vollständigen Pfad erstellen + collectDirectories(fullPath, directories); // Rekursiver Aufruf + } + file = root.openNextFile(); + } + root.close(); } // print filenames @@ -110,7 +110,7 @@ void printFileName(File file) { Serial.print(file.size(), DEC); time_t cr = file.getCreationTime(); time_t lw = file.getLastWrite(); - struct tm* tmstruct = localtime(&cr); + struct tm *tmstruct = localtime(&cr); Serial.printf("\tCREATION: %d-%02d-%02d %02d:%02d:%02d", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec); tmstruct = localtime(&lw); Serial.printf("\tLAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec); @@ -127,8 +127,7 @@ void printFilesInDirectory(File dir) { } if (file.isDirectory()) { continue; - } - else { + } else { printFileName(file); } } From dca0893db75f1cdc9b5dda886abda4043073562e Mon Sep 17 00:00:00 2001 From: hasenradball Date: Mon, 18 Nov 2024 21:27:34 +0100 Subject: [PATCH 05/11] additional intention corrections --- .../listFilesEnhanced/listfilesEnhanced.ino | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino index 5d8ceb5bd0..c4db614cf5 100644 --- a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino +++ b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino @@ -64,7 +64,7 @@ void listDirectory() { } -void printDirectoryName(const char* name, uint8_t level) { +void printDirectoryName(const char *name, uint8_t level) { for (uint8_t i = 0; i < level; ++i) { Serial.print(" "); } @@ -75,30 +75,30 @@ void printDirectoryName(const char* name, uint8_t level) { // helper function: combine path String joinPath(const String &base, const String &name) { - if (base.endsWith("/")) { - return base + name; - } - return base + "/" + name; + if (base.endsWith("/")) { + return base + name; + } + return base + "/" + name; } // recusive function to collect directory names void collectDirectories(const String &dirname, std::vector &directories) { - File root = SD.open(dirname); - if (!root || !root.isDirectory()) { - Serial.printf("Error: Verzeichnis %s konnte nicht geöffnet werden\n", dirname.c_str()); - return; - } - directories.push_back(dirname); // Verzeichnis speichern - - File file = root.openNextFile(); - while (file) { - if (file.isDirectory()) { - String fullPath = joinPath(dirname, file.name()); // Vollständigen Pfad erstellen - collectDirectories(fullPath, directories); // Rekursiver Aufruf - } - file = root.openNextFile(); - } - root.close(); + File root = SD.open(dirname); + if (!root || !root.isDirectory()) { + Serial.printf("Error: Verzeichnis %s konnte nicht geöffnet werden\n", dirname.c_str()); + return; + } + directories.push_back(dirname); // Verzeichnis speichern + + File file = root.openNextFile(); + while (file) { + if (file.isDirectory()) { + String fullPath = joinPath(dirname, file.name()); // Vollständigen Pfad erstellen + collectDirectories(fullPath, directories); // Rekursiver Aufruf + } + file = root.openNextFile(); + } + root.close(); } // print filenames From b1fd63ec0ddcf61281ba4d875e22b232da46bc16 Mon Sep 17 00:00:00 2001 From: hasenradball Date: Mon, 18 Nov 2024 22:56:53 +0100 Subject: [PATCH 06/11] last clang format issues corrected --- .../SD/examples/listFilesEnhanced/listfilesEnhanced.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino index c4db614cf5..4ad6bf995c 100644 --- a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino +++ b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino @@ -88,13 +88,13 @@ void collectDirectories(const String &dirname, std::vector &directories) Serial.printf("Error: Verzeichnis %s konnte nicht geöffnet werden\n", dirname.c_str()); return; } - directories.push_back(dirname); // Verzeichnis speichern + directories.push_back(dirname); // Verzeichnis speichern File file = root.openNextFile(); while (file) { if (file.isDirectory()) { - String fullPath = joinPath(dirname, file.name()); // Vollständigen Pfad erstellen - collectDirectories(fullPath, directories); // Rekursiver Aufruf + String fullPath = joinPath(dirname, file.name()); // Vollständigen Pfad erstellen + collectDirectories(fullPath, directories); // Rekursiver Aufruf } file = root.openNextFile(); } From ec1f91eafd5a14de9529d0d9d7886655dd23097c Mon Sep 17 00:00:00 2001 From: Hasenradball Date: Tue, 19 Nov 2024 15:42:03 +0100 Subject: [PATCH 07/11] Update libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino Co-authored-by: Max Prokhorov --- libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino index 4ad6bf995c..1d060f46e3 100644 --- a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino +++ b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino @@ -85,7 +85,7 @@ String joinPath(const String &base, const String &name) { void collectDirectories(const String &dirname, std::vector &directories) { File root = SD.open(dirname); if (!root || !root.isDirectory()) { - Serial.printf("Error: Verzeichnis %s konnte nicht geöffnet werden\n", dirname.c_str()); + Serial.printf("Error: Cannot open %s\n", dirname.c_str()); return; } directories.push_back(dirname); // Verzeichnis speichern From 4945785f875c8bad3ea0ca7894e85fc33580ef07 Mon Sep 17 00:00:00 2001 From: Hasenradball Date: Tue, 19 Nov 2024 15:42:48 +0100 Subject: [PATCH 08/11] Update libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino Co-authored-by: Max Prokhorov --- libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino index 1d060f46e3..6b453e7574 100644 --- a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino +++ b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino @@ -93,7 +93,7 @@ void collectDirectories(const String &dirname, std::vector &directories) File file = root.openNextFile(); while (file) { if (file.isDirectory()) { - String fullPath = joinPath(dirname, file.name()); // Vollständigen Pfad erstellen + String fullPath = joinPath(dirname, file.name()); collectDirectories(fullPath, directories); // Rekursiver Aufruf } file = root.openNextFile(); From 94e097b81425a9a873358136267eace26ae8ffea Mon Sep 17 00:00:00 2001 From: Hasenradball Date: Tue, 19 Nov 2024 15:42:59 +0100 Subject: [PATCH 09/11] Update libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino Co-authored-by: Max Prokhorov --- libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino index 6b453e7574..1bc691eedd 100644 --- a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino +++ b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino @@ -94,7 +94,7 @@ void collectDirectories(const String &dirname, std::vector &directories) while (file) { if (file.isDirectory()) { String fullPath = joinPath(dirname, file.name()); - collectDirectories(fullPath, directories); // Rekursiver Aufruf + collectDirectories(fullPath, directories); } file = root.openNextFile(); } From 228162ecfc5949b0471f7b0d3bb9da88da3be9a6 Mon Sep 17 00:00:00 2001 From: Hasenradball Date: Tue, 19 Nov 2024 15:43:08 +0100 Subject: [PATCH 10/11] Update libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino Co-authored-by: Max Prokhorov --- libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino index 1bc691eedd..aaab6d403f 100644 --- a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino +++ b/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino @@ -88,7 +88,7 @@ void collectDirectories(const String &dirname, std::vector &directories) Serial.printf("Error: Cannot open %s\n", dirname.c_str()); return; } - directories.push_back(dirname); // Verzeichnis speichern + directories.push_back(dirname); File file = root.openNextFile(); while (file) { From df5e93e22d627458a73db0f91450fa35cd7af6da Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 20 Nov 2024 19:12:37 +0300 Subject: [PATCH 11/11] case-sensitive, eof line break --- .../listfilesEnhanced.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename libraries/SD/examples/{listFilesEnhanced => listfilesEnhanced}/listfilesEnhanced.ino (99%) diff --git a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino b/libraries/SD/examples/listfilesEnhanced/listfilesEnhanced.ino similarity index 99% rename from libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino rename to libraries/SD/examples/listfilesEnhanced/listfilesEnhanced.ino index aaab6d403f..5daa3294ad 100644 --- a/libraries/SD/examples/listFilesEnhanced/listfilesEnhanced.ino +++ b/libraries/SD/examples/listfilesEnhanced/listfilesEnhanced.ino @@ -131,4 +131,4 @@ void printFilesInDirectory(File dir) { printFileName(file); } } -} \ No newline at end of file +}