Skip to content

Commit 280feb3

Browse files
author
Amanda Butler
authored
Merge pull request #1226 from ARMmbed/AnotherButler-patch-7
Update example in USBCDC.md
2 parents ed33248 + 91a5e11 commit 280feb3

File tree

8 files changed

+15
-479
lines changed

8 files changed

+15
-479
lines changed

docs/api/usb/USBCDC.md

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,7 @@ The USBCDC class emulates a basic serial port over USB. You can use this serial
1010

1111
## USBCDC example
1212

13-
```C++ TODO
14-
#include "mbed.h"
15-
#include "USBCDC.h"
16-
17-
USBCDC cdc;
18-
19-
int main(void) {
20-
21-
while(1)
22-
{
23-
char msg[] = "Hello world\r\n";
24-
cdc.send((uint8_t*)msg, strlen(msg));
25-
wait(1.0);
26-
}
27-
}
28-
```
13+
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_USB/USBCDC)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_USB/USBCDC/main.cpp)
2914

3015
## Related content
3116

docs/api/usb/USBHID.md

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -12,87 +12,13 @@ You can use the USBHID class to turn an Mbed board into an HID (Human Interface
1212

1313
### main.cpp
1414

15-
```C++
16-
#include <stdio.h>
17-
18-
#include "mbed.h"
19-
#include "drivers/USBHID.h"
20-
21-
// Declare a USBHID device
22-
USBHID HID(8, 8, 0x1234, 0x0006, 0x0001, true);
23-
24-
HID_REPORT output_report = {
25-
.length = 8,
26-
.data = {0}
27-
};
28-
HID_REPORT input_report = {
29-
.length = 0,
30-
.data = {0}
31-
};
32-
33-
DigitalOut led_out(LED1);
34-
35-
int main(void)
36-
{
37-
while (1) {
38-
39-
// Fill the report
40-
for (int i = 0; i < output_report.length; i++) {
41-
output_report.data[i] = rand() & UINT8_MAX;
42-
}
43-
44-
// Send the report
45-
HID.send(&output_report);
46-
47-
// Try to read a msg
48-
if (HID.read_nb(&input_report)) {
49-
led_out = !led_out;
50-
for (int i = 0; i < input_report.length; i++) {
51-
printf("%d ", input_report.data[i]);
52-
}
53-
printf("\r\n");
54-
}
55-
}
56-
}
57-
```
15+
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_USB/USBHID)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_USB/USBHID/main.cpp)
5816

5917
### USBHID.py
6018

6119
To use this script, first flash the Mbed board with the code above. Connect the target's USB to the host computer. Then run this script. The script will send 8 bytes of data (1 2 3 4 5 6 7 8) to the Mbed board and will read and print the data sent to the host computer by the Mbed board.
6220

63-
```Py
64-
from pywinusb import hid
65-
66-
# Whenever the host computer receives data from the
67-
# Mbed board, the received data is printed
68-
def on_data(data):
69-
print(f"Got message {data}")
70-
71-
'''
72-
Gets all HIDs currently connected to host computer,
73-
and sets the first device with string "mbed" in its
74-
vendor name equal to variable mbed. This variable
75-
will be used to send data to the Mbed board.
76-
'''
77-
all_hid_devices = hid.find_all_hid_devices()
78-
mbed_devices = [d for d in all_hid_devices if "mbed" in d.vendor_name]
79-
80-
if mbed_devices is None:
81-
raise ValueError("No HID devices found")
82-
83-
# A buffer of bytes representing the values 1-8
84-
# The first byte is the report ID which must be 0
85-
buffer = [0, 1, 2, 3, 4, 5, 6, 7, 8]
86-
87-
mbed_devices[0].open()
88-
# Set custom raw data handler
89-
mbed_devices[0].set_raw_data_handler(on_data)
90-
91-
# Send the message to the Mbed board
92-
out_report = mbed_devices[0].find_output_reports()
93-
out_report[0].set_raw_data(buffer)
94-
out_report[0].send()
95-
```
21+
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_USB/USBHID)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_USB/USBHID/USBHID.py)
9622

9723
## Related content
9824

docs/api/usb/USBKeyboard.md

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,7 @@ The USBKeyboard class provides the functionality of a keyboard. You can send key
1010

1111
## USBKeyboard example
1212

13-
```C++ TODO
14-
#include "mbed.h"
15-
#include "USBKeyboard.h"
16-
17-
USBKeyboard key;
18-
19-
int main(void)
20-
{
21-
while (1) {
22-
key.printf("Hello World\r\n");
23-
wait(1.0);
24-
}
25-
}
26-
```
13+
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_USB/USBKeyboard)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_USB/USBKeyboard/main.cpp)
2714

2815
## Related content
2916

docs/api/usb/USBMIDI.md

Lines changed: 2 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -22,181 +22,10 @@ The two examples below use a program called "Anvil Studio 32-bit" to play MIDI n
2222

