diff --git a/red/Cargo.toml b/red/Cargo.toml index 941b501..e94a547 100644 --- a/red/Cargo.toml +++ b/red/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" bytes = "1.2.1" euclid = "0.22.7" futures = "0.3.24" +gilrs = "0.10.1" lazy_static = "1.4.0" regex = "1.6.0" tokio = { version = "1.21.0", features = ["full"] } diff --git a/red/src/jogger.rs b/red/src/jogger.rs new file mode 100644 index 0000000..15a4f18 --- /dev/null +++ b/red/src/jogger.rs @@ -0,0 +1,31 @@ +use gilrs::Gilrs; +use tokio::sync::{oneshot, Mutex}; +use std::time::Duration; +use std::thread::sleep; + +pub struct Jogger { + speed_setpoint: Mutex<(f64, f64, f64)>, + terminator: oneshot::Sender<()>, +} + +impl Jogger { + pub async fn new() -> Result { + let (terminate_tx, mut terminate_rx) = oneshot::channel(); + tokio::task::spawn_blocking(move || { + let mut gilrs = Gilrs::new().unwrap(); + for (_id, gamepad) in gilrs.gamepads() { + println!("{} is {:?}", gamepad.name(), gamepad.power_info()); + } + while let Err(oneshot::error::TryRecvError::Empty) = terminate_rx.try_recv() { + sleep(Duration::from_millis(1)); + if let Some(event) = gilrs.next_event() { + println!("{}", event.id); + } + } + }); + Ok(Jogger { + speed_setpoint: Mutex::new((0.0, 0.0, 0.0)), + terminator: terminate_tx, + }) + } +} diff --git a/red/src/lib.rs b/red/src/lib.rs index 3f31bdf..dc85e2d 100644 --- a/red/src/lib.rs +++ b/red/src/lib.rs @@ -1 +1,2 @@ pub mod printer; +pub mod jogger; diff --git a/red/src/main.rs b/red/src/main.rs index eba2e0b..23dd947 100644 --- a/red/src/main.rs +++ b/red/src/main.rs @@ -1,44 +1,49 @@ #![warn(rust_2018_idioms)] - +use futures::never::Never; +use red::controller; use red::printer::gcode::*; use red::printer::Printer; +use std::time::Duration; const DEFAULT_TTY: &str = "/dev/ttyUSB0"; #[tokio::main] -async fn main() -> tokio_serial::Result<()> { - let mut printer = Printer::connect(DEFAULT_TTY).await.unwrap(); - printer - .send_gcode(Box::new(M114Command::new())) - .await - .unwrap(); - printer - .send_gcode(Box::new(G28Command::new(true, true, true))) - .await - .unwrap(); +async fn main() -> Never { + let jogger = controller::Jogger::new().await; loop { - tokio::time::sleep(std::time::Duration::from_secs(5)).await; - printer - .send_gcode(Box::new(G0Command { - x: Some(50.0), - y: Some(50.0), - z: Some(5.0), - e: None, - velocity: None, - })) - .await - .unwrap(); - tokio::time::sleep(std::time::Duration::from_secs(5)).await; - printer - .send_gcode(Box::new(G0Command { - x: Some(25.0), - y: Some(25.0), - z: Some(5.0), - e: None, - velocity: None, - })) - .await - .unwrap(); + tokio::time::sleep(Duration::from_secs(2)).await; } - Ok(()) + // let mut printer = Printer::connect(DEFAULT_TTY).await.unwrap(); + // printer + // .send_gcode(Box::new(M114Command::new())) + // .await + // .unwrap(); + // printer + // .send_gcode(Box::new(G28Command::new(true, true, true))) + // .await + // .unwrap(); + // loop { + // tokio::time::sleep(std::time::Duration::from_secs(5)).await; + // printer + // .send_gcode(Box::new(G0Command { + // x: Some(50.0), + // y: Some(50.0), + // z: Some(5.0), + // e: None, + // velocity: None, + // })) + // .await + // .unwrap(); + // tokio::time::sleep(std::time::Duration::from_secs(5)).await; + // printer + // .send_gcode(Box::new(G0Command { + // x: Some(25.0), + // y: Some(25.0), + // z: Some(5.0), + // e: None, + // velocity: None, + // })) + // .await + // .unwrap(); + // } }