1
1
/*
2
- This example demonstrates the usage of the "Arduino_UnifiedStorage" library with USB storage and internal storage.
3
- The code includes the necessary library and defines instances of the "USBStorage" and "InternalStorage" classes.
2
+ AdvancedUSBInternalOperations
4
3
5
- In the setup function, the code initializes the serial communication and mounts the USB storage and internal storage.
6
- It also reformats the internal storage to ensure a clean file system.
7
- Then, it creates a root directory in the internal storage and creates a subdirectory and a file inside it
4
+ Demonstrates advanced usage of the "Arduino_UnifiedStorage" library with USB & internal storage, including file operations.
5
+ Creates, copies, and moves files between storage types, and prints folder contents.
8
6
9
- The code writes some data to the file and demonstrates file operations.
10
- It copies the file from internal storage to USB storage and moves the subdirectory from internal storage to USB storage.
7
+ In the setup function, the code initializes serial communication, mounts both USB & internal storage and
8
+ reformats the internal storage for a clean file system. Then, it creates a root directory in the internal storage
9
+ and creates a subdirectory with a file inside it containing the string "Hello World!".
10
+
11
+ Then, it copies the file from internal storage to USB storage and moves the subdirectory from internal storage to USB storage.
12
+
13
+ After the file operations, the code prints the contents of both the USB storage and the internal storage.
14
+ It recursively prints the directories (marked as "[D]") and files (marked as "[F]") using the "printFolderContents" function.
15
+
16
+ Created 28th July 2023
17
+ By Cristian Dragomir
18
+
19
+ Modified 24th August 2023
20
+ By Ali Jahangiri
21
+
22
+ https://github.com/arduino-libraries/Arduino_UnifiedStorage/blob/main/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino
11
23
12
- After the file operations, the code prints the contents of both the USB storage and the internal storage.
13
- It recursively prints the directories (marked as "[D]") and files (marked as "[F]") using the "printFolderContents" function.
14
24
*/
15
25
16
26
#include " Arduino_UnifiedStorage.h"
17
27
18
-
28
+ // Two instances are made for the USB and internal storage respectively
19
29
USBStorage usbStorage = USBStorage();
20
30
InternalStorage internalStorage = InternalStorage();
21
31
22
-
32
+ // Helper function to prints the contents of a folder, including subdirectories (marked as "[D]") and files (marked as "[F]").
23
33
void printFolderContents (Folder dir, int indentation = 0 ) {
24
34
std::vector<Folder> directories = dir.getFolders ();
25
35
std::vector<UFile> files = dir.getFiles ();
@@ -57,25 +67,23 @@ void setup() {
57
67
Serial.println (errno);
58
68
}
59
69
60
-
61
70
// Mount the internal storage
62
- // Serial.println("Reformatting internal storage to make sure we have a clean FS");
63
- // internalStorage.format();
71
+ // Serial.println("Reformatting internal storage to make sure we have a clean FS");
72
+ // internalStorage.format();
64
73
if (internalStorage.begin ()){
65
74
Serial.println (" Internal storage mounted." );
66
75
} else {
67
76
Serial.println (errno);
68
77
}
69
78
70
-
71
79
// Create a root directory in the internal storage
72
80
Folder root = internalStorage.getRootFolder ();
73
81
74
- // Create a subdirectory and a file inside the root directory
82
+ // Create a subdirectory and a file (file.txt) inside the root directory
75
83
Folder subdir = root.createSubfolder (" subdir" );
76
84
UFile file = root.createFile (" file.txt" , FileMode::WRITE);
77
85
78
- // Write some data to the file
86
+ // Write "Hello World!" inside file.txt
79
87
file.write (" Hello, world!" );
80
88
file.close ();
81
89
@@ -97,11 +105,11 @@ void setup() {
97
105
Serial.println (getErrno ());
98
106
}
99
107
100
- // Print the content of the USB storage
108
+ // Print contents of the USB storage
101
109
// Serial.println("USB storage contents:");
102
- // printFolderContents(usbStorage.getRootFolder());
110
+ // printFolderContents(usbStorage.getRootFolder());
103
111
104
- // Print the content of the internal storage
112
+ // Print contents of the internal storage
105
113
Serial.println (" Internal storage contents:" );
106
114
printFolderContents (internalStorage.getRootFolder ());
107
115
}
0 commit comments