@@ -98,234 +98,19 @@ FILE *f = fopen("/usb/text.txt", "r+");
98
98
99
99
Below is an example sketch that can be used to ** list** files in a USB mass storage device.
100
100
101
- ``` arduino
102
- #include <DigitalOut.h>
103
- #include <FATFileSystem.h>
104
- #include <Arduino_USBHostMbed5.h>
105
-
106
- USBHostMSD msd;
107
- mbed::FATFileSystem usb("usb");
108
-
109
-
110
- void setup()
111
- {
112
- Serial.begin(115200);
113
-
114
- pinMode(PA_15, OUTPUT); //enable the USB-A port
115
- digitalWrite(PA_15, HIGH);
116
-
117
- while (!Serial)
118
- ;
119
-
120
- Serial.println("Starting USB Dir List example...");
121
-
122
- // if you are using a Max Carrier uncomment the following line
123
- // start_hub();
124
-
125
- while (!msd.connect()) {
126
- //while (!port.connected()) {
127
- delay(1000);
128
- }
129
-
130
- Serial.print("Mounting USB device... ");
131
- int err = usb.mount(&msd);
132
- if (err) {
133
- Serial.print("Error mounting USB device ");
134
- Serial.println(err);
135
- while (1);
136
- }
137
- Serial.println("done.");
138
-
139
- char buf[256];
140
-
141
- // Display the root directory
142
- Serial.print("Opening the root directory... ");
143
- DIR* d = opendir("/usb/");
144
- Serial.println(!d ? "Fail :(" : "Done");
145
- if (!d) {
146
- snprintf(buf, sizeof(buf), "error: %s (%d)\r\n", strerror(errno), -errno);
147
- Serial.print(buf);
148
- }
149
- Serial.println("done.");
150
-
151
- Serial.println("Root directory:");
152
- unsigned int count { 0 };
153
- while (true) {
154
- struct dirent* e = readdir(d);
155
- if (!e) {
156
- break;
157
- }
158
- count++;
159
- snprintf(buf, sizeof(buf), " %s\r\n", e->d_name);
160
- Serial.print(buf);
161
- }
162
- Serial.print(count);
163
- Serial.println(" files found!");
164
-
165
- snprintf(buf, sizeof(buf), "Closing the root directory... ");
166
- Serial.print(buf);
167
- fflush(stdout);
168
- err = closedir(d);
169
- snprintf(buf, sizeof(buf), "%s\r\n", (err < 0 ? "Fail :(" : "OK"));
170
- Serial.print(buf);
171
- if (err < 0) {
172
- snprintf(buf, sizeof(buf), "error: %s (%d)\r\n", strerror(errno), -errno);
173
- Serial.print(buf);
174
- }
175
- }
176
-
177
- void loop()
178
- {
179
- }
180
- ```
101
+ <CodeBlock url =" https://github.com/arduino-libraries/Arduino_USBHostMbed5/blob/main/examples/DirList/DirList.ino " className =" arduino " />
181
102
182
103
### File Read
183
104
184
105
Below is an example sketch that can be used to ** read** files from a USB mass storage device.
185
106
186
- ``` arduino
187
- #include <Arduino_USBHostMbed5.h>
188
- #include <DigitalOut.h>
189
- #include <FATFileSystem.h>
190
-
191
- USBHostMSD msd;
192
- mbed::FATFileSystem usb("usb");
193
-
194
- // If you are using a Portenta Machine Control uncomment the following line
195
- mbed::DigitalOut otg(PB_14, 0);
196
-
197
- void setup() {
198
- Serial.begin(115200);
199
-
200
- pinMode(PA_15, OUTPUT); //enable the USB-A port
201
- digitalWrite(PA_15, HIGH);
202
-
203
- while (!Serial);
204
-
205
- delay(2500);
206
- Serial.println("Starting USB File Read example...");
207
-
208
- // if you are using a Max Carrier uncomment the following line
209
- //start_hub();
210
-
211
- while (!msd.connect()) {
212
- delay(1000);
213
- }
214
-
215
- Serial.println("Mounting USB device...");
216
- int err = usb.mount(&msd);
217
- if (err) {
218
- Serial.print("Error mounting USB device ");
219
- Serial.println(err);
220
- while (1);
221
- }
222
- Serial.print("read done ");
223
- mbed::fs_file_t file;
224
- struct dirent *ent;
225
- int dirIndex = 0;
226
- int res = 0;
227
- Serial.println("Open file..");
228
- FILE *f = fopen("/usb/Arduino.txt", "r+");
229
- char buf[256];
230
- Serial.println("File content:");
231
-
232
- while (fgets(buf, 256, f) != NULL) {
233
- Serial.print(buf);
234
- }
235
-
236
- Serial.println("File closing");
237
- fflush(stdout);
238
- err = fclose(f);
239
- if (err < 0) {
240
- Serial.print("fclose error:");
241
- Serial.print(strerror(errno));
242
- Serial.print(" (");
243
- Serial.print(-errno);
244
- Serial.print(")");
245
- } else {
246
- Serial.println("File closed");
247
- }
248
- }
249
-
250
- void loop() {
251
-
252
- }
253
- ```
107
+ <CodeBlock url =" https://github.com/arduino-libraries/Arduino_USBHostMbed5/blob/main/examples/FileRead/FileRead.ino " className =" arduino " />
254
108
255
109
### File Write
256
110
257
111
Below is an example sketch that can be used to ** write** files from a USB mass storage device.
258
112
259
- ``` arduino
260
- #include <Arduino_USBHostMbed5.h>
261
- #include <DigitalOut.h>
262
- #include <FATFileSystem.h>
263
-
264
- USBHostMSD msd;
265
- mbed::FATFileSystem usb("usb");
266
-
267
- // mbed::DigitalOut pin5(PC_6, 0);
268
- mbed::DigitalOut otg(PB_8, 1);
269
-
270
- void setup() {
271
- Serial.begin(115200);
272
-
273
- pinMode(PA_15, OUTPUT); //enable the USB-A port
274
- digitalWrite(PA_15, HIGH);
275
-
276
- while (!Serial);
277
-
278
- msd.connect();
279
-
280
- while (!msd.connected()) {
281
- //while (!port.connected()) {
282
- delay(1000);
283
- }
284
-
285
- Serial.println("Mounting USB device...");
286
- int err = usb.mount(&msd);
287
- if (err) {
288
- Serial.print("Error mounting USB device ");
289
- Serial.println(err);
290
- while (1);
291
- }
292
- Serial.print("read done ");
293
- mbed::fs_file_t file;
294
- struct dirent *ent;
295
- int dirIndex = 0;
296
- int res = 0;
297
- Serial.println("Open /usb/numbers.txt");
298
- FILE *f = fopen("/usb/numbers.txt", "w+");
299
- for (int i = 0; i < 10; i++) {
300
- Serial.print("Writing numbers (");
301
- Serial.print(i);
302
- Serial.println("/10)");
303
- fflush(stdout);
304
- err = fprintf(f, "%d\n", i);
305
- if (err < 0) {
306
- Serial.println("Fail :(");
307
- error("error: %s (%d)\n", strerror(errno), -errno);
308
- }
309
- }
310
-
311
- Serial.println("File closing");
312
- fflush(stdout);
313
- err = fclose(f);
314
- if (err < 0) {
315
- Serial.print("fclose error:");
316
- Serial.print(strerror(errno));
317
- Serial.print(" (");
318
- Serial.print(-errno);
319
- Serial.print(")");
320
- } else {
321
- Serial.println("File closed");
322
- }
323
- }
324
-
325
- void loop() {
326
-
327
- }
328
- ```
113
+ <CodeBlock url =" https://github.com/arduino-libraries/Arduino_USBHostMbed5/blob/main/examples/FileWrite/FileWrite.ino " className =" arduino " />
329
114
330
115
### Datalogger Example
331
116
@@ -445,35 +230,7 @@ Please note that this library is in **Alpha** development stage. This means supp
445
230
446
231
*** The USBHostGiga library is not available in the Arduino IDE and needs to be installed manually. You can do so my navigating to ` Sketch ` > ` Include Library ` > ` Add .ZIP Library ` .***
447
232
448
- ``` arduino
449
- #include "USBHostGiga.h"
450
-
451
- //REDIRECT_STDOUT_TO(Serial)
452
- Keyboard keyb;
453
- HostSerial ser;
454
-
455
- void setup() {
456
- // put your setup code here, to run once:
457
- Serial.begin(115200);
458
- while (!Serial);
459
- pinMode(PA_15, OUTPUT);
460
- keyb.begin();
461
- ser.begin();
462
- }
463
-
464
-
465
- void loop() {
466
- if (keyb.available()) {
467
- auto _key = keyb.read();
468
- Serial.println(keyb.getAscii(_key));
469
- }
470
- while (ser.available()) {
471
- auto _char = ser.read();
472
- Serial.write(_char);
473
- }
474
- //delay(1);
475
- }
476
- ```
233
+ <CodeBlock url =" https://github.com/arduino-libraries/USBHostGiga/blob/master/examples/KeyboardGiga/KeyboardGiga.ino " className =" arduino " />
477
234
478
235
## USB HID
479
236
0 commit comments