3939static uint8_t const keycode2ascii [128 ][2 ] = { HID_KEYCODE_TO_ASCII };
4040
4141// Each HID instance can has multiple reports
42- static struct
43- {
42+ static struct {
4443 uint8_t report_count ;
4544 tuh_hid_report_info_t report_info [MAX_REPORT ];
46- }hid_info [CFG_TUH_HID ];
45+ } hid_info [CFG_TUH_HID ];
4746
4847static void process_kbd_report (hid_keyboard_report_t const * report );
4948static void process_mouse_report (hid_mouse_report_t const * report );
5049static void process_generic_report (uint8_t dev_addr , uint8_t instance , uint8_t const * report , uint16_t len );
5150
52- void hid_app_task (void )
53- {
51+ void hid_app_task (void ) {
5452 // nothing to do
5553}
5654
@@ -63,64 +61,57 @@ void hid_app_task(void)
6361// can be used to parse common/simple enough descriptor.
6462// Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE, it will be skipped
6563// 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 ) {
6865 printf ("HID device address = %d, instance = %d is mounted\r\n" , dev_addr , instance );
6966
7067 // Interface protocol (hid_interface_protocol_enum_t)
71- const char * protocol_str [] = { "None" , "Keyboard" , "Mouse" };
68+ const char * protocol_str [] = {"None" , "Keyboard" , "Mouse" };
7269 uint8_t const itf_protocol = tuh_hid_interface_protocol (dev_addr , instance );
7370
7471 printf ("HID Interface Protocol = %s\r\n" , protocol_str [itf_protocol ]);
7572
7673 // By default host stack will use activate boot protocol on supported interface.
7774 // 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 ) {
8076 hid_info [instance ].report_count = tuh_hid_parse_report_descriptor (hid_info [instance ].report_info , MAX_REPORT , desc_report , desc_len );
8177 printf ("HID has %u reports \r\n" , hid_info [instance ].report_count );
8278 }
8379
8480 // request to receive report
8581 // 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 )) {
8883 printf ("Error: cannot request to receive report\r\n" );
8984 }
9085}
9186
9287// 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 ) {
9589 printf ("HID device address = %d, instance = %d is unmounted\r\n" , dev_addr , instance );
9690}
9791
9892// 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 ) {
10194 uint8_t const itf_protocol = tuh_hid_interface_protocol (dev_addr , instance );
10295
103- switch (itf_protocol )
104- {
96+ switch (itf_protocol ) {
10597 case HID_ITF_PROTOCOL_KEYBOARD :
10698 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 ;
109101
110102 case HID_ITF_PROTOCOL_MOUSE :
111103 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 ;
114106
115107 default :
116108 // Generic report requires matching ReportID and contents with previous parsed report info
117109 process_generic_report (dev_addr , instance , report , len );
118- break ;
110+ break ;
119111 }
120112
121113 // 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 )) {
124115 printf ("Error: cannot request to receive report\r\n" );
125116 }
126117}
@@ -231,29 +222,24 @@ static void process_mouse_report(hid_mouse_report_t const * report)
231222//--------------------------------------------------------------------+
232223// Generic Report
233224//--------------------------------------------------------------------+
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 ) {
236226 (void ) dev_addr ;
237227 (void ) len ;
238228
239229 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 ;
242232
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 ) {
245234 // Simple report without report ID as 1st byte
246235 rpt_info = & rpt_info_arr [0 ];
247- }else
248- {
236+ } else {
249237 // Composite report, 1st byte is report ID, data starts from 2nd byte
250238 uint8_t const rpt_id = report [0 ];
251239
252240 // 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 ) {
257243 rpt_info = & rpt_info_arr [i ];
258244 break ;
259245 }
@@ -263,8 +249,7 @@ static void process_generic_report(uint8_t dev_addr, uint8_t instance, uint8_t c
263249 len -- ;
264250 }
265251
266- if (!rpt_info )
267- {
252+ if (!rpt_info ) {
268253 printf ("Couldn't find report info !\r\n" );
269254 return ;
270255 }
@@ -276,23 +261,22 @@ static void process_generic_report(uint8_t dev_addr, uint8_t instance, uint8_t c
276261 // - Consumer Control (Media Key) : Consumer, Consumer Control
277262 // - System Control (Power key) : Desktop, System Control
278263 // - 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 ) {
283266 case HID_USAGE_DESKTOP_KEYBOARD :
284267 TU_LOG1 ("HID receive keyboard report\r\n" );
285268 // 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 ;
288271
289272 case HID_USAGE_DESKTOP_MOUSE :
290273 TU_LOG1 ("HID receive mouse report\r\n" );
291274 // 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 ;
294277
295- default : break ;
278+ default :
279+ break ;
296280 }
297281 }
298282}
0 commit comments