1
Fork 0

poll gamepad state

This commit is contained in:
Andy Killorin 2025-02-04 13:04:42 -05:00
parent a7efcab3ce
commit 76ba58de69
Signed by: ank
GPG key ID: 23F9463ECB67FE8C

View file

@ -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;
}
}