Skip to content

Commit 2378cfc

Browse files
author
Amanda Butler
authored
Merge pull request #1153 from ARMmbed/hk-update-usbhid-snippets
Update USBHID snippet as API as changed
2 parents 703122b + a065b46 commit 2378cfc

File tree

1 file changed

+50
-50
lines changed

1 file changed

+50
-50
lines changed

docs/api/usb/USBHID.md

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

1313
### main.cpp
1414

15-
```C++ TODO
16-
#include "mbed.h"
17-
#include "USBHID.h"
18-
19-
//We declare a USBHID device. By default input and output reports are 64 bytes long.
20-
USBHID hid(true, 8, 8);
15+
```C++
16+
#include <stdio.h>
2117

22-
Serial pc(USBTX, USBRX);
18+
#include "mbed.h"
19+
#include "drivers/USBHID.h"
2320

24-
//This report will contain data to be sent
25-
HID_REPORT send_report;
26-
HID_REPORT recv_report;
21+
// Declare a USBHID device
22+
USBHID HID(8, 8, 0x1234, 0x0006, 0x0001, true);
2723

28-
DigitalOut l1(LED1);
24+
HID_REPORT output_report = {
25+
.length = 8,
26+
.data = {0}
27+
};
28+
HID_REPORT input_report = {
29+
.length = 0,
30+
.data = {0}
31+
};
2932

30-
int main(void) {
31-
send_report.length = 8;
33+
DigitalOut led_out(LED1);
3234

35+
int main(void)
36+
{
3337
while (1) {
3438

35-
//Fill the report
36-
for (int i = 0; i < send_report.length; i++)
37-
send_report.data[i] = rand() & 0xff;
39+
// Fill the report
40+
for (int i = 0; i < output_report.length; i++) {
41+
output_report.data[i] = rand() & UINT8_MAX;
42+
}
3843

39-
//Send the report
40-
hid.send(&send_report);
44+
// Send the report
45+
HID.send(&output_report);
4146

42-
//try to read a msg
43-
if(hid.read_nb(&recv_report)) {
44-
l1 = !l1;
45-
for(int i = 0; i < recv_report.length; i++) {
46-
pc.printf("%d ", recv_report.data[i]);
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]);
4752
}
48-
pc.printf("\r\n");
53+
printf("\r\n");
4954
}
5055
}
5156
}
52-
```
57+
```
5358
5459
### USBHID.py
5560
@@ -61,38 +66,33 @@ from pywinusb import hid
6166
# Whenever the host computer receives data from the
6267
# Mbed board, the received data is printed
6368
def on_data(data):
64-
print("Got message %s" % data)
69+
print(f"Got message {data}")
6570
6671
'''
6772
Gets all HIDs currently connected to host computer,
6873
and sets the first device with string "mbed" in its
6974
vendor name equal to variable mbed. This variable
7075
will be used to send data to the Mbed board.
7176
'''
72-
all_devices = hid.find_all_hid_devices()
73-
mbeds = [dev for dev in all_devices if dev.vendor_name.find("mbed") >= 0]
74-
if len(mbeds) == 0:
75-
print("No HID devices found")
76-
exit(-1)
77-
mbed = mbeds[0]
78-
79-
# Sends 8 bytes of data to the Mbed board
80-
# The Mbed board should receive the data "1 2 3 4 5 6 7 8"
81-
mbed.open()
82-
mbed.set_raw_data_handler(on_data)
83-
message = bytearray(9)
84-
message[1] = 1
85-
message[2] = 2
86-
message[3] = 3
87-
message[4] = 4
88-
message[5] = 5
89-
message[6] = 6
90-
message[7] = 7
91-
message[8] = 8
92-
93-
mbed.find_output_reports()[0].send(message)
94-
95-
```
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+
```
9696

9797
## Related content
9898

0 commit comments

Comments
 (0)