44 Demonstrates advanced usage of the "Arduino_UnifiedStorage" library with USB & internal storage, including file operations.
55 Creates, copies, and moves files between storage types, and prints folder contents.
66
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
88 reformats the internal storage for a clean file system. Then, it creates a root directory in the internal storage
99 and creates a subdirectory with a file inside it containing the string "Hello World!".
1010
2828USBStorage usbStorage;
2929InternalStorage internalStorage;
3030
31-
3231// Helper function to prints the contents of a folder, including subdirectories (marked as "[D]") and files (marked as "[F]").
3332void printFolderContents (Folder dir, int indentation = 0 ) {
3433 std::vector<Folder> directories = dir.getFolders ();
@@ -37,46 +36,45 @@ void printFolderContents(Folder dir, int indentation = 0) {
3736 // Print directories
3837 for (Folder subdir : directories) {
3938 for (int i = 0 ; i < indentation; i++) {
40- Serial. print (" " );
39+ Arduino_UnifiedStorage::debugPrint (" " );
4140 }
42- Serial. print (" [D] " );
43- Serial. println (subdir.getPath ());
41+ Arduino_UnifiedStorage::debugPrint (" [D] " );
42+ Arduino_UnifiedStorage::debugPrint (subdir.getPath ());
4443 printFolderContents (subdir, indentation + 1 );
4544 }
4645
4746 // Print files
4847 for (UFile file : files) {
4948 for (int i = 0 ; i < indentation; i++) {
50- Serial. print (" " );
49+ Arduino_UnifiedStorage::debugPrint (" " );
5150 }
52- Serial. print (" [F] " );
53- Serial. println (file.getPath ());
51+ Arduino_UnifiedStorage::debugPrint (" [F] " );
52+ Arduino_UnifiedStorage::debugPrint (file.getPath ());
5453 }
5554}
5655
57-
58-
5956void 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
6263
6364 // toggle this to enable debugging output
64- Arduino_UnifiedStorage::debuggingModeEnabled = false ;
65-
66- usbStorage = USBStorage ();
67- internalStorage = InternalStorage ();
65+ Arduino_UnifiedStorage::debuggingModeEnabled = true ;
6866
6967 // Mount the USB storage
7068 if (usbStorage.begin ()){
71- Serial. println (" USB storage mounted." );
69+ Arduino_UnifiedStorage::debugPrint (" USB storage mounted." );
7270 } else {
73- Serial. println ( errno);
71+ Arduino_UnifiedStorage::debugPrint ( String ( errno) );
7472 }
7573
7674 if (internalStorage.begin ()){
77- Serial. println (" Internal storage mounted." );
75+ Arduino_UnifiedStorage::debugPrint (" Internal storage mounted." );
7876 } else {
79- Serial. println ( errno);
77+ Arduino_UnifiedStorage::debugPrint ( String ( errno) );
8078 }
8179
8280 // Create a root directory in the internal storage
@@ -93,27 +91,27 @@ void setup() {
9391 // Copy the file from internal storage to USB storage
9492 bool success = file.copyTo (usbStorage.getRootFolder (), true );
9593 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." );
9795 } 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 ());
10098 }
10199
102100 // Move the subdirectory from internal storage to USB storage
103101 success = subdir.moveTo (usbStorage.getRootFolder (), true );
104102 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." );
106104 } 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 ());
109107 }
110108
111109 // 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 ());
114112
115113 // Print contents of the internal storage
116- Serial. println (" Internal storage contents:" );
114+ Arduino_UnifiedStorage::debugPrint (" Internal storage contents:" );
117115 printFolderContents (internalStorage.getRootFolder ());
118116}
119117
0 commit comments