poll gamepad state
This commit is contained in:
parent
a7efcab3ce
commit
76ba58de69
1 changed files with 22 additions and 7 deletions
|
@ -2,14 +2,14 @@ use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use common::Command;
|
use common::Command;
|
||||||
use gilrs::{Axis, Gilrs};
|
use gilrs::{Axis, Event, Gilrs};
|
||||||
use tokio::{io::{AsyncWriteExt, BufWriter}, net::TcpStream, time::sleep};
|
use tokio::{io::{AsyncWriteExt, BufWriter}, net::TcpStream, time::sleep};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
|
|
||||||
let gamepads = Gilrs::new().unwrap();
|
let mut gamepads = Gilrs::new().unwrap();
|
||||||
let (_, gamepad) = gamepads.gamepads().nth(0).context("no gamepads")?;
|
let (_, gamepad) = gamepads.gamepads().nth(0).context("no gamepads")?;
|
||||||
let ly = gamepad.axis_code(Axis::LeftStickY).context("no left joystick")?;
|
let ly = gamepad.axis_code(Axis::LeftStickY).context("no left joystick")?;
|
||||||
let rx = gamepad.axis_code(Axis::RightStickX).context("no right 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);
|
let mut robot_controller = BufWriter::new(robot_controller);
|
||||||
|
|
||||||
loop {
|
let mut active_gamepad = None;
|
||||||
let values = gamepad.state();
|
|
||||||
|
|
||||||
let ly = values.axis_data(ly).context("no left")?.value();
|
sleep(Duration::from_secs(5)).await; // bad
|
||||||
let rx = values.axis_data(rx).context("no left")?.value();
|
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);
|
let cmd = Command::Twist(ly, rx);
|
||||||
|
|
||||||
|
@ -36,7 +52,6 @@ async fn main() -> anyhow::Result<()> {
|
||||||
robot_controller.write_all(&encoded).await?;
|
robot_controller.write_all(&encoded).await?;
|
||||||
|
|
||||||
robot_controller.flush().await?;
|
robot_controller.flush().await?;
|
||||||
|
|
||||||
sleep(Duration::from_micros(3500)).await;
|
sleep(Duration::from_micros(3500)).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue