diff --git a/examples/advanced/advanced.ino b/examples/AdvancedUSBInternalOperations/AdvancedUSBInternalOperations.ino similarity index 56% rename from examples/advanced/advanced.ino rename to examples/AdvancedUSBInternalOperations/AdvancedUSBInternalOperations.ino index 00d2964..763ee3d 100644 --- a/examples/advanced/advanced.ino +++ b/examples/AdvancedUSBInternalOperations/AdvancedUSBInternalOperations.ino @@ -1,25 +1,35 @@ /* -This example demonstrates the usage of the "Arduino_UnifiedStorage" library with USB storage and internal storage. -The code includes the necessary library and defines instances of the "USBStorage" and "InternalStorage" classes. + AdvancedUSBInternalOperations -In the setup function, the code initializes the serial communication and mounts the USB storage and internal storage. -It also reformats the internal storage to ensure a clean file system. -Then, it creates a root directory in the internal storage and creates a subdirectory and a file inside it + 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. -The code writes some data to the file and demonstrates file operations. -It copies the file from internal storage to USB storage and moves the subdirectory from internal storage to USB storage. + 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!". + + Then, it copies the file from internal storage to USB storage and moves the subdirectory from internal storage to USB storage. + + After the file operations, the code prints the contents of both the USB storage and the internal storage. + It recursively prints the directories (marked as "[D]") and files (marked as "[F]") using the "printFolderContents" function. + + Created 28th July 2023 + By Cristian Dragomir + + Modified 24th August 2023 + By Ali Jahangiri + + https://github.com/arduino-libraries/Arduino_UnifiedStorage/blob/main/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino -After the file operations, the code prints the contents of both the USB storage and the internal storage. -It recursively prints the directories (marked as "[D]") and files (marked as "[F]") using the "printFolderContents" function. */ #include "Arduino_UnifiedStorage.h" - +// Two instances are made for the USB and internal storage respectively USBStorage usbStorage = USBStorage(); InternalStorage 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(); std::vector files = dir.getFiles(); @@ -57,25 +67,23 @@ void setup() { Serial.println(errno); } - // Mount the internal storage - // Serial.println("Reformatting internal storage to make sure we have a clean FS"); - // internalStorage.format(); + // Serial.println("Reformatting internal storage to make sure we have a clean FS"); + // internalStorage.format(); if(internalStorage.begin()){ Serial.println("Internal storage mounted."); } else { Serial.println(errno); } - // Create a root directory in the internal storage Folder root = internalStorage.getRootFolder(); - // Create a subdirectory and a file inside the root directory + // Create a subdirectory and a file (file.txt) inside the root directory Folder subdir = root.createSubfolder("subdir"); UFile file = root.createFile("file.txt", FileMode::WRITE); - // Write some data to the file + // Write "Hello World!" inside file.txt file.write("Hello, world!"); file.close(); @@ -97,11 +105,11 @@ void setup() { Serial.println(getErrno()); } - // Print the content of the USB storage + // Print contents of the USB storage //Serial.println("USB storage contents:"); - // printFolderContents(usbStorage.getRootFolder()); + //printFolderContents(usbStorage.getRootFolder()); - // Print the content of the internal storage + // Print contents of the internal storage Serial.println("Internal storage contents:"); printFolderContents(internalStorage.getRootFolder()); } diff --git a/examples/simple/simple.ino b/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino similarity index 52% rename from examples/simple/simple.ino rename to examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino index f9b1714..ee435d2 100644 --- a/examples/simple/simple.ino +++ b/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino @@ -1,36 +1,42 @@ /* -This examples demonstrates the usage of the "Arduino_UnifiedStorage" library, -which allows the program to easily switch between different storage mediums. + SimpleStorageWriteRead -By uncommenting the appropriate lines, you can choose to use either an SD card, -a USB storage device, or internal storage as the storage medium. + Demonstrates basic usage of the "Arduino_UnifiedStorage" library to write and read data to storage. + Supports SD card, USB storage, and internal storage (default, uncomment to choose). -The example code is set up to use an SD card by default. + In the setup function, the code initializes serial communication, mounts the storage medium, + creates a root directory with three subdirectories, and writes data to three files in each subdirectory. -In the setup function, the code initializes the serial communication and checks if the storage medium is successfully mounted. -It then creates a root directory and three subdirectories within it. -After creating the subdirectories, the code creates three files inside each subdirectory and writes data to them. + Following this, the code showcases reading data from files by using "seek" and "available" methods, + switching file modes to read, resetting file pointers to the start, + and printing the read data to the serial monitor using a while loop. + + Created 28th July 2023 + By Cristian Dragomir + + Modified 24th August 2023 + By Ali Jahangiri + + https://github.com/arduino-libraries/Arduino_UnifiedStorage/blob/main/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino -Next, the code demonstrates how to read data from the files using the "seek" and "available" methods. -It changes the mode of the files to read mode, moves the file pointers to the beginning, and reads the data from each file using a while loop. -The read data is printed to the serial monitor. */ #include "Arduino_UnifiedStorage.h" -SDStorage unifiedStorage = SDStorage(); // or -//USBStorage unifiedStorage = USBStorage() // or -//InternalStorage unifiedStorage = InternalStorage(); +// Uncomment one of the three lines below to select between SD card, USB or internal storage +//SDStorage unifiedStorage = SDStorage(); // Create an instance for interacting with SD card storage +//USBStorage unifiedStorage = USBStorage() // Create an instance for interacting with USB storage +InternalStorage unifiedStorage = InternalStorage(); // Create an instance for interacting with internal Flash storage (default) void setup() { Serial.begin(115200); while (!Serial); if(!unifiedStorage.begin()==0){ - Serial.println("error mounting SD Card"); + Serial.println("Error mounting storage device."); } - // Create a root directory + // Create a root directory in storage device Folder root = unifiedStorage.getRootFolder(); // Create subdirectories inside the root directory @@ -38,7 +44,7 @@ void setup() { Folder subdir2 = root.createSubfolder("subdir2"); Folder subdir3 = root.createSubfolder("subdir3"); - // Create files inside the subdirectories + // Create .txt files inside the subdirectories UFile file1 = subdir1.createFile("file1.txt", FileMode::WRITE); UFile file2 = subdir2.createFile("file2.txt", FileMode::WRITE); UFile file3 = subdir3.createFile("file3.txt", FileMode::WRITE);