You are viewing an older revision! See the latest version
USBHID
Beta only!
This library is in beta, and only works with the betamode compiler and the beta libraries.
To use these, ensure you have enabled /betamode for the compiler, and that you import these examples as the basis for your experiments to ensure the beta mbed library is pulled in.
The USBHID class can be used to send and receive messages over USB. For instance, you can define your own protocol and communicate between your computer and the Mbed with all capabilities of a USB communication. To use USBHID, you need a script running on the host side (computer). For instance on a 32 bits Windows 7 machine, you can use pywinusb.
USB pins are available on p31 (D+) and p32 (D-)
Hello World¶
#include "mbed.h"
#include "USBHID.h"
//We declare a USBHID device
USBHID hid;
//This report will contain data to be sent
HID_REPORT send_report;
int main(void) {
//Fill the report
for(int i = 0; i < 64; i++)
send_report.data[i] = i;
send_report.length = 64;
while (1) {
//Send the report
hid.send(&send_report);
wait(0.01);
}
}
Import programUSBHID_HelloWorld
USBHID Hello World
API¶
[Not converted]
Details¶
You can choose the length of exchanged packets. In this example, mbed leds are controlled by four switches. When you press a button, there is a message sent containing buttons state. According to this message, the mbed will receive back a new message to light on leds.
We need one byte to control leds and one byte to send buttons state.
#include "mbed.h"
#include "USBHID.h"
//We declare a USBHID device: it can send 1 byte and receive 1 byte
USBHID hid(1, 1);
//Two reports where will be stored values to send and received
HID_REPORT recv_report;
HID_REPORT send_report;
//Bus of leds
BusOut leds(LED1,LED2,LED3,LED4);
//Bus of buttons
BusInOut buttons(p21, p22, p23, p24);
int main(void) {
uint8_t p_bus = 0;
send_report.length = 1;
while (1) {
//If a data is received, update led bus
if (hid.readNB(&recv_report)) {
leds = recv_report.data[0];
}
//if the bus of buttons has changed, send a report
if (buttons.read() != p_bus) {
p_bus = buttons.read();
send_report.data[0] = p_bus;
hid.send(&send_report);
}
wait(0.01);
}
}
Import programUSBHID_LEDS
USBHID example with leds and buttons
