Skip to content

Commit 7fcd789

Browse files
Merge pull request #3 from arduino-libraries/aliphys/exampleRevision
[AE-158] Simple and Advanced sketch revision
2 parents d6dd6eb + 6d7f36c commit 7fcd789

File tree

2 files changed

+51
-37
lines changed

2 files changed

+51
-37
lines changed

examples/advanced/advanced.ino renamed to examples/AdvancedUSBInternalOperations/AdvancedUSBInternalOperations.ino

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
/*
2-
This example demonstrates the usage of the "Arduino_UnifiedStorage" library with USB storage and internal storage.
3-
The code includes the necessary library and defines instances of the "USBStorage" and "InternalStorage" classes.
2+
AdvancedUSBInternalOperations
43
5-
In the setup function, the code initializes the serial communication and mounts the USB storage and internal storage.
6-
It also reformats the internal storage to ensure a clean file system.
7-
Then, it creates a root directory in the internal storage and creates a subdirectory and a file inside it
4+
Demonstrates advanced usage of the "Arduino_UnifiedStorage" library with USB & internal storage, including file operations.
5+
Creates, copies, and moves files between storage types, and prints folder contents.
86
9-
The code writes some data to the file and demonstrates file operations.
10-
It copies the file from internal storage to USB storage and moves the subdirectory from internal storage to USB storage.
7+
In the setup function, the code initializes serial communication, mounts both USB & internal storage and
8+
reformats the internal storage for a clean file system. Then, it creates a root directory in the internal storage
9+
and creates a subdirectory with a file inside it containing the string "Hello World!".
10+
11+
Then, it copies the file from internal storage to USB storage and moves the subdirectory from internal storage to USB storage.
12+
13+
After the file operations, the code prints the contents of both the USB storage and the internal storage.
14+
It recursively prints the directories (marked as "[D]") and files (marked as "[F]") using the "printFolderContents" function.
15+
16+
Created 28th July 2023
17+
By Cristian Dragomir
18+
19+
Modified 24th August 2023
20+
By Ali Jahangiri
21+
22+
https://github.com/arduino-libraries/Arduino_UnifiedStorage/blob/main/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino
1123
12-
After the file operations, the code prints the contents of both the USB storage and the internal storage.
13-
It recursively prints the directories (marked as "[D]") and files (marked as "[F]") using the "printFolderContents" function.
1424
*/
1525

1626
#include "Arduino_UnifiedStorage.h"
1727

18-
28+
// Two instances are made for the USB and internal storage respectively
1929
USBStorage usbStorage = USBStorage();
2030
InternalStorage internalStorage = InternalStorage();
2131

22-
32+
// Helper function to prints the contents of a folder, including subdirectories (marked as "[D]") and files (marked as "[F]").
2333
void printFolderContents(Folder dir, int indentation = 0) {
2434
std::vector<Folder> directories = dir.getFolders();
2535
std::vector<UFile> files = dir.getFiles();
@@ -57,25 +67,23 @@ void setup() {
5767
Serial.println(errno);
5868
}
5969

60-
6170
// Mount the internal storage
62-
// Serial.println("Reformatting internal storage to make sure we have a clean FS");
63-
// internalStorage.format();
71+
// Serial.println("Reformatting internal storage to make sure we have a clean FS");
72+
// internalStorage.format();
6473
if(internalStorage.begin()){
6574
Serial.println("Internal storage mounted.");
6675
} else {
6776
Serial.println(errno);
6877
}
6978

70-
7179
// Create a root directory in the internal storage
7280
Folder root = internalStorage.getRootFolder();
7381

74-
// Create a subdirectory and a file inside the root directory
82+
// Create a subdirectory and a file (file.txt) inside the root directory
7583
Folder subdir = root.createSubfolder("subdir");
7684
UFile file = root.createFile("file.txt", FileMode::WRITE);
7785

78-
// Write some data to the file
86+
// Write "Hello World!" inside file.txt
7987
file.write("Hello, world!");
8088
file.close();
8189

@@ -97,11 +105,11 @@ void setup() {
97105
Serial.println(getErrno());
98106
}
99107

100-
// Print the content of the USB storage
108+
// Print contents of the USB storage
101109
//Serial.println("USB storage contents:");
102-
// printFolderContents(usbStorage.getRootFolder());
110+
//printFolderContents(usbStorage.getRootFolder());
103111

104-
// Print the content of the internal storage
112+
// Print contents of the internal storage
105113
Serial.println("Internal storage contents:");
106114
printFolderContents(internalStorage.getRootFolder());
107115
}

