From d3da56b3e56caf87e255e89ce89a000cde76216a Mon Sep 17 00:00:00 2001 From: Frederik Menke Date: Mon, 1 Jan 2024 13:22:26 +0100 Subject: [PATCH] Fix warnings --- red/src/gamepad.rs | 14 ++++++----- red/src/lib.rs | 1 + red/src/main.rs | 54 ------------------------------------------ red/src/spindle/mod.rs | 43 +++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 60 deletions(-) create mode 100644 red/src/spindle/mod.rs diff --git a/red/src/gamepad.rs b/red/src/gamepad.rs index f267bab..30adad3 100644 --- a/red/src/gamepad.rs +++ b/red/src/gamepad.rs @@ -8,7 +8,7 @@ use std::sync::Arc; use std::sync::Mutex; use std::thread::sleep; use std::time::Duration; -use tokio::sync::{mpsc, oneshot}; +use tokio::sync::mpsc; #[derive(Debug)] pub enum Axis { @@ -42,7 +42,6 @@ pub enum GamepadEvent { /// ``` pub struct Gamepad { speed_setpoint: Mutex<(f32, f32, f32)>, - terminator: oneshot::Sender<()>, } impl Gamepad { @@ -50,21 +49,24 @@ impl Gamepad { /// /// The tasks are terminated on drop. pub async fn new() -> Result, gilrs::Error> { - let (terminate_tx, mut terminate_rx) = oneshot::channel(); let (gamepad_tx, gamepad_rx) = mpsc::channel(8); let res = Arc::new(Gamepad { speed_setpoint: Mutex::new((0.0, 0.0, 0.0)), - terminator: terminate_tx, }); 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() { + loop { sleep(Duration::from_millis(1)); if let Some(event) = gilrs.next_event() { - Self::map_event(event).map(|event| gamepad_tx.blocking_send(event)); + if let Some(internal_event) = Self::map_event(event) { + if gamepad_tx.blocking_send(internal_event).is_err() { + // receiver dropped + break; + } + } } } }); diff --git a/red/src/lib.rs b/red/src/lib.rs index b3df052..536724c 100644 --- a/red/src/lib.rs +++ b/red/src/lib.rs @@ -1,3 +1,4 @@ pub mod gamepad; pub mod jogger; pub mod printer; +pub mod spindle; diff --git a/red/src/main.rs b/red/src/main.rs index a1c5b88..2888426 100644 --- a/red/src/main.rs +++ b/red/src/main.rs @@ -1,38 +1,11 @@ #![warn(rust_2018_idioms)] use futures::never::Never; -use i2c_linux::I2c; use red::gamepad; use red::jogger; use red::printer::Printer; use std::path::Path; -use std::time::Duration; use tokio_serial::SerialPortInfo; -const DEFAULT_TTY: &str = "/dev/ttyUSB0"; -const I2C_ADDRESS_EXTENDER: u8 = 25; -const I2C_REGISTER_SPINDLE_SPEED: u8 = 0; - -// available i2c functionality on the device as reported from -// the i2c library -// TENBIT_ADDR -// SMBUS_PEC -// SMBUS_QUICK -// SMBUS_READ_BYTE -// SMBUS_WRITE_BYTE -// SMBUS_READ_BYTE_DATA -// SMBUS_WRITE_BYTE_DATA -// SMBUS_READ_WORD_DATA -// SMBUS_WRITE_WORD_DATA -// SMBUS_PROC_CALL -// SMBUS_WRITE_BLOCK_DATA -// SMBUS_READ_I2C_BLOCK -// SMBUS_WRITE_I2C_BLOCK -// SMBUS_BYTE -// SMBUS_BYTE_DATA -// SMBUS_WORD_DATA -// SMBUS_I2C_BLOCK -// SMBUS_EMUL - #[tokio::main] async fn main() -> Never { println!("Entering App"); @@ -54,30 +27,3 @@ async fn main() -> Never { .unwrap(); jogger::jog(gamepad, printer).await } - -async fn write_to_spindle() -> Never { - let mut i2c = I2c::from_path("/dev/i2c-0").unwrap(); - let mut value = 0; - println!("functionality: {:?}", i2c.i2c_functionality()); - i2c.smbus_set_slave_address(I2C_ADDRESS_EXTENDER.into(), false) - .unwrap(); - loop { - value = u16::MAX - value; - match i2c.smbus_write_word_data(I2C_REGISTER_SPINDLE_SPEED, value) { - Ok(()) => println!("Wrote {} successfully", value), - Err(e) => println!("Error writing to device: {}", e), - }; - tokio::time::sleep(Duration::from_secs(1)).await; - } -} - -async fn write_to_printer() -> Never { - let mut printer = Printer::connect(DEFAULT_TTY).await.unwrap(); - printer.auto_home(true, true, true).await.unwrap(); - loop { - tokio::time::sleep(std::time::Duration::from_secs(5)).await; - printer.move_relative(50.0, 50.0, 5.0, None).await.unwrap(); - tokio::time::sleep(std::time::Duration::from_secs(5)).await; - printer.move_relative(25.0, 25.0, 5.0, None).await.unwrap(); - } -} diff --git a/red/src/spindle/mod.rs b/red/src/spindle/mod.rs new file mode 100644 index 0000000..b0a9e6b --- /dev/null +++ b/red/src/spindle/mod.rs @@ -0,0 +1,43 @@ +use futures::never::Never; +use i2c_linux::I2c; +use std::time::Duration; + +const I2C_ADDRESS_EXTENDER: u8 = 25; +const I2C_REGISTER_SPINDLE_SPEED: u8 = 0; + +// available i2c functionality on the device as reported from +// the i2c library +// TENBIT_ADDR +// SMBUS_PEC +// SMBUS_QUICK +// SMBUS_READ_BYTE +// SMBUS_WRITE_BYTE +// SMBUS_READ_BYTE_DATA +// SMBUS_WRITE_BYTE_DATA +// SMBUS_READ_WORD_DATA +// SMBUS_WRITE_WORD_DATA +// SMBUS_PROC_CALL +// SMBUS_WRITE_BLOCK_DATA +// SMBUS_READ_I2C_BLOCK +// SMBUS_WRITE_I2C_BLOCK +// SMBUS_BYTE +// SMBUS_BYTE_DATA +// SMBUS_WORD_DATA +// SMBUS_I2C_BLOCK +// SMBUS_EMUL + +pub async fn write_to_spindle() -> Never { + let mut i2c = I2c::from_path("/dev/i2c-0").unwrap(); + let mut value = 0; + println!("functionality: {:?}", i2c.i2c_functionality()); + i2c.smbus_set_slave_address(I2C_ADDRESS_EXTENDER.into(), false) + .unwrap(); + loop { + value = u16::MAX - value; + match i2c.smbus_write_word_data(I2C_REGISTER_SPINDLE_SPEED, value) { + Ok(()) => println!("Wrote {} successfully", value), + Err(e) => println!("Error writing to device: {}", e), + }; + tokio::time::sleep(Duration::from_secs(1)).await; + } +}