Skip to content

Commit 772fa4d

Browse files
before stashing
1 parent 2e5de5b commit 772fa4d

File tree

9 files changed

+241
-60
lines changed

9 files changed

+241
-60
lines changed

examples/advanced/advanced.ino

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,22 @@ void setup() {
5151
while (!Serial);
5252

5353
// Mount the USB storage
54-
usbStorage.begin();
55-
Serial.println("USB storage mounted.");
54+
if(usbStorage.begin()){
55+
Serial.println("USB storage mounted.");
56+
} else {
57+
Serial.println(errno);
58+
}
59+
5660

5761
// Mount the internal storage
58-
Serial.println("Reformatting internal storage to make sure we have a clean FS");
59-
internalStorage.format();
62+
// Serial.println("Reformatting internal storage to make sure we have a clean FS");
63+
// internalStorage.format();
64+
if(internalStorage.begin()){
65+
Serial.println("Internal storage mounted.");
66+
} else {
67+
Serial.println(errno);
68+
}
6069

61-
internalStorage.begin();
62-
Serial.println("Internal storage mounted.");
6370

6471
// Create a root directory in the internal storage
6572
Folder root = internalStorage.getRootFolder();
@@ -70,6 +77,7 @@ void setup() {
7077

7178
// Write some data to the file
7279
file.write("Hello, world!");
80+
file.close();
7381

7482
// Copy the file from internal storage to USB storage
7583
bool success = file.copyTo(usbStorage.getRootFolder());
@@ -90,8 +98,8 @@ void setup() {
9098
}
9199

92100
// Print the content of the USB storage
93-
Serial.println("USB storage contents:");
94-
printFolderContents(usbStorage.getRootFolder());
101+
//Serial.println("USB storage contents:");
102+
// printFolderContents(usbStorage.getRootFolder());
95103

96104
// Print the content of the internal storage
97105
Serial.println("Internal storage contents:");

examples/logger/logger.ino

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
This example demonstrates the usage of the "Arduino_UnifiedStorage" library for logging and backing up data to USB storage in case a USB Mass Storage device is inserted.
3+
4+
The code defines two main functions: "logData" and "performUpdate".
5+
The "logData" function logs sensor data by reading an analog sensor and writing the data to the log file.
6+
7+
The "performUpdate" function performs the update process by:
8+
* reading the last update size from a file (number of bytes)
9+
* copying the new data from the log file to a backup file
10+
* and updating the last update size.
11+
12+
INSTRUCTIONS
13+
* Make sure the QSPI storage of your board is properly partitioned.
14+
* You can do that by flashing the QSPIFormat example that can be found in the STM32H747_System folder
15+
* Open the serial monitor and select answer with "Y" when this appears "Do you want to use partition scheme 1? Y/[n]"
16+
* Reboot the board
17+
* Connect a RS485-enabled device to see the debugging output.
18+
* This sketch will log data, and check if there is any USB MSD Device connected to the USB Port of the Opta.
19+
The USB device is mounted and unmounted after every update operation. The first status LED is on when the USB drive is mounted.
20+
So as long as the status LED is off you can safely remove the drive.
21+
The skecth will log to internal storage in the meantime, and wait for the USB drive to be inserted again.
22+
*/
23+
24+
#include "Arduino_UnifiedStorage.h"
25+
#include <vector>
26+
27+
28+
constexpr auto baudrate { 115200 };
29+
30+
#if defined(ARDUINO_PORTENTA_H7_M7)
31+
#define USB_MOUNTED_LED LED_BLUE
32+
#elif defined(ARDUINO_PORTENTA_C33)
33+
#define USB_MOUNTED_LED LEDB
34+
#endif
35+
36+
37+
InternalStorage internalStorage = InternalStorage();
38+
USBStorage usbStorage = USBStorage();
39+
std::vector<String> sensorDataBuffer;
40+
41+
unsigned long bytesWritten = 0;
42+
unsigned long lastLog = 0;
43+
unsigned long lastMove = 0;
44+
unsigned long lastBackup = 0;
45+
46+
47+
bool backingUP = false;
48+
49+
50+
// Function to run a given method periodically
51+
void runPeriodically(void (*method)(), unsigned long interval, unsigned long* variable) {
52+
unsigned long currentMillis = millis();
53+
54+
if (currentMillis - *variable >= interval) {
55+
*variable = currentMillis;
56+
method(); // Call the provided method
57+
}
58+
}
59+
60+
// Function to log sensor data
61+
void logDataToRAM() {
62+
int timeStamp = millis();
63+
int sensorReading = analogRead(A0);
64+
String line = String(timeStamp) + "," + String(sensorReading) + "\n";
65+
sensorDataBuffer.push_back(line);
66+
}
67+
68+
void moveDataToQSPI() {
69+
if(!backingUP){
70+
UFile _logFile = internalStorage.getRootFolder().createFile("log.txt", FileMode::APPEND);
71+
for (const auto& line : sensorDataBuffer) {
72+
bytesWritten += _logFile.write(line); // Write the log line to the file
73+
}
74+
_logFile.close();
75+
sensorDataBuffer.clear();
76+
}
77+
}
78+
79+
80+
void performUpdate() {
81+
Folder usbRoot = usbStorage.getRootFolder(); // Get the root folder of the USB storage
82+
UFile logFile = internalStorage.getRootFolder().createFile("log.txt", FileMode::READ);
83+
UFile backupFile = usbRoot.createFile("backup_file.txt", FileMode::APPEND); // Create or open the backup file
84+
UFile lastUpdateFile = usbRoot.createFile("diff.txt", FileMode::READ); // Create or open the last update file
85+
86+
backingUP = true;
87+
int lastUpdateBytes = lastUpdateFile.readAsString().toInt(); // Read the last update size from the file
88+
89+
Serial.print("Last update bytes: "); Serial.println(lastUpdateBytes);
90+
91+
if (lastUpdateBytes >= bytesWritten) {
92+
Serial.println("No new data to copy.");
93+
backupFile.close();
94+
lastUpdateFile.close();
95+
backingUP = false;
96+
return;
97+
}
98+
99+
logFile.seek(lastUpdateBytes); // Move the file pointer to the last update position
100+
unsigned long totalBytesToMove = bytesWritten - lastUpdateBytes;
101+
Serial.print("New update bytes: "); Serial.println(totalBytesToMove);
102+
103+
uint8_t buffer[totalBytesToMove];
104+
size_t bytesRead = logFile.read(buffer, totalBytesToMove);
105+
size_t bytesMoved = backupFile.write(buffer, bytesRead); // Only write the bytes that haven't been backed up yet
106+
107+
Serial.println("Successfully copied " + String(bytesMoved) + " new bytes.");
108+
109+
lastUpdateFile.changeMode(FileMode::WRITE); // Open the last update file in write mode
110+
lastUpdateFile.write(String(lastUpdateBytes + bytesMoved)); // Update the last update size
111+
112+
backupFile.close();
113+
logFile.close();
114+
lastUpdateFile.close();
115+
116+
Serial.println();
117+
usbStorage.unmount(); // Unmount the USB storage
118+
119+
digitalWrite(USB_MOUNTED_LED, HIGH);
120+
backingUP = false;
121+
}
122+
123+
124+
void disconnect(){
125+
126+
}
127+
// Function to backup data to USB storage
128+
void backupToUSB() {
129+
if (usbStorage.isAvailable()) {
130+
Serial.println("USB Mass storage is available");
131+
delay(100);
132+
if (!usbStorage.isConnected()) {
133+
134+
Serial.println("Mounting USB Mass Storage");
135+
digitalWrite(USB_MOUNTED_LED, LOW);
136+
if(usbStorage.begin()){
137+
performUpdate();
138+
}
139+
140+
141+
142+
} else if (usbStorage.isConnected()) {
143+
Serial.println("USB Mass storage is connected, performing update");
144+
performUpdate();
145+
146+
}
147+
} else {
148+
Serial.println("USB Mass storage is not available");
149+
}
150+
151+
152+
}
153+
154+
155+
void setup() {
156+
Serial.begin(115200);
157+
while (!Serial);
158+
pinMode(USB_MOUNTED_LED, OUTPUT);
159+
Serial.println("Formatting internal storage...");
160+
int formatted = internalStorage.format();
161+
Serial.print("QSPI Format status: "); Serial.println(formatted);
162+
163+
//configureRS485(baudrate);
164+
//Serial.println("RS485 goes brrr...");
165+
166+
if (!internalStorage.begin() == 0) {
167+
Serial.println("Failed to initialize internal storage");
168+
return;
169+
} else {
170+
Serial.println("Initialized storage");
171+
}
172+
173+
}
174+
175+
void loop() {
176+
usbStorage.checkConnection();
177+
runPeriodically(logDataToRAM, 100, &lastLog);
178+
runPeriodically(moveDataToQSPI, 1000, &lastMove);
179+
runPeriodically(backupToUSB, 10000, &lastBackup);
180+
}

examples/opta_logger/opta_logger.ino

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ INSTRUCTIONS
2424
#include "Arduino_UnifiedStorage.h"
2525
#include <vector>
2626

27+
#if defined(ARDUINO_PORTENTA_H7_M7)
2728
#define USB_MOUNTED_LED LED_BLUE
28-
29+
#elif defined(ARDUINO_PORTENTA_C33)
30+
#define USB_MOUNTED LED LEDB
31+
#endif
2932
constexpr auto baudrate { 115200 };
3033

3134

@@ -74,62 +77,50 @@ void moveDataToQSPI() {
7477
}
7578

7679

77-
78-
79-
// Function to perform the update process
8080
void performUpdate() {
8181
Folder usbRoot = usbStorage.getRootFolder(); // Get the root folder of the USB storage
8282
UFile logFile = internalStorage.getRootFolder().createFile("log.txt", FileMode::READ);
8383
UFile backupFile = usbRoot.createFile("backup_file.txt", FileMode::APPEND); // Create or open the backup file
8484
UFile lastUpdateFile = usbRoot.createFile("diff.txt", FileMode::READ); // Create or open the last update file
8585

8686
backingUP = true;
87-
Serial.println("Opening diff file");
8887
int lastUpdateBytes = lastUpdateFile.readAsString().toInt(); // Read the last update size from the file
8988

90-
Serial.println(lastUpdateBytes);
91-
89+
Serial.print("Last update bytes: "); Serial.println(lastUpdateBytes);
9290

93-
if (lastUpdateBytes > bytesWritten) {
94-
Serial.println("everytime");
95-
lastUpdateFile.changeMode(FileMode::WRITE); // Open the log file in write mode
96-
lastUpdateFile.write(String(0)); // Reset the log file by writing 0 as the last update size
97-
lastUpdateBytes = 0;
91+
if (lastUpdateBytes >= bytesWritten) {
92+
Serial.println("No new data to copy.");
93+
backupFile.close();
94+
lastUpdateFile.close();
95+
backingUP = false;
96+
return;
9897
}
9998

10099
logFile.seek(lastUpdateBytes); // Move the file pointer to the last update position
101-
unsigned long bytesMoved = 0;
102100
unsigned long totalBytesToMove = bytesWritten - lastUpdateBytes;
103-
Serial.println(totalBytesToMove);
104-
Serial.println("Ready to copy data");
105-
106-
while (logFile.available()) {
107-
int data = logFile.read(); // Read a byte from the log file
101+
Serial.print("New update bytes: "); Serial.println(totalBytesToMove);
108102

109-
if (data != -1) {
110-
backupFile.write(data); // Write the byte to the backup file
111-
bytesMoved++;
112-
}
113-
}
114-
Serial.println("Succesfully copied data");
103+
uint8_t buffer[totalBytesToMove];
104+
size_t bytesRead = logFile.read(buffer, totalBytesToMove);
105+
size_t bytesMoved = backupFile.write(buffer, bytesRead); // Only write the bytes that haven't been backed up yet
115106

116-
// Close the backup file
107+
Serial.println("Successfully copied " + String(bytesMoved) + " new bytes.");
117108

118109
lastUpdateFile.changeMode(FileMode::WRITE); // Open the last update file in write mode
119-
lastUpdateFile.write(String(bytesMoved)); // Write the updated last update size to the file
110+
lastUpdateFile.write(String(lastUpdateBytes + bytesMoved)); // Update the last update size
120111

121-
backupFile.close();
112+
backupFile.close();
122113
logFile.close();
123114
lastUpdateFile.close();
124-
delay(100);
125115

126-
Serial.println("Succesfully updated diff file");
116+
Serial.println();
127117
usbStorage.unmount(); // Unmount the USB storage
128118

129119
digitalWrite(USB_MOUNTED_LED, HIGH);
130120
backingUP = false;
131121
}
132122

123+
133124
void disconnect(){
134125

135126
}
@@ -157,9 +148,6 @@ void backupToUSB() {
157148
Serial.println("USB Mass storage is not available");
158149
}
159150

160-
if(usbStorage.isConnected() && !usbStorage.isAvailable()){
161-
Serial.println("we should never get here");
162-
}
163151

164152
}
165153

@@ -189,5 +177,4 @@ void loop() {
189177
runPeriodically(logDataToRAM, 100, &lastLog);
190178
runPeriodically(moveDataToQSPI, 1000, &lastMove);
191179
runPeriodically(backupToUSB, 10000, &lastBackup);
192-
193180
}

src/Arduino_UnifiedStorage.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class Arduino_UnifiedStorage {
5050
#endif
5151

5252

53-
extern Arduino_UnifiedStorage UnifiedStorage;
5453

5554
#endif
5655

src/Folder.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,30 @@ UFile Folder::createFile(String fileName, FileMode fmode) {
3838

3939
bool Folder::remove() {
4040
// Remove all files in the directory
41-
std::vector<UFile> files = this->getFiles();
42-
for (UFile file : files) {
43-
file.remove();
44-
}
41+
if(this->exists()){
42+
std::vector<UFile> files = this->getFiles();
43+
for (UFile file : files) {
44+
Serial.println(file.getPathString());
45+
file.remove();
4546

46-
// Remove all subfolders in the directory
47-
std::vector<Folder> folders = this->getFolders();
48-
for (Folder directory : folders) {
49-
directory.remove();
50-
}
47+
}
5148

52-
// Remove the current directory
53-
if (::remove(this->path.c_str()) == 0) {
54-
return true;
55-
} else {
56-
// Error occurred while removing the directory
57-
return false;
49+
// Remove all subfolders in the directory
50+
std::vector<Folder> folders = this->getFolders();
51+
for (Folder directory : folders) {
52+
Serial.println(directory.getPathString());
53+
directory.remove();
54+
}
55+
56+
// Remove the current directory
57+
if (::remove(this->path.c_str()) == 0) {
58+
return true;
59+
} else {
60+
// Error occurred while removing the directory
61+
return false;
62+
}
5863
}
64+
5965
}
6066

6167
bool Folder::rename(const char* newDirname) {

src/InternalStorage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include "Arduino_UnifiedStorage.h"
77

8-
class InternalStorage : public UnifiedStorage {
8+
class InternalStorage : public Arduino_UnifiedStorage {
99

1010

1111
public:

0 commit comments

Comments
 (0)