Skip to content

Update example in USBCDC.md #1226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions docs/api/usb/USBCDC.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,7 @@ The USBCDC class emulates a basic serial port over USB. You can use this serial

## USBCDC example

```C++ TODO
#include "mbed.h"
#include "USBCDC.h"

USBCDC cdc;

int main(void) {

while(1)
{
char msg[] = "Hello world\r\n";
cdc.send((uint8_t*)msg, strlen(msg));
wait(1.0);
}
}
```
[![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)

## Related content

Expand Down
78 changes: 2 additions & 76 deletions docs/api/usb/USBHID.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,87 +12,13 @@ You can use the USBHID class to turn an Mbed board into an HID (Human Interface

### main.cpp

```C++
#include <stdio.h>

#include "mbed.h"
#include "drivers/USBHID.h"

// Declare a USBHID device
USBHID HID(8, 8, 0x1234, 0x0006, 0x0001, true);

HID_REPORT output_report = {
.length = 8,
.data = {0}
};
HID_REPORT input_report = {
.length = 0,
.data = {0}
};

DigitalOut led_out(LED1);

int main(void)
{
while (1) {

// Fill the report
for (int i = 0; i < output_report.length; i++) {
output_report.data[i] = rand() & UINT8_MAX;
}

// Send the report
HID.send(&output_report);

// Try to read a msg
if (HID.read_nb(&input_report)) {
led_out = !led_out;
for (int i = 0; i < input_report.length; i++) {
printf("%d ", input_report.data[i]);
}
printf("\r\n");
}
}
}
```
[![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)

### USBHID.py

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.

```Py
from pywinusb import hid

# Whenever the host computer receives data from the
# Mbed board, the received data is printed
def on_data(data):
print(f"Got message {data}")

'''
Gets all HIDs currently connected to host computer,
and sets the first device with string "mbed" in its
vendor name equal to variable mbed. This variable
will be used to send data to the Mbed board.
'''
all_hid_devices = hid.find_all_hid_devices()
mbed_devices = [d for d in all_hid_devices if "mbed" in d.vendor_name]

if mbed_devices is None:
raise ValueError("No HID devices found")

# A buffer of bytes representing the values 1-8
# The first byte is the report ID which must be 0
buffer = [0, 1, 2, 3, 4, 5, 6, 7, 8]

mbed_devices[0].open()
# Set custom raw data handler
mbed_devices[0].set_raw_data_handler(on_data)

# Send the message to the Mbed board
out_report = mbed_devices[0].find_output_reports()
out_report[0].set_raw_data(buffer)
out_report[0].send()
```
[![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)

## Related content

Expand Down
15 changes: 1 addition & 14 deletions docs/api/usb/USBKeyboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,7 @@ The USBKeyboard class provides the functionality of a keyboard. You can send key

## USBKeyboard example

```C++ TODO
#include "mbed.h"
#include "USBKeyboard.h"

USBKeyboard key;

int main(void)
{
while (1) {
key.printf("Hello World\r\n");
wait(1.0);
}
}
```
[![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)

## Related content

Expand Down
175 changes: 2 additions & 173 deletions docs/api/usb/USBMIDI.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,181 +22,10 @@ The two examples below use a program called "Anvil Studio 32-bit" to play MIDI n

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

```C++ TODO
#include "mbed.h"
#include "USBMIDI.h"

USBMIDI midi;

int main() {
while (1) {
for(int i=48; i<83; i++) { // send some messages!
midi.write(MIDIMessage::NoteOn(i));
wait(0.25);
midi.write(MIDIMessage::NoteOff(i));
wait(0.5);
}
}
}
```
[![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)

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

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:

```C++ TODO
#include "mbed.h"
#include "USBMIDI.h"

#define REST -1
#define C 0
#define Cs 1
#define D 2
#define Ds 3
#define E 4
#define F 5
#define Fs 6
#define G 7
#define Gs 8
#define A 9
#define As 10
#define B 11

#define OCTAVE0 0
#define OCTAVE1 12
#define OCTAVE2 24
#define OCTAVE3 36
#define OCTAVE4 48
#define OCTAVE5 60
#define OCTAVE6 72
#define OCTAVE7 84

#define WHOLE_NOTE 1.15
#define HALF_NOTE (WHOLE_NOTE / 2)
#define QUARTER_NOTE (WHOLE_NOTE / 4)
#define EIGHTH_NOTE (WHOLE_NOTE / 8)
#define SIXTEENTH_NOTE (WHOLE_NOTE / 16)

#define THREE_EIGHTHS_NOTE (EIGHTH_NOTE * 3)
#define THREE_FORTHS_NOTE (QUARTER_NOTE * 3)

USBMIDI midi;

void PlayNote(int note, int octave, float duration){
if(note == REST){
wait(duration);
}
else{
midi.write(MIDIMessage::NoteOn(note + octave));
wait(duration);
midi.write(MIDIMessage::NoteOff(note + octave));
}
}

void TakeMeOutToTheBallGame(){
//https://www.bethsnotesplus.com/2012/09/take-me-out-to-ball-game.html
PlayNote(C, OCTAVE5, HALF_NOTE);
PlayNote(C, OCTAVE6, QUARTER_NOTE);

PlayNote(A, OCTAVE5, QUARTER_NOTE);
PlayNote(G, OCTAVE5, QUARTER_NOTE);
PlayNote(E, OCTAVE5, QUARTER_NOTE);

PlayNote(G, OCTAVE5, THREE_FORTHS_NOTE);

PlayNote(D, OCTAVE5, THREE_FORTHS_NOTE);

PlayNote(C, OCTAVE5, HALF_NOTE);
PlayNote(C, OCTAVE6, QUARTER_NOTE);

PlayNote(A, OCTAVE5, QUARTER_NOTE);
PlayNote(G, OCTAVE5, QUARTER_NOTE);
PlayNote(E, OCTAVE5, QUARTER_NOTE);

PlayNote(G, OCTAVE5, THREE_FORTHS_NOTE);

PlayNote(G, OCTAVE5, HALF_NOTE);
PlayNote(REST, 0, QUARTER_NOTE);

PlayNote(A, OCTAVE5, QUARTER_NOTE);
PlayNote(Gs, OCTAVE5, QUARTER_NOTE);
PlayNote(A, OCTAVE5, QUARTER_NOTE);

PlayNote(E, OCTAVE5, QUARTER_NOTE);
PlayNote(F, OCTAVE5, QUARTER_NOTE);
PlayNote(G, OCTAVE5, QUARTER_NOTE);

PlayNote(A, OCTAVE5, HALF_NOTE);
PlayNote(F, OCTAVE5, QUARTER_NOTE);

PlayNote(D, OCTAVE5, THREE_FORTHS_NOTE);

PlayNote(A, OCTAVE5, HALF_NOTE);
PlayNote(A, OCTAVE5, QUARTER_NOTE);

PlayNote(A, OCTAVE5, QUARTER_NOTE);
PlayNote(B, OCTAVE5, QUARTER_NOTE);
PlayNote(C, OCTAVE6, QUARTER_NOTE);

PlayNote(D, OCTAVE6, QUARTER_NOTE);
PlayNote(B, OCTAVE5, QUARTER_NOTE);
PlayNote(A, OCTAVE5, QUARTER_NOTE);

PlayNote(G, OCTAVE5, QUARTER_NOTE);
PlayNote(E, OCTAVE5, QUARTER_NOTE);
PlayNote(D, OCTAVE5, QUARTER_NOTE);

PlayNote(C, OCTAVE5, HALF_NOTE);
PlayNote(C, OCTAVE6, QUARTER_NOTE);

PlayNote(A, OCTAVE5, QUARTER_NOTE);
PlayNote(G, OCTAVE5, QUARTER_NOTE);
PlayNote(E, OCTAVE5, QUARTER_NOTE);

PlayNote(G, OCTAVE5, THREE_FORTHS_NOTE);

PlayNote(D, OCTAVE5, HALF_NOTE);
PlayNote(D, OCTAVE5, QUARTER_NOTE);

PlayNote(C, OCTAVE5, HALF_NOTE);
PlayNote(D, OCTAVE5, QUARTER_NOTE);

PlayNote(E, OCTAVE5, QUARTER_NOTE);
PlayNote(F, OCTAVE5, QUARTER_NOTE);
PlayNote(G, OCTAVE5, QUARTER_NOTE);

PlayNote(A, OCTAVE5, THREE_FORTHS_NOTE);

PlayNote(A, OCTAVE5, QUARTER_NOTE);
PlayNote(A, OCTAVE5, QUARTER_NOTE);
PlayNote(B, OCTAVE5, QUARTER_NOTE);

PlayNote(C, OCTAVE6, THREE_FORTHS_NOTE);

PlayNote(C, OCTAVE6, THREE_FORTHS_NOTE);

PlayNote(C, OCTAVE6, QUARTER_NOTE);
PlayNote(B, OCTAVE5, QUARTER_NOTE);
PlayNote(A, OCTAVE5, QUARTER_NOTE);

PlayNote(G, OCTAVE5, QUARTER_NOTE);
PlayNote(Fs, OCTAVE5, QUARTER_NOTE);
PlayNote(G, OCTAVE5, QUARTER_NOTE);

PlayNote(A, OCTAVE5, THREE_FORTHS_NOTE);

PlayNote(B, OCTAVE5, THREE_FORTHS_NOTE);

PlayNote(C, OCTAVE6, THREE_FORTHS_NOTE);

PlayNote(C, OCTAVE6, HALF_NOTE);
PlayNote(REST, 0, QUARTER_NOTE);
}

int main() {
while (1) {
TakeMeOutToTheBallGame();
}
}

```
[![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)
20 changes: 4 additions & 16 deletions docs/api/usb/USBMSD.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,13 @@ You can use the USBMSD interface to emulate a mass storage device over USB. You

[![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)

## USBMSD example
## USBMSD SDBlockDevice example

```C++ TODO
#include "mbed.h"
#include "SDBlockDevice.h"
#include "USBMSD.h"
[![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)

SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4);
USBMSD usb(&sd);
## USBMSD HeapBlockDevice example

int main() {

while(true) {
usb.process();
}

return 0;
}
```
[![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)

## Related content

Expand Down
Loading