Skip to content

Commit

Permalink
fix(Legion Go): Add status events for the lego controller
Browse files Browse the repository at this point in the history
  • Loading branch information
pastaq authored and Derek J. Clark committed Mar 10, 2024
1 parent cfe64b0 commit c57815e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 32 deletions.
47 changes: 44 additions & 3 deletions src/drivers/lego/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use packed_struct::{types::SizedInteger, PackedStruct};
use super::{
event::{
AccelerometerEvent, AccelerometerInput, AxisEvent, BinaryInput, ButtonEvent, Event,
JoyAxisInput, TouchAxisInput, TriggerEvent, TriggerInput, WheelEvent,
JoyAxisInput, StatusEvent, StatusInput, TouchAxisInput, TriggerEvent, TriggerInput,
},
hid_report::{
DInputDataLeftReport, DInputDataRightReport, KeyboardDataReport, MouseDataReport,
Expand Down Expand Up @@ -605,8 +605,49 @@ impl Driver {
}),
));

// State events
// TODO: Add state events.
// Status events
if state.l_controller_battery != old_state.l_controller_battery {
events.push(Event::Status(StatusEvent::LeftControllerBattery(
StatusInput {
value: state.l_controller_battery,
},
)));
}
if state.l_controller_mode0 != old_state.l_controller_mode0 {
events.push(Event::Status(StatusEvent::LeftControllerMode0(
StatusInput {
value: state.l_controller_mode0,
},
)));
}
if state.l_controller_mode1 != old_state.l_controller_mode1 {
events.push(Event::Status(StatusEvent::LeftControllerMode1(
StatusInput {
value: state.l_controller_mode1,
},
)));
}
if state.r_controller_battery != old_state.r_controller_battery {
events.push(Event::Status(StatusEvent::RightControllerBattery(
StatusInput {
value: state.r_controller_battery,
},
)));
}
if state.r_controller_mode0 != old_state.r_controller_mode0 {
events.push(Event::Status(StatusEvent::RightControllerMode0(
StatusInput {
value: state.r_controller_mode0,
},
)));
}
if state.r_controller_mode1 != old_state.r_controller_mode1 {
events.push(Event::Status(StatusEvent::RightControllerMode1(
StatusInput {
value: state.r_controller_mode1,
},
)));
}
};

events
Expand Down
62 changes: 38 additions & 24 deletions src/drivers/lego/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub enum Event {
Accelerometer(AccelerometerEvent),
Axis(AxisEvent),
Trigger(TriggerEvent),
Status(StatusEvent),
}

/// Binary input contain either pressed or unpressed
Expand All @@ -13,6 +14,33 @@ pub struct BinaryInput {
pub pressed: bool,
}

/// Axis input contain (x, y) coordinates
#[derive(Clone, Debug)]
pub struct TouchAxisInput {
pub x: u16,
pub y: u16,
}

/// Axis input contain (x, y) coordinates
#[derive(Clone, Debug)]
pub struct JoyAxisInput {
pub x: u8,
pub y: u8,
}

/// AccelerometerInput represents the state of the accelerometer (x, y, z) values
#[derive(Clone, Debug)]
pub struct AccelerometerInput {
pub x: u8,
pub y: u8,
}

// Status inputs contain some value that corresponds to the current status of a device.
#[derive(Clone, Debug)]
pub struct StatusInput {
pub value: u8,
}

/// Button events represend binary inputs
#[derive(Clone, Debug)]
pub enum ButtonEvent {
Expand Down Expand Up @@ -65,20 +93,6 @@ pub enum ButtonEvent {
/// Mouse wheel click on the back of the right controller
MouseClick(BinaryInput),
}

/// Axis input contain (x, y) coordinates
#[derive(Clone, Debug)]
pub struct TouchAxisInput {
pub x: u16,
pub y: u16,
}

/// Axis input contain (x, y) coordinates
#[derive(Clone, Debug)]
pub struct JoyAxisInput {
pub x: u8,
pub y: u8,
}
/// Axis events are events that have (x, y) values
#[derive(Clone, Debug)]
pub enum AxisEvent {
Expand All @@ -100,16 +114,6 @@ pub enum TriggerEvent {
MouseWheel(TriggerInput),
}

/// Wheel events contain positive values indicating up and negative numbers indicating down
#[derive(Clone, Debug)]
pub enum WheelEvent {}
/// AccelerometerInput represents the state of the accelerometer (x, y, z) values
#[derive(Clone, Debug)]
pub struct AccelerometerInput {
pub x: u8,
pub y: u8,
}

/// AccelerometerEvent has data from the accelerometer
#[derive(Clone, Debug)]
pub enum AccelerometerEvent {
Expand All @@ -118,3 +122,13 @@ pub enum AccelerometerEvent {
//CenterAccelerometer(AccelerometerInput),
// TODO: Gyro from sysfs
}

#[derive(Clone, Debug)]
pub enum StatusEvent {
LeftControllerBattery(StatusInput),
LeftControllerMode0(StatusInput),
LeftControllerMode1(StatusInput),
RightControllerBattery(StatusInput),
RightControllerMode0(StatusInput),
RightControllerMode1(StatusInput),
}
13 changes: 8 additions & 5 deletions src/drivers/lego/hid_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,14 @@ impl ReportType {
// Somehow this has pitch, roll, and yaw in two 8 bit numbers. Perhaps it is incomplete (only two), or each
// number is 5 bits? Reslution of 31 is really low. bytes 29 and 30 are left controller, 31 and 32
// are right controller.
// Also note bytes 5, 6, 7, 8, 11, and 12. These change when the controller is synced either docked
// or wireless and indicate the mode for each side. 5 and 7 are 100 (or 99) when the device is fully
// synced, or 0 when diconnected (left/right respectively). 6 and 11 correspond to if the left
// Also note bytes 6, 8, 11, and 12. These change when the controller is synced either docked
// or wireless and indicate the mode for each side. 6 and 11 correspond to if the left
// controller is docked 6:4, 11:2 or wireless 6:1, 11:3. 7 and 12 are for the right controller,
// same numbers.
//
// Battery Indicators
// 5 and 7 are left/right controller battery level, 0-100
//
// Left Accel
// Not sure what the different numbers mean yet
// # ReportID: 4 / 0xffa00003: 60 , 116 , 1 , 0 , 99 , 1 , 99 , 1 , 1 , 1 , 1 , 3 , 3 , -128 , -128 , -128 , -128 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , -128 , 0 , 0 , 0 , 0 , -16 , -67 , -128 , -128 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
Expand Down Expand Up @@ -272,11 +275,11 @@ pub struct XInputDataReport {
pub unk_4: u8,

#[packed_field(bytes = "5")]
pub l_controller_synced: u8,
pub l_controller_battery: u8,
#[packed_field(bytes = "6")]
pub l_controller_mode0: u8,
#[packed_field(bytes = "7")]
pub r_controller_synced: u8,
pub r_controller_battery: u8,
#[packed_field(bytes = "8")]
pub r_controller_mode0: u8,

Expand Down

0 comments on commit c57815e

Please sign in to comment.