Skip to content

Commit bcf74e9

Browse files
committed
update(gamepad): update xinput report and desc
Signed-off-by: sakumisu <1203593632@qq.com>
1 parent bce3346 commit bcf74e9

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

class/gamepad/usb_gamepad.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ struct xinput_out_report {
143143
} __PACKED;
144144

145145
// clang-format off
146-
#define XINPUT_DESCRIPTOR_LEN (9 + 16 + 7 + 7)
146+
#define XINPUT_DESCRIPTOR_LEN (9 + 17 + 7 + 7)
147147

148148
#define XINPUT_DESCRIPTOR_INIT(bInterfaceNumber, out_ep, in_ep) \
149149
USB_INTERFACE_DESCRIPTOR_INIT(bInterfaceNumber, 0x00, 0x02, 0xff, 0x5d, 0x01, 0x00), /* XInput proprietary descriptor (0x21) */ \
150-
16, 0x21, 0x00, 0x01, 0x01, 0x24, in_ep, 0x14, 0x03, 0x00, 0x03, 0x13, out_ep, 0x00, 0x03, 0x00, \
151-
USB_ENDPOINT_DESCRIPTOR_INIT(in_ep, 0x03, 32, 0x01), \
150+
0x11, 0x21, 0x00, 0x01, 0x01, 0x25, in_ep, 0x14, 0x00, 0x00, 0x00, 0x00, 0x13, out_ep, 0x08, 0x00, 0x00, \
151+
USB_ENDPOINT_DESCRIPTOR_INIT(in_ep, 0x03, 32, 0x01), \
152152
USB_ENDPOINT_DESCRIPTOR_INIT(out_ep, 0x03, 32, 0x08)
153153
// clang-format on
154154

class/gamepad/usbd_gamepad.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ static int xinput_vendor_class_request_handler(uint8_t busid, struct usb_setup_p
2121
return 0;
2222
}
2323

24+
// Convert analog value from Joypad (0-255, center 128) to signed 16-bit
25+
static int16_t convert_axis_to_s16(uint8_t value)
26+
{
27+
int32_t scaled = ((int32_t)value - 128) * 256;
28+
if (scaled < -32768) scaled = -32768;
29+
if (scaled > 32767) scaled = 32767;
30+
return (int16_t)scaled;
31+
}
32+
33+
// Convert and invert axis (for Y-axis where convention differs)
34+
static int16_t convert_axis_to_s16_inverted(uint8_t value)
35+
{
36+
int32_t scaled = -((int32_t)value - 128) * 256;
37+
if (scaled < -32768) scaled = -32768;
38+
if (scaled > 32767) scaled = 32767;
39+
return (int16_t)scaled;
40+
}
41+
2442
int usbd_gamepad_xinput_send_report(uint8_t ep, struct usb_gamepad_report *report)
2543
{
2644
struct xinput_in_report *xinput_report;
@@ -68,6 +86,13 @@ int usbd_gamepad_xinput_send_report(uint8_t ep, struct usb_gamepad_report *repor
6886
if (xinput_report->rt == 0 && (report->buttons & USB_GAMEPAD_BUTTON_R2))
6987
xinput_report->rt = 0xFF;
7088

89+
// Analog sticks (signed 16-bit, -32768 to +32767)
90+
// Y-axis inverted: input 0=down, XInput convention positive=up
91+
xinput_report->lx = convert_axis_to_s16(report->lx);
92+
xinput_report->ly = convert_axis_to_s16_inverted(report->ly);
93+
xinput_report->rx = convert_axis_to_s16(report->rx);
94+
xinput_report->ry = convert_axis_to_s16_inverted(report->ry);
95+
7196
return usbd_ep_start_write(0, ep, gamepad_report_buffer, sizeof(struct xinput_in_report));
7297
}
7398

demo/gamepad_template.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
8989

9090
static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
9191
{
92-
if (index >= (sizeof(string_descriptors) / sizeof(char *))) {
92+
if (index >= 4) {
9393
return NULL;
9494
}
9595

0 commit comments

Comments
 (0)