Skip to content

Commit 83ce9db

Browse files
authored
Merge pull request #60 from arduino-libraries/AE-593_Ensure_Arduino_UnifiedStorage_Examples_Work
Ae 593 ensure arduino unified storage examples work
2 parents 83801a6 + bb947a7 commit 83ce9db

File tree

8 files changed

+133
-121
lines changed

8 files changed

+133
-121
lines changed

examples/AdvancedUSBInternalOperations/AdvancedUSBInternalOperations.ino

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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
@@ -28,7 +28,6 @@
2828
USBStorage usbStorage;
2929
InternalStorage internalStorage;
3030

31-
3231
// Helper function to prints the contents of a folder, including subdirectories (marked as "[D]") and files (marked as "[F]").
3332
void 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-
5956
void 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

examples/BackupInternalPartitions/BackupInternalPartitions.ino

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
This code demonstrates how the "Arduino_UnifiedStorage" can be used to access multiple partitions on the internal storage,
55
and transfer information to a USB Mass storage device.
66
7-
In the setup function, the code initializes serial communication, and registers a callback for the insertion of the USB Drive.
7+
In the setup function, the code initializes Serial communication, and registers a callback for the insertion of the USB Drive.
88
99
If the device is successfully mounted, a folder for this instance of a backup will be created on the USB Drive.
1010
@@ -38,13 +38,12 @@ volatile boolean connected = false;
3838

3939
USBStorage thumbDrive;
4040

