From 8d74ff09426f2058e6642060f259424a0ee1b42d Mon Sep 17 00:00:00 2001 From: Frederik Menke Date: Mon, 20 Nov 2023 22:22:44 +0100 Subject: [PATCH] Detect serial port path automatically --- red/src/jogger.rs | 2 +- red/src/main.rs | 19 +++++++++++++++++-- red/src/printer/mod.rs | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/red/src/jogger.rs b/red/src/jogger.rs index 6200e6b..a811ddd 100644 --- a/red/src/jogger.rs +++ b/red/src/jogger.rs @@ -1,6 +1,6 @@ use crate::gamepad::Gamepad; use crate::printer::gcode::{G0Command, G91Command}; -use crate::printer::{GcodeCommand, Printer}; +use crate::printer::{Printer}; use euclid::{vec3, Vector3D}; use futures::never::Never; use std::sync::Arc; diff --git a/red/src/main.rs b/red/src/main.rs index 86c3b97..c82dadb 100644 --- a/red/src/main.rs +++ b/red/src/main.rs @@ -5,7 +5,9 @@ use red::gamepad; use red::jogger; use red::printer::gcode::*; 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; @@ -35,9 +37,22 @@ const I2C_REGISTER_SPINDLE_SPEED: u8 = 0; #[tokio::main] async fn main() -> 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::>(); + 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 gamepad = gamepad::Gamepad::new().await.unwrap(); - let printer = Printer::connect("/dev/ttyUSB2").await.unwrap(); - // TODO: Make printer do relative movements + let printer = Printer::connect(&port_path.as_os_str().to_string_lossy()) + .await + .unwrap(); jogger::jog(gamepad, printer).await } diff --git a/red/src/printer/mod.rs b/red/src/printer/mod.rs index 864821a..dde1cf7 100644 --- a/red/src/printer/mod.rs +++ b/red/src/printer/mod.rs @@ -14,7 +14,7 @@ use tokio_util::codec::{Decoder, Encoder}; pub use gcode::{GcodeCommand, GcodeReply}; -use crate::printer::gcode::{G91Command, M997Command}; +use crate::printer::gcode::G91Command; use self::gcode::GcodeReplyError;