Skip to content

Commit 3b5be8d

Browse files
cleaned up examples, fixed RS485, fixed spelling errors
1 parent 7f2e8e3 commit 3b5be8d

File tree

10 files changed

+108
-109
lines changed

10 files changed

+108
-109
lines changed

examples/AdvancedUSBInternalOperations/AdvancedUSBInternalOperations.ino

+8-4
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@
2222
https://github.com/arduino-libraries/Arduino_UnifiedStorage/blob/main/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino
2323
2424
*/
25-
26-
#define ARDUINO_UNIFIED_STORAGE_DEBUG
2725
#include "Arduino_UnifiedStorage.h"
2826

2927
// Two instances are made for the USB and internal storage respectively
30-
USBStorage usbStorage = USBStorage();
31-
InternalStorage internalStorage = InternalStorage();
28+
USBStorage usbStorage;
29+
InternalStorage internalStorage;
3230

3331

3432
// Helper function to prints the contents of a folder, including subdirectories (marked as "[D]") and files (marked as "[F]").
@@ -62,6 +60,12 @@ void setup() {
6260
Serial.begin(115200);
6361
while (!Serial);
6462

63+
// toggle this to to true to enable logging output
64+
Arduino_UnifiedStorage::loggingEnabled = false;
65+
66+
usbStorage = USBStorage();
67+
internalStorage = InternalStorage();
68+
6569
// Mount the USB storage
6670
if(usbStorage.begin()){
6771
Serial.println("USB storage mounted.");

examples/BackupInternalPartitions/BackupInternalPartitions.ino

+33-37
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ constexpr boolean createFakeFiles = true;
3636
boolean done = false;
3737
volatile boolean connected = false;
3838

39-
USBStorage thumbDrive = USBStorage();
39+
USBStorage thumbDrive;
4040

4141

4242
void addSomeFakeFiles(Folder * folder){
@@ -72,14 +72,6 @@ void move(Folder * source, Folder * dest){
7272
}
7373

7474

75-
void onConnected(){
76-
connected = true;
77-
}
78-
79-
void onDisconnected(){
80-
connected = false;
81-
}
82-
8375

8476

8577

@@ -89,46 +81,50 @@ void setup(){
8981
Serial.begin(115200);
9082
while(!Serial);
9183

92-
Serial.println("USB Thumb Drive has been inserted");
93-
bool thumbMounted = thumbDrive.begin(FS_FAT);
94-
if(thumbMounted){
95-
Serial.println("USB Thumb Drive has been mounted");
84+
// toggle this to to true to enable logging output
85+
Arduino_UnifiedStorage::loggingEnabled = false;
9686

97-
Folder thumbRoot = thumbDrive.getRootFolder();
98-
String folderName = "InternalBackup_" + String(millis());
99-
Serial.println(folderName);
100-
Folder backupFolder = thumbRoot.createSubfolder(folderName);
87+
thumbDrive = USBStorage();
10188

102-
int partitionIndex = 0;
89+
bool thumbMounted = thumbDrive.begin(FS_FAT);
90+
if(thumbMounted){
91+
Serial.println("USB Thumb Drive has been mounted");
10392

104-
std::vector<Partition> partitions = InternalStorage::readPartitions();
105-
Serial.println("Found " + String(partitions.size()) + " partitions on internalStorage \n");
93+
Folder thumbRoot = thumbDrive.getRootFolder();
94+
String folderName = "InternalBackup_" + String(millis());
95+
Serial.println(folderName);
96+
Folder backupFolder = thumbRoot.createSubfolder(folderName);
10697

107-
for (auto part: partitions){
108-
partitionIndex++;
109-
const char * partitionName = createPartitionName(partitionIndex);
110-
Folder thisPartitionBackupFolder = backupFolder.createSubfolder(partitionName);
98+
int partitionIndex = 0;
11199

112-
InternalStorage thisPartition = InternalStorage(partitionIndex, partitionName, part.fileSystemType);
113-
thisPartition.begin();
100+
std::vector<Partition> partitions = InternalStorage::readPartitions();
101+
Serial.println("Found " + String(partitions.size()) + " partitions on internalStorage \n");
114102

115-
Folder partitionRootFolder = thisPartition.getRootFolder();
116-
Serial.println(partitionRootFolder.getPathAsString());
103+
for (auto part: partitions){
104+
partitionIndex++;
105+
const char * partitionName = createPartitionName(partitionIndex);
106+
Folder thisPartitionBackupFolder = backupFolder.createSubfolder(partitionName);
117107

118-
if(createFakeFiles){
119-
addSomeFakeFiles(&partitionRootFolder);
120-
}
108+
InternalStorage thisPartition = InternalStorage(partitionIndex, partitionName, part.fileSystemType);
109+
thisPartition.begin();
121110

122-
move(&partitionRootFolder, &thisPartitionBackupFolder);
123-
thisPartition.unmount();
124-
}
111+
Folder partitionRootFolder = thisPartition.getRootFolder();
112+
Serial.println(partitionRootFolder.getPathAsString());
125113

126-
thumbDrive.unmount();
127-
114+
if(createFakeFiles){
115+
addSomeFakeFiles(&partitionRootFolder);
116+
}
128117

129-
Serial.println("DONE, you can restart the board now");
118+
move(&partitionRootFolder, &thisPartitionBackupFolder);
119+
thisPartition.unmount();
130120
}
131121

122+
thumbDrive.unmount();
123+
124+
125+
Serial.println("DONE, you can restart the board now");
126+
}
127+
132128

133129
}
134130

examples/Callbacks/Callbacks.ino

-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515

1616

17-
1817
#include "Arduino_UnifiedStorage.h"
1918

2019
#if defined(ARDUINO_PORTENTA_H7_M7)
@@ -42,5 +41,4 @@ void setup(){
4241
}
4342

4443
void loop(){
45-
4644
}

examples/InternalStoragePartitioning/InternalStoragePartitioning.ino

+14-3
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@
3636
#include <vector>
3737

3838
// Create a vector of partitions with one partition of 16MB using LittleFS
39-
std::vector<Partition> partitioningScheme = {{2048, FS_FAT}, {6144, FS_FAT}, {8192, FS_LITTLEFS}};
40-
39+
std::vector<Partition> partitioningScheme = {
40+
{1024, FS_FAT}, // 1 MB for certificates
41+
{5120, FS_FAT}, // 5 MB for OTA firmware updates
42+
{8192, FS_LITTLEFS} // 8 MB for user data
43+
};
4144

4245
// Function to test writing to a file in the specified storage partition
4346
void testWriting(Arduino_UnifiedStorage *storage) {
@@ -95,9 +98,17 @@ void setup() {
9598
Serial.begin(115200);
9699
while (!Serial);
97100

101+
Arduino_UnifiedStorage::loggingEnabled = true;
102+
98103
// Partition the storage with the specified partitions
99-
InternalStorage::partition(partitioningScheme);
104+
bool partitioned = InternalStorage::partition(partitioningScheme);
100105

106+
if(partitioned){
107+
Serial.println("Succesfully partitioned the storage");
108+
} else {
109+
Serial.println("Failed to partition the storage, partition scheme probably invalid");
110+
Serial.println("Turn on logging to see more details");
111+
}
101112
// Test all partitions
102113
testAllPartitions(partitioningScheme);
103114

examples/Logger/Logger.ino

+9-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535
#endif
3636

3737

38-
InternalStorage internalStorage = InternalStorage();
39-
USBStorage usbStorage = USBStorage();
38+
InternalStorage internalStorage;
39+
USBStorage usbStorage;
40+
4041
Folder backupFolder = Folder();
4142
bool usbIntialized = false;
4243
std::vector<String> sensorDataBuffer;
@@ -171,7 +172,6 @@ void backupToUSB() {
171172

172173

173174
void setup() {
174-
//beginRS485(baudrate);
175175
randomSeed(analogRead(A0));
176176

177177
#if !defined(ARDUINO_OPTA)
@@ -181,6 +181,12 @@ void setup() {
181181
beginRS485(115200);
182182
#endif
183183

184+
// toggle this to to true to enable logging output
185+
Arduino_UnifiedStorage::loggingEnabled = false;
186+
187+
usbStorage = USBStorage();
188+
internalStorage = InternalStorage();
189+
184190
usbStorage.onConnect(connectionCallback);
185191
usbStorage.onDisconnect(disconnectionCallback);
186192

examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino

+3-4
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,15 @@ void printFolderContents(Folder dir, int indentation = 0) {
6464
// Uncomment one of the three lines below to select between SD card, USB or internal storage
6565
//SDStorage unifiedStorage = SDStorage(); // Create an instance for interacting with SD card storage
6666
//USBStorage unifiedStorage = USBStorage() // Create an instance for interacting with USB storage
67-
InternalStorage internalStorage = InternalStorage();
67+
InternalStorage internalStorage;
6868

6969

7070
void setup() {
71-
72-
Arduino_UnifiedStorage::loggingEnabled = true;
73-
7471
Serial.begin(115200);
7572
while (!Serial);
7673

74+
// toggle this to to true to enable logging output
75+
Arduino_UnifiedStorage::loggingEnabled = false;
7776

7877

7978
internalStorage = InternalStorage();

src/Arduino_UnifiedStorage.cpp

+36-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
11
#include "Arduino_UnifiedStorage.h"
22

3+
#if !defined(HAS_SERIAL) && defined(HAS_RS485)
4+
#include <ArduinoRS485.h>
5+
6+
7+
[[gnu::unused]] static void beginRS485(const int baudrate){
8+
const auto bitduration { 1.f / baudrate };
9+
const auto wordlen { 9.6f }; // OR 10.0f depending on the channel configuration
10+
const auto preDelayBR { bitduration * wordlen * 3.5f * 1e6 };
11+
const auto postDelayBR { bitduration * wordlen * 3.5f * 1e6 };
12+
13+
RS485.begin(baudrate);
14+
RS485.setDelays(preDelayBR, postDelayBR);
15+
RS485.noReceive();
16+
}
17+
18+
[[gnu::unused]] static void debugPrintRS485(String s){
19+
static bool rs485Initialized = false;
20+
if (!rs485Initialized) {
21+
beginRS485(115200);
22+
rs485Initialized = true;
23+
}
24+
RS485.beginTransmission();
25+
RS485.write(s.c_str(), strlen(s.c_str()));
26+
RS485.write('\n');
27+
RS485.endTransmission();
28+
}
29+
30+
#endif
331

432
bool Arduino_UnifiedStorage::loggingEnabled = false;
533

634
void Arduino_UnifiedStorage::debugPrint(String message){
7-
if(Arduino_UnifiedStorage::loggingEnabled){
8-
Serial.println(message);
9-
}
10-
}
35+
#if defined(HAS_SERIAL)
36+
if(Serial){
37+
Serial.println(message);
38+
}
39+
#elif defined(HAS_RS485) && !defined(HAS_SERIAL)
40+
debugPrintRS485(message);
41+
#endif
42+
}

src/Folder.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ bool Folder::rename(const char* newDirname) {
8484
if (result == 0) {
8585
// Update the internal directory path
8686
this->path = newPath;
87-
Arduino_UnifiedStorage::debugPrint("[Folder][rename][INFO] Succesfully renamed folder: " + String(this->path.c_str()));
87+
Arduino_UnifiedStorage::debugPrint("[Folder][rename][INFO] Successfully renamed folder: " + String(this->path.c_str()));
8888
return true;
8989
} else {
9090
// Error occurred while renaming the directory

src/Partitioning.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ bool Partitioning::eraseMBRSector(BlockDeviceType * blockDevice)
88
memset(buffer, 0xFF, sizeof(buffer));
99

1010
if(blockDevice-> program(buffer, 0, sizeof(buffer)) == 0){
11-
Arduino_UnifiedStorage::debugPrint("[Partitioning][INFO] MBR Sector Erased");
11+
Arduino_UnifiedStorage::debugPrint("[Partitioning][eraseMBRSector][INFO] MBR Sector Erased");
1212
if(blockDevice -> erase(0, mbrBlockSize) == 0){
13-
Arduino_UnifiedStorage::debugPrint("Partitioing][INFO] MBR Sector Erased");
13+
Arduino_UnifiedStorage::debugPrint("[Partitioning][eraseMBRSector][INFO] MBR Sector Erased");
1414
return true;
1515
} else {
1616
return false;
@@ -34,10 +34,10 @@ bool Partitioning::isPartitionSchemeValid(BlockDeviceType * blockDevice, std::ve
3434
}
3535

3636
if(totalSize <= driveSize && partitions.size() <= maximumMBRPartitions){
37-
Arduino_UnifiedStorage::debugPrint("[Partitioning][INFO] Partitioning Scheme is Valid");
37+
Arduino_UnifiedStorage::debugPrint("[Partitioning][isPartitioningSchemeValid][INFO] Partitioning Scheme is Valid");
3838
return true;
3939
} else {
40-
Arduino_UnifiedStorage::debugPrint("[Partitioning][INFO] Partitioning Scheme is Invalid");
40+
Arduino_UnifiedStorage::debugPrint("[Partitioning][isPartitioningSchemeValid][INFO] Partitioning Scheme is Invalid");
4141
return false;
4242
}
4343
}

src/Utils.h

-47
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,6 @@
1515
}
1616

1717

18-
#if !defined(HAS_SERIAL) && defined(HAS_RS485)
19-
#include <ArduinoRS485.h>
20-
21-
22-
[[gnu::unused]] static void beginRS485(const int baudrate){
23-
const auto bitduration { 1.f / baudrate };
24-
const auto wordlen { 9.6f }; // OR 10.0f depending on the channel configuration
25-
const auto preDelayBR { bitduration * wordlen * 3.5f * 1e6 };
26-
const auto postDelayBR { bitduration * wordlen * 3.5f * 1e6 };
27-
28-
RS485.begin(baudrate);
29-
RS485.setDelays(preDelayBR, postDelayBR);
30-
RS485.noReceive();
31-
}
32-
33-
[[gnu::unused]] static void debugPrintRS485(String s){
34-
static bool rs485Initialized = false;
35-
if (!rs485Initialized) {
36-
beginRS485(115200);
37-
rs485Initialized = true;
38-
}
39-
RS485.beginTransmission();
40-
RS485.write(s.c_str(), strlen(s.c_str()));
41-
RS485.write('\n');
42-
RS485.endTransmission();
43-
}
44-
45-
#endif
46-
47-
[[gnu::unused]] static void debugPrint(String s){
48-
/*
49-
50-
if (Arduino_UnifiedStorage::loggingEnabled == true){
51-
#if defined(HAS_SERIAL)
52-
if(Serial){
53-
Serial.println(s);
54-
}
55-
#elif defined(HAS_RS485) && !defined(HAS_SERIAL)
56-
debugPrintRS485(s);
57-
#endif
58-
59-
}
60-
61-
*/
62-
}
63-
64-
6518
[[gnu::unused]] static const char* createPartitionName(int number) {
6619
if (number < 1 || number > 26) {
6720
// Handle out-of-range numbers or errors as needed

0 commit comments

Comments
 (0)