From 3e3044077e0b6549f66ba2cc310fa28a7f18139d Mon Sep 17 00:00:00 2001 From: "Derek J. Clark" Date: Sat, 9 Mar 2024 22:52:49 -0800 Subject: [PATCH] fix(Legion Go): Rename Accel to Gyro. Fix some warnings. --- src/drivers/lego/driver.rs | 95 ++++++++++++++++----------------- src/drivers/lego/event.rs | 13 +++-- src/drivers/lego/hid_report.rs | 8 +-- src/input/source/hidraw/lego.rs | 4 +- 4 files changed, 58 insertions(+), 62 deletions(-) diff --git a/src/drivers/lego/driver.rs b/src/drivers/lego/driver.rs index 4bbdebdd..1ddeafd2 100644 --- a/src/drivers/lego/driver.rs +++ b/src/drivers/lego/driver.rs @@ -1,17 +1,16 @@ -use std::{collections::HashMap, error::Error, ffi::CString, u8, vec}; +use std::{error::Error, ffi::CString, u8, vec}; use hidapi::HidDevice; -use packed_struct::{types::SizedInteger, PackedStruct}; +use packed_struct::PackedStruct; use super::{ event::{ - AccelerometerEvent, AccelerometerInput, AxisEvent, BinaryInput, ButtonEvent, Event, - JoyAxisInput, MouseWheelInput, StatusEvent, StatusInput, TouchAxisInput, TriggerEvent, - TriggerInput, + AxisEvent, BinaryInput, ButtonEvent, Event, GyroEvent, GyroInput, JoyAxisInput, + MouseWheelInput, StatusEvent, StatusInput, TouchAxisInput, TriggerEvent, TriggerInput, }, hid_report::{ DInputDataLeftReport, DInputDataRightReport, KeyboardDataReport, MouseDataReport, - ReportType, TouchpadDataReport, XInputDataReport, + TouchpadDataReport, XInputDataReport, }, }; @@ -36,12 +35,12 @@ pub const XINPUT_DATA: u8 = 0x04; // Input report axis ranges // TODO: actual mouse range // TODO: ACCEL Left/Right X/Y, Z? -pub const ACCEL_X_MAX: f64 = 255.0; -pub const ACCEL_X_MIN: f64 = 0.0; -pub const ACCEL_Y_MAX: f64 = 255.0; -pub const ACCEL_Y_MIN: f64 = 0.0; +//pub const GYRO_X_MAX: f64 = 255.0; +//pub const GYRO_X_MIN: f64 = 0.0; +//pub const GYRO_Y_MAX: f64 = 255.0; +//pub const GYRO_Y_MIN: f64 = 0.0; pub const MOUSE_WHEEL_MAX: f64 = 120.0; -pub const MOUSE_WHEEL_MIN: f64 = -120.0; +//pub const MOUSE_WHEEL_MIN: f64 = -120.0; pub const MOUSE_X_MAX: f64 = 2048.0; pub const MOUSE_X_MIN: f64 = -2048.0; pub const MOUSE_Y_MAX: f64 = 2048.0; @@ -55,19 +54,19 @@ pub const STICK_X_MIN: f64 = 0.0; pub const STICK_Y_MAX: f64 = 255.0; pub const STICK_Y_MIN: f64 = 0.0; pub const TRIGG_MAX: f64 = 255.0; -pub const TRIGG_MIN: f64 = 0.0; +//pub const TRIGG_MIN: f64 = 0.0; // NORMALIZED AXIS -pub const ACCEL_X_NORM: f64 = 1.0 / ACCEL_X_MAX; -pub const ACCEL_Y_NORM: f64 = 1.0 / ACCEL_Y_MAX; -pub const MOUSE_WHEEL_NORM: f64 = 1.0 / MOUSE_WHEEL_MAX; -pub const MOUSE_X_NORM: f64 = 1.0 / MOUSE_X_MAX; -pub const MOUSE_Y_NORM: f64 = 1.0 / MOUSE_Y_MAX; -pub const PAD_X_AXIS_NORM: f64 = 1.0 / PAD_X_MAX; -pub const PAD_Y_AXIS_NORM: f64 = 1.0 / PAD_Y_MAX; -pub const STICK_X_AXIS_NORM: f64 = 1.0 / STICK_X_MAX; -pub const STICK_Y_AXIS_NORM: f64 = 1.0 / STICK_Y_MAX; -pub const TRIGG_AXIS_NORM: f64 = 1.0 / TRIGG_MAX; +//pub const GYRO_X_NORM: f64 = 1.0 / GYRO_X_MAX; +//pub const ACCEL_Y_NORM: f64 = 1.0 / GYRO_Y_MAX; +//pub const MOUSE_WHEEL_NORM: f64 = 1.0 / MOUSE_WHEEL_MAX; +//pub const MOUSE_X_NORM: f64 = 1.0 / MOUSE_X_MAX; +//pub const MOUSE_Y_NORM: f64 = 1.0 / MOUSE_Y_MAX; +//pub const PAD_X_AXIS_NORM: f64 = 1.0 / PAD_X_MAX; +//pub const PAD_Y_AXIS_NORM: f64 = 1.0 / PAD_Y_MAX; +//pub const STICK_X_AXIS_NORM: f64 = 1.0 / STICK_X_MAX; +//pub const STICK_Y_AXIS_NORM: f64 = 1.0 / STICK_Y_MAX; +//pub const TRIGG_AXIS_NORM: f64 = 1.0 / TRIGG_MAX; pub struct Driver { dinputl_state: Option, @@ -211,13 +210,13 @@ impl Driver { /// Translate the state into individual events fn translate_dinputl(&self, old_state: Option) -> Vec { - let mut events = Vec::new(); - let Some(state) = self.dinputl_state else { + let events = Vec::new(); + let Some(_) = self.dinputl_state else { return events; }; // Translate state changes into events if they have changed - if let Some(old_state) = old_state {} + if let Some(_) = old_state {} events } @@ -255,13 +254,13 @@ impl Driver { /// Translate the state into individual events fn translate_dinputr(&self, old_state: Option) -> Vec { - let mut events = Vec::new(); - let Some(state) = self.dinputr_state else { + let events = Vec::new(); + let Some(_) = self.dinputr_state else { return events; }; // Translate state changes into events if they have changed - if let Some(old_state) = old_state {} + if let Some(_) = old_state {} events } @@ -299,13 +298,13 @@ impl Driver { /// Translate the state into individual events fn translate_keyboard(&self, old_state: Option) -> Vec { - let mut events = Vec::new(); - let Some(state) = self.keyboard_state else { + let events = Vec::new(); + let Some(_) = self.keyboard_state else { return events; }; // Translate state changes into events if they have changed - if let Some(old_state) = old_state {} + if let Some(_) = old_state {} events } @@ -343,13 +342,13 @@ impl Driver { /// Translate the state into individual events fn translate_mouse(&self, old_state: Option) -> Vec { - let mut events = Vec::new(); - let Some(state) = self.mouse_state else { + let events = Vec::new(); + let Some(_) = self.mouse_state else { return events; }; // Translate state changes into events if they have changed - if let Some(old_state) = old_state {} + if let Some(_) = old_state {} events } @@ -387,13 +386,13 @@ impl Driver { /// Translate the state into individual events fn translate_touch(&self, old_state: Option) -> Vec { - let mut events = Vec::new(); - let Some(state) = self.touchpad_state else { + let events = Vec::new(); + let Some(_) = self.touchpad_state else { return events; }; // Translate state changes into events if they have changed - if let Some(old_state) = old_state {} + if let Some(_) = old_state {} events } @@ -595,18 +594,16 @@ impl Driver { } // Accelerometer events - events.push(Event::Accelerometer(AccelerometerEvent::LeftAccelerometer( - AccelerometerInput { - x: state.left_accel_0, - y: state.left_accel_1, - }, - ))); - events.push(Event::Accelerometer( - AccelerometerEvent::RightAccelerometer(AccelerometerInput { - x: state.right_accel_0, - y: state.right_accel_1, - }), - )); + events.push(Event::Gyro(GyroEvent::LeftGyro(GyroInput { + x: state.left_gyro_x, + y: state.left_gyro_y, + z: 0, + }))); + events.push(Event::Gyro(GyroEvent::RightGyro(GyroInput { + x: state.right_gyro_x, + y: state.right_gyro_y, + z: 0, + }))); // Status events if state.l_controller_battery != old_state.l_controller_battery { diff --git a/src/drivers/lego/event.rs b/src/drivers/lego/event.rs index 17be02d1..a992da91 100644 --- a/src/drivers/lego/event.rs +++ b/src/drivers/lego/event.rs @@ -2,7 +2,7 @@ #[derive(Clone, Debug)] pub enum Event { Button(ButtonEvent), - Accelerometer(AccelerometerEvent), + Gyro(GyroEvent), Axis(AxisEvent), Trigger(TriggerEvent), Status(StatusEvent), @@ -36,9 +36,10 @@ pub struct JoyAxisInput { /// AccelerometerInput represents the state of the accelerometer (x, y, z) values #[derive(Clone, Debug)] -pub struct AccelerometerInput { +pub struct GyroInput { pub x: u8, pub y: u8, + pub z: u8, } // Status inputs contain some value that corresponds to the current status of a device. @@ -130,11 +131,9 @@ pub enum TriggerEvent { /// AccelerometerEvent has data from the accelerometer #[derive(Clone, Debug)] -pub enum AccelerometerEvent { - LeftAccelerometer(AccelerometerInput), - RightAccelerometer(AccelerometerInput), - //CenterAccelerometer(AccelerometerInput), - // TODO: Gyro from sysfs +pub enum GyroEvent { + LeftGyro(GyroInput), + RightGyro(GyroInput), } #[derive(Clone, Debug)] diff --git a/src/drivers/lego/hid_report.rs b/src/drivers/lego/hid_report.rs index 7fb65f7f..5a4737ed 100644 --- a/src/drivers/lego/hid_report.rs +++ b/src/drivers/lego/hid_report.rs @@ -351,13 +351,13 @@ pub struct XInputDataReport { pub touch_y_0: u16, #[packed_field(bytes = "30")] - pub left_accel_0: u8, + pub left_gyro_x: u8, #[packed_field(bytes = "31")] - pub left_accel_1: u8, + pub left_gyro_y: u8, #[packed_field(bytes = "32")] - pub right_accel_0: u8, + pub right_gyro_x: u8, #[packed_field(bytes = "33")] - pub right_accel_1: u8, + pub right_gyro_y: u8, #[packed_field(bytes = "34")] pub unk_34: u8, diff --git a/src/input/source/hidraw/lego.rs b/src/input/source/hidraw/lego.rs index 49da925d..cf4a7148 100644 --- a/src/input/source/hidraw/lego.rs +++ b/src/input/source/hidraw/lego.rs @@ -4,7 +4,7 @@ use hidapi::DeviceInfo; use tokio::sync::broadcast; use crate::{ - drivers::lego::{self, driver::Driver, event::MouseAxisInput}, + drivers::lego::{self, driver::Driver}, input::{ capability::{ Capability, Gamepad, GamepadAxis, GamepadButton, GamepadTrigger, Mouse, MouseButton, @@ -296,7 +296,7 @@ fn translate_event(event: lego::event::Event) -> NativeEvent { */ lego::event::Event::Axis(axis) => match axis.clone() { lego::event::AxisEvent::Touchpad(_) => { - NativeEvent::new(Capability::NotImplemented, normalize_axis_value(axis)) + NativeEvent::new(Capability::Mouse(Mouse::Motion), normalize_axis_value(axis)) } lego::event::AxisEvent::LStick(_) => NativeEvent::new( Capability::Gamepad(Gamepad::Axis(GamepadAxis::LeftStick)),