Skip to content

Commit cbc8e82

Browse files
committed
Cleanup of analysis model
1 parent 9c8c488 commit cbc8e82

File tree

8 files changed

+274
-354
lines changed

8 files changed

+274
-354
lines changed

main/badge_hid_drivers.c

Lines changed: 90 additions & 105 deletions
Large diffs are not rendered by default.

main/badge_hid_drivers.h

Lines changed: 51 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#define HID_TYPE_LOCAL 0x2
1919

2020
// HID Main Item Tags
21-
#define HID_TAG_INPUT 0x8
22-
#define HID_TAG_COLLECTION 0xA
21+
#define HID_TAG_INPUT 0x8
22+
#define HID_TAG_COLLECTION 0xA
2323
#define HID_TAG_END_COLLECTION 0xC
2424

2525
// HID Global Item Tags
@@ -29,23 +29,23 @@
2929
#define HID_TAG_REPORT_ID 0x8
3030

3131
// HID Local Item Tags
32-
#define HID_TAG_USAGE 0x0
33-
#define HID_TAG_USAGE_MIN 0x1
34-
#define HID_TAG_USAGE_MAX 0x2
32+
#define HID_TAG_USAGE 0x0
33+
#define HID_TAG_USAGE_MIN 0x1
34+
#define HID_TAG_USAGE_MAX 0x2
3535

3636
// HID Special Item
3737
#define HID_LONG_ITEM_PREFIX 0xFE
3838

3939
// Usage Pages
40-
#define USAGE_PAGE_GENERIC_DESKTOP 0x01
41-
#define USAGE_PAGE_BUTTON 0x09
42-
#define USAGE_PAGE_CONSUMER 0x0C
40+
#define USAGE_PAGE_GENERIC_DESKTOP 0x01
41+
#define USAGE_PAGE_BUTTON 0x09
42+
#define USAGE_PAGE_CONSUMER 0x0C
4343

4444
// Usages
45-
#define USAGE_X 0x30
46-
#define USAGE_Y 0x31
47-
#define USAGE_WHEEL 0x38
48-
#define USAGE_TILT 0x48
45+
#define USAGE_X 0x30
46+
#define USAGE_Y 0x31
47+
#define USAGE_WHEEL 0x38
48+
#define USAGE_TILT 0x48
4949
#define USAGE_CONSUMER_TILT 0x0238
5050

5151
// HID Item Types
@@ -54,8 +54,8 @@
5454
#define HID_TYPE_LOCAL 0x2
5555

5656
// HID Main Item Tags
57-
#define HID_TAG_INPUT 0x8
58-
#define HID_TAG_COLLECTION 0xA
57+
#define HID_TAG_INPUT 0x8
58+
#define HID_TAG_COLLECTION 0xA
5959
#define HID_TAG_END_COLLECTION 0xC
6060

6161
// HID Global Item Tags
@@ -65,29 +65,29 @@
6565
#define HID_TAG_REPORT_ID 0x8
6666

6767
// HID Local Item Tags
68-
#define HID_TAG_USAGE 0x0
69-
#define HID_TAG_USAGE_MIN 0x1
70-
#define HID_TAG_USAGE_MAX 0x2
68+
#define HID_TAG_USAGE 0x0
69+
#define HID_TAG_USAGE_MIN 0x1
70+
#define HID_TAG_USAGE_MAX 0x2
7171

7272
// HID Special Item
7373
#define HID_LONG_ITEM_PREFIX 0xFE
7474

7575
// Usage Pages
76-
#define USAGE_PAGE_GENERIC_DESKTOP 0x01
77-
#define USAGE_PAGE_SIMULATION 0x02
78-
#define USAGE_PAGE_BUTTON 0x09
79-
#define USAGE_PAGE_CONSUMER 0x0C
76+
#define USAGE_PAGE_GENERIC_DESKTOP 0x01
77+
#define USAGE_PAGE_SIMULATION 0x02
78+
#define USAGE_PAGE_BUTTON 0x09
79+
#define USAGE_PAGE_CONSUMER 0x0C
8080

8181
// Generic Desktop Usages
82-
#define USAGE_X 0x30
83-
#define USAGE_Y 0x31
84-
#define USAGE_Z 0x32
85-
#define USAGE_RX 0x33
86-
#define USAGE_RY 0x34
87-
#define USAGE_RZ 0x35
88-
#define USAGE_HATSWITCH 0x39
89-
#define USAGE_WHEEL 0x38
90-
#define USAGE_TILT 0x48
82+
#define USAGE_X 0x30
83+
#define USAGE_Y 0x31
84+
#define USAGE_Z 0x32
85+
#define USAGE_RX 0x33
86+
#define USAGE_RY 0x34
87+
#define USAGE_RZ 0x35
88+
#define USAGE_HATSWITCH 0x39
89+
#define USAGE_WHEEL 0x38
90+
#define USAGE_TILT 0x48
9191

