Skip to content

Commit f855ed8

Browse files
committed
AE-593: Small fixes for example sketches.
1 parent aefc656 commit f855ed8

File tree

6 files changed

+60
-59
lines changed

6 files changed

+60
-59
lines changed

examples/AdvancedUSBInternalOperations/AdvancedUSBInternalOperations.ino

Lines changed: 9 additions & 11 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();
@@ -54,18 +53,17 @@ void printFolderContents(Folder dir, int indentation = 0) {
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
6465
Arduino_UnifiedStorage::debuggingModeEnabled = false;
6566

66-
usbStorage = USBStorage();
67-
internalStorage = InternalStorage();
68-
6967
// Mount the USB storage
7068
if(usbStorage.begin()){
7169
Serial.println("USB storage mounted.");
@@ -109,8 +107,8 @@ void setup() {
109107
}
110108

111109
// Print contents of the USB storage
112-
//Serial.println("USB storage contents:");
113-
//printFolderContents(usbStorage.getRootFolder());
110+
Serial.println("USB storage contents:");
111+
printFolderContents(usbStorage.getRootFolder());
114112

115113
// Print contents of the internal storage
116114
Serial.println("Internal storage contents:");

examples/BackupInternalPartitions/BackupInternalPartitions.ino

Lines changed: 5 additions & 12 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,7 +38,6 @@ volatile boolean connected = false;
3838

3939
USBStorage thumbDrive;
4040

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

@@ -72,20 +71,19 @@ void move(Folder * source, Folder * dest){
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){
9189
Serial.println("USB Thumb Drive has been mounted");
@@ -121,14 +119,9 @@ void setup(){
121119

122120
thumbDrive.unmount();
123121

124-
125122
Serial.println("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

examples/Logger/Logger.ino

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "Arduino_UnifiedStorage.h"
2626
#include <vector>
2727

28-
2928
#if defined(ARDUINO_PORTENTA_H7_M7)
3029
#define USB_MOUNTED_LED LED_BLUE
3130
#elif defined(ARDUINO_PORTENTA_C33)
@@ -53,12 +52,13 @@ bool backingUP = false;
5352
void connectionCallback(){
5453
usbAvailable = true;
5554
usbStorage.removeOnConnectCallback();
55+
Serial.println("USB drive connected.");
5656
}
5757

5858
void disconnectionCallback(){
5959
usbAvailable = false;
6060
usbStorage.onConnect(connectionCallback);
61-
61+
Serial.println("USB drive disconnected.");
6262
}
6363
// Function to run a given method periodically
6464
void runPeriodically(void (*method)(), unsigned long interval, unsigned long* variable) {
@@ -89,7 +89,6 @@ void moveDataToQSPI() {
8989
}
9090
}
9191

92-
9392
void performUpdate() {
9493
UFile logFile = internalStorage.getRootFolder().createFile("log.txt", FileMode::READ);
9594
UFile backupFile = backupFolder.createFile("backup_file.txt", FileMode::APPEND); // Create or open the backup file
@@ -126,7 +125,6 @@ void performUpdate() {
126125
logFile.close();
127126
lastUpdateFile.close();
128127

129-
130128
usbStorage.unmount(); // Unmount the USB storage
131129

132130
digitalWrite(USB_MOUNTED_LED, HIGH);
@@ -166,27 +164,22 @@ void backupToUSB() {
166164
} else {
167165
Arduino_UnifiedStorage::debugPrint("USB Mass storage is not available ");
168166
}
169-
170-
171167
}
172168

173169

174170
void setup() {
175171
randomSeed(analogRead(A0));
176172

177-
#if !defined(ARDUINO_OPTA)
178-
Serial.begin(115200);
179-
while(!Serial);
180-
#else
181-
beginRS485(115200);
182-
#endif
173+
#if !defined(ARDUINO_OPTA)
174+
Serial.begin(115200);
175+
while(!Serial);
176+
#else
177+
beginRS485(115200);
178+
#endif
183179

184180
// toggle this to enable debugging output
185181
Arduino_UnifiedStorage::debuggingModeEnabled = false;
186182

187-
usbStorage = USBStorage();
188-
internalStorage = InternalStorage();
189-
190183
usbStorage.onConnect(connectionCallback);
191184
usbStorage.onDisconnect(disconnectionCallback);
192185

@@ -195,20 +188,15 @@ void setup() {
195188
int formatted = internalStorage.format(FS_LITTLEFS);
196189
Arduino_UnifiedStorage::debugPrint("QSPI Format status: " + String(formatted));
197190

198-
199-
200191
if (!internalStorage.begin()) {
201192
Arduino_UnifiedStorage::debugPrint("Failed to initialize internal storage ");
202193
return;
203194
} else {
204195
Arduino_UnifiedStorage::debugPrint("Initialized storage ");
205196
}
206-
207197
}
208198

209199
void loop() {
210-
211-
212200
runPeriodically(logDataToRAM, 100, &lastLog);
213201
runPeriodically(moveDataToQSPI, 1000, &lastMove);
214202
runPeriodically(backupToUSB, 10000, &lastBackup);

examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@
2929

3030
#include "Arduino_UnifiedStorage.h"
3131

32+
// Set one of the following to "true" and the rest to "false" in order to select one storage medium
33+
#define USE_SD_STORAGE false
34+
#define USE_USB_STORAGE false
35+
#define USE_INTERNAL_STORAGE true
36+
37+
#if defined(USE_SD_STORAGE) && (USE_SD_STORAGE == true)
38+
SDStorage storage; // Create an instance for interacting with SD card storage
39+
#elif defined(USE_USB_STORAGE) && (USE_USB_STORAGE == true)
40+
USBStorage storage; // Create an instance for interacting with USB storage
41+
#elif defined(USE_INTERNAL_STORAGE) && (USE_INTERNAL_STORAGE == true)
42+
InternalStorage storage;
43+
#else
44+
#error "No valid storage option defined! Please define one of USE_SD_STORAGE, USE_USB_STORAGE, or USE_INTERNAL_STORAGE as true."
45+
#endif
3246

3347
void printFolderContents(Folder dir, int indentation = 0) {
3448
std::vector<Folder> directories = dir.getFolders();
@@ -54,25 +68,19 @@ void printFolderContents(Folder dir, int indentation = 0) {
5468
}
5569
}
5670

57-
58-
// Uncomment one of the three lines below to select between SD card, USB or internal storage
59-
//SDStorage storage; // Create an instance for interacting with SD card storage
60-
//USBStorage storage; // Create an instance for interacting with USB storage
61-
InternalStorage storage;
62-
63-
6471
void setup() {
65-
Serial.begin(115200);
66-
while (!Serial);
72+
73+
// if we are on the Arduino Opta, and have decided to log on an USB drive connected to the USB-C connecter, we have to output the serial data through the RJ45 channel.
74+
#if (defined(ARDUINO_OPTA)) && (defined(USE_USB_STORAGE) && (USE_USB_STORAGE == true))
75+
beginRS485(115200);
76+
#else
77+
Serial.begin(115200);
78+
while (!Serial);
79+
#endif
6780

6881
// toggle this to enable debugging output
6982
Arduino_UnifiedStorage::debuggingModeEnabled = false;
7083

71-
72-
storage = InternalStorage();
73-
// storage = SDStorage(); // Uncomment this line to use SD card storage
74-
// storage = USBStorage(); // Uncomment this line to use USB storage
75-
7684
if(!storage.begin()){
7785
Serial.println("Error mounting storage device.");
7886
}

0 commit comments

Comments
 (0)