Skip to content

Commit 9c8c488

Browse files
committed
Local tests success
1 parent 8c97777 commit 9c8c488

File tree

6 files changed

+54
-35
lines changed

6 files changed

+54
-35
lines changed

main/badge_hid_drivers.c

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
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

147148
esp_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+
272290
esp_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");

main/badge_hid_drivers.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,37 +127,38 @@ typedef struct {
127127
} mouse_field_layout_t;
128128

129129
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+
130139
uint8_t report_id;
131140

132-
bool has_dpad;
133141
uint16_t dpad_bit_offset;
134142
uint8_t dpad_bit_size;
135143

136-
bool has_buttons;
137144
uint16_t button_bit_offset;
138145
uint8_t button_bit_count;
139146

140-
bool has_lx;
141147
uint16_t lx_bit_offset;
142148
uint8_t lx_bit_size;
143149

144-
bool has_ly;
145150
uint16_t ly_bit_offset;
146151
uint8_t ly_bit_size;
147152

148-
bool has_rx;
149153
uint16_t rx_bit_offset;
150154
uint8_t rx_bit_size;
151155

152-
bool has_ry;
153156
uint16_t ry_bit_offset;
154157
uint8_t ry_bit_size;
155158

156-
bool has_lt;
157159
uint16_t lt_bit_offset;
158160
uint8_t lt_bit_size;
159161

160-
bool has_rt;
161162
uint16_t rt_bit_offset;
162163
uint8_t rt_bit_size;
163164
} gamepad_field_layout_t;

test_native/badge_hid_drivers.o

112 Bytes
Binary file not shown.

test_native/test

32 Bytes
Binary file not shown.

test_native/test_hid.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void print_layout(gamepad_field_layout_t* layout) {
5656

5757
int main(void) {
5858
mouse_field_layout_t mouse_layout = {0};
59-
assert(analyze_mouse_layout(mouse1_desc, mouse1_len, &mouse_layout));
59+
assert(ESP_OK == analyze_mouse_layout(mouse1_desc, mouse1_len, &mouse_layout));
6060

6161
assert(mouse_layout.has_buttons);
6262
assert(mouse_layout.has_x);
@@ -81,7 +81,7 @@ int main(void) {
8181

8282
printf("Logitech M705 passed\n");
8383

84-
assert(analyze_mouse_layout(mouse2_desc, mouse2_len, &mouse_layout));
84+
assert(ESP_OK == analyze_mouse_layout(mouse2_desc, mouse2_len, &mouse_layout));
8585

8686
assert(mouse_layout.has_buttons);
8787
assert(mouse_layout.has_x);
@@ -103,7 +103,7 @@ int main(void) {
103103

104104
printf("Trust Kuza passed\n");
105105

106-
assert(analyze_mouse_layout(mouse3_desc, mouse3_len, &mouse_layout));
106+
assert(ESP_OK == analyze_mouse_layout(mouse3_desc, mouse3_len, &mouse_layout));
107107

108108
assert(mouse_layout.has_buttons);
109109
assert(mouse_layout.has_x);
@@ -126,7 +126,7 @@ int main(void) {
126126
printf("Fujitsu M520 passed\n");
127127

128128
gamepad_field_layout_t pad_layout = {0};
129-
assert(analyze_gamepad_layout(gamepad1_desc, gamepad1_len, &pad_layout));
129+
assert(ESP_OK == analyze_gamepad_layout(gamepad1_desc, gamepad1_len, &pad_layout));
130130
print_layout(&pad_layout);
131131

132132
assert(pad_layout.has_dpad);
@@ -138,28 +138,28 @@ int main(void) {
138138
assert(pad_layout.has_lt);
139139
assert(pad_layout.has_rt);
140140

141-
assert(20 == pad_layout.dpad_bit_offset);
141+
assert(8 == pad_layout.dpad_bit_offset);
142142
assert(4 == pad_layout.dpad_bit_size);
143143

144-
assert(24 == pad_layout.button_bit_offset);
144+
assert(16 == pad_layout.button_bit_offset);
145145
assert(15 == pad_layout.button_bit_count);
146146

147-
assert(40 == pad_layout.lx_bit_offset);
147+
assert(32 == pad_layout.lx_bit_offset);
148148
assert(8 == pad_layout.lx_bit_size);
149149

150-
assert(48 == pad_layout.ly_bit_offset);
150+
assert(40 == pad_layout.ly_bit_offset);
151151
assert(8 == pad_layout.ly_bit_size);
152152

153-
assert(64 == pad_layout.rx_bit_offset);
153+
assert(48 == pad_layout.rx_bit_offset);
154154
assert(8 == pad_layout.rx_bit_size);
155155

156-
assert(72 == pad_layout.ry_bit_offset);
156+
assert(56 == pad_layout.ry_bit_offset);
157157
assert(8 == pad_layout.ry_bit_size);
158158

159-
assert(88 == pad_layout.lt_bit_offset);
159+
assert(64 == pad_layout.lt_bit_offset);
160160
assert(8 == pad_layout.lt_bit_size);
161161

162-
assert(96 == pad_layout.rt_bit_offset);
162+
assert(72 == pad_layout.rt_bit_offset);
163163
assert(8 == pad_layout.rt_bit_size);
164164

165165
printf("Stadia controller passed\n");

test_native/test_hid.o

40 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)