2323
Below is an example to send a series of MIDI notes to the host PC:
2424

25-
```C++ TODO
26-
#include "mbed.h"
27-
#include "USBMIDI.h"
28-
29-
USBMIDI midi;
30-
31-
int main() {
32-
while (1) {
33-
for(int i=48; i<83; i++) { // send some messages!
34-
midi.write(MIDIMessage::NoteOn(i));
35-
wait(0.25);
36-
midi.write(MIDIMessage::NoteOff(i));
37-
wait(0.5);
38-
}
39-
}
40-
}
41-
```
25+
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_USB/USBMIDI)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_USB/USBMIDI/main.cpp)
4226

4327
## Play "Take Me Out to the Ball Game" example
4428

4529
You can use USBMIDI to play an entire song, not just a series of notes. "Take Me Out to the Ball Game" is a popular song in the public domain. To play "Take Me Out to the Ball Game" (public domain) using MIDI over USB on the host PC:
4630

47-
```C++ TODO
48-
#include "mbed.h"
49-
#include "USBMIDI.h"
50-
51-
#define REST -1
52-
#define C 0
53-
#define Cs 1
54-
#define D 2
55-
#define Ds 3
56-
#define E 4
57-
#define F 5
58-
#define Fs 6
59-
#define G 7
60-
#define Gs 8
61-
#define A 9
62-
#define As 10
63-
#define B 11
64-
65-
#define OCTAVE0 0
66-
#define OCTAVE1 12
67-
#define OCTAVE2 24
68-
#define OCTAVE3 36
69-
#define OCTAVE4 48
70-
#define OCTAVE5 60
71-
#define OCTAVE6 72
72-
#define OCTAVE7 84
73-
74-
#define WHOLE_NOTE 1.15
75-
#define HALF_NOTE (WHOLE_NOTE / 2)
76-
#define QUARTER_NOTE (WHOLE_NOTE / 4)
77-
#define EIGHTH_NOTE (WHOLE_NOTE / 8)
78-
#define SIXTEENTH_NOTE (WHOLE_NOTE / 16)
79-
80-
#define THREE_EIGHTHS_NOTE (EIGHTH_NOTE * 3)
81-
#define THREE_FORTHS_NOTE (QUARTER_NOTE * 3)
82-
83-
USBMIDI midi;
84-
85-
void PlayNote(int note, int octave, float duration){
86-
if(note == REST){
87-
wait(duration);
88-
}
89-
else{
90-
midi.write(MIDIMessage::NoteOn(note + octave));
91-
wait(duration);
92-
midi.write(MIDIMessage::NoteOff(note + octave));
93-
}
94-
}
95-
96-
void TakeMeOutToTheBallGame(){
97-
//https://www.bethsnotesplus.com/2012/09/take-me-out-to-ball-game.html
98-
PlayNote(C, OCTAVE5, HALF_NOTE);
99-
PlayNote(C, OCTAVE6, QUARTER_NOTE);
100-
101-
PlayNote(A, OCTAVE5, QUARTER_NOTE);
102-
PlayNote(G, OCTAVE5, QUARTER_NOTE);
103-
PlayNote(E, OCTAVE5, QUARTER_NOTE);
104-
105-
PlayNote(G, OCTAVE5, THREE_FORTHS_NOTE);
106-
107-
PlayNote(D, OCTAVE5, THREE_FORTHS_NOTE);
108-
109-
PlayNote(C, OCTAVE5, HALF_NOTE);
110-
PlayNote(C, OCTAVE6, QUARTER_NOTE);
111-
112-
PlayNote(A, OCTAVE5, QUARTER_NOTE);
113-
PlayNote(G, OCTAVE5, QUARTER_NOTE);
114-
PlayNote(E, OCTAVE5, QUARTER_NOTE);
115-
116-
PlayNote(G, OCTAVE5, THREE_FORTHS_NOTE);
117-
118-
PlayNote(G, OCTAVE5, HALF_NOTE);
119-
PlayNote(REST, 0, QUARTER_NOTE);
120-
121-
PlayNote(A, OCTAVE5, QUARTER_NOTE);
122-
PlayNote(Gs, OCTAVE5, QUARTER_NOTE);
123-
PlayNote(A, OCTAVE5, QUARTER_NOTE);
124-
125-
PlayNote(E, OCTAVE5, QUARTER_NOTE);
126-
PlayNote(F, OCTAVE5, QUARTER_NOTE);
127-
PlayNote(G, OCTAVE5, QUARTER_NOTE);
128-
129-
PlayNote(A, OCTAVE5, HALF_NOTE);
130-
PlayNote(F, OCTAVE5, QUARTER_NOTE);
131-
132-
PlayNote(D, OCTAVE5, THREE_FORTHS_NOTE);
133-
134-
PlayNote(A, OCTAVE5, HALF_NOTE);
135-
PlayNote(A, OCTAVE5, QUARTER_NOTE);
136-
137-
PlayNote(A, OCTAVE5, QUARTER_NOTE);
138-
PlayNote(B, OCTAVE5, QUARTER_NOTE);
139-
PlayNote(C, OCTAVE6, QUARTER_NOTE);
140-
141-
PlayNote(D, OCTAVE6, QUARTER_NOTE);
142-
PlayNote(B, OCTAVE5, QUARTER_NOTE);
143-
PlayNote(A, OCTAVE5, QUARTER_NOTE);
144-
145-
PlayNote(G, OCTAVE5, QUARTER_NOTE);
146-
PlayNote(E, OCTAVE5, QUARTER_NOTE);
147-
PlayNote(D, OCTAVE5, QUARTER_NOTE);
148-
149-
PlayNote(C, OCTAVE5, HALF_NOTE);
150-
PlayNote(C, OCTAVE6, QUARTER_NOTE);
151-
152-
PlayNote(A, OCTAVE5, QUARTER_NOTE);
153-
PlayNote(G, OCTAVE5, QUARTER_NOTE);
154-
PlayNote(E, OCTAVE5, QUARTER_NOTE);
155-
156-
PlayNote(G, OCTAVE5, THREE_FORTHS_NOTE);
157-
158-
PlayNote(D, OCTAVE5, HALF_NOTE);
159-
PlayNote(D, OCTAVE5, QUARTER_NOTE);
160-
161-
PlayNote(C, OCTAVE5, HALF_NOTE);
162-
PlayNote(D, OCTAVE5, QUARTER_NOTE);
163-
164-
PlayNote(E, OCTAVE5, QUARTER_NOTE);
165-
PlayNote(F, OCTAVE5, QUARTER_NOTE);
166-
PlayNote(G, OCTAVE5, QUARTER_NOTE);
167-
168-
PlayNote(A, OCTAVE5, THREE_FORTHS_NOTE);
169-
170-
PlayNote(A, OCTAVE5, QUARTER_NOTE);
171-
PlayNote(A, OCTAVE5, QUARTER_NOTE);
172-
PlayNote(B, OCTAVE5, QUARTER_NOTE);
173-
174-
PlayNote(C, OCTAVE6, THREE_FORTHS_NOTE);
175-
176-
PlayNote(C, OCTAVE6, THREE_FORTHS_NOTE);
177-
178-
PlayNote(C, OCTAVE6, QUARTER_NOTE);
179-
PlayNote(B, OCTAVE5, QUARTER_NOTE);
180-
PlayNote(A, OCTAVE5, QUARTER_NOTE);
181-
182-
PlayNote(G, OCTAVE5, QUARTER_NOTE);
183-
PlayNote(Fs, OCTAVE5, QUARTER_NOTE);
184-
PlayNote(G, OCTAVE5, QUARTER_NOTE);
185-
186-
PlayNote(A, OCTAVE5, THREE_FORTHS_NOTE);
187-
188-
PlayNote(B, OCTAVE5, THREE_FORTHS_NOTE);
189-
190-
PlayNote(C, OCTAVE6, THREE_FORTHS_NOTE);
191-
192-
PlayNote(C, OCTAVE6, HALF_NOTE);
193-
PlayNote(REST, 0, QUARTER_NOTE);
194-
}
195-
196-
int main() {
197-
while (1) {
198-
TakeMeOutToTheBallGame();
199-
}
200-
}
201-
202-
```
31+
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_USB/USBMIDI_Take_Me_Out_to_the_Ball_Game)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_USB/USBMIDI_Take_Me_Out_to_the_Ball_Game/main.cpp)

