From aefc656cff4a044c1ac8fa9d0b2d8cc698bce21c Mon Sep 17 00:00:00 2001 From: Bogdan Ivanus Date: Mon, 4 Aug 2025 13:36:53 +0300 Subject: [PATCH 1/4] AE-593: Fixes bugs introduced with previous commits. --- src/InternalStorage.cpp | 1 + src/Partitioning.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/InternalStorage.cpp b/src/InternalStorage.cpp index 3edc15d..079bc04 100644 --- a/src/InternalStorage.cpp +++ b/src/InternalStorage.cpp @@ -95,6 +95,7 @@ Folder InternalStorage::getRootFolder(){ bool InternalStorage::format(FileSystems fs){ FileSystemType * tmpFileSystem = nullptr; + this -> begin(); this -> unmount(); this -> fileSystemType = fs; diff --git a/src/Partitioning.cpp b/src/Partitioning.cpp index 8db353a..de9af76 100644 --- a/src/Partitioning.cpp +++ b/src/Partitioning.cpp @@ -180,20 +180,20 @@ std::vector Partitioning::readPartitions(BlockDeviceType * blockDevic fatProbeFileSystem -> unmount(); partition.fileSystemType = FS_FAT; partitions.push_back(partition); - + delete mbrBlocKDevice; + delete fatProbeFileSystem; } else if (littleFsProbeFilesystem -> mount(mbrBlocKDevice) == 0){ Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] Partition " + String(partitionIndex) + " is formatted with LittleFS file system"); littleFsProbeFilesystem -> unmount(); partition.fileSystemType = FS_LITTLEFS; partitions.push_back(partition); + delete mbrBlocKDevice; + delete littleFsProbeFilesystem; } else { Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] Partition " + String(partitionIndex) + " is not formatted with a recognized file system"); } } - delete mbrBlocKDevice; - delete fatProbeFileSystem; - delete littleFsProbeFilesystem; } blockDevice->deinit(); delete[] buffer; From f855ed89e245de76f2fc5110a0eca7834817657b Mon Sep 17 00:00:00 2001 From: Bogdan Ivanus Date: Wed, 6 Aug 2025 17:12:13 +0300 Subject: [PATCH 2/4] AE-593: Small fixes for example sketches. --- .../AdvancedUSBInternalOperations.ino | 20 +++++------ .../BackupInternalPartitions.ino | 17 +++------ examples/Callbacks/Callbacks.ino | 16 ++++++++- .../InternalStoragePartitioning.ino | 2 +- examples/Logger/Logger.ino | 28 +++++---------- .../SimpleStorageWriteRead.ino | 36 +++++++++++-------- 6 files changed, 60 insertions(+), 59 deletions(-) diff --git a/examples/AdvancedUSBInternalOperations/AdvancedUSBInternalOperations.ino b/examples/AdvancedUSBInternalOperations/AdvancedUSBInternalOperations.ino index 11f73ea..d591f51 100644 --- a/examples/AdvancedUSBInternalOperations/AdvancedUSBInternalOperations.ino +++ b/examples/AdvancedUSBInternalOperations/AdvancedUSBInternalOperations.ino @@ -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!". @@ -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 directories = dir.getFolders(); @@ -54,18 +53,17 @@ void printFolderContents(Folder dir, int indentation = 0) { } } - - 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(); - // Mount the USB storage if(usbStorage.begin()){ Serial.println("USB storage mounted."); @@ -109,8 +107,8 @@ void setup() { } // Print contents of the USB storage - //Serial.println("USB storage contents:"); - //printFolderContents(usbStorage.getRootFolder()); + Serial.println("USB storage contents:"); + printFolderContents(usbStorage.getRootFolder()); // Print contents of the internal storage Serial.println("Internal storage contents:"); diff --git a/examples/BackupInternalPartitions/BackupInternalPartitions.ino b/examples/BackupInternalPartitions/BackupInternalPartitions.ino index d181d76..aff6e03 100644 --- a/examples/BackupInternalPartitions/BackupInternalPartitions.ino +++ b/examples/BackupInternalPartitions/BackupInternalPartitions.ino @@ -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. @@ -38,7 +38,6 @@ volatile boolean connected = false; USBStorage thumbDrive; - void addSomeFakeFiles(Folder * folder){ Serial.println("Adding some fake files to: " + String(folder -> getPathAsString())); @@ -72,20 +71,19 @@ void move(Folder * source, Folder * 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"); @@ -121,14 +119,9 @@ void setup(){ thumbDrive.unmount(); - Serial.println("DONE, you can restart the board now"); } - - } - void loop(){ - } diff --git a/examples/Callbacks/Callbacks.ino b/examples/Callbacks/Callbacks.ino index 5b215d5..fc8bc20 100644 --- a/examples/Callbacks/Callbacks.ino +++ b/examples/Callbacks/Callbacks.ino @@ -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) @@ -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(){ diff --git a/examples/InternalStoragePartitioning/InternalStoragePartitioning.ino b/examples/InternalStoragePartitioning/InternalStoragePartitioning.ino index 69b2693..1764eba 100644 --- a/examples/InternalStoragePartitioning/InternalStoragePartitioning.ino +++ b/examples/InternalStoragePartitioning/InternalStoragePartitioning.ino @@ -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 diff --git a/examples/Logger/Logger.ino b/examples/Logger/Logger.ino index fcfcaab..996c3bd 100644 --- a/examples/Logger/Logger.ino +++ b/examples/Logger/Logger.ino @@ -25,7 +25,6 @@ #include "Arduino_UnifiedStorage.h" #include - #if defined(ARDUINO_PORTENTA_H7_M7) #define USB_MOUNTED_LED LED_BLUE #elif defined(ARDUINO_PORTENTA_C33) @@ -53,12 +52,13 @@ bool backingUP = false; void connectionCallback(){ usbAvailable = true; usbStorage.removeOnConnectCallback(); + Serial.println("USB drive connected."); } void disconnectionCallback(){ usbAvailable = false; usbStorage.onConnect(connectionCallback); - + Serial.println("USB drive disconnected."); } // Function to run a given method periodically void runPeriodically(void (*method)(), unsigned long interval, unsigned long* variable) { @@ -89,7 +89,6 @@ void moveDataToQSPI() { } } - void performUpdate() { UFile logFile = internalStorage.getRootFolder().createFile("log.txt", FileMode::READ); UFile backupFile = backupFolder.createFile("backup_file.txt", FileMode::APPEND); // Create or open the backup file @@ -126,7 +125,6 @@ void performUpdate() { logFile.close(); lastUpdateFile.close(); - usbStorage.unmount(); // Unmount the USB storage digitalWrite(USB_MOUNTED_LED, HIGH); @@ -166,27 +164,22 @@ void backupToUSB() { } else { Arduino_UnifiedStorage::debugPrint("USB Mass storage is not available "); } - - } void setup() { randomSeed(analogRead(A0)); - #if !defined(ARDUINO_OPTA) - Serial.begin(115200); - while(!Serial); - #else - beginRS485(115200); - #endif +#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(); - usbStorage.onConnect(connectionCallback); usbStorage.onDisconnect(disconnectionCallback); @@ -195,20 +188,15 @@ void setup() { int formatted = internalStorage.format(FS_LITTLEFS); Arduino_UnifiedStorage::debugPrint("QSPI Format status: " + String(formatted)); - - if (!internalStorage.begin()) { Arduino_UnifiedStorage::debugPrint("Failed to initialize internal storage "); return; } else { Arduino_UnifiedStorage::debugPrint("Initialized storage "); } - } void loop() { - - runPeriodically(logDataToRAM, 100, &lastLog); runPeriodically(moveDataToQSPI, 1000, &lastMove); runPeriodically(backupToUSB, 10000, &lastBackup); diff --git a/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino b/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino index 51a4a65..b74c8d0 100644 --- a/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino +++ b/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino @@ -29,6 +29,20 @@ #include "Arduino_UnifiedStorage.h" +// Set one of the following to "true" and the rest to "false" in order to select one storage medium +#define USE_SD_STORAGE false +#define USE_USB_STORAGE false +#define USE_INTERNAL_STORAGE true + +#if defined(USE_SD_STORAGE) && (USE_SD_STORAGE == true) +SDStorage storage; // Create an instance for interacting with SD card storage +#elif defined(USE_USB_STORAGE) && (USE_USB_STORAGE == true) +USBStorage storage; // Create an instance for interacting with USB storage +#elif defined(USE_INTERNAL_STORAGE) && (USE_INTERNAL_STORAGE == true) +InternalStorage storage; +#else +#error "No valid storage option defined! Please define one of USE_SD_STORAGE, USE_USB_STORAGE, or USE_INTERNAL_STORAGE as true." +#endif void printFolderContents(Folder dir, int indentation = 0) { std::vector directories = dir.getFolders(); @@ -54,25 +68,19 @@ void printFolderContents(Folder dir, int indentation = 0) { } } - -// Uncomment one of the three lines below to select between SD card, USB or internal storage -//SDStorage storage; // Create an instance for interacting with SD card storage -//USBStorage storage; // Create an instance for interacting with USB storage -InternalStorage storage; - - void setup() { - Serial.begin(115200); - while (!Serial); + +// 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. +#if (defined(ARDUINO_OPTA)) && (defined(USE_USB_STORAGE) && (USE_USB_STORAGE == true)) + beginRS485(115200); +#else + Serial.begin(115200); + while (!Serial); +#endif // toggle this to enable debugging output Arduino_UnifiedStorage::debuggingModeEnabled = false; - - storage = InternalStorage(); - // storage = SDStorage(); // Uncomment this line to use SD card storage - // storage = USBStorage(); // Uncomment this line to use USB storage - if(!storage.begin()){ Serial.println("Error mounting storage device."); } From 55f408c97afff2ad58fa17a68244c44600087532 Mon Sep 17 00:00:00 2001 From: Bogdan Ivanus Date: Fri, 8 Aug 2025 13:44:09 +0300 Subject: [PATCH 3/4] AE-593: Replaced all "Serial.print()" with "Arduino_UnifiedStorage::debugPrint" to allow automatic remapping of the serial output channel on all boards, including the Opta, which uses the RJ45 to print log data. --- .../AdvancedUSBInternalOperations.ino | 38 +++++++++---------- .../BackupInternalPartitions.ino | 22 +++++------ examples/Logger/Logger.ino | 6 +-- .../SimpleStorageWriteRead.ino | 30 +++++++-------- 4 files changed, 48 insertions(+), 48 deletions(-) diff --git a/examples/AdvancedUSBInternalOperations/AdvancedUSBInternalOperations.ino b/examples/AdvancedUSBInternalOperations/AdvancedUSBInternalOperations.ino index d591f51..241c579 100644 --- a/examples/AdvancedUSBInternalOperations/AdvancedUSBInternalOperations.ino +++ b/examples/AdvancedUSBInternalOperations/AdvancedUSBInternalOperations.ino @@ -36,20 +36,20 @@ 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()); } } @@ -62,19 +62,19 @@ void setup() { #endif // toggle this to enable debugging output - Arduino_UnifiedStorage::debuggingModeEnabled = false; + 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 @@ -91,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:"); + 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()); } diff --git a/examples/BackupInternalPartitions/BackupInternalPartitions.ino b/examples/BackupInternalPartitions/BackupInternalPartitions.ino index aff6e03..e7702b6 100644 --- a/examples/BackupInternalPartitions/BackupInternalPartitions.ino +++ b/examples/BackupInternalPartitions/BackupInternalPartitions.ino @@ -39,11 +39,11 @@ volatile boolean connected = false; USBStorage thumbDrive; void addSomeFakeFiles(Folder * folder){ - Serial.println("Adding some fake files to: " + String(folder -> getPathAsString())); + Arduino_UnifiedStorage::debugPrint("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::debugPrint("\t * " + thisFile.getPathAsString()); thisFile.write("writing stuff to the file"); thisFile.close(); } @@ -52,7 +52,7 @@ 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::debugPrint("\t * " + thisFile.getPathAsString()); thisFile.write("writing stuff to the file"); thisFile.close(); } @@ -60,12 +60,12 @@ void addSomeFakeFiles(Folder * folder){ void move(Folder * source, Folder * dest){ for(Folder f: source -> getFolders()){ - Serial.println("Copying folder :" + String(f.getPathAsString())); + Arduino_UnifiedStorage::debugPrint("Copying folder :" + String(f.getPathAsString())); f.moveTo(*dest); } for(UFile f: source -> getFiles()){ - Serial.println("Copying file :" + String(f.getPathAsString())); + Arduino_UnifiedStorage::debugPrint("Copying file :" + String(f.getPathAsString())); f.moveTo(*dest); } } @@ -82,21 +82,21 @@ void setup(){ #endif // toggle this to enable debugging output - Arduino_UnifiedStorage::debuggingModeEnabled = false; + Arduino_UnifiedStorage::debuggingModeEnabled = true; bool thumbMounted = thumbDrive.begin(FS_FAT); if(thumbMounted){ - Serial.println("USB Thumb Drive has been mounted"); + Arduino_UnifiedStorage::debugPrint("USB Thumb Drive has been mounted"); Folder thumbRoot = thumbDrive.getRootFolder(); String folderName = "InternalBackup_" + String(millis()); - Serial.println(folderName); + Arduino_UnifiedStorage::debugPrint(folderName); Folder backupFolder = thumbRoot.createSubfolder(folderName); int partitionIndex = 0; std::vector partitions = InternalStorage::readPartitions(); - Serial.println("Found " + String(partitions.size()) + " partitions on internalStorage \n"); + Arduino_UnifiedStorage::debugPrint("Found " + String(partitions.size()) + " partitions on internalStorage \n"); for (auto part: partitions){ partitionIndex++; @@ -107,7 +107,7 @@ void setup(){ thisPartition.begin(); Folder partitionRootFolder = thisPartition.getRootFolder(); - Serial.println(partitionRootFolder.getPathAsString()); + Arduino_UnifiedStorage::debugPrint(partitionRootFolder.getPathAsString()); if(createFakeFiles){ addSomeFakeFiles(&partitionRootFolder); @@ -119,7 +119,7 @@ void setup(){ thumbDrive.unmount(); - Serial.println("DONE, you can restart the board now"); + Arduino_UnifiedStorage::debugPrint("DONE, you can restart the board now"); } } diff --git a/examples/Logger/Logger.ino b/examples/Logger/Logger.ino index 996c3bd..eea0aa6 100644 --- a/examples/Logger/Logger.ino +++ b/examples/Logger/Logger.ino @@ -52,13 +52,13 @@ bool backingUP = false; void connectionCallback(){ usbAvailable = true; usbStorage.removeOnConnectCallback(); - Serial.println("USB drive connected."); + Arduino_UnifiedStorage::debugPrint("USB drive connected."); } void disconnectionCallback(){ usbAvailable = false; usbStorage.onConnect(connectionCallback); - Serial.println("USB drive disconnected."); + Arduino_UnifiedStorage::debugPrint("USB drive disconnected."); } // Function to run a given method periodically void runPeriodically(void (*method)(), unsigned long interval, unsigned long* variable) { @@ -178,7 +178,7 @@ void setup() { #endif // toggle this to enable debugging output - Arduino_UnifiedStorage::debuggingModeEnabled = false; + Arduino_UnifiedStorage::debuggingModeEnabled = true; usbStorage.onConnect(connectionCallback); usbStorage.onDisconnect(disconnectionCallback); diff --git a/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino b/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino index b74c8d0..fe34794 100644 --- a/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino +++ b/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino @@ -51,20 +51,20 @@ 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()); } } @@ -79,10 +79,10 @@ void setup() { #endif // toggle this to enable debugging output - Arduino_UnifiedStorage::debuggingModeEnabled = false; + Arduino_UnifiedStorage::debuggingModeEnabled = true; if(!storage.begin()){ - Serial.println("Error mounting storage device."); + Arduino_UnifiedStorage::debugPrint("Error mounting storage device."); } // Create a root directory in storage device @@ -104,7 +104,7 @@ void setup() { file3.write("This is file 3."); // Read data from the files using seek and available - Serial.println("Reading data from files using seek and available:"); + Arduino_UnifiedStorage::debugPrint("Reading data from files using seek and available:"); // Close and open files in reading mode file1.changeMode(FileMode::READ); @@ -116,25 +116,25 @@ void setup() { file1.seek(0); // Move the file pointer to the beginning while (file1.available()) { char data = file1.read(); - Serial.write(data); + Arduino_UnifiedStorage::debugPrint(String(data)); } - Serial.println(); + Arduino_UnifiedStorage::debugPrint("\n"); // Read data from file2 file2.seek(0); // Move the file pointer to the beginning while (file2.available()) { char data = file2.read(); - Serial.print(data); + Arduino_UnifiedStorage::debugPrint(String(data)); } - Serial.println(); + Arduino_UnifiedStorage::debugPrint("\n"); // Read data from file3 file3.seek(0); // Move the file pointer to the beginning while (file3.available()) { char data = file3.read(); - Serial.print(data); + Arduino_UnifiedStorage::debugPrint(String(data)); } - Serial.println(); + Arduino_UnifiedStorage::debugPrint("\n"); printFolderContents(storage.getRootFolder()); } From bb947a7f423d3a2d52e1fc600630ca759ba6df85 Mon Sep 17 00:00:00 2001 From: Bogdan Ivanus Date: Tue, 12 Aug 2025 15:19:03 +0300 Subject: [PATCH 4/4] AE-593: Changed call to "Arduino_UnifiedStorage::debugPrint" into "Arduino_UnifiedStorage::testPrint" which allows correct print outputs on all boards (including the Opta) without the clutter of the extra debug information. --- .../BackupInternalPartitions.ino | 22 ++++----- examples/Logger/Logger.ino | 34 +++++++------- .../SimpleStorageWriteRead.ino | 46 +++++++++++-------- 3 files changed, 56 insertions(+), 46 deletions(-) diff --git a/examples/BackupInternalPartitions/BackupInternalPartitions.ino b/examples/BackupInternalPartitions/BackupInternalPartitions.ino index e7702b6..e3f132d 100644 --- a/examples/BackupInternalPartitions/BackupInternalPartitions.ino +++ b/examples/BackupInternalPartitions/BackupInternalPartitions.ino @@ -39,11 +39,11 @@ volatile boolean connected = false; USBStorage thumbDrive; void addSomeFakeFiles(Folder * folder){ - Arduino_UnifiedStorage::debugPrint("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); - Arduino_UnifiedStorage::debugPrint("\t * " + thisFile.getPathAsString()); + Arduino_UnifiedStorage::testPrint("\t * " + thisFile.getPathAsString()); thisFile.write("writing stuff to the file"); thisFile.close(); } @@ -52,7 +52,7 @@ 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); - Arduino_UnifiedStorage::debugPrint("\t * " + thisFile.getPathAsString()); + Arduino_UnifiedStorage::testPrint("\t * " + thisFile.getPathAsString()); thisFile.write("writing stuff to the file"); thisFile.close(); } @@ -60,12 +60,12 @@ void addSomeFakeFiles(Folder * folder){ void move(Folder * source, Folder * dest){ for(Folder f: source -> getFolders()){ - Arduino_UnifiedStorage::debugPrint("Copying folder :" + String(f.getPathAsString())); + Arduino_UnifiedStorage::testPrint("Copying folder :" + String(f.getPathAsString())); f.moveTo(*dest); } for(UFile f: source -> getFiles()){ - Arduino_UnifiedStorage::debugPrint("Copying file :" + String(f.getPathAsString())); + Arduino_UnifiedStorage::testPrint("Copying file :" + String(f.getPathAsString())); f.moveTo(*dest); } } @@ -82,21 +82,21 @@ void setup(){ #endif // toggle this to enable debugging output - Arduino_UnifiedStorage::debuggingModeEnabled = true; + Arduino_UnifiedStorage::debuggingModeEnabled = false; bool thumbMounted = thumbDrive.begin(FS_FAT); if(thumbMounted){ - Arduino_UnifiedStorage::debugPrint("USB Thumb Drive has been mounted"); + Arduino_UnifiedStorage::testPrint("USB Thumb Drive has been mounted"); Folder thumbRoot = thumbDrive.getRootFolder(); String folderName = "InternalBackup_" + String(millis()); - Arduino_UnifiedStorage::debugPrint(folderName); + Arduino_UnifiedStorage::testPrint(folderName); Folder backupFolder = thumbRoot.createSubfolder(folderName); int partitionIndex = 0; std::vector partitions = InternalStorage::readPartitions(); - Arduino_UnifiedStorage::debugPrint("Found " + String(partitions.size()) + " partitions on internalStorage \n"); + Arduino_UnifiedStorage::testPrint("Found " + String(partitions.size()) + " partitions on internalStorage \n"); for (auto part: partitions){ partitionIndex++; @@ -107,7 +107,7 @@ void setup(){ thisPartition.begin(); Folder partitionRootFolder = thisPartition.getRootFolder(); - Arduino_UnifiedStorage::debugPrint(partitionRootFolder.getPathAsString()); + Arduino_UnifiedStorage::testPrint(partitionRootFolder.getPathAsString()); if(createFakeFiles){ addSomeFakeFiles(&partitionRootFolder); @@ -119,7 +119,7 @@ void setup(){ thumbDrive.unmount(); - Arduino_UnifiedStorage::debugPrint("DONE, you can restart the board now"); + Arduino_UnifiedStorage::testPrint("DONE, you can restart the board now"); } } diff --git a/examples/Logger/Logger.ino b/examples/Logger/Logger.ino index eea0aa6..39d17fd 100644 --- a/examples/Logger/Logger.ino +++ b/examples/Logger/Logger.ino @@ -52,13 +52,13 @@ bool backingUP = false; void connectionCallback(){ usbAvailable = true; usbStorage.removeOnConnectCallback(); - Arduino_UnifiedStorage::debugPrint("USB drive connected."); + Arduino_UnifiedStorage::testPrint("USB drive connected."); } void disconnectionCallback(){ usbAvailable = false; usbStorage.onConnect(connectionCallback); - Arduino_UnifiedStorage::debugPrint("USB drive disconnected."); + Arduino_UnifiedStorage::testPrint("USB drive disconnected."); } // Function to run a given method periodically void runPeriodically(void (*method)(), unsigned long interval, unsigned long* variable) { @@ -97,10 +97,10 @@ void performUpdate() { backingUP = true; unsigned lastUpdateBytes = lastUpdateFile.readAsString().toInt(); // Read the last update size from the file - Arduino_UnifiedStorage::debugPrint("Last update bytes: " + String(lastUpdateBytes)); + Arduino_UnifiedStorage::testPrint("Last update bytes: " + String(lastUpdateBytes)); if (lastUpdateBytes >= bytesWritten) { - Arduino_UnifiedStorage::debugPrint("No new data to copy. "); + Arduino_UnifiedStorage::testPrint("No new data to copy. "); backupFile.close(); lastUpdateFile.close(); backingUP = false; @@ -109,14 +109,14 @@ void performUpdate() { logFile.seek(lastUpdateBytes); // Move the file pointer to the last update position unsigned long totalBytesToMove = bytesWritten - lastUpdateBytes; - Arduino_UnifiedStorage::debugPrint("New update bytes: " + String(totalBytesToMove)); + Arduino_UnifiedStorage::testPrint("New update bytes: " + String(totalBytesToMove)); uint8_t* buffer = new uint8_t[totalBytesToMove]; size_t bytesRead = logFile.read(buffer, totalBytesToMove); size_t bytesMoved = backupFile.write(buffer, bytesRead); // Only write the bytes that haven't been backed up yet - Arduino_UnifiedStorage::debugPrint("Successfully copied " + String(bytesMoved) + " new bytes. "); + Arduino_UnifiedStorage::testPrint("Successfully copied " + String(bytesMoved) + " new bytes. "); lastUpdateFile.changeMode(FileMode::WRITE); // Open the last update file in write mode lastUpdateFile.write(String(lastUpdateBytes + bytesMoved)); // Update the last update size @@ -137,32 +137,32 @@ void performUpdate() { void backupToUSB() { if(usbAvailable && !usbIntialized){ usbStorage.begin(); - Arduino_UnifiedStorage::debugPrint("First drive insertion, creating folders... "); + Arduino_UnifiedStorage::testPrint("First drive insertion, creating folders... "); Folder usbRoot = usbStorage.getRootFolder(); String folderName = "LoggerBackup" + String(random(9999)); backupFolder = usbRoot.createSubfolder(folderName); - Arduino_UnifiedStorage::debugPrint("Successfully created backup folder: " + backupFolder.getPathAsString()); + Arduino_UnifiedStorage::testPrint("Successfully created backup folder: " + backupFolder.getPathAsString()); usbStorage.unmount(); usbIntialized = true; } else if(usbAvailable && usbIntialized) { - Arduino_UnifiedStorage::debugPrint("USB Mass storage is available "); + Arduino_UnifiedStorage::testPrint("USB Mass storage is available "); delay(100); if (!usbStorage.isMounted()) { - Arduino_UnifiedStorage::debugPrint("Mounting USB Mass Storage "); + Arduino_UnifiedStorage::testPrint("Mounting USB Mass Storage "); digitalWrite(USB_MOUNTED_LED, LOW); if(usbStorage.begin()){ performUpdate(); } } else if (usbStorage.isMounted()) { - Arduino_UnifiedStorage::debugPrint("USB Mass storage is connected, performing update "); + Arduino_UnifiedStorage::testPrint("USB Mass storage is connected, performing update "); performUpdate(); } } else { - Arduino_UnifiedStorage::debugPrint("USB Mass storage is not available "); + Arduino_UnifiedStorage::testPrint("USB Mass storage is not available "); } } @@ -178,21 +178,21 @@ void setup() { #endif // toggle this to enable debugging output - Arduino_UnifiedStorage::debuggingModeEnabled = true; + Arduino_UnifiedStorage::debuggingModeEnabled = false; usbStorage.onConnect(connectionCallback); usbStorage.onDisconnect(disconnectionCallback); pinMode(USB_MOUNTED_LED, OUTPUT); - Arduino_UnifiedStorage::debugPrint("Formatting internal storage... "); + Arduino_UnifiedStorage::testPrint("Formatting internal storage... "); int formatted = internalStorage.format(FS_LITTLEFS); - Arduino_UnifiedStorage::debugPrint("QSPI Format status: " + String(formatted)); + Arduino_UnifiedStorage::testPrint("QSPI Format status: " + String(formatted)); if (!internalStorage.begin()) { - Arduino_UnifiedStorage::debugPrint("Failed to initialize internal storage "); + Arduino_UnifiedStorage::testPrint("Failed to initialize internal storage "); return; } else { - Arduino_UnifiedStorage::debugPrint("Initialized storage "); + Arduino_UnifiedStorage::testPrint("Initialized storage "); } } diff --git a/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino b/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino index fe34794..a885b8c 100644 --- a/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino +++ b/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino @@ -51,25 +51,28 @@ void printFolderContents(Folder dir, int indentation = 0) { // Print directories for (Folder subdir : directories) { for (int i = 0; i < indentation; i++) { - Arduino_UnifiedStorage::debugPrint(" "); + Arduino_UnifiedStorage::testPrint(" "); } - Arduino_UnifiedStorage::debugPrint("[D] "); - Arduino_UnifiedStorage::debugPrint(subdir.getPath()); + Arduino_UnifiedStorage::testPrint("[D] "); + Arduino_UnifiedStorage::testPrint(subdir.getPath()); printFolderContents(subdir, indentation + 1); } // Print files for (UFile file : files) { for (int i = 0; i < indentation; i++) { - Arduino_UnifiedStorage::debugPrint(" "); + Arduino_UnifiedStorage::testPrint(" "); } - Arduino_UnifiedStorage::debugPrint("[F] "); - Arduino_UnifiedStorage::debugPrint(file.getPath()); + Arduino_UnifiedStorage::testPrint("[F] "); + Arduino_UnifiedStorage::testPrint(file.getPath()); } } void setup() { + uint8_t index = 0u; + char data[20]; + // 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. #if (defined(ARDUINO_OPTA)) && (defined(USE_USB_STORAGE) && (USE_USB_STORAGE == true)) beginRS485(115200); @@ -79,10 +82,10 @@ void setup() { #endif // toggle this to enable debugging output - Arduino_UnifiedStorage::debuggingModeEnabled = true; + Arduino_UnifiedStorage::debuggingModeEnabled = false; if(!storage.begin()){ - Arduino_UnifiedStorage::debugPrint("Error mounting storage device."); + Arduino_UnifiedStorage::testPrint("Error mounting storage device."); } // Create a root directory in storage device @@ -104,7 +107,8 @@ void setup() { file3.write("This is file 3."); // Read data from the files using seek and available - Arduino_UnifiedStorage::debugPrint("Reading data from files using seek and available:"); + Arduino_UnifiedStorage::testPrint("Reading data from files using seek and available:"); + Arduino_UnifiedStorage::testPrint("\n\r"); // Close and open files in reading mode file1.changeMode(FileMode::READ); @@ -114,27 +118,33 @@ void setup() { // Read data from file1 file1.seek(0); // Move the file pointer to the beginning + //memset(data, 0u, sizeof(data)); + std::fill(std::begin(data), std::end(data), 0u); while (file1.available()) { - char data = file1.read(); - Arduino_UnifiedStorage::debugPrint(String(data)); + data[index++] = file1.read(); } - Arduino_UnifiedStorage::debugPrint("\n"); + Arduino_UnifiedStorage::testPrint(data); // Read data from file2 file2.seek(0); // Move the file pointer to the beginning + index = 0u; + //memset(data, 0u, sizeof(data)); + std::fill(std::begin(data), std::end(data), 0u); while (file2.available()) { - char data = file2.read(); - Arduino_UnifiedStorage::debugPrint(String(data)); + data[index++] = file2.read(); } - Arduino_UnifiedStorage::debugPrint("\n"); + Arduino_UnifiedStorage::testPrint(data); // Read data from file3 file3.seek(0); // Move the file pointer to the beginning + index = 0u; + //memset(data, 0u, sizeof(data)); + std::fill(std::begin(data), std::end(data), 0u); while (file3.available()) { - char data = file3.read(); - Arduino_UnifiedStorage::debugPrint(String(data)); + data[index++] = file3.read(); } - Arduino_UnifiedStorage::debugPrint("\n"); + Arduino_UnifiedStorage::testPrint(data); + Arduino_UnifiedStorage::testPrint("\n\r"); printFolderContents(storage.getRootFolder()); }