-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAdvancedUSBInternalOperations.ino
122 lines (94 loc) · 3.93 KB
/
AdvancedUSBInternalOperations.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
AdvancedUSBInternalOperations
Demonstrates advanced usage of the "Arduino_UnifiedStorage" library with USB & internal storage, including file operations.
Creates, copies, and moves files between storage types, and prints folder contents.
In the setup function, the code initializes serial communication, mounts both USB & internal storage and
reformats the internal storage for a clean file system. Then, it creates a root directory in the internal storage
and creates a subdirectory with a file inside it containing the string "Hello World!".
Then, it copies the file from internal storage to USB storage and moves the subdirectory from internal storage to USB storage.
After the file operations, the code prints the contents of both the USB storage and the internal storage.
It recursively prints the directories (marked as "[D]") and files (marked as "[F]") using the "printFolderContents" function.
Created 28th July 2023
By Cristian Dragomir
Modified 24th August 2023
By Ali Jahangiri
https://github.com/arduino-libraries/Arduino_UnifiedStorage/blob/main/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino
*/
#include "Arduino_UnifiedStorage.h"
// Two instances are made for the USB and internal storage respectively
USBStorage usbStorage;
InternalStorage internalStorage;
// Helper function to prints the contents of a folder, including subdirectories (marked as "[D]") and files (marked as "[F]").
void printFolderContents(Folder dir, int indentation = 0) {
std::vector<Folder> directories = dir.getFolders();
std::vector<UFile> files = dir.getFiles();
// Print directories
for (Folder subdir : directories) {
for (int i = 0; i < indentation; i++) {
Serial.print(" ");
}
Serial.print("[D] ");
Serial.println(subdir.getPath());
printFolderContents(subdir, indentation + 1);
}
// Print files
for (UFile file : files) {
for (int i = 0; i < indentation; i++) {
Serial.print(" ");
}
Serial.print("[F] ");
Serial.println(file.getPath());
}
}
void setup() {
Serial.begin(115200);
while (!Serial);
// toggle this to enable debugging output
Arduino_UnifiedStorage::debuggingModeEnabled = false;
usbStorage = USBStorage();
internalStorage = InternalStorage();
// Mount the USB storage
if(usbStorage.begin()){
Serial.println("USB storage mounted.");
} else {
Serial.println(errno);
}
if(internalStorage.begin()){
Serial.println("Internal storage mounted.");
} else {
Serial.println(errno);
}
// Create a root directory in the internal storage
Folder root = internalStorage.getRootFolder();
// Create a subdirectory and a file (file.txt) inside the root directory
Folder subdir = root.createSubfolder("subdir");
UFile file = root.createFile("file.txt", FileMode::WRITE);
// Write "Hello World!" inside file.txt
file.write("Hello, world!");
file.close();
// Copy the file from internal storage to USB storage
bool success = file.copyTo(usbStorage.getRootFolder(), true);
if (success) {
Serial.println("File copied successfully from internal storage to USB storage.");
} else {
Serial.println("Failed to copy file from internal storage to USB storage.");
Serial.println(getErrno());
}
// Move the subdirectory from internal storage to USB storage
success = subdir.moveTo(usbStorage.getRootFolder(), true);
if (success) {
Serial.println("Subdirectory moved successfully from internal storage to USB storage.");
} else {
Serial.println("Failed to move subdirectory from internal storage to USB storage.");
Serial.println(getErrno());
}
// Print contents of the USB storage
//Serial.println("USB storage contents:");
//printFolderContents(usbStorage.getRootFolder());
// Print contents of the internal storage
Serial.println("Internal storage contents:");
printFolderContents(internalStorage.getRootFolder());
}
void loop() {
// Nothing to do here
}