9292
// Simulation Control Usages
9393
#define USAGE_ACCELERATOR 0xC4
@@ -97,114 +97,35 @@
9797
#define USAGE_CONSUMER_TILT 0x0238
9898

9999
// Button Usage Range
100-
#define USAGE_BUTTON_MIN 0x01
101-
#define USAGE_BUTTON_MAX 0x20
102-
100+
#define USAGE_BUTTON_MIN 0x01
101+
#define USAGE_BUTTON_MAX 0x20
103102

104103
typedef struct {
105-
bool has_buttons;
106-
bool has_x;
107-
bool has_y;
108-
bool has_scroll;
109-
bool has_tilt;
110-
111-
uint8_t report_id;
112-
113-
uint16_t button_bit_offset;
114-
uint8_t button_bit_count;
115-
116-
uint16_t x_bit_offset;
117-
uint8_t x_bit_size;
118-
119-
uint16_t y_bit_offset;
120-
uint8_t y_bit_size;
121-
122-
uint16_t scroll_bit_offset;
123-
uint8_t scroll_bit_size;
124-
125-
uint16_t tilt_bit_offset;
126-
uint8_t tilt_bit_size;
127-
} mouse_field_layout_t;
104+
bool present;
105+
uint16_t offset;
106+
uint8_t size;
107+
} field_info_t;
128108

129109
typedef struct {
130-
bool has_dpad;
131-
bool has_buttons;
132-
bool has_lx;
133-
bool has_ly;
134-
bool has_rx;
135-
bool has_ry;
136-
bool has_lt;
137-
bool has_rt;
138-
139-
uint8_t report_id;
140-
141-
uint16_t dpad_bit_offset;
142-
uint8_t dpad_bit_size;
143-
144-
uint16_t button_bit_offset;
145-
uint8_t button_bit_count;
146-
147-
uint16_t lx_bit_offset;
148-
uint8_t lx_bit_size;
149-
150-
uint16_t ly_bit_offset;
151-
uint8_t ly_bit_size;
152-
153-
uint16_t rx_bit_offset;
154-
uint8_t rx_bit_size;
155-
156-
uint16_t ry_bit_offset;
157-
uint8_t ry_bit_size;
158-
159-
uint16_t lt_bit_offset;
160-
uint8_t lt_bit_size;
161-
162-
uint16_t rt_bit_offset;
163-
uint8_t rt_bit_size;
110+
uint8_t report_id;
111+
field_info_t dpad;
112+
field_info_t buttons;
113+
field_info_t lx, ly;
114+
field_info_t rx, ry;
115+
field_info_t lt, rt;
164116
} gamepad_field_layout_t;
165117

166-
// Structure to hold parsed HID field information
167-
typedef struct {
168-
const char* name; ///< Field name (e.g., "button", "axis")
169-
uint8_t report_id; ///< Report ID associated with this field
170-
uint8_t* data; ///< Pointer to first byte of value in report data
171-
uint16_t offset; ///< Bit offset in report (0-n)
172-
uint8_t size; ///< Size in bits (1-32)
173-
uint8_t count; ///< Number of items in an array (e.g., 5 buttons)
174-
uint16_t usage_page; ///< Usage page ID
175-
uint16_t usage_ids[32]; ///< Store up to 32 usages per field
176-
uint16_t usage_id; ///< Usage ID within the page
177-
const char* usage_name; ///< Human-readable name of the usage
178-
179-
union {
180-
struct {
181-
uint8_t data : 1; ///< Constant value (1) or variable (0)
182-
uint8_t array : 1; ///< Array of values (1) or single value (0)
183-
uint8_t relative : 1; ///< Relative value
184-
uint8_t wrap : 1; ///< Wraps around (e.g., hat switch)
185-
uint8_t nonlinear : 1; ///< Non-linear mapping
186-
uint8_t preferred : 1; ///< Preferred state
187-
uint8_t null : 1; ///< No null position
188-
uint8_t dummy : 1; ///< Unused bit
189-
};
190-
uint8_t val;
191-
} flags;
192-
193-
struct {
194-
int32_t min;
195-
int32_t max;
196-
} logic_range;
197-
198-
struct {
199-
uint32_t min;
200-
uint32_t max;
201-
} usage_range;
202-
} hid_field_info_t;
118+
// Mouse layout: X, Y, Wheel, Buttons up to 8
119+
// Buttons are bit-packed after axes
203120

204121
typedef struct {
205-
size_t num_fields;
206-
hid_field_info_t* fields;
207-
} hid_report_descriptor_t;
122+
uint8_t report_id;
123+
field_info_t x;
124+
field_info_t y;
125+
field_info_t scroll;
126+
field_info_t tilt;
127+
field_info_t buttons; // count via size: number of bits
128+
} mouse_field_layout_t;
208129

