Skip to content

Commit d7610cf

Browse files
committed
Move headers and update examples
1 parent abe17c9 commit d7610cf

File tree

4 files changed

+120
-27
lines changed

4 files changed

+120
-27
lines changed

examples/DirList/DirList.ino

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Portenta - FileRead
3+
4+
The sketch shows how to mount an usb storage device and how to
5+
read from an existing file.
6+
to use this sketch create a .txt file named Arduino.txt,
7+
in your storage device and write some content inside.
8+
9+
The circuit:
10+
- Portenta H7
11+
12+
This example code is in the public domain.
13+
*/
14+
#include <DigitalOut.h>
15+
#include <FATFileSystem.h>
16+
#include <USBHostMbed5.h>
17+
18+
USBHostMSD msd;
19+
mbed::FATFileSystem usb("usb");
20+
21+
mbed::DigitalOut pin5(PC_6, 0);
22+
23+
// If you are using a Portenta Machine Control uncomment the following line
24+
mbed::DigitalOut otg(PB_14, 0);
25+
26+
#define BUFFER_MAX_LEN 64
27+
void setup()
28+
{
29+
Serial.begin(115200);
30+
while (!Serial)
31+
;
32+
33+
delay(2500);
34+
Serial.println("Starting USB Dir List example...");
35+
36+
// if you are using a Max Carrier uncomment the following line
37+
//start_hub();
38+
39+
while (!msd.connect()) {
40+
//while (!port.connected()) {
41+
delay(1000);
42+
}
43+
44+
Serial.print("Mounting USB device... ");
45+
int err = usb.mount(&msd);
46+
if (err) {
47+
Serial.print("Error mounting USB device ");
48+
Serial.println(err);
49+
while (1)
50+
;
51+
}
52+
Serial.println("done.");
53+
54+
char buf[256] {};
55+
// Display the root directory
56+
Serial.print("Opening the root directory... ");
57+
DIR* d = opendir("/usb/");
58+
Serial.println(!d ? "Fail :(" : "Done");
59+
if (!d) {
60+
snprintf(buf, sizeof(buf), "error: %s (%d)\r\n", strerror(errno), -errno);
61+
Serial.print(buf);
62+
}
63+
Serial.println("done.");
64+
65+
Serial.println("Root directory:");
66+
unsigned int count { 0 };
67+
while (true) {
68+
struct dirent* e = readdir(d);
69+
if (!e) {
70+
break;
71+
}
72+
count++;
73+
snprintf(buf, sizeof(buf), " %s\r\n", e->d_name);
74+
Serial.print(buf);
75+
}
76+
Serial.print(count);
77+
Serial.println(" files found!");
78+
79+
snprintf(buf, sizeof(buf), "Closing the root directory... ");
80+
Serial.print(buf);
81+
fflush(stdout);
82+
err = closedir(d);
83+
snprintf(buf, sizeof(buf), "%s\r\n", (err < 0 ? "Fail :(" : "OK"));
84+
Serial.print(buf);
85+
if (err < 0) {
86+
snprintf(buf, sizeof(buf), "error: %s (%d)\r\n", strerror(errno), -errno);
87+
Serial.print(buf);
88+
}
89+
}
90+
91+
void loop()
92+
{
93+
}

examples/FileRead/FileRead.ino

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,35 @@
1111
1212
This example code is in the public domain.
1313
*/
14-
#include "USBHostMbed5.h"
15-
#include "USBHostMSD/USBHostMSD.h"
16-
#include "USBHostHub/USBHostHub.h"
17-
#include "USBHostSerial/USBHostSerial.h"
18-
#include "DigitalOut.h"
19-
#include "FATFileSystem.h"
14+
#include <USBHostMbed5.h>
15+
#include <DigitalOut.h>
16+
#include <FATFileSystem.h>
2017

2118
USBHostMSD msd;
22-
mbed::FATFileSystem fs("fs");
19+
mbed::FATFileSystem usb("usb");
2320

2421
mbed::DigitalOut pin5(PC_6, 0);
2522

2623
// If you are using a Portenta Machine Control uncomment the following line
27-
//mbed::DigitalOut otg(PB_8, 1);
24+
mbed::DigitalOut otg(PB_14, 0);
2825

2926
#define BUFFER_MAX_LEN 64
3027
void setup() {
3128
Serial.begin(115200);
3229
while (!Serial);
3330

31+
delay(2500);
32+
Serial.println("Starting USB File Read example...");
33+
3434
// if you are using a Max Carrier uncomment the following line
3535
//start_hub();
3636

3737
while (!msd.connect()) {
38-
//while (!port.connected()) {
3938
delay(1000);
4039
}
4140

4241
Serial.println("Mounting USB device...");
43-
int err = fs.mount(&msd);
42+
int err = usb.mount(&msd);
4443
if (err) {
4544
Serial.print("Error mounting USB device ");
4645
Serial.println(err);
@@ -52,12 +51,12 @@ void setup() {
5251
int dirIndex = 0;
5352
int res = 0;
5453
Serial.println("Open file..");
55-
FILE *f = fopen("/fs/Arduino.txt", "r+");
56-
char buf[2];
57-
Serial.println("File conntet:");
54+
FILE *f = fopen("/usb/Arduino.txt", "r+");
55+
char buf[256];
56+
Serial.println("File content:");
5857

59-
while (fgets(buf, 2, f) != NULL) {
60-
Serial.println(buf);
58+
while (fgets(buf, 256, f) != NULL) {
59+
Serial.print(buf);
6160
}
6261

6362
Serial.println("File closing");
@@ -76,4 +75,4 @@ void setup() {
7675

7776
void loop() {
7877

79-
}
78+
}

examples/FileWrite/FileWrite.ino

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
#include "USBHostMbed5.h"
2-
#include "USBHostMSD/USBHostMSD.h"
3-
#include "USBHostHub/USBHostHub.h"
4-
#include "USBHostSerial/USBHostSerial.h"
5-
#include "DigitalOut.h"
6-
#include "FATFileSystem.h"
1+
#include <USBHostMbed5.h>
2+
#include <DigitalOut.h>
3+
#include <FATFileSystem.h>
74

85
USBHostMSD msd;
9-
mbed::FATFileSystem fs("fs");
6+
mbed::FATFileSystem usb("usb");
107

11-
mbed::DigitalOut pin5(PC_6, 0);
8+
// mbed::DigitalOut pin5(PC_6, 0);
129
mbed::DigitalOut otg(PB_8, 1);
1310

1411
void setup() {
@@ -22,7 +19,7 @@ void setup() {
2219
}
2320

2421
Serial.println("Mounting USB device...");
25-
int err = fs.mount(&msd);
22+
int err = usb.mount(&msd);
2623
if (err) {
2724
Serial.print("Error mounting USB device ");
2825
Serial.println(err);
@@ -33,8 +30,8 @@ void setup() {
3330
struct dirent *ent;
3431
int dirIndex = 0;
3532
int res = 0;
36-
Serial.println("Open /fs/numbers.txt");
37-
FILE *f = fopen("/fs/numbers.txt", "w+");
33+
Serial.println("Open /usb/numbers.txt");
34+
FILE *f = fopen("/usb/numbers.txt", "w+");
3835
for (int i = 0; i < 10; i++) {
3936
Serial.print("Writing numbers (");
4037
Serial.print(i);

src/USBHostMbed5.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
#include "USBHostHub/USBHostHub.h"
3+
#include "USBHostMSD/USBHostMSD.h"
4+
#include "USBHostSerial/USBHostSerial.h"

0 commit comments

Comments
 (0)