diff --git a/interface/src/main.rs b/interface/src/main.rs index 10037f6..5b0fe62 100644 --- a/interface/src/main.rs +++ b/interface/src/main.rs @@ -2,14 +2,14 @@ use std::time::Duration; use anyhow::Context; use common::Command; -use gilrs::{Axis, Gilrs}; +use gilrs::{Axis, Event, Gilrs}; use tokio::{io::{AsyncWriteExt, BufWriter}, net::TcpStream, time::sleep}; #[tokio::main] async fn main() -> anyhow::Result<()> { println!("Hello, world!"); - let gamepads = Gilrs::new().unwrap(); + let mut gamepads = Gilrs::new().unwrap(); let (_, gamepad) = gamepads.gamepads().nth(0).context("no gamepads")?; let ly = gamepad.axis_code(Axis::LeftStickY).context("no left joystick")?; let rx = gamepad.axis_code(Axis::RightStickX).context("no right joystick")?; @@ -22,11 +22,27 @@ async fn main() -> anyhow::Result<()> { let mut robot_controller = BufWriter::new(robot_controller); - loop { - let values = gamepad.state(); + let mut active_gamepad = None; - let ly = values.axis_data(ly).context("no left")?.value(); - let rx = values.axis_data(rx).context("no left")?.value(); + sleep(Duration::from_secs(5)).await; // bad + loop { + while let Some(Event { id,..}) = gamepads.next_event() { + active_gamepad = Some(id); + } + + let Some(gamepad) = active_gamepad else { + continue; + }; + + let gamepad = gamepads.gamepad(gamepad); + + + + let values = gamepad.state(); + dbg!(values); + + let ly= values.axis_data(ly).map(|d| d.value()).unwrap_or(0.0); + let rx= values.axis_data(rx).map(|d| d.value()).unwrap_or(0.0); let cmd = Command::Twist(ly, rx); @@ -36,7 +52,6 @@ async fn main() -> anyhow::Result<()> { robot_controller.write_all(&encoded).await?; robot_controller.flush().await?; - sleep(Duration::from_micros(3500)).await; } }