4
4
Demonstrates advanced usage of the "Arduino_UnifiedStorage" library with USB & internal storage, including file operations.
5
5
Creates, copies, and moves files between storage types, and prints folder contents.
6
6
7
- In the setup function, the code initializes serial communication, mounts both USB & internal storage and
7
+ In the setup function, the code initializes Serial communication, mounts both USB & internal storage and
8
8
reformats the internal storage for a clean file system. Then, it creates a root directory in the internal storage
9
9
and creates a subdirectory with a file inside it containing the string "Hello World!".
10
10
28
28
USBStorage usbStorage;
29
29
InternalStorage internalStorage;
30
30
31
-
32
31
// Helper function to prints the contents of a folder, including subdirectories (marked as "[D]") and files (marked as "[F]").
33
32
void printFolderContents (Folder dir, int indentation = 0 ) {
34
33
std::vector<Folder> directories = dir.getFolders ();
@@ -37,46 +36,45 @@ void printFolderContents(Folder dir, int indentation = 0) {
37
36
// Print directories
38
37
for (Folder subdir : directories) {
39
38
for (int i = 0 ; i < indentation; i++) {
40
- Serial. print (" " );
39
+ Arduino_UnifiedStorage::debugPrint (" " );
41
40
}
42
- Serial. print (" [D] " );
43
- Serial. println (subdir.getPath ());
41
+ Arduino_UnifiedStorage::debugPrint (" [D] " );
42
+ Arduino_UnifiedStorage::debugPrint (subdir.getPath ());
44
43
printFolderContents (subdir, indentation + 1 );
45
44
}
46
45
47
46
// Print files
48
47
for (UFile file : files) {
49
48
for (int i = 0 ; i < indentation; i++) {
50
- Serial. print (" " );
49
+ Arduino_UnifiedStorage::debugPrint (" " );
51
50
}
52
- Serial. print (" [F] " );
53
- Serial. println (file.getPath ());
51
+ Arduino_UnifiedStorage::debugPrint (" [F] " );
52
+ Arduino_UnifiedStorage::debugPrint (file.getPath ());
54
53
}
55
54
}
56
55
57
-
58
-
59
56
void setup () {
60
- Serial.begin (115200 );
61
- while (!Serial);
57
+ #if !defined(ARDUINO_OPTA)
58
+ Serial.begin (115200 );
59
+ while (!Serial);
60
+ #else
61
+ beginRS485 (115200 );
62
+ #endif
62
63
63
64
// toggle this to enable debugging output
64
- Arduino_UnifiedStorage::debuggingModeEnabled = false ;
65
-
66
- usbStorage = USBStorage ();
67
- internalStorage = InternalStorage ();
65
+ Arduino_UnifiedStorage::debuggingModeEnabled = true ;
68
66
69
67
// Mount the USB storage
70
68
if (usbStorage.begin ()){
71
- Serial. println (" USB storage mounted." );
69
+ Arduino_UnifiedStorage::debugPrint (" USB storage mounted." );
72
70
} else {
73
- Serial. println ( errno);
71
+ Arduino_UnifiedStorage::debugPrint ( String ( errno) );
74
72
}
75
73
76
74
if (internalStorage.begin ()){
77
- Serial. println (" Internal storage mounted." );
75
+ Arduino_UnifiedStorage::debugPrint (" Internal storage mounted." );
78
76
} else {
79
- Serial. println ( errno);
77
+ Arduino_UnifiedStorage::debugPrint ( String ( errno) );
80
78
}
81
79
82
80
// Create a root directory in the internal storage
@@ -93,27 +91,27 @@ void setup() {
93
91
// Copy the file from internal storage to USB storage
94
92
bool success = file.copyTo (usbStorage.getRootFolder (), true );
95
93
if (success) {
96
- Serial. println (" File copied successfully from internal storage to USB storage." );
94
+ Arduino_UnifiedStorage::debugPrint (" File copied successfully from internal storage to USB storage." );
97
95
} else {
98
- Serial. println (" Failed to copy file from internal storage to USB storage." );
99
- Serial. println (getErrno ());
96
+ Arduino_UnifiedStorage::debugPrint (" Failed to copy file from internal storage to USB storage." );
97
+ Arduino_UnifiedStorage::debugPrint (getErrno ());
100
98
}
101
99
102
100
// Move the subdirectory from internal storage to USB storage
103
101
success = subdir.moveTo (usbStorage.getRootFolder (), true );
104
102
if (success) {
105
- Serial. println (" Subdirectory moved successfully from internal storage to USB storage." );
103
+ Arduino_UnifiedStorage::debugPrint (" Subdirectory moved successfully from internal storage to USB storage." );
106
104
} else {
107
- Serial. println (" Failed to move subdirectory from internal storage to USB storage." );
108
- Serial. println (getErrno ());
105
+ Arduino_UnifiedStorage::debugPrint (" Failed to move subdirectory from internal storage to USB storage." );
106
+ Arduino_UnifiedStorage::debugPrint (getErrno ());
109
107
}
110
108
111
109
// Print contents of the USB storage
112
- // Serial.println ("USB storage contents:");
113
- // printFolderContents(usbStorage.getRootFolder());
110
+ Arduino_UnifiedStorage::debugPrint (" USB storage contents:" );
111
+ printFolderContents (usbStorage.getRootFolder ());
114
112
115
113
// Print contents of the internal storage
116
- Serial. println (" Internal storage contents:" );
114
+ Arduino_UnifiedStorage::debugPrint (" Internal storage contents:" );
117
115
printFolderContents (internalStorage.getRootFolder ());
118
116
}
119
117
0 commit comments