Skip to content

Commit c203f0a

Browse files
committed
Add code explanation
1 parent 9fd43bf commit c203f0a

File tree

1 file changed

+259
-5
lines changed
  • content/hardware/03.nano/boards/nano-esp32/tutorials/spiff

1 file changed

+259
-5
lines changed

content/hardware/03.nano/boards/nano-esp32/tutorials/spiff/spiff.md

Lines changed: 259 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ The SPIFFS (Serial Peripheral Interface Flash File System) is a file system desi
1111

1212
***Before proceeding with the SPIFFS file system setup on Nano ESP, please be aware that the mounting instructions mentioned in a comment on the code from the packaged built-in example are tailored for specific older IDE versions such as IDE 1.8. These instructions might not be relevant to IDE 2.0, as it does not support the utilization of custom tools, which are unnecessary in this case.***
1313

14-
## Software & Hardware Needed
14+
## Hardware & Software Needed
1515

1616
- [Arduino Nano ESP32](https://store.arduino.cc/nano-esp32)
1717
- [Arduino IDE](https://www.arduino.cc/en/software)
18-
- [Arduino ESP32 Core](https://github.com/arduino/arduino-esp32)
18+
- [Arduino ESP32 Core](https://github.com/arduino/arduino-esp32) installed (done in the Arduino IDE).
1919

2020
## Set Up SPIFFS File System
2121

22-
Start by opening the **SPIFFS_Test example** by going to:
22+
Start by opening the **SPIFFS_Test example** by navigating to:
2323

2424
- File > Examples > SPIFFS > SPIFFS_Test
2525

26+
***The example is also included at the bottom (see [Example](#example)), together with an explanation of how the code work.***
27+
2628
![Open Example](./assets/openExample.png)
2729

2830
Select the correct **Partition Scheme** by clicking:
@@ -33,7 +35,7 @@ Select the correct **Partition Scheme** by clicking:
3335

3436
Prepare your following these steps:
3537

36-
- Take your **unplugged** board connect a jumper cable between the **GND** and **B1** pins.
38+
- Take your **unplugged** board and connect a jumper cable between the **GND** and **B1** pins.
3739

3840
- **Plug it in** and the RGB LED will turn on with a green or blue color.
3941

@@ -77,4 +79,256 @@ Open the serial monitor and you should see the following output:
7779

7880
![Serial Output](./assets/serialOutput.png)
7981

80-
***Note: It might take a short while for the messages to be printed. If you don't see any messages after one minute repeat the process.***
82+
***Note: It might take a short while for the messages to be printed. If you don't see any messages after one minute repeat the process.***
83+
84+
## Example
85+
86+
As mentioned above the code is taken from **File > Examples > SPIFFS > SPIFFS_Test** and can also be found inside the IDE's example section. It showcases different features available when using the SPIFSF file system.
87+
88+
```arduino
89+
#include "FS.h"
90+
#include "SPIFFS.h"
91+
92+
/* You only need to format SPIFFS the first time you run a
93+
test or else use the SPIFFS plugin to create a partition
94+
https://github.com/me-no-dev/arduino-esp32fs-plugin */
95+
#define FORMAT_SPIFFS_IF_FAILED true
96+
97+
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
98+
Serial.printf("Listing directory: %s\r\n", dirname);
99+
100+
File root = fs.open(dirname);
101+
if(!root){
102+
Serial.println("- failed to open directory");
103+
return;
104+
}
105+
if(!root.isDirectory()){
106+
Serial.println(" - not a directory");
107+
return;
108+
}
109+
110+
File file = root.openNextFile();
111+
while(file){
112+
if(file.isDirectory()){
113+
Serial.print(" DIR : ");
114+
Serial.println(file.name());
115+
if(levels){
116+
listDir(fs, file.path(), levels -1);
117+
}
118+
} else {
119+
Serial.print(" FILE: ");
120+
Serial.print(file.name());
121+
Serial.print("\tSIZE: ");
122+
Serial.println(file.size());
123+
}
124+
file = root.openNextFile();
125+
}
126+
}
127+
128+
void readFile(fs::FS &fs, const char * path){
129+
Serial.printf("Reading file: %s\r\n", path);
130+
131+
File file = fs.open(path);
132+
if(!file || file.isDirectory()){
133+
Serial.println("- failed to open file for reading");
134+
return;
135+
}
136+
137+
Serial.println("- read from file:");
138+
while(file.available()){
139+
Serial.write(file.read());
140+
}
141+
file.close();
142+
}
143+
144+
void writeFile(fs::FS &fs, const char * path, const char * message){
145+
Serial.printf("Writing file: %s\r\n", path);
146+
147+
File file = fs.open(path, FILE_WRITE);
148+
if(!file){
149+
Serial.println("- failed to open file for writing");
150+
return;
151+
}
152+
if(file.print(message)){
153+
Serial.println("- file written");
154+
} else {
155+
Serial.println("- write failed");
156+
}
157+
file.close();
158+
}
159+
160+
void appendFile(fs::FS &fs, const char * path, const char * message){
161+
Serial.printf("Appending to file: %s\r\n", path);
162+
163+
File file = fs.open(path, FILE_APPEND);
164+
if(!file){
165+
Serial.println("- failed to open file for appending");
166+
return;
167+
}
168+
if(file.print(message)){
169+
Serial.println("- message appended");
170+
} else {
171+
Serial.println("- append failed");
172+
}
173+
file.close();
174+
}
175+
176+
void renameFile(fs::FS &fs, const char * path1, const char * path2){
177+
Serial.printf("Renaming file %s to %s\r\n", path1, path2);
178+
if (fs.rename(path1, path2)) {
179+
Serial.println("- file renamed");
180+
} else {
181+
Serial.println("- rename failed");
182+
}
183+
}
184+
185+
void deleteFile(fs::FS &fs, const char * path){
186+
Serial.printf("Deleting file: %s\r\n", path);
187+
if(fs.remove(path)){
188+
Serial.println("- file deleted");
189+
} else {
190+
Serial.println("- delete failed");
191+
}
192+
}
193+
194+
void testFileIO(fs::FS &fs, const char * path){
195+
Serial.printf("Testing file I/O with %s\r\n", path);
196+
197+
static uint8_t buf[512];
198+
size_t len = 0;
199+
File file = fs.open(path, FILE_WRITE);
200+
if(!file){
201+
Serial.println("- failed to open file for writing");
202+
return;
203+
}
204+
205+
size_t i;
206+
Serial.print("- writing" );
207+
uint32_t start = millis();
208+
for(i=0; i<2048; i++){
209+
if ((i & 0x001F) == 0x001F){
210+
Serial.print(".");
211+
}
212+
file.write(buf, 512);
213+
}
214+
Serial.println("");
215+
uint32_t end = millis() - start;
216+
Serial.printf(" - %u bytes written in %u ms\r\n", 2048 * 512, end);
217+
file.close();
218+
219+
file = fs.open(path);
220+
start = millis();
221+
end = start;
222+
i = 0;
223+
if(file && !file.isDirectory()){
224+
len = file.size();
225+
size_t flen = len;
226+
start = millis();
227+
Serial.print("- reading" );
228+
while(len){
229+
size_t toRead = len;
230+
if(toRead > 512){
231+
toRead = 512;
232+
}
233+
file.read(buf, toRead);
234+
if ((i++ & 0x001F) == 0x001F){
235+
Serial.print(".");
236+
}
237+
len -= toRead;
238+
}
239+
Serial.println("");
240+
end = millis() - start;
241+
Serial.printf("- %u bytes read in %u ms\r\n", flen, end);
242+
file.close();
243+
} else {
244+
Serial.println("- failed to open file for reading");
245+
}
246+
}
247+
248+
void setup(){
249+
Serial.begin(115200);
250+
if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
251+
Serial.println("SPIFFS Mount Failed");
252+
return;
253+
}
254+
255+
listDir(SPIFFS, "/", 0);
256+
writeFile(SPIFFS, "/hello.txt", "Hello ");
257+
appendFile(SPIFFS, "/hello.txt", "World!\r\n");
258+
readFile(SPIFFS, "/hello.txt");
259+
renameFile(SPIFFS, "/hello.txt", "/foo.txt");
260+
readFile(SPIFFS, "/foo.txt");
261+
deleteFile(SPIFFS, "/foo.txt");
262+
testFileIO(SPIFFS, "/test.txt");
263+
deleteFile(SPIFFS, "/test.txt");
264+
Serial.println( "Test complete" );
265+
}
266+
267+
void loop(){
268+
269+
}
270+
```
271+
272+
### How it works?
273+
274+
- First, the **necessary libraries** are included:
275+
276+
```arduino
277+
#include "FS.h"
278+
#include "SPIFFS.h"
279+
```
280+
281+
- The constant `FORMAT_SPIFFS_IF_FAILED` is used to specify whether the SPIFFS file system should be formatted if it fails to mount. If it's `true`, the file system will be formatted:
282+
283+
```arduino
284+
#define FORMAT_SPIFFS_IF_FAILED true
285+
```
286+
287+
- The code consists of several **functions** defined in the code showcasing **different features** such as:
288+
- `void listDir()` = List Files
289+
- `void readFile()` = Read Files
290+
- `void writeFile()` = Write Files
291+
- `void appendFile()` = Append a message to an existing file
292+
- `void renameFile()` = Rename Files
293+
- `void deleteFile()` = Delete Files
294+
- `void testFileIO()` = Testing a file's input and output by writing a large amount of data and then reading it.
295+
296+
- Before calling all the functions mentioned above **two things** are done inside `setup()`:
297+
- The serial communication is initialized with a baud rate of 115200.
298+
- The SPIFFS file system is mounted and if `FORMAT_SPIFFS_IF_FAILED` is `true` it's formatted.
299+
300+
```arduino
301+
void setup(){
302+
Serial.begin(115200);
303+
if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
304+
Serial.println("SPIFFS Mount Failed");
305+
return;
306+
}
307+
...
308+
}
309+
```
310+
311+
Lastly, all functions are called with parameters:
312+
313+
```arduino
314+
void setup(){
315+
Serial.begin(115200);
316+
if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
317+
Serial.println("SPIFFS Mount Failed");
318+
return;
319+
}
320+
321+
listDir(SPIFFS, "/", 0);
322+
writeFile(SPIFFS, "/hello.txt", "Hello ");
323+
appendFile(SPIFFS, "/hello.txt", "World!\r\n");
324+
readFile(SPIFFS, "/hello.txt");
325+
renameFile(SPIFFS, "/hello.txt", "/foo.txt");
326+
readFile(SPIFFS, "/foo.txt");
327+
deleteFile(SPIFFS, "/foo.txt");
328+
testFileIO(SPIFFS, "/test.txt");
329+
deleteFile(SPIFFS, "/test.txt");
330+
Serial.println( "Test complete" );
331+
}
332+
```
333+
334+
- All messages printed on the serial monitor are defined in the respective function, except "Test Complete" which printed by `setup()` once everything is done.

0 commit comments

Comments
 (0)