41-
4241
void addSomeFakeFiles(Folder * folder){
43-
Serial.println("Adding some fake files to: " + String(folder -> getPathAsString()));
42+
Arduino_UnifiedStorage::testPrint("Adding some fake files to: " + String(folder -> getPathAsString()));
4443

4544
for (int i = 0; i < random(0, 9); i++){
4645
UFile thisFile = folder -> createFile("File_"+ String(random(999)), FileMode::WRITE);
47-
Serial.println("\t * " + thisFile.getPathAsString());
46+
Arduino_UnifiedStorage::testPrint("\t * " + thisFile.getPathAsString());
4847
thisFile.write("writing stuff to the file");
4948
thisFile.close();
5049
}
@@ -53,52 +52,51 @@ void addSomeFakeFiles(Folder * folder){
5352
Folder subfolder = folder -> createSubfolder("ChildFolder_"+ String(random(999)));
5453
for (int i = 0; i < random(0, 9); i++){
5554
UFile thisFile = subfolder.createFile("File_"+ String(random(999)), FileMode::WRITE);
56-
Serial.println("\t * " + thisFile.getPathAsString());
55+
Arduino_UnifiedStorage::testPrint("\t * " + thisFile.getPathAsString());
5756
thisFile.write("writing stuff to the file");
5857
thisFile.close();
5958
}
6059
}
6160

6261
void move(Folder * source, Folder * dest){
6362
for(Folder f: source -> getFolders()){
64-
Serial.println("Copying folder :" + String(f.getPathAsString()));
63+
Arduino_UnifiedStorage::testPrint("Copying folder :" + String(f.getPathAsString()));
6564
f.moveTo(*dest);
6665
}
6766

6867
for(UFile f: source -> getFiles()){
69-
Serial.println("Copying file :" + String(f.getPathAsString()));
68+
Arduino_UnifiedStorage::testPrint("Copying file :" + String(f.getPathAsString()));
7069
f.moveTo(*dest);
7170
}
7271
}
7372

7473

75-
76-
77-
7874
void setup(){
7975
randomSeed(analogRead(A0));
8076

77+
#if !defined(ARDUINO_OPTA)
8178
Serial.begin(115200);
8279
while(!Serial);
80+
#else
81+
beginRS485(115200);
82+
#endif
8383

8484
// toggle this to enable debugging output
8585
Arduino_UnifiedStorage::debuggingModeEnabled = false;
8686

87-
thumbDrive = USBStorage();
88-
8987
bool thumbMounted = thumbDrive.begin(FS_FAT);
9088
if(thumbMounted){
91-
Serial.println("USB Thumb Drive has been mounted");
89+
Arduino_UnifiedStorage::testPrint("USB Thumb Drive has been mounted");
9290

9391
Folder thumbRoot = thumbDrive.getRootFolder();
9492
String folderName = "InternalBackup_" + String(millis());
95-
Serial.println(folderName);
93+
Arduino_UnifiedStorage::testPrint(folderName);
9694
Folder backupFolder = thumbRoot.createSubfolder(folderName);
9795

9896
int partitionIndex = 0;
9997

10098
std::vector<Partition> partitions = InternalStorage::readPartitions();
101-
Serial.println("Found " + String(partitions.size()) + " partitions on internalStorage \n");
99+
Arduino_UnifiedStorage::testPrint("Found " + String(partitions.size()) + " partitions on internalStorage \n");
102100

103101
for (auto part: partitions){
104102
partitionIndex++;
@@ -109,7 +107,7 @@ void setup(){
109107
thisPartition.begin();
110108

111109
Folder partitionRootFolder = thisPartition.getRootFolder();
112-
Serial.println(partitionRootFolder.getPathAsString());
110+
Arduino_UnifiedStorage::testPrint(partitionRootFolder.getPathAsString());
113111

114112
if(createFakeFiles){
115113
addSomeFakeFiles(&partitionRootFolder);
@@ -121,14 +119,9 @@ void setup(){
121119

122120
thumbDrive.unmount();
123121

124-
125-
Serial.println("DONE, you can restart the board now");
122+
Arduino_UnifiedStorage::testPrint("DONE, you can restart the board now");
126123
}
127-
128-
129124
}
130125

131-
132126
void loop(){
133-
134127
}

examples/Callbacks/Callbacks.ino

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
- You can also customize the LED indicators for different boards according to your hardware configuration.
1414
*/
1515

16-
1716
#include "Arduino_UnifiedStorage.h"
1817

1918
#if defined(ARDUINO_PORTENTA_H7_M7)
@@ -27,17 +26,32 @@
2726
USBStorage usbStorage = USBStorage();
2827

2928
void connectionCallback(){
29+
#if defined(ARDUINO_PORTENTA_H7_M7)
30+
digitalWrite(CALLBACK_LED, LOW);
31+
#elif defined(ARDUINO_PORTENTA_C33)
32+
digitalWrite(CALLBACK_LED, LOW);
33+
#elif defined(ARDUINO_OPTA)
3034
digitalWrite(CALLBACK_LED, HIGH);
35+
#endif
3136
}
3237

3338
void disconnectionCallback(){
39+
#if defined(ARDUINO_PORTENTA_H7_M7)
40+
digitalWrite(CALLBACK_LED, HIGH);
41+
#elif defined(ARDUINO_PORTENTA_C33)
42+
digitalWrite(CALLBACK_LED, HIGH);
43+
#elif defined(ARDUINO_OPTA)
3444
digitalWrite(CALLBACK_LED, LOW);
45+
#endif
3546
}
3647

3748
void setup(){
3849
pinMode(CALLBACK_LED, OUTPUT);
3950
usbStorage.onConnect(connectionCallback);
4051
usbStorage.onDisconnect(disconnectionCallback);
52+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_C33)
53+
digitalWrite(CALLBACK_LED, HIGH);
54+
#endif
4155
}
4256

4357
void loop(){

examples/InternalStoragePartitioning/InternalStoragePartitioning.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
2424
INSTRUCTIONS:
2525
1. Check compatibility with your board and make sure you have "POSIXStorage" and "Arduino_UnifiedStorage" installed
26-
2. Connect your board to the serial monitor
26+
2. Connect your board to the Serial monitor
2727
3. Wait for the sketch to run
2828
4. Modify the partitioning scheme according to your needs
2929

0 commit comments

Comments
 (0)