examples/simple/simple.ino renamed to examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
11
/*
2-
This examples demonstrates the usage of the "Arduino_UnifiedStorage" library,
3-
which allows the program to easily switch between different storage mediums.
2+
SimpleStorageWriteRead
43
5-
By uncommenting the appropriate lines, you can choose to use either an SD card,
6-
a USB storage device, or internal storage as the storage medium.
4+
Demonstrates basic usage of the "Arduino_UnifiedStorage" library to write and read data to storage.
5+
Supports SD card, USB storage, and internal storage (default, uncomment to choose).
76
8-
The example code is set up to use an SD card by default.
7+
In the setup function, the code initializes serial communication, mounts the storage medium,
8+
creates a root directory with three subdirectories, and writes data to three files in each subdirectory.
99
10-
In the setup function, the code initializes the serial communication and checks if the storage medium is successfully mounted.
11-
It then creates a root directory and three subdirectories within it.
12-
After creating the subdirectories, the code creates three files inside each subdirectory and writes data to them.
10+
Following this, the code showcases reading data from files by using "seek" and "available" methods,
11+
switching file modes to read, resetting file pointers to the start,
12+
and printing the read data to the serial monitor using a while loop.
13+
14+
Created 28th July 2023
15+
By Cristian Dragomir
16+
17+
Modified 24th August 2023
18+
By Ali Jahangiri
19+
20+
https://github.com/arduino-libraries/Arduino_UnifiedStorage/blob/main/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino
1321
14-
Next, the code demonstrates how to read data from the files using the "seek" and "available" methods.
15-
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.
16-
The read data is printed to the serial monitor.
1722
*/
1823

1924
#include "Arduino_UnifiedStorage.h"
2025

21-
SDStorage unifiedStorage = SDStorage(); // or
22-
//USBStorage unifiedStorage = USBStorage() // or
23-
//InternalStorage unifiedStorage = InternalStorage();
26+
// Uncomment one of the three lines below to select between SD card, USB or internal storage
27+
//SDStorage unifiedStorage = SDStorage(); // Create an instance for interacting with SD card storage
28+
//USBStorage unifiedStorage = USBStorage() // Create an instance for interacting with USB storage
29+
InternalStorage unifiedStorage = InternalStorage(); // Create an instance for interacting with internal Flash storage (default)
2430

2531
void setup() {
2632
Serial.begin(115200);
2733
while (!Serial);
2834

2935
if(!unifiedStorage.begin()==0){
30-
Serial.println("error mounting SD Card");
36+
Serial.println("Error mounting storage device.");
3137
}
32-
// Create a root directory
3338

39+
// Create a root directory in storage device
3440
Folder root = unifiedStorage.getRootFolder();
3541

3642
// Create subdirectories inside the root directory
3743
Folder subdir1 = root.createSubfolder("subdir1");
3844
Folder subdir2 = root.createSubfolder("subdir2");
3945
Folder subdir3 = root.createSubfolder("subdir3");
4046

41-
// Create files inside the subdirectories
47+
// Create .txt files inside the subdirectories
4248
UFile file1 = subdir1.createFile("file1.txt", FileMode::WRITE);
4349
UFile file2 = subdir2.createFile("file2.txt", FileMode::WRITE);
4450
UFile file3 = subdir3.createFile("file3.txt", FileMode::WRITE);

0 commit comments

Comments
 (0)