209130
esp_err_t analyze_mouse_layout(const uint8_t* desc, int desc_len, mouse_field_layout_t* layout_out);
210131
esp_err_t analyze_gamepad_layout(const uint8_t* desc, int desc_len, gamepad_field_layout_t* layout_out);

main/badge_hid_host.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,11 +623,11 @@ static void hid_host_device_event(hid_host_device_handle_t hid_device_handle, co
623623
ESP_ERROR_CHECK(hid_host_device_open(hid_device_handle, &dev_config));
624624

625625
if (HID_SUBCLASS_BOOT_INTERFACE == dev_params.sub_class) {
626-
ESP_ERROR_CHECK(hid_class_request_set_protocol(hid_device_handle, HID_REPORT_PROTOCOL_BOOT));
627626
if (HID_PROTOCOL_KEYBOARD == dev_params.proto) {
627+
ESP_ERROR_CHECK(hid_class_request_set_protocol(hid_device_handle, HID_REPORT_PROTOCOL_BOOT));
628628
ESP_ERROR_CHECK(hid_class_request_set_idle(hid_device_handle, 0, 0));
629629
} else if (HID_PROTOCOL_MOUSE == dev_params.proto) { // Luxury mouse support
630-
hid_class_request_set_protocol(hid_device_handle, HID_REPORT_PROTOCOL_REPORT);
630+
ESP_ERROR_CHECK(hid_class_request_set_protocol(hid_device_handle, HID_REPORT_PROTOCOL_REPORT));
631631
}
632632
}
633633
ESP_ERROR_CHECK(hid_host_device_start(hid_device_handle));

test_native/badge_hid_drivers.o

-824 Bytes
Binary file not shown.

test_native/test

-672 Bytes
Binary file not shown.

test_native/test_descriptors.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,22 @@ const uint8_t gamepad1_desc[] = {
5353
0x05, 0x81, 0x01, 0x85, 0x05, 0x06, 0x0f, 0x00, 0x09, 0x97, 0x75, 0x10, 0x95, 0x02, 0x27, 0xff,
5454
0xff, 0x00, 0x00, 0x91, 0x02, 0xc0
5555
};
56-
const size_t gamepad1_len = sizeof(gamepad1_desc);
56+
const size_t gamepad1_len = sizeof(gamepad1_desc);
57+
58+
// Stadia Controller
59+
const uint8_t gamepad2_desc[] = {
60+
0x05, 0x01, 0x09, 0x05, 0xa1, 0x01, 0x85, 0x03, 0x05, 0x01, 0x75, 0x04, 0x95, 0x01, 0x25, 0x07,
61+
0x46, 0x3b, 0x01, 0x65, 0x14, 0x09, 0x39, 0x81, 0x42, 0x45, 0x00, 0x65, 0x00, 0x75, 0x01, 0x95,
62+
0x04, 0x81, 0x01, 0x05, 0x09, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x0f, 0x09, 0x12, 0x09,
63+
0x11, 0x09, 0x14, 0x09, 0x13, 0x09, 0x0d, 0x09, 0x0c, 0x09, 0x0b, 0x09, 0x0f, 0x09, 0x0e, 0x09,
64+
0x08, 0x09, 0x07, 0x09, 0x05, 0x09, 0x04, 0x09, 0x02, 0x09, 0x01, 0x81, 0x02, 0x75, 0x01, 0x95,
65+
0x01, 0x81, 0x01, 0x05, 0x01, 0x15, 0x01, 0x26, 0xff, 0x00, 0x09, 0x01, 0xa1, 0x00, 0x09, 0x30,
66+
0x09, 0x31, 0x75, 0x08, 0x95, 0x02, 0x81, 0x02, 0xc0, 0x09, 0x01, 0xa1, 0x00, 0x09, 0x32, 0x09,
67+
0x35, 0x75, 0x08, 0x95, 0x02, 0x81, 0x02, 0xc0, 0x05, 0x02, 0x75, 0x08, 0x95, 0x02, 0x15, 0x00,
68+
0x26, 0xff, 0x00, 0x09, 0xc5, 0x09, 0xc4, 0x81, 0x02, 0x05, 0x0c, 0x15, 0x00, 0x25, 0x01, 0x09,
69+
0xe9, 0x09, 0xea, 0x75, 0x01, 0x95, 0x02, 0x81, 0x02, 0x09, 0xcd, 0x95, 0x01, 0x81, 0x02, 0x95,
70+
0x05, 0x81, 0x01, 0x85, 0x05, 0x06, 0x0f, 0x00, 0x09, 0x97, 0x75, 0x10, 0x95, 0x02, 0x27, 0xff,
71+
0xff, 0x00, 0x00, 0x91, 0x02, 0xc0
72+
};
73+
const size_t gamepad2_len = sizeof(gamepad2_desc);
74+

0 commit comments

Comments
 (0)