1
+ #include < Arduino_UnifiedStorage.h>
2
+
3
+ #define HAS_USB
4
+ #define HAS_SD
5
+ #define HAS_QSPI
6
+
7
+ #if defined(HAS_USB)
8
+ USBStorage usb = USBStorage();
9
+ #endif
10
+
11
+ #if defined(HAS_SD)
12
+ SDStorage sd = SDStorage();
13
+ #endif
14
+
15
+ #if defined(HAS_QSPI)
16
+ InternalStorage internal = InternalStorage(2 , " user" , FS_FAT);
17
+ #endif
18
+
19
+
20
+ bool testFileCreationWithOpen (Folder root) {
21
+ UFile file = UFile ();
22
+ String path = root.getPathString () + " /test_open.txt" ;
23
+ Serial.println (path);
24
+ if (file.open (path, FileMode::WRITE)) {
25
+ Serial.println (" \n --- Test creating file using file.open ---" );
26
+ Serial.println (" Test creating file using file.open - Success" );
27
+ file.close ();
28
+ file.remove ();
29
+ return true ;
30
+ } else {
31
+ Serial.println (" Test creating file using file.open - Failed. Error: " + String (getErrno ()));
32
+ return false ;
33
+ }
34
+
35
+ }
36
+
37
+ bool testFileCreationWithCreate (Folder root) {
38
+ Serial.println (" \n --- Test creating file using root.createFile ---" );
39
+ UFile file = root.createFile (" test_create.txt" , FileMode::WRITE);
40
+ if (file.exists ()) {
41
+ Serial.println (" Test creating file using root.createFile - Success" );
42
+ file.close ();
43
+ file.remove ();
44
+ return true ;
45
+ } else {
46
+ Serial.println (" Test creating file using root.createFile - Failed. Error: " + String (getErrno ()));
47
+ return false ;
48
+ }
49
+ }
50
+
51
+ bool testWritingCharBuffer (Folder root) {
52
+ UFile file = root.createFile (" test_write.txt" , FileMode::WRITE);
53
+ if (file.exists ()) {
54
+ Serial.println (" \n --- Test writing char * buffer ---" );
55
+ size_t bytesWritten = file.write (reinterpret_cast <const uint8_t *>(" Hello, World!" ), 13 );
56
+ Serial.println (" Bytes written to file: " + String (bytesWritten));
57
+ file.close ();
58
+ file.remove ();
59
+ return bytesWritten == 13 ;
60
+ } else {
61
+ Serial.println (" Test writing char * buffer - Failed. Error: " + String (getErrno ()));
62
+ return false ;
63
+ }
64
+ }
65
+
66
+ bool testWritingWithString (Folder root) {
67
+ UFile file = root.createFile (" test_write_string.txt" , FileMode::WRITE);
68
+ if (file.exists ()) {
69
+ Serial.println (" \n --- Test writing String ---" );
70
+ String data = " This is a test with String data." ;
71
+ size_t bytesWritten = file.write (data);
72
+ Serial.println (" Bytes written to file (with String): " + String (bytesWritten));
73
+ file.close ();
74
+ file.remove ();
75
+ return true ;
76
+
77
+ } else {
78
+ Serial.println (" Test writing with String data type - Failed. Error: " + String (getErrno ()));
79
+ return false ;
80
+ }
81
+
82
+ }
83
+
84
+ bool testAppending (Folder root) {
85
+ UFile file = root.createFile (" test_append.txt" , FileMode::WRITE);
86
+ if (file.exists ()) {
87
+ Serial.println (" \n --- Test appending ---" );
88
+ String data = " Appending some more data." ;
89
+ size_t bytesWritten = file.write (reinterpret_cast <const uint8_t *>(data.c_str ()), data.length ());
90
+ Serial.println (" Bytes written to file (appending): " + String (bytesWritten));
91
+ file.close ();
92
+ file.remove ();
93
+ return true ;
94
+ } else {
95
+ Serial.println (" Test appending to the file - Failed. Error: " + String (getErrno ()));
96
+ return false ;
97
+ }
98
+ }
99
+
100
+ bool testReadingAll (Folder root) {
101
+ UFile file = root.createFile (" test_read.txt" , FileMode::WRITE);
102
+ if (file.exists ()) {
103
+ file.write (reinterpret_cast <const uint8_t *>(" Hello, World!" ), 13 );
104
+ file.close ();
105
+
106
+ if (file.open (root.getPathString () + " /test_read.txt" , FileMode::READ)) {
107
+ char buffer[file.available ()];
108
+ size_t bytesRead = file.read (reinterpret_cast <uint8_t *>(buffer), sizeof (buffer));
109
+ buffer[bytesRead] = ' \0 ' ; // Null-terminate the string
110
+ Serial.println (" \n --- Test reading everything from the file ---" );
111
+ Serial.println (" Read from file (test_read.txt): " + String (buffer));
112
+ file.close ();
113
+
114
+ file.remove ();
115
+
116
+ return true ;
117
+ } else {
118
+ Serial.println (" Test reading everything from the file - Failed. Error: " + String (getErrno ()));
119
+ return false ;
120
+ }
121
+ } else {
122
+ Serial.println (" Test reading everything from the file - Failed to create file. Error: " + String (getErrno ()));
123
+ return false ;
124
+
125
+ }
126
+
127
+ return false ;
128
+ }
129
+
130
+ bool testSeeking (Folder root) {
131
+
132
+ UFile file = root.createFile (" test_seek.txt" , FileMode::WRITE);
133
+ if (file.exists ()) {
134
+ file.write (reinterpret_cast <const uint8_t *>(" Hello, World!" ), 13 );
135
+ file.close ();
136
+
137
+
138
+ if (file.open (root.getPathString () + " /test_seek.txt" , FileMode::READ)) {
139
+ Serial.println (" \n --- Test seeking file ---" );
140
+ file.seek (7 );
141
+ char buffer[20 ];
142
+ size_t bytesRead = file.read (reinterpret_cast <uint8_t *>(buffer), sizeof (buffer));
143
+ buffer[bytesRead] = ' \0 ' ; // Null-terminate the string
144
+ Serial.println (" Read after seeking: " + String (buffer));
145
+ file.close ();
146
+ file.remove ();
147
+ return true ;
148
+ } else {
149
+ Serial.println (" Test seeking in the file - Failed. Error: " + String (getErrno ()));
150
+ return false ;
151
+ }
152
+ } else {
153
+ Serial.println (" Test seeking in the file - Failed to create file. Error: " + String (getErrno ()));
154
+ return false ;
155
+ }
156
+
157
+ }
158
+
159
+ bool testAvailableData (Folder root) {
160
+ UFile file = root.createFile (" test_available.txt" , FileMode::WRITE);
161
+ if (file.exists ()) {
162
+ file.write (reinterpret_cast <const uint8_t *>(" Hello, World!" ), 13 );
163
+ file.close ();
164
+
165
+ if (file.open (root.getPathString () + " /test_available.txt" , FileMode::READ)) {
166
+ Serial.println (" \n --- Test available data ---" );
167
+ int availableBytes = file.available ();
168
+ Serial.println (" Available bytes in file (test_available.txt): " + String (availableBytes));
169
+ file.close ();
170
+ file.remove ();
171
+ return true ;
172
+ } else {
173
+ Serial.println (" Test checking available data in the file - Failed. Error: " + String (getErrno ()));
174
+ return false ;
175
+ }
176
+ } else {
177
+ Serial.println (" Test checking available data in the file - Failed to create file. Error: " + String (getErrno ()));
178
+ return false ;
179
+ }
180
+
181
+ return false ;
182
+ }
183
+
184
+ bool testCopyingFile (Folder root) {
185
+ // Test copying a file
186
+ UFile sourceFile = root.createFile (" test_source_copy.txt" , FileMode::WRITE);
187
+ if (sourceFile.exists ()) {
188
+ sourceFile.write (reinterpret_cast <const uint8_t *>(" Hello, World!" ), 13 );
189
+ sourceFile.close ();
190
+
191
+ Folder destinationFolder = root.createSubfolder (" test_folder_copy" );
192
+
193
+ if (destinationFolder.exists ()) {
194
+ Serial.println (" \n --- Test copying a file ---" );
195
+ Serial.println (" Source file name: " + String (sourceFile.getPathString ()));
196
+
197
+ if (sourceFile.copyTo (destinationFolder)) {
198
+ Serial.println (" File copied successfully!" );
199
+ sourceFile.close ();
200
+ sourceFile.remove ();
201
+ destinationFolder.remove ();
202
+ return true ;
203
+ } else {
204
+ Serial.println (" File copying failed. Error: " + String (getErrno ()));
205
+ return false ;
206
+ }
207
+ } else {
208
+ Serial.println (" Test copying a file - Failed. Error: " + String (getErrno ()));
209
+ return false ;
210
+ }
211
+
212
+ } else {
213
+ Serial.println (" Test copying a file - Failed to create destination folder. Error: " + String (getErrno ()));
214
+ return false ;
215
+ }
216
+ }
217
+
218
+ bool testMovingFile (Folder root) {
219
+ UFile sourceFileMove = root.createFile (" test_source_move.txt" , FileMode::WRITE);
220
+ if (sourceFileMove.exists ()) {
221
+ sourceFileMove.write (reinterpret_cast <const uint8_t *>(" Hello, World!" ), 13 );
222
+ sourceFileMove.close ();
223
+
224
+ Folder destinationFolder = root.createSubfolder (" test_folder_move" );
225
+ if (destinationFolder.exists ()) {
226
+ UFile movedFile = sourceFileMove;
227
+ if (movedFile.exists ()) {
228
+ Serial.println (" \n --- Test moving a file ---" );
229
+ Serial.println (" Source file name: " + String (sourceFileMove.getPathString ()));
230
+ Serial.println (" Destination file name: " + String (destinationFolder.getPathString ()) + " /test_source_move.txt" );
231
+ if (sourceFileMove.moveTo (destinationFolder)) {
232
+ Serial.println (" File moved successfully!" );
233
+ sourceFileMove.close ();
234
+ movedFile.close ();
235
+ movedFile.remove ();
236
+ destinationFolder.remove ();
237
+ sourceFileMove.remove ();
238
+ return true ;
239
+ } else {
240
+ Serial.println (" File moving failed. Error: " + String (getErrno ()));
241
+ return false ;
242
+ }
243
+ } else {
244
+ Serial.println (" Test moving a file - Failed. Error: " + String (getErrno ()));
245
+ return false ;
246
+ }
247
+ } else {
248
+ Serial.println (" Test moving a file - Failed to create destination folder. Error: " + String (getErrno ()));
249
+ return false ;
250
+ }
251
+ } else {
252
+ Serial.println (" Test moving a file - Failed. Error: " + String (getErrno ()));
253
+ return false ;
254
+ }
255
+ }
256
+
257
+ void printFolderContents (Folder dir, int indentation = 0 ) {
258
+ std::vector<Folder> directories = dir.getFolders ();
259
+ std::vector<UFile>files = dir.getFiles ();
260
+
261
+ // Print directories
262
+ for (Folder subdir : directories) {
263
+ for (int i = 0 ; i < indentation; i++) {
264
+ Serial.print (" " );
265
+ }
266
+ Serial.print (" [D] " );
267
+ Serial.println (subdir.getPath ());
268
+ printFolderContents (subdir, indentation + 1 );
269
+ }
270
+
271
+ // Print files
272
+ for (UFile file : files) {
273
+ for (int i = 0 ; i < indentation; i++) {
274
+ Serial.print (" " );
275
+ }
276
+ Serial.print (" [F] " );
277
+ Serial.println (file.getPath ());
278
+ }
279
+ }
280
+
281
+ void runTests (Arduino_UnifiedStorage * storage, String storageType) {
282
+ if (storage->begin ()) {
283
+ Folder root = storage->getRootFolder ();
284
+
285
+ Serial.println (" \n === Testing " + storageType + " ===" );
286
+ Serial.println (" ========= File Tests =========" );
287
+
288
+ testFileCreationWithOpen (root);
289
+ testFileCreationWithCreate (root);
290
+ testWritingCharBuffer (root);
291
+ testWritingWithString (root);
292
+ testAppending (root);
293
+ testReadingAll (root);
294
+ testSeeking (root);
295
+ testAvailableData (root);
296
+ testCopyingFile (root);
297
+ testMovingFile (root);
298
+
299
+ Serial.println (" \n ========= FS Contents after File Tests =========" );
300
+ Serial.println (" Should be empty if deletion was succesful" );
301
+
302
+ printFolderContents (root);
303
+ Serial.println (" =============================\n " );
304
+
305
+ storage -> unmount ();
306
+ }
307
+ }
308
+
309
+ void setup (){
310
+ Serial.begin (115200 );
311
+ while (!Serial);
312
+
313
+ #if defined(HAS_USB)
314
+ runTests (&usb, " USB" );
315
+ #endif
316
+
317
+ #if defined(HAS_QSPI)
318
+ runTests (&internal, " QSPI" );
319
+ #endif
320
+
321
+ #if defined(HAS_SD)
322
+ runTests (&sd, " SD" );
323
+ #endif
324
+
325
+ }
326
+
327
+ void loop (){
328
+
329
+ }
0 commit comments