39
39
static uint8_t const keycode2ascii [128 ][2 ] = { HID_KEYCODE_TO_ASCII };
40
40
41
41
// Each HID instance can has multiple reports
42
- static struct
43
- {
42
+ static struct {
44
43
uint8_t report_count ;
45
44
tuh_hid_report_info_t report_info [MAX_REPORT ];
46
- }hid_info [CFG_TUH_HID ];
45
+ } hid_info [CFG_TUH_HID ];
47
46
48
47
static void process_kbd_report (hid_keyboard_report_t const * report );
49
48
static void process_mouse_report (hid_mouse_report_t const * report );
50
49
static void process_generic_report (uint8_t dev_addr , uint8_t instance , uint8_t const * report , uint16_t len );
51
50
52
- void hid_app_task (void )
53
- {
51
+ void hid_app_task (void ) {
54
52
// nothing to do
55
53
}
56
54
@@ -63,64 +61,57 @@ void hid_app_task(void)
63
61
// can be used to parse common/simple enough descriptor.
64
62
// Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE, it will be skipped
65
63
// therefore report_desc = NULL, desc_len = 0
66
- void tuh_hid_mount_cb (uint8_t dev_addr , uint8_t instance , uint8_t const * desc_report , uint16_t desc_len )
67
- {
64
+ void tuh_hid_mount_cb (uint8_t dev_addr , uint8_t instance , uint8_t const * desc_report , uint16_t desc_len ) {
68
65
printf ("HID device address = %d, instance = %d is mounted\r\n" , dev_addr , instance );
69
66
70
67
// Interface protocol (hid_interface_protocol_enum_t)
71
- const char * protocol_str [] = { "None" , "Keyboard" , "Mouse" };
68
+ const char * protocol_str [] = {"None" , "Keyboard" , "Mouse" };
72
69
uint8_t const itf_protocol = tuh_hid_interface_protocol (dev_addr , instance );
73
70
74
71
printf ("HID Interface Protocol = %s\r\n" , protocol_str [itf_protocol ]);
75
72
76
73
// By default host stack will use activate boot protocol on supported interface.
77
74
// Therefore for this simple example, we only need to parse generic report descriptor (with built-in parser)
78
- if ( itf_protocol == HID_ITF_PROTOCOL_NONE )
79
- {
75
+ if (itf_protocol == HID_ITF_PROTOCOL_NONE ) {
80
76
hid_info [instance ].report_count = tuh_hid_parse_report_descriptor (hid_info [instance ].report_info , MAX_REPORT , desc_report , desc_len );
81
77
printf ("HID has %u reports \r\n" , hid_info [instance ].report_count );
82
78
}
83
79
84
80
// request to receive report
85
81
// tuh_hid_report_received_cb() will be invoked when report is available
86
- if ( !tuh_hid_receive_report (dev_addr , instance ) )
87
- {
82
+ if (!tuh_hid_receive_report (dev_addr , instance )) {
88
83
printf ("Error: cannot request to receive report\r\n" );
89
84
}
90
85
}
91
86
92
87
// Invoked when device with hid interface is un-mounted
93
- void tuh_hid_umount_cb (uint8_t dev_addr , uint8_t instance )
94
- {
88
+ void tuh_hid_umount_cb (uint8_t dev_addr , uint8_t instance ) {
95
89
printf ("HID device address = %d, instance = %d is unmounted\r\n" , dev_addr , instance );
96
90
}
97
91
98
92
// Invoked when received report from device via interrupt endpoint
99
- void tuh_hid_report_received_cb (uint8_t dev_addr , uint8_t instance , uint8_t const * report , uint16_t len )
100
- {
93
+ void tuh_hid_report_received_cb (uint8_t dev_addr , uint8_t instance , uint8_t const * report , uint16_t len ) {
101
94
uint8_t const itf_protocol = tuh_hid_interface_protocol (dev_addr , instance );
102
95
103
- switch (itf_protocol )
104
- {
96
+ switch (itf_protocol ) {
105
97
case HID_ITF_PROTOCOL_KEYBOARD :
106
98
TU_LOG2 ("HID receive boot keyboard report\r\n" );
107
- process_kbd_report ( (hid_keyboard_report_t const * ) report );
108
- break ;
99
+ process_kbd_report ((hid_keyboard_report_t const * ) report );
100
+ break ;
109
101
110
102
case HID_ITF_PROTOCOL_MOUSE :
111
103
TU_LOG2 ("HID receive boot mouse report\r\n" );
112
- process_mouse_report ( (hid_mouse_report_t const * ) report );
113
- break ;
104
+ process_mouse_report ((hid_mouse_report_t const * ) report );
105
+ break ;
114
106
115
107
default :
116
108
// Generic report requires matching ReportID and contents with previous parsed report info
117
109
process_generic_report (dev_addr , instance , report , len );
118
- break ;
110
+ break ;
119
111
}
120
112
121
113
// continue to request to receive report
122
- if ( !tuh_hid_receive_report (dev_addr , instance ) )
123
- {
114
+ if (!tuh_hid_receive_report (dev_addr , instance )) {
124
115
printf ("Error: cannot request to receive report\r\n" );
125
116
}
126
117
}
@@ -231,29 +222,24 @@ static void process_mouse_report(hid_mouse_report_t const * report)
231
222
//--------------------------------------------------------------------+
232
223
// Generic Report
233
224
//--------------------------------------------------------------------+
234
- static void process_generic_report (uint8_t dev_addr , uint8_t instance , uint8_t const * report , uint16_t len )
235
- {
225
+ static void process_generic_report (uint8_t dev_addr , uint8_t instance , uint8_t const * report , uint16_t len ) {
236
226
(void ) dev_addr ;
237
227
(void ) len ;
238
228
239
229
uint8_t const rpt_count = hid_info [instance ].report_count ;
240
- tuh_hid_report_info_t * rpt_info_arr = hid_info [instance ].report_info ;
241
- tuh_hid_report_info_t * rpt_info = NULL ;
230
+ tuh_hid_report_info_t * rpt_info_arr = hid_info [instance ].report_info ;
231
+ tuh_hid_report_info_t * rpt_info = NULL ;
242
232
243
- if ( rpt_count == 1 && rpt_info_arr [0 ].report_id == 0 )
244
- {
233
+ if (rpt_count == 1 && rpt_info_arr [0 ].report_id == 0 ) {
245
234
// Simple report without report ID as 1st byte
246
235
rpt_info = & rpt_info_arr [0 ];
247
- }else
248
- {
236
+ } else {
249
237
// Composite report, 1st byte is report ID, data starts from 2nd byte
250
238
uint8_t const rpt_id = report [0 ];
251
239
252
240
// Find report id in the array
253
- for (uint8_t i = 0 ; i < rpt_count ; i ++ )
254
- {
255
- if (rpt_id == rpt_info_arr [i ].report_id )
256
- {
241
+ for (uint8_t i = 0 ; i < rpt_count ; i ++ ) {
242
+ if (rpt_id == rpt_info_arr [i ].report_id ) {
257
243
rpt_info = & rpt_info_arr [i ];
258
244
break ;
259
245
}
@@ -263,8 +249,7 @@ static void process_generic_report(uint8_t dev_addr, uint8_t instance, uint8_t c
263
249
len -- ;
264
250
}
265
251
266
- if (!rpt_info )
267
- {
252
+ if (!rpt_info ) {
268
253
printf ("Couldn't find report info !\r\n" );
269
254
return ;
270
255
}
@@ -276,23 +261,22 @@ static void process_generic_report(uint8_t dev_addr, uint8_t instance, uint8_t c
276
261
// - Consumer Control (Media Key) : Consumer, Consumer Control
277
262
// - System Control (Power key) : Desktop, System Control
278
263
// - Generic (vendor) : 0xFFxx, xx
279
- if ( rpt_info -> usage_page == HID_USAGE_PAGE_DESKTOP )
280
- {
281
- switch (rpt_info -> usage )
282
- {
264
+ if (rpt_info -> usage_page == HID_USAGE_PAGE_DESKTOP ) {
265
+ switch (rpt_info -> usage ) {
283
266
case HID_USAGE_DESKTOP_KEYBOARD :
284
267
TU_LOG1 ("HID receive keyboard report\r\n" );
285
268
// Assume keyboard follow boot report layout
286
- process_kbd_report ( (hid_keyboard_report_t const * ) report );
287
- break ;
269
+ process_kbd_report ((hid_keyboard_report_t const * ) report );
270
+ break ;
288
271
289
272
case HID_USAGE_DESKTOP_MOUSE :
290
273
TU_LOG1 ("HID receive mouse report\r\n" );
291
274
// Assume mouse follow boot report layout
292
- process_mouse_report ( (hid_mouse_report_t const * ) report );
293
- break ;
275
+ process_mouse_report ((hid_mouse_report_t const * ) report );
276
+ break ;
294
277
295
- default : break ;
278
+ default :
279
+ break ;
296
280
}
297
281
}
298
282
}
0 commit comments