Refactor gamepad code to be sync

This commit is contained in:
zaubentrucker 2024-12-25 00:42:02 +01:00
parent 9a450cfe9c
commit efcc13bea7

View file

@ -4,11 +4,11 @@ use gilrs::Button::LeftTrigger2;
use gilrs::Button::RightTrigger2;
use gilrs::EventType::*;
use gilrs::Gilrs;
use std::sync::mpsc;
use std::sync::Arc;
use std::sync::Mutex;
use std::thread::sleep;
use std::time::Duration;
use tokio::sync::mpsc;
#[derive(Debug)]
pub enum Axis {
@ -41,73 +41,70 @@ pub enum GamepadEvent {
/// }
/// ```
pub struct Gamepad {
speed_setpoint: Mutex<(f32, f32, f32)>,
gilrs: Gilrs,
// Cached speed setpoint for (X, Y, Positive-Z, Negative-Z)
speed_setpoint: (f32, f32, f32, f32),
}
impl Gamepad {
/// Create a new `Gamepad` and spawn the underlying tasks for updating internal setpoints
///
/// The tasks are terminated on drop.
pub async fn new() -> Result<Arc<Self>, gilrs::Error> {
let (gamepad_tx, gamepad_rx) = mpsc::channel(8);
let res = Arc::new(Gamepad {
speed_setpoint: Mutex::new((0.0, 0.0, 0.0)),
});
tokio::task::spawn_blocking(move || {
let mut gilrs = Gilrs::new().unwrap();
for (_id, gamepad) in gilrs.gamepads() {
println!("{} is {:?}", gamepad.name(), gamepad.power_info());
}
loop {
sleep(Duration::from_millis(1));
if let Some(event) = gilrs.next_event() {
if let Some(internal_event) = Self::map_event(event) {
if gamepad_tx.blocking_send(internal_event).is_err() {
// receiver dropped
break;
}
}
}
}
});
pub fn new() -> Result<Self, gilrs::Error> {
let gilrs = Gilrs::new().unwrap();
for (_id, gamepad) in gilrs.gamepads() {
println!(
"Found gamepad {}: {:?}",
gamepad.name(),
gamepad.power_info()
);
}
tokio::spawn(Self::handle_events(res.clone(), gamepad_rx));
Ok(res)
Ok(Self {
gilrs,
speed_setpoint: (0.0, 0.0, 0.0, 0.0),
})
}
/// Get the current speed that the user is dialing in on the gamepad
pub fn speed_setpoint(&self) -> (f32, f32, f32) {
*self.speed_setpoint.lock().unwrap()
fn get_next(&mut self) -> Option<GamepadEvent> {
self.gilrs
.next_event()
.and_then(|event| Self::map_event(event))
}
/// Update the setpoints in accordance to incoming `GamepadEvent`s
async fn handle_events(self_arc: Arc<Self>, mut events_rx: mpsc::Receiver<GamepadEvent>) {
// There is two z-axes that counter each other.
let mut z_positive = 0.0;
let mut z_negative = 0.0;
while let Some(event) = events_rx.recv().await {
// Get all `GamepadEvent`s since the last call
pub fn get_pending(&mut self) -> Vec<GamepadEvent> {
(0..).map_while(|_| self.get_next()).collect()
}
/// Update the current speed via the movements on the controller and return the current setpoint
///
/// If you don't want to update, just supply an empty `updates` slice
pub fn speed_setpoint(&mut self, updates: &[GamepadEvent]) -> (f32, f32, f32) {
for event in updates {
match event {
GamepadEvent::TerminatePressed => break,
GamepadEvent::AxisPosition(axis, value) => {
// I won't panic if you don't panic!
let mut speed_setpoint = self_arc.speed_setpoint.lock().unwrap();
let mut speed_setpoint = self.speed_setpoint;
match axis {
Axis::X => speed_setpoint.0 = value,
Axis::Y => speed_setpoint.1 = value,
Axis::X => speed_setpoint.0 = *value,
Axis::Y => speed_setpoint.1 = *value,
Axis::ZPositive => {
z_positive = value;
speed_setpoint.2 = z_positive - z_negative;
speed_setpoint.2 = *value;
}
Axis::ZNegative => {
z_negative = value;
speed_setpoint.2 = z_positive - z_negative;
speed_setpoint.3 = *value;
}
}
}
}
}
println!("Game pad quit!")
(
self.speed_setpoint.0,
self.speed_setpoint.1,
self.speed_setpoint.2 - self.speed_setpoint.3,
)
}
fn map_event(event: gilrs::Event) -> Option<GamepadEvent> {