Skip to content

Commit 02e852f

Browse files
fixed examples
1 parent 541952e commit 02e852f

File tree

9 files changed

+307
-332
lines changed

9 files changed

+307
-332
lines changed

examples/AdvancedInternalStorage/AdvancedInternalStorage.ino

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 80 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
11
/*
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
2230
*/
2331

2432
#include <Arduino_UnifiedStorage.h>
2533

2634

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;
3638

3739
USBStorage thumbDrive = USBStorage();
3840

3941

4042
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()));
4244

4345
for (int i = 0; i < random(0, 9); i++){
4446
UFile thisFile = folder -> createFile("File_"+ String(random(999)), FileMode::WRITE);
@@ -59,61 +61,80 @@ void addSomeFakeFiles(Folder * folder){
5961

6062
void move(Folder * source, Folder * dest){
6163
for(Folder f: source -> getFolders()){
62-
Serial.println("* copying folder :" + String(f.getPathAsString()));
64+
Serial.println("Copying folder :" + String(f.getPathAsString()));
6365
f.moveTo(*dest);
6466
}
6567

6668
for(UFile f: source -> getFiles()){
67-
Serial.println("* copying file :" + String(f.getPathAsString()));
69+
Serial.println("Copying file :" + String(f.getPathAsString()));
6870
f.moveTo(*dest);
6971
}
7072
}
7173

7274

75+
void onConnected(){
76+
connected = true;
77+
}
78+
79+
void onDisconnected(){
80+
connected = false;
81+
}
7382

7483

7584

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-
}
8685

8786
void setup(){
8887
randomSeed(analogRead(A0));
8988

90-
9189
Serial.begin(115200);
9290
while(!Serial);
9391

94-
int thumbMounted = thumbDrive.begin(FS_FAT);
95-
Serial.println("* usb drive mounted:" + String(thumbMounted));
92+
thumbDrive.onConnect(onConnected);
9693

94+
Serial.println("Waiting for a USB thumb drive to be plugged in...");
9795

98-
Folder thumbRoot = thumbDrive.getRootFolder();
99-
String folderName = "InternalBackup_" + String(millis());
100-
Folder backupFolder = thumbRoot.createSubfolder(folderName);
96+
}
10197

102-
int partitionIndex = 0;
103-
for (auto part: InternalStorage::readPartitions()){
104-
partitionBackupFolderName = "Part" + String(partitionIndex);
105-
backupFolder.createFolder(partitionBackupFolderName);
106-
107-
108-
}
10998

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");
111105

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);
113109

114-
}
110+
int partitionIndex = 0;
115111

112+
std::vector<Partition> partitions = InternalStorage::readPartitions();
113+
Serial.println("Found " + String(partitions.size()) + " partitions on internalStorage \n");
116114

117-
void loop(){
115+
for (auto part: partitions){
116+
partitionIndex++;
117+
const char * partitionName = createPartitionName(partitionIndex);
118+
Folder thisPartitionBackupFolder = backupFolder.createSubfolder(partitionName);
118119

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+
}
119140
}

examples/Callbacks/Callbacks.ino

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
/*
3+
Callbacks
4+
5+
This code demonstrates the usage of the "Arduino_UnifiedStorage" library to handle USB Mass Storage device connection and disconnection callbacks for different supported Arduino boards.
6+
The code defines callback functions for USB device connection and uses LED indicators to signal when a USB device is connected (LED turned on) or disconnected (LED turned off).
7+
8+
INSTRUCTIONS:
9+
- Make sure you have "POSIXStorage" and "Arduino_UnifiedStorage" installed
10+
- Connect an Arduino board to a computer via USB and run this code.
11+
- The code will turn on the designated LED when a USB Mass Storage device is connected and turn it off when the device is disconnected.
12+
- This code can be adapted to perform actions or operations when a USB device is connected or disconnected.
13+
- You can also customize the LED indicators for different boards according to your hardware configuration.
14+
*/
15+
16+
17+
18+
#include "Arduino_UnifiedStorage.h"
19+
20+
#if defined(ARDUINO_PORTENTA_H7_M7)
21+
#define CALLBACK_LED LED_BLUE
22+
#elif defined(ARDUINO_PORTENTA_C33)
23+
#define CALLBACK_LED LEDB
24+
#elif defined(ARDUINO_OPTA)
25+
#define CALLBACK_LED LED_D0
26+
#endif
27+
28+
USBStorage usbStorage = USBStorage();
29+
30+
void connectionCallback(){
31+
digitalWrite(CALLBACK_LED, HIGH);
32+
}
33+
34+
void disconnectionCallback(){
35+
digitalWrite(CALLBACK_LED, LOW);
36+
}
37+
38+
void setup(){
39+
pinMode(CALLBACK_LED, OUTPUT);
40+
usbStorage.onConnect(connectionCallback);
41+
usbStorage.onDisconnect(disconnectionCallback);
42+
}
43+
44+
void loop(){
45+
46+
}

0 commit comments

Comments
 (0)