Skip to content

Commit a5b5cbc

Browse files
authored
Merge pull request #18 from manchoz/opta_example
Add an example for Arduino Opta
2 parents 921ed19 + bad22f5 commit a5b5cbc

File tree

4 files changed

+139
-1
lines changed

4 files changed

+139
-1
lines changed

.github/workflows/compile-examples.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ jobs:
3737
- fqbn: arduino:mbed_giga:giga
3838
platforms: |
3939
- name: arduino:mbed_giga
40+
- fqbn: arduino:mbed_opta:opta
41+
platforms: |
42+
- name: arduino:mbed_opta
4043
4144
steps:
4245
- name: Checkout repository
@@ -53,6 +56,7 @@ jobs:
5356
- source-path: ./
5457
- name: Arduino_Portenta_OTA
5558
- name: Arduino_DebugUtils
59+
- name: ArduinoRS485
5660
sketch-paths: |
5761
- examples
5862
enable-deltas-report: true

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
build
22
.idea/
3+
.DS_Store
4+
inols*.log

examples/OptaDirList/OptaDirList.ino

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
Opta - DirList
3+
4+
The sketch shows how to mount an usb storage device and how to
5+
get a list of the existing folders and files.
6+
It will print debugging output to the RS485 port.
7+
8+
The circuit:
9+
- Arduino Opta RS485 or WiFi
10+
11+
This example code is in the public domain.
12+
*/
13+
14+
#include <Arduino_USBHostMbed5.h>
15+
#include <DigitalOut.h>
16+
#include <FATFileSystem.h>
17+
18+
USBHostMSD msd;
19+
mbed::FATFileSystem usb("usb");
20+
21+
#include <ArduinoRS485.h>
22+
constexpr auto baudrate { 115200 };
23+
24+
void setup()
25+
{
26+
configureRS485(baudrate);
27+
printlnRS485("Starting USB Dir List example...");
28+
29+
30+
printRS485("Please, connect the USB device...");
31+
while (!msd.connect()) {
32+
printRS485(".");
33+
delay(1000);
34+
}
35+
36+
char buf[256] {};
37+
38+
printlnRS485("");
39+
printRS485("Mounting USB device... ");
40+
int err = usb.mount(&msd);
41+
42+
if (err) {
43+
printRS485("Error mounting USB device ");
44+
snprintf(buf, sizeof(buf), "error: %s (%d)\r\n", -err == EINVAL ? "Invalid Filesystem" : "Unknown Error", -err);
45+
printlnRS485(buf);
46+
while (1)
47+
;
48+
}
49+
printlnRS485("done.");
50+
51+
// Display the root directory
52+
printRS485("Opening the root directory... ");
53+
DIR* d = opendir("/usb/");
54+
if (!d) {
55+
snprintf(buf, sizeof(buf), "error: %s (%d)", strerror(errno), -errno);
56+
printRS485(buf);
57+
}
58+
printlnRS485("done.");
59+
60+
printlnRS485("Root directory:");
61+
unsigned int count { 0 };
62+
while (true) {
63+
struct dirent* e = readdir(d);
64+
if (!e) {
65+
break;
66+
}
67+
count++;
68+
snprintf(buf, sizeof(buf), " %s\r\n", e->d_name);
69+
printRS485(buf);
70+
}
71+
printRS485(count);
72+
printlnRS485(" files found!");
73+
74+
snprintf(buf, sizeof(buf), "Closing the root directory... ");
75+
printRS485(buf);
76+
fflush(stdout);
77+
err = closedir(d);
78+
snprintf(buf, sizeof(buf), "%s\r\n", (err < 0 ? "Fail :(" : "OK"));
79+
printRS485(buf);
80+
if (err < 0) {
81+
snprintf(buf, sizeof(buf), "error: %s (%d)\r\n", strerror(errno), -errno);
82+
printRS485(buf);
83+
}
84+
}
85+
86+
void loop()
87+
{
88+
delay(1000);
89+
// handle disconnection and reconnection
90+
if (!msd.connected()) {
91+
msd.connect();
92+
}
93+
}
94+
95+
size_t printRS485(char* message)
96+
{
97+
RS485.beginTransmission();
98+
auto len = strlen(message);
99+
RS485.write(message, len);
100+
RS485.endTransmission();
101+
}
102+
103+
size_t printRS485(int num)
104+
{
105+
char message[256] {};
106+
sprintf(message, "%d", num);
107+
RS485.beginTransmission();
108+
auto len = strlen(message);
109+
RS485.write(message, len);
110+
RS485.endTransmission();
111+
}
112+
113+
size_t printlnRS485(char* message)
114+
{
115+
printRS485(message);
116+
RS485.beginTransmission();
117+
RS485.write('\r');
118+
RS485.write('\n');
119+
RS485.endTransmission();
120+
}
121+
122+
void configureRS485(const int baudrate)
123+
{
124+
const auto bitduration { 1.f / baudrate };
125+
const auto wordlen { 9.6f }; // OR 10.0f depending on the channel configuration
126+
const auto preDelayBR { bitduration * wordlen * 3.5f * 1e6 };
127+
const auto postDelayBR { bitduration * wordlen * 3.5f * 1e6 };
128+
129+
RS485.begin(baudrate);
130+
RS485.setDelays(preDelayBR, postDelayBR);
131+
RS485.noReceive();
132+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ sentence=ARM Mbed5 USBHOST library port for Arduino.
66
paragraph=This library contains an Arduino port for the ARM Mbed5 USBHost library for STM32H747 based boards.
77
category=Communication
88
url=https://github.com/arduino-libraries/Arduino_USBHostMbed5
9-
architectures=mbed,mbed_portenta,mbed_giga
9+
architectures=mbed,mbed_portenta,mbed_giga,mbed_opta

0 commit comments

Comments
 (0)