From 0ab690dae117fcc4e8fc88558e7f473aef3faa47 Mon Sep 17 00:00:00 2001 From: Christoffer Martinsson Date: Thu, 29 May 2025 12:11:29 +0200 Subject: [PATCH] Fixed Linux issue that detect joystick as gamepad --- rp2040/src/usb_joystick_device.rs | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/rp2040/src/usb_joystick_device.rs b/rp2040/src/usb_joystick_device.rs index aac6b94..1d4c292 100644 --- a/rp2040/src/usb_joystick_device.rs +++ b/rp2040/src/usb_joystick_device.rs @@ -67,17 +67,19 @@ pub const JOYSTICK_DESCRIPTOR: &[u8] = &[ 0x09, 0x04, // Usage (Joystick) 0xA1, 0x01, // Collection (Application) - // 6 signed 16-bit axes: X, Y, Z, Rx, Ry, Rz + // 8 signed 16-bit axes: X, Y, Z, Rx, Ry, Rz, Slider, Dial 0x09, 0x30, // Usage (X) 0x09, 0x31, // Usage (Y) 0x09, 0x32, // Usage (Z) 0x09, 0x33, // Usage (Rx) 0x09, 0x34, // Usage (Ry) 0x09, 0x35, // Usage (Rz) + 0x09, 0x36, // Usage (Slider) + 0x09, 0x37, // Usage (Dial) 0x16, 0x00, 0x80, // Logical Minimum (-32768) 0x26, 0xFF, 0x7F, // Logical Maximum (32767) 0x75, 0x10, // Report Size (16) - 0x95, 0x06, // Report Count (6) + 0x95, 0x08, // Report Count (8) 0x81, 0x02, // Input (Data,Var,Abs) // 26 Buttons (1-bit each) @@ -106,16 +108,18 @@ pub struct JoystickReport { pub rx: i16, // 16bit pub ry: i16, // 16bit pub rz: i16, // 16bit + pub slider: i16, // 16bit + pub dial: i16, // 16bit pub buttons: u32, // 32bit } pub struct Joystick<'a, B: UsbBus> { - interface: Interface<'a, B, InBytes16, OutNone, ReportSingle>, + interface: Interface<'a, B, InBytes32, OutNone, ReportSingle>, } impl Joystick<'_, B> { pub fn write_report(&mut self, report: &JoystickReport) -> Result<(), UsbHidError> { - let mut data: [u8; 16] = [0; 16]; + let mut data: [u8; 20] = [0; 20]; // Did not make the packed struct work, so doing it manually data[0] = report.x as u8; @@ -130,10 +134,14 @@ impl Joystick<'_, B> { data[9] = (report.ry >> 8) as u8; data[10] = report.rz as u8; data[11] = (report.rz >> 8) as u8; - data[12] = report.buttons as u8; - data[13] = (report.buttons >> 8) as u8; - data[14] = (report.buttons >> 16) as u8; - data[15] = (report.buttons >> 24) as u8; + data[12] = report.slider as u8; + data[13] = (report.slider >> 8) as u8; + data[14] = report.dial as u8; + data[15] = (report.dial >> 8) as u8; + data[16] = report.buttons as u8; + data[17] = (report.buttons >> 8) as u8; + data[18] = (report.buttons >> 16) as u8; + data[19] = (report.buttons >> 24) as u8; self.interface .write_report(&data) @@ -143,7 +151,7 @@ impl Joystick<'_, B> { } impl<'a, B: UsbBus> DeviceClass<'a> for Joystick<'a, B> { - type I = Interface<'a, B, InBytes16, OutNone, ReportSingle>; + type I = Interface<'a, B, InBytes32, OutNone, ReportSingle>; fn interface(&mut self) -> &mut Self::I { &mut self.interface @@ -157,7 +165,7 @@ impl<'a, B: UsbBus> DeviceClass<'a> for Joystick<'a, B> { } pub struct JoystickConfig<'a> { - interface: InterfaceConfig<'a, InBytes16, OutNone, ReportSingle>, + interface: InterfaceConfig<'a, InBytes32, OutNone, ReportSingle>, } impl Default for JoystickConfig<'_> { @@ -178,7 +186,7 @@ impl Default for JoystickConfig<'_> { impl<'a> JoystickConfig<'a> { #[must_use] - pub fn new(interface: InterfaceConfig<'a, InBytes16, OutNone, ReportSingle>) -> Self { + pub fn new(interface: InterfaceConfig<'a, InBytes32, OutNone, ReportSingle>) -> Self { Self { interface } } }