diff --git a/rootfs/usr/lib/udev/rules.d/99-inputplumber-device-setup.rules b/rootfs/usr/lib/udev/rules.d/99-inputplumber-device-setup.rules index 6cd221b1..3d1918c2 100644 --- a/rootfs/usr/lib/udev/rules.d/99-inputplumber-device-setup.rules +++ b/rootfs/usr/lib/udev/rules.d/99-inputplumber-device-setup.rules @@ -8,7 +8,7 @@ ACTION=="add|change|bind", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="e31[01]", ACTION=="add|change|bind", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="e31[01]", SUBSYSTEM=="hid", DRIVER=="hid-lenovo-go-s", ATTR{gamepad/auto_sleep_time}="0", ATTR{gamepad/dpad_mode}="8-way", ATTR{gamepad/poll_rate}="250", ATTR{os_mode}="linux", ATTR{touchpad/linux_mode}="absolute", GOTO="inputplumber_setup_end" # Legion Go/Go2 Controller Settings -ACTION=="add|change|bind", ATTRS{idVendor}=="17ef", ATTRS{idProduct}=="61e[bcde]", SUBSYSTEM=="hid", DRIVER=="hid-lenovo-go", ATTR{os_mode}="linux", ATTR{left_handle/imu_bypass_enable}="true", ATTR{right_handle/imu_bypass_enable}="true", ATTR{touchpad/vibration_enable}="false", GOTO="inputplumber_setup_end" +ACTION=="add|change|bind", ATTRS{idVendor}=="17ef", ATTRS{idProduct}=="61e[bcde]", SUBSYSTEM=="hid", DRIVER=="hid-lenovo-go", ATTR{os_mode}="linux", ATTR{left_handle/imu_bypass_enabled}="true", ATTR{right_handle/imu_bypass_enabled}="true", ATTR{touchpad/vibration_enabled}="false", GOTO="inputplumber_setup_end" # ASUS # ROG Ally Controller Settings diff --git a/rootfs/usr/share/inputplumber/devices/50-ayaneo_2.yaml b/rootfs/usr/share/inputplumber/devices/50-ayaneo_2.yaml index 2a5c13a0..67cb8dbd 100644 --- a/rootfs/usr/share/inputplumber/devices/50-ayaneo_2.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-ayaneo_2.yaml @@ -60,8 +60,8 @@ source_devices: imu: mount_matrix: x: [0, -1, 0] - y: [0, 0, 1] - z: [-1, 0, 0] + y: [-1, 0, 0] + z: [0, 0, -1] #RGB - group: led diff --git a/rootfs/usr/share/inputplumber/devices/50-ayaneo_2021.yaml b/rootfs/usr/share/inputplumber/devices/50-ayaneo_2021.yaml index 368cedd3..784fa23a 100644 --- a/rootfs/usr/share/inputplumber/devices/50-ayaneo_2021.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-ayaneo_2021.yaml @@ -66,8 +66,8 @@ source_devices: imu: mount_matrix: x: [0, -1, 0] - y: [0, 0, 1] - z: [-1, 0, 0] + y: [-1, 0, 0] + z: [0, 0, -1] # Optional configuration for the composite device options: diff --git a/rootfs/usr/share/inputplumber/devices/50-ayaneo_2s.yaml b/rootfs/usr/share/inputplumber/devices/50-ayaneo_2s.yaml index 4b1e2abb..3cb236ad 100644 --- a/rootfs/usr/share/inputplumber/devices/50-ayaneo_2s.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-ayaneo_2s.yaml @@ -63,8 +63,8 @@ source_devices: imu: mount_matrix: x: [0, -1, 0] - y: [0, 0, 1] - z: [-1, 0, 0] + y: [-1, 0, 0] + z: [0, 0, -1] #RGB - group: led diff --git a/rootfs/usr/share/inputplumber/devices/50-ayaneo_air.yaml b/rootfs/usr/share/inputplumber/devices/50-ayaneo_air.yaml index 91ca4055..cccb74d7 100644 --- a/rootfs/usr/share/inputplumber/devices/50-ayaneo_air.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-ayaneo_air.yaml @@ -52,10 +52,10 @@ source_devices: name: "{i2c-BMI0160:00,bmi260}" config: imu: - mount_matrix: # TODO: Verify on actual hardware + mount_matrix: x: [0, -1, 0] - y: [0, 0, 1] - z: [-1, 0, 0] + y: [-1, 0, 0] + z: [0, 0, -1] #RGB - group: led diff --git a/rootfs/usr/share/inputplumber/devices/50-ayaneo_air_plus.yaml b/rootfs/usr/share/inputplumber/devices/50-ayaneo_air_plus.yaml index 1509b5df..389104ee 100644 --- a/rootfs/usr/share/inputplumber/devices/50-ayaneo_air_plus.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-ayaneo_air_plus.yaml @@ -66,8 +66,8 @@ source_devices: imu: mount_matrix: x: [0, -1, 0] - y: [0, 0, -1] - z: [1, 0, 0] + y: [1, 0, 0] + z: [0, 0, 1] #RGB - group: led diff --git a/rootfs/usr/share/inputplumber/devices/50-ayaneo_air_plus_mendo.yaml b/rootfs/usr/share/inputplumber/devices/50-ayaneo_air_plus_mendo.yaml index 3dc916b1..6b3dc358 100644 --- a/rootfs/usr/share/inputplumber/devices/50-ayaneo_air_plus_mendo.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-ayaneo_air_plus_mendo.yaml @@ -57,8 +57,8 @@ source_devices: mount_matrix: # TODO: Verify on actual hardware x: [0, -1, 0] - y: [0, 0, -1] - z: [-1, 0, 0] + y: [1, 0, 0] + z: [0, 0, 1] #RGB - group: led diff --git a/rootfs/usr/share/inputplumber/devices/50-ayaneo_flip.yaml b/rootfs/usr/share/inputplumber/devices/50-ayaneo_flip.yaml index c84c1a03..c7741b72 100644 --- a/rootfs/usr/share/inputplumber/devices/50-ayaneo_flip.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-ayaneo_flip.yaml @@ -60,8 +60,8 @@ source_devices: imu: mount_matrix: x: [0, -1, 0] - y: [0, 0, 1] - z: [-1, 0, 0] + y: [0, 0, -1] + z: [1, 0, 0] - group: touchscreen udev: properties: diff --git a/rootfs/usr/share/inputplumber/devices/50-ayaneo_next.yaml b/rootfs/usr/share/inputplumber/devices/50-ayaneo_next.yaml index e2fcae4c..0df4e809 100644 --- a/rootfs/usr/share/inputplumber/devices/50-ayaneo_next.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-ayaneo_next.yaml @@ -71,8 +71,8 @@ source_devices: imu: mount_matrix: x: [0, -1, 0] - y: [0, 0, 1] - z: [1, 0, 0] + y: [-1, 0, 0] + z: [0, 0, -1] # Optional configuration for the composite device options: diff --git a/rootfs/usr/share/inputplumber/devices/50-ayn_loki_max.yaml b/rootfs/usr/share/inputplumber/devices/50-ayn_loki_max.yaml index d5e426e3..9892ee2a 100644 --- a/rootfs/usr/share/inputplumber/devices/50-ayn_loki_max.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-ayn_loki_max.yaml @@ -50,8 +50,8 @@ source_devices: imu: mount_matrix: x: [1, 0, 0] - y: [0, 0, 1] - z: [0, -1, 0] + y: [0, -1, 0] + z: [0, 0, -1] #RGB - group: led diff --git a/rootfs/usr/share/inputplumber/devices/50-ayn_loki_mini_pro.yaml b/rootfs/usr/share/inputplumber/devices/50-ayn_loki_mini_pro.yaml index c4599ccd..27f078fb 100644 --- a/rootfs/usr/share/inputplumber/devices/50-ayn_loki_mini_pro.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-ayn_loki_mini_pro.yaml @@ -40,8 +40,8 @@ source_devices: mount_matrix: # TODO: Verify on actual hardware x: [1, 0, 0] - y: [0, 0, 1] - z: [0, 1, 0] + y: [0, -1, 0] + z: [0, 0, -1] #RGB - group: led diff --git a/rootfs/usr/share/inputplumber/devices/50-ayn_loki_zero.yaml b/rootfs/usr/share/inputplumber/devices/50-ayn_loki_zero.yaml index fa00929b..48094d00 100644 --- a/rootfs/usr/share/inputplumber/devices/50-ayn_loki_zero.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-ayn_loki_zero.yaml @@ -39,8 +39,8 @@ source_devices: imu: mount_matrix: # TODO: Verify on actual hardware x: [1, 0, 0] - y: [0, 0, 1] - z: [0, 1, 0] + y: [0, -1, 0] + z: [0, 0, -1] #RGB - group: led diff --git a/rootfs/usr/share/inputplumber/devices/50-legion_go.yaml b/rootfs/usr/share/inputplumber/devices/50-legion_go.yaml index ae86c53a..df0b16e5 100644 --- a/rootfs/usr/share/inputplumber/devices/50-legion_go.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-legion_go.yaml @@ -222,8 +222,8 @@ source_devices: imu: mount_matrix: x: [0, 1, 0] - y: [0, 0, 1] - z: [-1, 0, 0] + y: [-1, 0, 0] + z: [0, 0, 1] accel_correction: 10.0 # adm_sfh rdesktop scale compensation gyro_correction: 100.0 - group: imu @@ -233,9 +233,9 @@ source_devices: imu: mount_matrix: x: [0, -1, 0] - y: [0, 0, 1] - z: [1, 0, 0] - accel_correction: 10.0 # adm_sfh rdesktop scale compensation + y: [1, 0, 0] + z: [0, 0, -1] + accel_correction: 10.5 # adm_sfh rdesktop scale compensation gyro_correction: 100.0 # Optional configuration for the composite device diff --git a/rootfs/usr/share/inputplumber/devices/50-msi_claw_a8_bz2e.yaml b/rootfs/usr/share/inputplumber/devices/50-msi_claw_a8_bz2e.yaml index 23ca796e..858ef992 100644 --- a/rootfs/usr/share/inputplumber/devices/50-msi_claw_a8_bz2e.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-msi_claw_a8_bz2e.yaml @@ -142,10 +142,6 @@ source_devices: name: gyro_3d config: imu: - mount_matrix: - x: [1, 0, 0] - y: [0, 0, 1] - z: [0, 1, 0] accel_correction: 10.0 # adm_sfh rdesktop scale compensation gyro_correction: 100.0 - group: imu @@ -155,9 +151,9 @@ source_devices: imu: mount_matrix: x: [-1, 0, 0] - y: [0, 0, 1] - z: [0, -1, 0] - accel_correction: 10.0 # adm_sfh rdesktop scale compensation + y: [0, -1, 0] + z: [0, 0, -1] + accel_correction: 10.5 # adm_sfh rdesktop scale compensation gyro_correction: 100.0 # Optional configuration for the composite device diff --git a/rootfs/usr/share/inputplumber/devices/50-onexplayer_3.yaml b/rootfs/usr/share/inputplumber/devices/50-onexplayer_3.yaml index c6dce18c..8d3ca96a 100644 --- a/rootfs/usr/share/inputplumber/devices/50-onexplayer_3.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-onexplayer_3.yaml @@ -49,9 +49,9 @@ source_devices: iio: name: bmi260 mount_matrix: - x: [0, 1, 0] - y: [1, 0, 0] - z: [0, 0, 1] + x: [0, -1, 0] + y: [-1, 0, 0] + z: [0, 0, -1] # Optional configuration for the composite device options: diff --git a/rootfs/usr/share/inputplumber/devices/50-onexplayer_onexfly.yaml b/rootfs/usr/share/inputplumber/devices/50-onexplayer_onexfly.yaml index 72dbfe99..5fe1b960 100644 --- a/rootfs/usr/share/inputplumber/devices/50-onexplayer_onexfly.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-onexplayer_onexfly.yaml @@ -73,8 +73,8 @@ source_devices: imu: mount_matrix: x: [0, -1, 0] - y: [0, 0, 1] - z: [-1, 0, 0] + y: [-1, 0, 0] + z: [0, 0, -1] # Optional configuration for the composite device options: diff --git a/rootfs/usr/share/inputplumber/devices/50-onexplayer_x1.yaml b/rootfs/usr/share/inputplumber/devices/50-onexplayer_x1.yaml index d4f3aa58..6252d3ca 100644 --- a/rootfs/usr/share/inputplumber/devices/50-onexplayer_x1.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-onexplayer_x1.yaml @@ -63,8 +63,8 @@ source_devices: mount_matrix: # TODO: Verify on actual hardware x: [0, 1, 0] - y: [0, 0, 1] - z: [-1, 0, 0] + y: [1, 0, 0] + z: [0, 0, -1] # External / docked controllers: joined dock supplies power and 2.4 GHz to the # handheld. The gamepad entry is the receiver's XInput-style device; the fe02 diff --git a/rootfs/usr/share/inputplumber/devices/50-orangepi_neo.yaml b/rootfs/usr/share/inputplumber/devices/50-orangepi_neo.yaml index a160036c..b02fea71 100644 --- a/rootfs/usr/share/inputplumber/devices/50-orangepi_neo.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-orangepi_neo.yaml @@ -64,8 +64,8 @@ source_devices: imu: mount_matrix: x: [1, 0, 0] - y: [0, 0, 1] - z: [0, -1, 0] + y: [0, -1, 0] + z: [0, 0, -1] # Optional configuration for the composite device options: diff --git a/rootfs/usr/share/inputplumber/devices/50-rog_ally.yaml b/rootfs/usr/share/inputplumber/devices/50-rog_ally.yaml index 70e78b30..f1d43721 100644 --- a/rootfs/usr/share/inputplumber/devices/50-rog_ally.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-rog_ally.yaml @@ -80,8 +80,8 @@ source_devices: imu: mount_matrix: x: [1, 0, 0] - y: [0, 0, 1] - z: [0, -1, 0] + y: [0, -1, 0] + z: [0, 0, -1] - group: led udev: sys_name: ally:rgb:joystick_rings diff --git a/rootfs/usr/share/inputplumber/devices/50-rog_ally_x.yaml b/rootfs/usr/share/inputplumber/devices/50-rog_ally_x.yaml index deee2d4f..2632aec6 100644 --- a/rootfs/usr/share/inputplumber/devices/50-rog_ally_x.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-rog_ally_x.yaml @@ -79,8 +79,8 @@ source_devices: imu: mount_matrix: x: [1, 0, 0] - y: [0, 0, 1] - z: [0, -1, 0] + y: [0, -1, 0] + z: [0, 0, -1] - group: led udev: sys_name: ally:rgb:joystick_rings diff --git a/rootfs/usr/share/inputplumber/devices/50-rog_xbox_ally.yaml b/rootfs/usr/share/inputplumber/devices/50-rog_xbox_ally.yaml index 09e2b2e5..76253821 100644 --- a/rootfs/usr/share/inputplumber/devices/50-rog_xbox_ally.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-rog_xbox_ally.yaml @@ -79,8 +79,8 @@ source_devices: mount_matrix: # TODO: Verify on actual hardware x: [1, 0, 0] - y: [0, 0, 1] - z: [0, 1, 0] + y: [0, -1, 0] + z: [0, 0, -1] - group: led udev: sys_name: ally:rgb:joystick_rings diff --git a/rootfs/usr/share/inputplumber/devices/50-rog_xbox_ally_x20.yaml b/rootfs/usr/share/inputplumber/devices/50-rog_xbox_ally_x20.yaml index 24bd3e61..c4a0efab 100644 --- a/rootfs/usr/share/inputplumber/devices/50-rog_xbox_ally_x20.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-rog_xbox_ally_x20.yaml @@ -75,8 +75,8 @@ source_devices: imu: mount_matrix: x: [-1, 0, 0] - y: [0, 0, 1] - z: [0, -1, 0] + y: [0, -1, 0] + z: [0, 0, 1] - group: led udev: sys_name: ally:rgb:joystick_rings diff --git a/rootfs/usr/share/inputplumber/devices/50-zotac-zone.yaml b/rootfs/usr/share/inputplumber/devices/50-zotac-zone.yaml index e4be78c4..8dbbf89c 100644 --- a/rootfs/usr/share/inputplumber/devices/50-zotac-zone.yaml +++ b/rootfs/usr/share/inputplumber/devices/50-zotac-zone.yaml @@ -73,8 +73,8 @@ source_devices: imu: mount_matrix: x: [0, -1, 0] - y: [0, 0, 1] - z: [-1, 0, 0] + y: [-1, 0, 0] + z: [0, 0, -1] #RGB - group: led diff --git a/src/drivers/iio_imu/driver.rs b/src/drivers/iio_imu/driver.rs index 56a9cb7b..123ec530 100644 --- a/src/drivers/iio_imu/driver.rs +++ b/src/drivers/iio_imu/driver.rs @@ -118,6 +118,18 @@ impl Driver { }) } + /// The event kinds the device's discovered channel groups can produce. + pub fn supported_events(&self) -> Vec { + let mut events = Vec::new(); + if !self.layout.accel.is_empty() { + events.push(Event::Accelerometer(AxisData::default())); + } + if !self.layout.gyro.is_empty() { + events.push(Event::Gyroscope(AxisData::default())); + } + events + } + pub fn update_filtered_events(&mut self, events: Vec) { self.filtered_events = events; } diff --git a/src/drivers/iio_imu/info.rs b/src/drivers/iio_imu/info.rs index 499d50ab..ece4a433 100644 --- a/src/drivers/iio_imu/info.rs +++ b/src/drivers/iio_imu/info.rs @@ -29,33 +29,24 @@ pub struct MountMatrix { impl MountMatrix { /// Create a new [MountMatrix] from the given mount matrix string - /// Example: - /// "1, 0, 0; 0, 1, 0; 0, 0, 1" + /// Examples: + /// "1, 0, 0; 0, 1, 0; 0, 0, 1", "1 0 0 0 1 0 0 0 1" pub fn new(matrix_str: String) -> Result> { - let parts: Vec<&str> = matrix_str.split(';').map(|part| part.trim()).collect(); - if parts.len() != 3 { - return Err("Not enough parts found in the matrix".into()); + let values: Result, _> = matrix_str + .split(|c: char| c == ',' || c == ';' || c.is_whitespace()) + .filter(|part| !part.is_empty()) + .map(|part| part.parse::()) + .collect(); + let values = values?; + if values.len() != 9 { + return Err("Expected nine numbers in the matrix".into()); } - let mut matrix = MountMatrix::default(); - for (i, part) in parts.into_iter().enumerate() { - let sub_parts: Vec<&str> = part.split(',').map(|part| part.trim()).collect(); - if sub_parts.len() != 3 { - return Err("Not enough subparts".into()); - } - let x = sub_parts.first().unwrap().parse::()?; - let y = sub_parts.get(1).unwrap().parse::()?; - let z = sub_parts.get(2).unwrap().parse::()?; - - match i { - 0 => matrix.x = (x, y, z), - 1 => matrix.y = (x, y, z), - 2 => matrix.z = (x, y, z), - _ => (), - } - } - - Ok(matrix) + Ok(MountMatrix { + x: (values[0], values[1], values[2]), + y: (values[3], values[4], values[5]), + z: (values[6], values[7], values[8]), + }) } } diff --git a/src/drivers/legion_go/go2_driver.rs b/src/drivers/legion_go/go2_driver.rs index 2a802693..54f21ba1 100644 --- a/src/drivers/legion_go/go2_driver.rs +++ b/src/drivers/legion_go/go2_driver.rs @@ -11,7 +11,7 @@ use super::{ AxisEvent, BinaryInput, Event, GamepadButtonEvent, ImuAxisInput, JoyAxisInput, MouseWheelInput, TriggerEvent, TriggerInput, }, - hid_report::{GamepadMode, XInputDataReport}, + hid_report::{ConnectedState, GamepadMode, XInputDataReport}, GO2_PIDS, GP_IID, TIMEOUT, VID, XINPUT_COMMAND_ID, XINPUT_DATA, XINPUT_PACKET_SIZE, }; @@ -187,6 +187,75 @@ impl Driver { old_state } + /// One handle's raw accelerometer axes, sign-corrected so both handles agree. + fn get_accel_raw(&self, state: &XInputDataReport, right: bool) -> (i32, i32, i32) { + let (x, y, z) = if right { + ( + state.right_accel_x, + state.right_accel_y, + state.right_accel_z, + ) + } else { + (state.left_accel_x, state.left_accel_y, state.left_accel_z) + }; + let z: i32 = if right { i32::from(z) } else { -i32::from(z) }; + + (-i32::from(x), -i32::from(y), z) + } + + /// One handle's raw gyroscope axes, sign-corrected so both handles agree. + fn get_gyro_raw(&self, state: &XInputDataReport, right: bool) -> (i32, i32, i32) { + let (x, y, z) = if right { + (state.right_gyro_x, state.right_gyro_y, state.right_gyro_z) + } else { + (state.left_gyro_x, state.left_gyro_y, state.left_gyro_z) + }; + let z_sign: i32 = match (self.gen, right) { + (LegionGoGen::Gen2, _) => -1, + (LegionGoGen::Gen1, true) => -1, + (LegionGoGen::Gen1, false) => 1, + }; + + (-i32::from(x), -i32::from(y), z_sign * i32::from(z)) + } + + /// Map accelerometer axes onto the pitch, roll and yaw of the report. + fn get_accel_corrected(axes: (i32, i32, i32)) -> ImuAxisInput { + let (x, y, z) = axes; + let (pitch, roll, yaw) = (x, -z, y); + + ImuAxisInput { + pitch: pitch as i16, + roll: roll as i16, + yaw: yaw as i16, + } + } + + /// Map gyroscope axes onto the pitch, roll and yaw of the report. + fn get_gyro_corrected(axes: (i32, i32, i32)) -> ImuAxisInput { + let (x, y, z) = axes; + + ImuAxisInput { + pitch: x as i16, + roll: z as i16, + yaw: y as i16, + } + } + + /// Average the handles that report attached, so a detached half cannot halve it. + fn average_attached(handles: &[(i32, i32, i32)]) -> Option<(i32, i32, i32)> { + if handles.is_empty() { + return None; + } + + let count = handles.len() as i32; + let x = handles.iter().map(|handle| handle.0).sum::() / count; + let y = handles.iter().map(|handle| handle.1).sum::() / count; + let z = handles.iter().map(|handle| handle.2).sum::() / count; + + Some((x, y, z)) + } + /// Translate the state into individual events fn translate_xinput(&mut self, old_state: Option) -> Vec { let mut events = Vec::new(); @@ -481,32 +550,25 @@ impl Driver { log::trace!("Left controller connected state: {:?}", state.l_con_state); log::trace!("Right controller connected state: {:?}", state.r_con_state); } + let left_attached = state.l_con_state == ConnectedState::Attached; + let right_attached = state.r_con_state == ConnectedState::Attached; + if state.left_accel_x != old_state.left_accel_x || state.left_accel_y != old_state.left_accel_y || state.left_accel_z != old_state.left_accel_z { - self.emit( - &mut events, - Event::Axis(AxisEvent::LeftAccel(ImuAxisInput { - pitch: -state.left_accel_x, - roll: -state.left_accel_y, - yaw: -state.left_accel_z, - })), - ) + let value = Self::get_accel_corrected(self.get_accel_raw(&state, false)); + self.emit(&mut events, Event::Axis(AxisEvent::LeftAccel(value))); } + if state.right_accel_x != old_state.right_accel_x || state.right_accel_y != old_state.right_accel_y || state.right_accel_z != old_state.right_accel_z { - self.emit( - &mut events, - Event::Axis(AxisEvent::RightAccel(ImuAxisInput { - pitch: -state.right_accel_x, - roll: -state.right_accel_y, - yaw: state.right_accel_z, - })), - ) + let value = Self::get_accel_corrected(self.get_accel_raw(&state, true)); + self.emit(&mut events, Event::Axis(AxisEvent::RightAccel(value))); } + if state.left_accel_x != old_state.left_accel_x || state.left_accel_y != old_state.left_accel_y || state.left_accel_z != old_state.left_accel_z @@ -514,43 +576,34 @@ impl Driver { || state.right_accel_y != old_state.right_accel_y || state.right_accel_z != old_state.right_accel_z { - self.emit( - &mut events, - Event::Axis(AxisEvent::MultiAccel(ImuAxisInput { - pitch: -(state.left_accel_x + state.right_accel_x) / 2, - roll: -(state.left_accel_y + state.right_accel_y) / 2, - yaw: (-state.left_accel_z + state.right_accel_z) / 2, - })), - ) + let mut handles = Vec::new(); + if left_attached { + handles.push(self.get_accel_raw(&state, false)); + } + if right_attached { + handles.push(self.get_accel_raw(&state, true)); + } + + if let Some(axes) = Self::average_attached(&handles) { + let value = Self::get_accel_corrected(axes); + self.emit(&mut events, Event::Axis(AxisEvent::MultiAccel(value))); + } } + if state.left_gyro_x != old_state.left_gyro_x || state.left_gyro_y != old_state.left_gyro_y || state.left_gyro_z != old_state.left_gyro_z { - self.emit( - &mut events, - Event::Axis(AxisEvent::LeftGyro(ImuAxisInput { - pitch: -state.left_gyro_x, - roll: -state.left_gyro_y, - yaw: match self.gen { - LegionGoGen::Gen2 => -state.left_gyro_z, - LegionGoGen::Gen1 => state.left_gyro_z, - }, - })), - ) + let value = Self::get_gyro_corrected(self.get_gyro_raw(&state, false)); + self.emit(&mut events, Event::Axis(AxisEvent::LeftGyro(value))); } + if state.right_gyro_x != old_state.right_gyro_x || state.right_gyro_y != old_state.right_gyro_y || state.right_gyro_z != old_state.right_gyro_z { - self.emit( - &mut events, - Event::Axis(AxisEvent::RightGyro(ImuAxisInput { - pitch: -state.right_gyro_x, - roll: -state.right_gyro_y, - yaw: -state.right_gyro_z, - })), - ) + let value = Self::get_gyro_corrected(self.get_gyro_raw(&state, true)); + self.emit(&mut events, Event::Axis(AxisEvent::RightGyro(value))); } if state.left_gyro_x != old_state.left_gyro_x @@ -560,17 +613,18 @@ impl Driver { || state.right_gyro_y != old_state.right_gyro_y || state.right_gyro_z != old_state.right_gyro_z { - self.emit( - &mut events, - Event::Axis(AxisEvent::MultiGyro(ImuAxisInput { - pitch: -(state.left_gyro_x + state.right_gyro_x) / 2, - roll: -(state.left_gyro_y + state.right_gyro_y) / 2, - yaw: match self.gen { - LegionGoGen::Gen2 => -(state.left_gyro_z + state.right_gyro_z) / 2, - LegionGoGen::Gen1 => (state.left_gyro_z - state.right_gyro_z) / 2, - }, - })), - ) + let mut handles = Vec::new(); + if left_attached { + handles.push(self.get_gyro_raw(&state, false)); + } + if right_attached { + handles.push(self.get_gyro_raw(&state, true)); + } + + if let Some(axes) = Self::average_attached(&handles) { + let value = Self::get_gyro_corrected(axes); + self.emit(&mut events, Event::Axis(AxisEvent::MultiGyro(value))); + } } } events diff --git a/src/drivers/legion_go/mod.rs b/src/drivers/legion_go/mod.rs index 24d5314b..a5546ab0 100644 --- a/src/drivers/legion_go/mod.rs +++ b/src/drivers/legion_go/mod.rs @@ -81,8 +81,7 @@ pub const TRIGG_MAX: f64 = 255.0; // Accel scale is 4096 LSB/g (±2 g) pub const GO2_ACCEL_RAW_TO_MPS2: f64 = 9.80665 / 4096.0; -// Gyro scale is 32768 LSB full scale over +/-2000 deg/s (16.384 LSB/(deg/s)) -pub const GO2_GYRO_RAW_TO_RAD_S: f64 = (2000.0 / 32768.0) * (PI / 180.0); +pub const GO2_GYRO_RAW_TO_RAD_S: f64 = (70.0 / 1000.0) * (PI / 180.0); fn default_event_filter() -> Vec { vec![ diff --git a/src/drivers/legion_go_s/imu_driver.rs b/src/drivers/legion_go_s/imu_driver.rs index 86a84ac2..d8081fa6 100644 --- a/src/drivers/legion_go_s/imu_driver.rs +++ b/src/drivers/legion_go_s/imu_driver.rs @@ -149,11 +149,9 @@ impl IMUDriver { if state.x != old_state.x || state.y != old_state.y || state.z != old_state.z { self.emit( &mut events, - Event::Inertia(InertialEvent::Accelerometer(InertialInput { - x: -state.x.to_primitive(), - y: state.z.to_primitive(), - z: -state.y.to_primitive(), - })), + Event::Inertia(InertialEvent::Accelerometer(Self::get_axes_corrected( + state, + ))), ) }; @@ -175,14 +173,19 @@ impl IMUDriver { if state.x != old_state.x || state.y != old_state.y || state.z != old_state.z { self.emit( &mut events, - Event::Inertia(InertialEvent::Gyroscope(InertialInput { - x: -state.x.to_primitive(), - y: -state.z.to_primitive(), - z: -state.y.to_primitive(), - })), + Event::Inertia(InertialEvent::Gyroscope(Self::get_axes_corrected(state))), ) }; events } + + /// The report's three axes with their signs corrected. + fn get_axes_corrected(state: InertialDataReport) -> InertialInput { + InertialInput { + x: -state.x.to_primitive(), + y: -state.y.to_primitive(), + z: -state.z.to_primitive(), + } + } } diff --git a/src/input/event/value.rs b/src/input/event/value.rs index 253579da..4de43d24 100644 --- a/src/input/event/value.rs +++ b/src/input/event/value.rs @@ -128,11 +128,37 @@ pub fn denormalize_unsigned_value_u8(normal_value: f64, max: f64) -> u8 { (normal_value * max).round() as u8 } +/// Scale an acceleration vector down so that no axis leaves the range the report can carry. +pub fn scale_accel_vector( + x: Option, + y: Option, + z: Option, + mps2_to_raw: f64, +) -> (Option, Option, Option) { + let peak = [x, y, z] + .into_iter() + .flatten() + .map(|value| value.abs()) + .fold(0.0_f64, f64::max); + let full_scale_mps2 = i16::MAX as f64 / mps2_to_raw; + + if peak <= full_scale_mps2 { + return (x, y, z); + } + + let scale = full_scale_mps2 / peak; + ( + x.map(|value| value * scale), + y.map(|value| value * scale), + z.map(|value| value * scale), + ) +} + /// Converts an accelerometer value from m/s² to a raw device count. pub fn denormalize_accel_value_i16(value_mps2: f64, mps2_to_raw: f64) -> i16 { let raw = (value_mps2 * mps2_to_raw).round(); if raw.is_nan() || raw < i16::MIN as f64 || raw > i16::MAX as f64 { - log::warn!("Accelerometer value {value_mps2} m/s² is out of i16 range ({raw}), clamping"); + log::warn!("Accelerometer value {value_mps2} m/s² was not scaled first ({raw})"); } raw as i16 } diff --git a/src/input/source/iio/accel_gyro_3d.rs b/src/input/source/iio/accel_gyro_3d.rs index 53af26ee..9d573e50 100644 --- a/src/input/source/iio/accel_gyro_3d.rs +++ b/src/input/source/iio/accel_gyro_3d.rs @@ -12,8 +12,8 @@ use crate::{ }; use super::common::{ - corrections_from_config, mount_matrix_from_config, sample_rate_from_config, translate_events, - CAPABILITIES, + capabilities_from_events, corrections_from_config, mount_matrix_from_config, + sample_rate_from_config, translate_events, }; pub struct AccelGyro3dImu { @@ -54,7 +54,7 @@ impl SourceInputDevice for AccelGyro3dImu { /// Returns the possible input events this device is capable of emitting fn get_capabilities(&self) -> Result, InputError> { - Ok(CAPABILITIES.into()) + Ok(capabilities_from_events(&self.driver.supported_events())) } fn update_event_filter(&mut self, events: HashSet) -> Result<(), InputError> { diff --git a/src/input/source/iio/bmi_imu.rs b/src/input/source/iio/bmi_imu.rs index 3456115b..f4b2637f 100644 --- a/src/input/source/iio/bmi_imu.rs +++ b/src/input/source/iio/bmi_imu.rs @@ -12,7 +12,8 @@ use crate::{ }; use super::common::{ - mount_matrix_from_config, sample_rate_from_config, translate_events, CAPABILITIES, + capabilities_from_events, corrections_from_config, mount_matrix_from_config, + sample_rate_from_config, translate_events, }; pub struct BmiImu { @@ -28,6 +29,7 @@ impl BmiImu { ) -> Result> { let mount_matrix = mount_matrix_from_config(config.as_ref()); let sample_rate = sample_rate_from_config(config.as_ref()); + let (accel_correction, gyro_correction) = corrections_from_config(config.as_ref()); let id = device_info.sysname(); let trigger_name = format!("inputplumber-{}", id.replace(':', "_")); @@ -35,8 +37,8 @@ impl BmiImu { id, mount_matrix, sample_rate, - 1.0, - 1.0, + accel_correction, + gyro_correction, TriggerStrategy::Hrtimer(trigger_name), )?; @@ -53,7 +55,7 @@ impl SourceInputDevice for BmiImu { /// Returns the possible input events this device is capable of emitting fn get_capabilities(&self) -> Result, InputError> { - Ok(CAPABILITIES.into()) + Ok(capabilities_from_events(&self.driver.supported_events())) } fn update_event_filter(&mut self, events: HashSet) -> Result<(), InputError> { diff --git a/src/input/source/iio/common.rs b/src/input/source/iio/common.rs index 4e05297a..00eb17cc 100644 --- a/src/input/source/iio/common.rs +++ b/src/input/source/iio/common.rs @@ -112,8 +112,7 @@ impl EventCapability for event::Event { } } -/// List of all capabilities that the iio IMU drivers implement -pub(super) const CAPABILITIES: &[Capability] = &[ - Capability::Accelerometer(Source::Center), - Capability::Gyroscope(Source::Center), -]; +/// The capabilities a source implements, from the events its driver reports. +pub(super) fn capabilities_from_events(events: &[event::Event]) -> Vec { + events.iter().map(|event| event.to_capability()).collect() +} diff --git a/src/input/target/dualsense.rs b/src/input/target/dualsense.rs index 4a787d20..fd38be51 100644 --- a/src/input/target/dualsense.rs +++ b/src/input/target/dualsense.rs @@ -40,7 +40,7 @@ use crate::{ value::{ denormalize_accel_value_i16, denormalize_gyro_value_i16, denormalize_signed_value_u8, denormalize_unsigned_value_u16, - denormalize_unsigned_value_u8, InputValue, + denormalize_unsigned_value_u8, scale_accel_vector, InputValue, }, }, output_capability::{OutputCapability, LED}, @@ -618,6 +618,7 @@ impl DualSenseDevice { } Capability::Accelerometer(_) => { if let InputValue::Vector3 { x, y, z } = value { + let (x, y, z) = scale_accel_vector(x, y, z, DS5_MPS2_TO_ACCEL_RAW); if let Some(x) = x { state.accel_x = Integer::from_primitive(denormalize_accel_value_i16( x, diff --git a/src/input/target/horipad_steam.rs b/src/input/target/horipad_steam.rs index e345bd2c..f07cee25 100644 --- a/src/input/target/horipad_steam.rs +++ b/src/input/target/horipad_steam.rs @@ -18,7 +18,8 @@ use crate::{ native::{NativeEvent, ScheduledNativeEvent}, value::{ denormalize_accel_value_i16, denormalize_gyro_value_i16, - denormalize_signed_value_u8, denormalize_unsigned_value_u8, InputValue, + denormalize_signed_value_u8, denormalize_unsigned_value_u8, scale_accel_vector, + InputValue, }, }, output_capability::OutputCapability, @@ -270,6 +271,7 @@ impl HoripadSteamDevice { } Capability::Accelerometer(_) => { if let InputValue::Vector3 { x, y, z } = value { + let (x, y, z) = scale_accel_vector(x, y, z, HORIPAD_MPS2_TO_ACCEL_RAW); if let Some(x) = x { self.state.accel_x = Integer::from_primitive(denormalize_accel_value_i16( x, diff --git a/src/input/target/steam_deck.rs b/src/input/target/steam_deck.rs index 7ae5ccde..64e24480 100644 --- a/src/input/target/steam_deck.rs +++ b/src/input/target/steam_deck.rs @@ -42,7 +42,8 @@ use crate::{ native::{NativeEvent, ScheduledNativeEvent}, value::{ denormalize_accel_value_i16, denormalize_gyro_value_i16, - denormalize_signed_value_i16, denormalize_unsigned_value_u16, InputValue, + denormalize_signed_value_i16, denormalize_unsigned_value_u16, scale_accel_vector, + InputValue, }, }, output_capability::{Haptic, OutputCapability}, @@ -866,6 +867,7 @@ impl SteamDeckDevice { } Capability::Accelerometer(_) => { if let InputValue::Vector3 { x, y, z } = value { + let (x, y, z) = scale_accel_vector(x, y, z, DECK_MPS2_TO_ACCEL_RAW); if let Some(x) = x { self.state.accel_x = Integer::from_primitive(denormalize_accel_value_i16( x, diff --git a/src/input/target/steam_deck_uhid.rs b/src/input/target/steam_deck_uhid.rs index 4cf84219..b42bea69 100644 --- a/src/input/target/steam_deck_uhid.rs +++ b/src/input/target/steam_deck_uhid.rs @@ -35,7 +35,8 @@ use crate::{ native::{NativeEvent, ScheduledNativeEvent}, value::{ denormalize_accel_value_i16, denormalize_gyro_value_i16, - denormalize_signed_value_i16, denormalize_unsigned_value_u16, InputValue, + denormalize_signed_value_i16, denormalize_unsigned_value_u16, scale_accel_vector, + InputValue, }, }, output_capability::{Haptic, OutputCapability}, @@ -390,6 +391,7 @@ impl SteamDeckUhidDevice { } Capability::Accelerometer(_) => { if let InputValue::Vector3 { x, y, z } = value { + let (x, y, z) = scale_accel_vector(x, y, z, DECK_MPS2_TO_ACCEL_RAW); if let Some(x) = x { self.state.accel_x = Integer::from_primitive(denormalize_accel_value_i16( x, diff --git a/src/input/target/ultimate_2.rs b/src/input/target/ultimate_2.rs index 27a75931..168a0b36 100644 --- a/src/input/target/ultimate_2.rs +++ b/src/input/target/ultimate_2.rs @@ -24,7 +24,7 @@ use crate::{ native::{NativeEvent, ScheduledNativeEvent}, value::{ denormalize_accel_value_i16, denormalize_gyro_value_i16, - denormalize_unsigned_value_u8, InputValue, + denormalize_unsigned_value_u8, scale_accel_vector, InputValue, }, }, output_capability::OutputCapability, @@ -224,6 +224,7 @@ impl Ultimate2WirelessDevice { } Capability::Accelerometer(_) => { if let InputValue::Vector3 { x, y, z } = value { + let (x, y, z) = scale_accel_vector(x, y, z, ULTIMATE_2_MPS2_TO_ACCEL_RAW); // Invert the SDL standard frame rotation so the wire fields // match a real Ultimate 2 for the same pose. if let Some(x) = x { diff --git a/src/input/target/unified_gamepad.rs b/src/input/target/unified_gamepad.rs index 05ac7594..753748d8 100644 --- a/src/input/target/unified_gamepad.rs +++ b/src/input/target/unified_gamepad.rs @@ -27,7 +27,10 @@ use crate::{ composite_device::client::CompositeDeviceClient, event::{ native::NativeEvent, - value::{denormalize_accel_value_i16, denormalize_gyro_value_i16, InputValue}, + value::{ + denormalize_accel_value_i16, denormalize_gyro_value_i16, scale_accel_vector, + InputValue, + }, }, output_capability::OutputCapability, output_event::OutputEvent, @@ -680,17 +683,21 @@ impl From for StateUpdate { } Capability::Accelerometer(_) => { let value = match event.get_value() { - InputValue::Vector3 { x, y, z } => Int16Vector3Update { - x: x.map(|x| { - denormalize_accel_value_i16(x, UNIFIED_GAMEPAD_MPS2_TO_ACCEL_RAW) - }), - y: y.map(|y| { - denormalize_accel_value_i16(y, UNIFIED_GAMEPAD_MPS2_TO_ACCEL_RAW) - }), - z: z.map(|z| { - denormalize_accel_value_i16(z, UNIFIED_GAMEPAD_MPS2_TO_ACCEL_RAW) - }), - }, + InputValue::Vector3 { x, y, z } => { + let (x, y, z) = + scale_accel_vector(x, y, z, UNIFIED_GAMEPAD_MPS2_TO_ACCEL_RAW); + Int16Vector3Update { + x: x.map(|x| { + denormalize_accel_value_i16(x, UNIFIED_GAMEPAD_MPS2_TO_ACCEL_RAW) + }), + y: y.map(|y| { + denormalize_accel_value_i16(y, UNIFIED_GAMEPAD_MPS2_TO_ACCEL_RAW) + }), + z: z.map(|z| { + denormalize_accel_value_i16(z, UNIFIED_GAMEPAD_MPS2_TO_ACCEL_RAW) + }), + } + } _ => { return Self::default(); }