diff --git a/red/Cargo.toml b/red/Cargo.toml index 99628e4..98749c3 100644 --- a/red/Cargo.toml +++ b/red/Cargo.toml @@ -6,7 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -bytes = "1.2.1" euclid = "0.22.7" futures = "0.3.24" gilrs = "0.10.1" @@ -15,6 +14,4 @@ lazy_static = "1.4.0" regex = "1.6.0" serialport = "4.6.1" tokio = { version = "1.21.0", features = ["full"] } -tokio-serial = "5.4.3" -tokio-util = { version = "0.7.4", features = ["codec"] } diff --git a/red/src/jogger.rs b/red/src/jogger.rs index b9438c2..1da8840 100644 --- a/red/src/jogger.rs +++ b/red/src/jogger.rs @@ -1,12 +1,8 @@ -use std::ops::DerefMut; use crate::gamepad::Gamepad; use crate::printer::Printer; use euclid::{vec3, Vector3D}; use futures::never::Never; -use std::sync::{Arc, Mutex}; use std::time::Duration; -use tokio::io::{AsyncRead, AsyncWrite}; -use tokio::time::sleep; /// Time that a single movement command should take /// diff --git a/red/src/main.rs b/red/src/main.rs index d706a3e..1f9a680 100644 --- a/red/src/main.rs +++ b/red/src/main.rs @@ -2,11 +2,9 @@ use futures::never::Never; use red::gamepad::Gamepad; -use red::printer::Printer; use red::jogger; -use std::path::Path; -use std::sync::{Arc, Mutex}; -use tokio_serial::SerialPortInfo; +use red::printer::Printer; +use std::path::{Path}; fn main() -> Never { jog() @@ -14,23 +12,30 @@ fn main() -> Never { fn jog() -> Never { let mut gamepad = Gamepad::new().expect("Failed to open gamepad"); - + println!("Entering App"); - let serial_ports = tokio_serial::available_ports() - .expect("Could not list serial ports") - .into_iter() - .filter(|port| port.port_name.contains("ttyUSB")) - .collect::<Vec<SerialPortInfo>>(); - let port = match serial_ports.len() { - 1 => serial_ports.to_owned().pop().unwrap(), - 0 => panic!("No USB serial port found! Is the green board disconnected or turned off?"), - _ => panic!("There are multiple USB serial ports and we have no way to check which one is the Marlin board!") - }; - let port_path = Path::new("/dev").join(Path::new(&port.port_name).file_name().unwrap()); - println!("Found serial port: {:?}", &port_path); - - let printer = Printer::connect_to_path(&port_path.as_os_str().to_string_lossy()).unwrap(); - + let dev_dir = std::fs::read_dir(Path::new("/dev")).expect("Failed to open device directory"); + let mut usb_tty_ports: Vec<String> = dev_dir + .filter_map(|entry| { + entry + .ok() + .and_then(|entry| entry.path().to_str().map(|s| s.to_string())) + .filter(|path| path.contains("ttyUSB")) + }) + .collect(); + + if usb_tty_ports.len() > 1 { + eprintln!("Found more than one ttyUSB port! Picking the first one...") + } + let port_path = usb_tty_ports.pop().expect("No USB serial port found!"); + + eprintln!("Found serial port: {:?}", &port_path); + + let printer = Printer::connect_to_path( + &port_path + ) + .unwrap(); + jogger::jog(&mut gamepad, printer) } @@ -41,32 +46,4 @@ fn print_gamepad_events() -> Never { println!("speed setpoint: {:?}", gamepad.speed_set_point(&events)); std::thread::sleep(std::time::Duration::from_millis(500)); } -} - -fn look_for_printer() -> Never { - println!("Entering App"); - let serial_ports = tokio_serial::available_ports() - .expect("Could not list serial ports") - .into_iter() - .filter(|port| port.port_name.contains("ttyUSB")) - .collect::<Vec<SerialPortInfo>>(); - let port = match serial_ports.len() { - 1 => serial_ports.to_owned().pop().unwrap(), - 0 => panic!("No USB serial port found! Is the green board disconnected or turned off?"), - _ => panic!("There are multiple USB serial ports and we have no way to check which one is the Marlin board!") - }; - let port_path = Path::new("/dev").join(Path::new(&port.port_name).file_name().unwrap()); - println!("Found serial port: {:?}", &port_path); - let mut printer = Printer::connect_to_path(&port_path.as_os_str().to_string_lossy()).unwrap(); - printer - .move_relative(10., 0., 0., None) - .expect("Could not move printer head"); - println!("{:?}", printer.printer_state()); - printer - .move_relative(-10., 0., 0., None) - .expect("Could not move printer head"); - loop { - println!("{:?}", printer.printer_state()); - std::thread::sleep(std::time::Duration::from_millis(1000)); - } -} +} \ No newline at end of file diff --git a/red/src/printer/mod.rs b/red/src/printer/mod.rs index 6a4f409..2dce60c 100644 --- a/red/src/printer/mod.rs +++ b/red/src/printer/mod.rs @@ -482,7 +482,7 @@ impl Printer { } pub fn connect_to_path(port_path: &str) -> Result<Self, PrinterError> { - let mut port = serialport::new(port_path, BAUD_RATE) + let port = serialport::new(port_path, BAUD_RATE) .timeout(Duration::from_millis(100)) .open_native() .expect("Unable to open serial port");