1
1
/*
2
- BackupInternalPartitions
3
-
4
- This code demonstrates how the "Arduino_UnifiedStorage" can be used to access multiple partitions on the internal storage,
5
- and transfer information to a USB Mass storage device.
6
-
7
- In the setup function, the code initializes serial communication, mounts both USB & internal storage.
8
- It then creates a root directory in the internal storage and creates a subdirectory with files inside it.
9
-
10
- The "addSomeFakeFiles" function generates random files in the specified folder, simulating real data.
11
-
12
- Afterward, it copies files from internal storage to USB storage and moves folders from each partition internal storage to USB storage.
13
-
14
- The "move" function is responsible for transferring folders and files between storage locations.
15
-
16
- The "backupPartitionsC33" and "backupPartitionsH7" functions backup partitions based on the board type, as each have a different default scheme.
17
-
18
- Created: 31th August 2023
19
- By: Cristian Dragomir
20
-
21
- Source: https://github.com/arduino-libraries/Arduino_UnifiedStorage/blob/main/examples/BackupInternalPartitions/BackupInternalPartitions.ino
2
+ BackupInternalPartitions
3
+
4
+ This code demonstrates how the "Arduino_UnifiedStorage" can be used to access multiple partitions on the internal storage,
5
+ and transfer information to a USB Mass storage device.
6
+
7
+ In the setup function, the code initializes serial communication, and registers a callback for the insertion of the USB Drive.
8
+
9
+ If the device is succesfully mounted, a folder for this instance of a backup will be created on the USB Drive.
10
+
11
+ Afterwards the sketch does the following:
12
+ - lists all partitions available on the InternalStorage
13
+ - mounts each partition
14
+ - creates a sub folder for each partition,
15
+ - copies everything on that partition to the coresponding subfolder.
16
+
17
+ The "addSomeFakeFiles" function generates random files in the specified folder, simulating real data.
18
+ The "move" function is responsible for transferring folders and files between storage locations.
19
+
20
+ INSTRUCTIONS
21
+ - Make sure you have "POSIXStorage" and "Arduino_UnifiedStorage" installed
22
+ - Insert a USB Drive whenever you want
23
+ - Wait for the sketch to finish, it will display the following "DONE, you can restart the board now" when succesful
24
+ - Unplug the USB device and inspect its contents.
25
+
26
+ Created: 31th August 2023
27
+ By: Cristian Dragomir
28
+
29
+ Source: https://github.com/arduino-libraries/Arduino_UnifiedStorage/blob/main/examples/BackupInternalPartitions/BackupInternalPartitions.ino
22
30
*/
23
31
24
32
#include < Arduino_UnifiedStorage.h>
25
33
26
34
27
- #if defined(ARDUINO_PORTENTA_C33)
28
- InternalStorage ota = InternalStorage(1 , " ota" , FS_FAT);
29
- InternalStorage data = InternalStorage(2 , " data" , FS_FAT);
30
-
31
- #elif defined(ARDUINO_PORTENTA_H7_M7)
32
- InternalStorage wifi = InternalStorage(1 , " wifi" , FS_FAT);
33
- InternalStorage ota = InternalStorage(2 , " ota" , FS_FAT);
34
- InternalStorage data = InternalStorage(3 , " data" , FS_FAT);
35
- #endif
35
+ constexpr boolean createFakeFiles = true ;
36
+ boolean done = false ;
37
+ volatile boolean connected = false ;
36
38
37
39
USBStorage thumbDrive = USBStorage();
38
40
39
41
40
42
void addSomeFakeFiles (Folder * folder){
41
- Serial.println (" * adding some fake files to: " + String (folder -> getPathAsString ()));
43
+ Serial.println (" Adding some fake files to: " + String (folder -> getPathAsString ()));
42
44
43
45
for (int i = 0 ; i < random (0 , 9 ); i++){
44
46
UFile thisFile = folder -> createFile (" File_" + String (random (999 )), FileMode::WRITE);
@@ -59,61 +61,80 @@ void addSomeFakeFiles(Folder * folder){
59
61
60
62
void move (Folder * source, Folder * dest){
61
63
for (Folder f: source -> getFolders ()){
62
- Serial.println (" * copying folder :" + String (f.getPathAsString ()));
64
+ Serial.println (" Copying folder :" + String (f.getPathAsString ()));
63
65
f.moveTo (*dest);
64
66
}
65
67
66
68
for (UFile f: source -> getFiles ()){
67
- Serial.println (" * copying file :" + String (f.getPathAsString ()));
69
+ Serial.println (" Copying file :" + String (f.getPathAsString ()));
68
70
f.moveTo (*dest);
69
71
}
70
72
}
71
73
72
74
75
+ void onConnected (){
76
+ connected = true ;
77
+ }
78
+
79
+ void onDisconnected (){
80
+ connected = false ;
81
+ }
73
82
74
83
75
84
76
- void backupPartitions (){
77
- Folder otaRoot = ota.getRootFolder ();
78
- addSomeFakeFiles (&otaRoot);
79
- Folder otaFolder = backupFolder -> createSubfolder (" ota" );
80
- move (&otaRoot, &otaFolder);
81
- ota.unmount ();
82
- } else {
83
- Serial.println (" OTA partition not mounted, cannot proceed" );
84
- }
85
- }
86
85
87
86
void setup (){
88
87
randomSeed (analogRead (A0));
89
88
90
-
91
89
Serial.begin (115200 );
92
90
while (!Serial);
93
91
94
- int thumbMounted = thumbDrive.begin (FS_FAT);
95
- Serial.println (" * usb drive mounted:" + String (thumbMounted));
92
+ thumbDrive.onConnect (onConnected);
96
93
94
+ Serial.println (" Waiting for a USB thumb drive to be plugged in..." );
97
95
98
- Folder thumbRoot = thumbDrive.getRootFolder ();
99
- String folderName = " InternalBackup_" + String (millis ());
100
- Folder backupFolder = thumbRoot.createSubfolder (folderName);
96
+ }
101
97
102
- int partitionIndex = 0 ;
103
- for (auto part: InternalStorage::readPartitions ()){
104
- partitionBackupFolderName = " Part" + String (partitionIndex);
105
- backupFolder.createFolder (partitionBackupFolderName);
106
-
107
-
108
- }
109
98
110
- thumbDrive.unmount ();
99
+ void loop (){
100
+ if (connected && !done){
101
+ Serial.println (" USB Thumb Drive has been inserted" );
102
+ bool thumbMounted = thumbDrive.begin (FS_FAT);
103
+ if (thumbMounted){
104
+ Serial.println (" USB Thumb Drive has been mounted" );
111
105
112
- Serial.println (" DONE, you can restart the board now" );
106
+ Folder thumbRoot = thumbDrive.getRootFolder ();
107
+ String folderName = " InternalBackup_" + String (millis ());
108
+ Folder backupFolder = thumbRoot.createSubfolder (folderName);
113
109
114
- }
110
+ int partitionIndex = 0 ;
115
111
112
+ std::vector<Partition> partitions = InternalStorage::readPartitions ();
113
+ Serial.println (" Found " + String (partitions.size ()) + " partitions on internalStorage \n " );
116
114
117
- void loop (){
115
+ for (auto part: partitions){
116
+ partitionIndex++;
117
+ const char * partitionName = createPartitionName (partitionIndex);
118
+ Folder thisPartitionBackupFolder = backupFolder.createSubfolder (partitionName);
118
119
120
+ InternalStorage thisPartition = InternalStorage (partitionIndex, partitionName, part.fileSystemType );
121
+ thisPartition.begin ();
122
+
123
+ Folder partitionRootFolder = thisPartition.getRootFolder ();
124
+ Serial.println (partitionRootFolder.getPathAsString ());
125
+
126
+ if (createFakeFiles){
127
+ addSomeFakeFiles (&partitionRootFolder);
128
+ }
129
+
130
+ move (&partitionRootFolder, &thisPartitionBackupFolder);
131
+ thisPartition.unmount ();
132
+ }
133
+
134
+ thumbDrive.unmount ();
135
+ done = true ;
136
+
137
+ Serial.println (" DONE, you can restart the board now" );
138
+ }
139
+ }
119
140
}
0 commit comments