@@ -12,44 +12,49 @@ You can use the USBHID class to turn an Mbed board into an HID (Human Interface
12
12
13
13
### main.cpp
14
14
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>
21
17
22
- Serial pc(USBTX, USBRX);
18
+ #include " mbed.h"
19
+ #include " drivers/USBHID.h"
23
20
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);
27
23
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
+ };
29
32
30
- int main(void) {
31
- send_report.length = 8;
33
+ DigitalOut led_out(LED1);
32
34
35
+ int main(void)
36
+ {
33
37
while (1) {
34
38
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
+ }
38
43
39
- //Send the report
40
- hid .send(&send_report );
44
+ // Send the report
45
+ HID .send(&output_report );
41
46
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]);
47
52
}
48
- pc. printf("\r\n");
53
+ printf("\r\n");
49
54
}
50
55
}
51
56
}
52
- ```
57
+ ```
53
58
54
59
### USBHID.py
55
60
@@ -61,38 +66,33 @@ from pywinusb import hid
61
66
# Whenever the host computer receives data from the
62
67
# Mbed board, the received data is printed
63
68
def on_data(data):
64
- print("Got message %s" % data)
69
+ print(f "Got message { data}" )
65
70
66
71
'''
67
72
Gets all HIDs currently connected to host computer,
68
73
and sets the first device with string "mbed" in its
69
74
vendor name equal to variable mbed. This variable
70
75
will be used to send data to the Mbed board.
71
76
'''
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
+ ```
96
96
97
97
## Related content
98
98
0 commit comments