1010 */
1111#include "badge_hid_drivers.h"
1212#include <string.h>
13+ #include "esp_err.h"
1314#include "esp_log.h"
1415#include "usb/hid.h"
1516
@@ -141,7 +142,7 @@ esp_err_t analyze_mouse_layout(const uint8_t* desc, int desc_len, mouse_field_la
141142 }
142143 }
143144
144- return layout_out -> has_x && layout_out -> has_y ;
145+ return layout_out -> has_x && layout_out -> has_y ? ESP_OK : ESP_FAIL ;
145146}
146147
147148esp_err_t analyze_gamepad_layout (const uint8_t * desc , int desc_len , gamepad_field_layout_t * layout_out ) {
@@ -172,6 +173,11 @@ esp_err_t analyze_gamepad_layout(const uint8_t* desc, int desc_len, gamepad_fiel
172173 data |= ((uint32_t )desc [i ++ ]) << (j * 8 );
173174 }
174175
176+ // Reset usage list when starting new section
177+ if (type == HID_TYPE_GLOBAL && (tag == HID_TAG_USAGE_PAGE || tag == HID_TAG_REPORT_ID )) {
178+ usage_index = 0 ;
179+ }
180+
175181 switch (type ) {
176182 case HID_TYPE_GLOBAL :
177183 switch (tag ) {
@@ -191,25 +197,33 @@ esp_err_t analyze_gamepad_layout(const uint8_t* desc, int desc_len, gamepad_fiel
191197 }
192198 break ;
193199
194- case HID_TYPE_LOCAL :
200+ case HID_TYPE_LOCAL :
195201 if (tag == HID_TAG_USAGE && usage_index < 32 ) {
196202 usages [usage_index ++ ] = data ;
197203 }
198204 break ;
199205
200206 case HID_TYPE_MAIN :
201207 if (tag == HID_TAG_INPUT ) {
202- if (usage_page == USAGE_PAGE_BUTTON && usage_index >= 1 && usages [0 ] >= USAGE_BUTTON_MIN && usages [0 ] <= USAGE_BUTTON_MAX ) {
208+ bool is_constant = (data & 0x01 ) != 0 ;
209+
210+ if (usage_page == USAGE_PAGE_BUTTON && usage_index >= 1 &&
211+ usages [0 ] >= USAGE_BUTTON_MIN && usages [0 ] <= USAGE_BUTTON_MAX ) {
203212 if (!layout_out -> has_buttons ) layout_out -> button_bit_offset = bit_offset ;
204- layout_out -> has_buttons = true;
213+ layout_out -> has_buttons = true;
205214 layout_out -> button_bit_count += report_count ;
206- bit_offset += report_size * report_count ;
207- last_input_was_button_array = true ;
215+ last_input_was_button_array = true ;
216+ bit_offset += report_size * report_count ;
208217 } else if (usage_page == USAGE_PAGE_BUTTON && usage_index == 0 && last_input_was_button_array ) {
209218 bit_offset += report_size * report_count ;
210219 } else {
211- for (int u = 0 ; u < usage_index ; u ++ ) {
212- uint16_t usage = usages [u ];
220+ for (int u = 0 ; u < report_count ; u ++ ) {
221+ uint16_t usage = (u < usage_index ) ? usages [u ] :
222+ (usage_index > 0 ? usages [usage_index - 1 ] : 0xFFFF );
223+ if (usage == 0xFFFF ) {
224+ bit_offset += report_size ;
225+ continue ;
226+ }
213227
214228 if (usage_page == USAGE_PAGE_GENERIC_DESKTOP ) {
215229 switch (usage ) {
@@ -239,7 +253,8 @@ esp_err_t analyze_gamepad_layout(const uint8_t* desc, int desc_len, gamepad_fiel
239253 layout_out -> ry_bit_size = report_size ;
240254 break ;
241255 }
242- } else if (usage_page == USAGE_PAGE_SIMULATION && (usage == USAGE_ACCELERATOR || usage == USAGE_BRAKE )) {
256+ } else if (usage_page == USAGE_PAGE_SIMULATION &&
257+ (usage == USAGE_ACCELERATOR || usage == USAGE_BRAKE )) {
243258 if (!layout_out -> has_lt ) {
244259 layout_out -> has_lt = true;
245260 layout_out -> lt_bit_offset = bit_offset ;
@@ -253,22 +268,25 @@ esp_err_t analyze_gamepad_layout(const uint8_t* desc, int desc_len, gamepad_fiel
253268
254269 bit_offset += report_size ;
255270 }
256- // Handle excess usages vs report count
257- if (usage_index == 0 || usage_index < report_count ) {
258- bit_offset += report_size * (report_count - usage_index );
259- }
271+
260272 last_input_was_button_array = false;
261273 }
262274
263- usage_index = 0 ;
275+ // Clear usages after each input block
264276 }
277+ usage_index = 0 ;
278+
265279 break ;
266280 }
267281 }
268282
269- return layout_out -> has_dpad && layout_out -> has_buttons ;
283+ return ESP_OK ;
270284}
271285
286+
287+
288+
289+
272290esp_err_t decode_descriptor_register_driver (const uint8_t * const desc , const int desc_len , const uint8_t proto ) {
273291 if (HID_PROTOCOL_KEYBOARD == proto ) {
274292 ESP_LOGI (TAG , "Keyboard uses generic (boot) driver" );
0 commit comments