From c9b6ca5bf5383af89af74dbc597e00ac4ec54c2c Mon Sep 17 00:00:00 2001 From: per1234 Date: Sat, 23 Apr 2022 16:00:33 -0700 Subject: [PATCH 1/2] Avoid unnecessary calls in `rewindDirectory` reference code The reference page for `File::rewindDirectory` provides a sketch demonstrating its usage. This code calls the function after iterating over each directory on the card. This would be necessary if the `printDirectory` function was configured to iterate over that directory multiple times, but it is not. So the calls are redundant. Moving the call to the `setup` function has the following benefits: - Makes the use of the function more prominent. The reader can get an overview of what the program is doing without delving into the `printDirectory` function - Avoids the misconception that it is required to call the function recursively as was done before --- docs/api.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/api.md b/docs/api.md index cd81b3a..595c656 100644 --- a/docs/api.md +++ b/docs/api.md @@ -833,6 +833,7 @@ void setup() { SD.begin(10); root = SD.open("/"); printDirectory(root, 0); + root.rewindDirectory(); // Return to the first file in the directory Serial.println("Done!"); } @@ -845,8 +846,6 @@ void printDirectory(File dir, int numTabs) { File entry = dir.openNextFile(); if (!entry) { // No more files - // Return to the first file in the directory - dir.rewindDirectory(); break; } From 0df9a9c5f1e3f745a311c79645b43b3fb1bdd4fa Mon Sep 17 00:00:00 2001 From: per1234 Date: Sat, 23 Apr 2022 17:05:09 -0700 Subject: [PATCH 2/2] Print the card file structure twice in `rewindDirectory` reference code `File::rewindDirectory` is used to reset the current position in the file system. Previously, the file structure was only iterated over once, in which case rewinding is not necessary and will have no observable effect. In the following sequence, it is necessary and does have an observable effect: 1. Iterate over file structure using `File::openNextFile` 2. Reset current position using `File::rewindDirectory` 3. Iterate over file structure using `File::openNextFile` The reader can simply remove the `File::rewindDirectory` call from the program that follows this sequence for a clear demonstration of its purpose. --- docs/api.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/api.md b/docs/api.md index 595c656..a154ef3 100644 --- a/docs/api.md +++ b/docs/api.md @@ -833,7 +833,13 @@ void setup() { SD.begin(10); root = SD.open("/"); printDirectory(root, 0); + Serial.println(); + + Serial.println("PRINT AGAIN"); + Serial.println("-----------"); root.rewindDirectory(); // Return to the first file in the directory + printDirectory(root, 0); + Serial.println("Done!"); }