Skip to content

Ae 593 ensure arduino unified storage examples work #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Demonstrates advanced usage of the "Arduino_UnifiedStorage" library with USB & internal storage, including file operations.
Creates, copies, and moves files between storage types, and prints folder contents.

In the setup function, the code initializes serial communication, mounts both USB & internal storage and
In the setup function, the code initializes Serial communication, mounts both USB & internal storage and
reformats the internal storage for a clean file system. Then, it creates a root directory in the internal storage
and creates a subdirectory with a file inside it containing the string "Hello World!".

Expand All @@ -28,7 +28,6 @@
USBStorage usbStorage;
InternalStorage internalStorage;


// Helper function to prints the contents of a folder, including subdirectories (marked as "[D]") and files (marked as "[F]").
void printFolderContents(Folder dir, int indentation = 0) {
std::vector<Folder> directories = dir.getFolders();
Expand All @@ -37,46 +36,45 @@ void printFolderContents(Folder dir, int indentation = 0) {
// Print directories
for (Folder subdir : directories) {
for (int i = 0; i < indentation; i++) {
Serial.print(" ");
Arduino_UnifiedStorage::debugPrint(" ");
}
Serial.print("[D] ");
Serial.println(subdir.getPath());
Arduino_UnifiedStorage::debugPrint("[D] ");
Arduino_UnifiedStorage::debugPrint(subdir.getPath());
printFolderContents(subdir, indentation + 1);
}

// Print files
for (UFile file : files) {
for (int i = 0; i < indentation; i++) {
Serial.print(" ");
Arduino_UnifiedStorage::debugPrint(" ");
}
Serial.print("[F] ");
Serial.println(file.getPath());
Arduino_UnifiedStorage::debugPrint("[F] ");
Arduino_UnifiedStorage::debugPrint(file.getPath());
}
}



void setup() {
Serial.begin(115200);
while (!Serial);
#if !defined(ARDUINO_OPTA)
Serial.begin(115200);
while(!Serial);
#else
beginRS485(115200);
#endif

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

usbStorage = USBStorage();
internalStorage = InternalStorage();
Arduino_UnifiedStorage::debuggingModeEnabled = true;

// Mount the USB storage
if(usbStorage.begin()){
Serial.println("USB storage mounted.");
Arduino_UnifiedStorage::debugPrint("USB storage mounted.");
} else {
Serial.println(errno);
Arduino_UnifiedStorage::debugPrint(String(errno));
}

if(internalStorage.begin()){
Serial.println("Internal storage mounted.");
Arduino_UnifiedStorage::debugPrint("Internal storage mounted.");
} else {
Serial.println(errno);
Arduino_UnifiedStorage::debugPrint(String(errno));
}

// Create a root directory in the internal storage
Expand All @@ -93,27 +91,27 @@ void setup() {
// Copy the file from internal storage to USB storage
bool success = file.copyTo(usbStorage.getRootFolder(), true);
if (success) {
Serial.println("File copied successfully from internal storage to USB storage.");
Arduino_UnifiedStorage::debugPrint("File copied successfully from internal storage to USB storage.");
} else {
Serial.println("Failed to copy file from internal storage to USB storage.");
Serial.println(getErrno());
Arduino_UnifiedStorage::debugPrint("Failed to copy file from internal storage to USB storage.");
Arduino_UnifiedStorage::debugPrint(getErrno());
}

// Move the subdirectory from internal storage to USB storage
success = subdir.moveTo(usbStorage.getRootFolder(), true);
if (success) {
Serial.println("Subdirectory moved successfully from internal storage to USB storage.");
Arduino_UnifiedStorage::debugPrint("Subdirectory moved successfully from internal storage to USB storage.");
} else {
Serial.println("Failed to move subdirectory from internal storage to USB storage.");
Serial.println(getErrno());
Arduino_UnifiedStorage::debugPrint("Failed to move subdirectory from internal storage to USB storage.");
Arduino_UnifiedStorage::debugPrint(getErrno());
}

// Print contents of the USB storage
//Serial.println("USB storage contents:");
//printFolderContents(usbStorage.getRootFolder());
Arduino_UnifiedStorage::debugPrint("USB storage contents:");
printFolderContents(usbStorage.getRootFolder());

// Print contents of the internal storage
Serial.println("Internal storage contents:");
Arduino_UnifiedStorage::debugPrint("Internal storage contents:");
printFolderContents(internalStorage.getRootFolder());
}

Expand Down
37 changes: 15 additions & 22 deletions examples/BackupInternalPartitions/BackupInternalPartitions.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
This code demonstrates how the "Arduino_UnifiedStorage" can be used to access multiple partitions on the internal storage,
and transfer information to a USB Mass storage device.

In the setup function, the code initializes serial communication, and registers a callback for the insertion of the USB Drive.
In the setup function, the code initializes Serial communication, and registers a callback for the insertion of the USB Drive.

If the device is successfully mounted, a folder for this instance of a backup will be created on the USB Drive.

Expand Down Expand Up @@ -38,13 +38,12 @@ volatile boolean connected = false;

USBStorage thumbDrive;


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

for (int i = 0; i < random(0, 9); i++){
UFile thisFile = folder -> createFile("File_"+ String(random(999)), FileMode::WRITE);
Serial.println("\t * " + thisFile.getPathAsString());
Arduino_UnifiedStorage::testPrint("\t * " + thisFile.getPathAsString());
thisFile.write("writing stuff to the file");
thisFile.close();
}
Expand All @@ -53,52 +52,51 @@ void addSomeFakeFiles(Folder * folder){
Folder subfolder = folder -> createSubfolder("ChildFolder_"+ String(random(999)));
for (int i = 0; i < random(0, 9); i++){
UFile thisFile = subfolder.createFile("File_"+ String(random(999)), FileMode::WRITE);
Serial.println("\t * " + thisFile.getPathAsString());
Arduino_UnifiedStorage::testPrint("\t * " + thisFile.getPathAsString());
thisFile.write("writing stuff to the file");
thisFile.close();
}
}

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

for(UFile f: source -> getFiles()){
Serial.println("Copying file :" + String(f.getPathAsString()));
Arduino_UnifiedStorage::testPrint("Copying file :" + String(f.getPathAsString()));
f.moveTo(*dest);
}
}





void setup(){
randomSeed(analogRead(A0));

#if !defined(ARDUINO_OPTA)
Serial.begin(115200);
while(!Serial);
#else
beginRS485(115200);
#endif

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

thumbDrive = USBStorage();

bool thumbMounted = thumbDrive.begin(FS_FAT);
if(thumbMounted){
Serial.println("USB Thumb Drive has been mounted");
Arduino_UnifiedStorage::testPrint("USB Thumb Drive has been mounted");

Folder thumbRoot = thumbDrive.getRootFolder();
String folderName = "InternalBackup_" + String(millis());
Serial.println(folderName);
Arduino_UnifiedStorage::testPrint(folderName);
Folder backupFolder = thumbRoot.createSubfolder(folderName);

int partitionIndex = 0;

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

for (auto part: partitions){
partitionIndex++;
Expand All @@ -109,7 +107,7 @@ void setup(){
thisPartition.begin();

Folder partitionRootFolder = thisPartition.getRootFolder();
Serial.println(partitionRootFolder.getPathAsString());
Arduino_UnifiedStorage::testPrint(partitionRootFolder.getPathAsString());

if(createFakeFiles){
addSomeFakeFiles(&partitionRootFolder);
Expand All @@ -121,14 +119,9 @@ void setup(){

thumbDrive.unmount();


Serial.println("DONE, you can restart the board now");
Arduino_UnifiedStorage::testPrint("DONE, you can restart the board now");
}


}


void loop(){

}
16 changes: 15 additions & 1 deletion examples/Callbacks/Callbacks.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
- You can also customize the LED indicators for different boards according to your hardware configuration.
*/


#include "Arduino_UnifiedStorage.h"

#if defined(ARDUINO_PORTENTA_H7_M7)
Expand All @@ -27,17 +26,32 @@
USBStorage usbStorage = USBStorage();

void connectionCallback(){
#if defined(ARDUINO_PORTENTA_H7_M7)
digitalWrite(CALLBACK_LED, LOW);
#elif defined(ARDUINO_PORTENTA_C33)
digitalWrite(CALLBACK_LED, LOW);
#elif defined(ARDUINO_OPTA)
digitalWrite(CALLBACK_LED, HIGH);
#endif
}

void disconnectionCallback(){
#if defined(ARDUINO_PORTENTA_H7_M7)
digitalWrite(CALLBACK_LED, HIGH);
#elif defined(ARDUINO_PORTENTA_C33)
digitalWrite(CALLBACK_LED, HIGH);
#elif defined(ARDUINO_OPTA)
digitalWrite(CALLBACK_LED, LOW);
#endif
}

void setup(){
pinMode(CALLBACK_LED, OUTPUT);
usbStorage.onConnect(connectionCallback);
usbStorage.onDisconnect(disconnectionCallback);
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_C33)
digitalWrite(CALLBACK_LED, HIGH);
#endif
}

void loop(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
INSTRUCTIONS:
1. Check compatibility with your board and make sure you have "POSIXStorage" and "Arduino_UnifiedStorage" installed
2. Connect your board to the serial monitor
2. Connect your board to the Serial monitor
3. Wait for the sketch to run
4. Modify the partitioning scheme according to your needs
Expand Down
Loading
Loading