Skip to content

Commit

Permalink
USB: cleanup button parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
gyurco committed Jan 17, 2025
1 parent 002d29e commit 94b0549
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion usb/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ static uint8_t usb_hid_init(usb_device_t *dev, usb_device_descriptor_t *dev_desc
info->iface[i].conf.joystick_mouse.axis[k].logical.min,
info->iface[i].conf.joystick_mouse.axis[k].logical.max);

for(k=0;k<4;k++)
for(k=0;k<info->iface[i].conf.joystick_mouse.button_count;k++)
iprintf("Button%d: @%d/%d\n", k,
info->iface[i].conf.joystick_mouse.button[k].byte_offset,
info->iface[i].conf.joystick_mouse.button[k].bitmask);
Expand Down
9 changes: 5 additions & 4 deletions usb/hidparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,20 @@ bool parse_report_descriptor(uint8_t *rep, uint16_t rep_size, hid_report_t *conf
if(btns) {
if((conf->type == REPORT_TYPE_JOYSTICK) ||
(conf->type == REPORT_TYPE_MOUSE)) {
// scan for up to four buttons
// scan for up to 12 buttons
char b;
for(b=0;b<12;b++) {
conf->joystick_mouse.button_count = 0;
for(b=0;b<MAX_BUTTONS;b++) {
if(report_count > b) {
uint16_t this_bit = bit_count+b;
uint16_t this_bit = bit_count+b*report_size;

hidp_debugf("BUTTON%d @ %d (byte %d, mask %d)", b,
this_bit, this_bit/8, 1 << (this_bit%8));

conf->joystick_mouse.button[b].byte_offset = this_bit/8;
conf->joystick_mouse.button[b].bitmask = 1 << (this_bit%8);
conf->joystick_mouse.button_count++;
}
conf->joystick_mouse.button_count = report_count * report_size;
}

// we found at least one button which is all we want to accept this as a valid
Expand Down
3 changes: 2 additions & 1 deletion usb/hidparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define REPORT_TYPE_JOYSTICK 3

#define MAX_AXES 4
#define MAX_BUTTONS 12

// currently only joysticks are supported
typedef struct {
Expand All @@ -28,7 +29,7 @@ typedef struct {
struct {
uint8_t byte_offset;
uint8_t bitmask;
} button[12]; // 12 buttons max
} button[MAX_BUTTONS]; // 12 buttons max

struct {
uint16_t offset;
Expand Down

0 comments on commit 94b0549

Please sign in to comment.