docs/api/usb/USBMSD.md

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,13 @@ You can use the USBMSD interface to emulate a mass storage device over USB. You
88

99
[![View code](https://www.mbed.com/embed/?type=library)](https://os.mbed.com/docs/mbed-os/development/mbed-os-api-doxy/class_u_s_b_m_s_d.html)
1010

11-
## USBMSD example
11+
## USBMSD SDBlockDevice example
1212

13-
```C++ TODO
14-
#include "mbed.h"
15-
#include "SDBlockDevice.h"
16-
#include "USBMSD.h"
13+
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_USB/USBMSD_SDBlockDevice)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_USB/USBMSD_SDBlockDevice/main.cpp)
1714

18-
SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4);
19-
USBMSD usb(&sd);
15+
## USBMSD HeapBlockDevice example
2016

21-
int main() {
22-
23-
while(true) {
24-
usb.process();
25-
}
26-
27-
return 0;
28-
}
29-
```
17+
[![View code](https://www.mbed.com/embed/?url=https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_USB/USBMSD_HeapBlockDevice)](https://github.com/ARMmbed/mbed-os-examples-docs_only/blob/master/APIs_USB/USBMSD_HeapBlockDevice/main.cpp)
3018

3119
## Related content
3220

0 commit comments

Comments
 (0)