From b5df68e32cba3f9ba88b0bd2bc56b54ee1437e4b Mon Sep 17 00:00:00 2001 From: krarpit Date: Thu, 8 Aug 2024 00:32:33 -0700 Subject: [PATCH] modify builder to enable using_usb_serial configuration --- rustfmt.toml | 2 +- src/maestro/builder.rs | 41 ++++++++++++++++++++++++++++++------ src/maestro/constants/mod.rs | 6 ++++++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/rustfmt.toml b/rustfmt.toml index 5d2b127..5671f79 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -28,7 +28,7 @@ match_block_trailing_comma = true # Function fn_single_line = false -fn_args_layout = "Tall" +fn_params_layout = "Tall" # fn_call_width = 60 force_multiline_blocks = false diff --git a/src/maestro/builder.rs b/src/maestro/builder.rs index 5455132..e5449c3 100644 --- a/src/maestro/builder.rs +++ b/src/maestro/builder.rs @@ -28,9 +28,11 @@ use rppal::uart::Uart; use crate::errors::Error; use crate::maestro::constants::Baudrate; +use crate::maestro::constants::VIRTUAL_COMMAND_PORT; use crate::maestro::internals; use crate::maestro::Maestro; + #[derive(Default)] /// ### Purpose: /// A builder into which configurations for the [`super::maestro::Maestro`] can @@ -43,8 +45,14 @@ pub struct Builder { /// ### Purpose: /// How long to wait for a response before quitting and returning. pub block_duration: Option, + + /// ### Purpose: + /// Indicates whether the Maestro is connected using USB. + pub using_usb_serial: bool, } +// TODO: @krarpit typestate for builder to provide a method to configure virtual serial +// port to connect to impl Builder { /// ### Purpose: /// Convenience function to configure the baudrate for this builder. @@ -62,6 +70,16 @@ impl Builder { ..self } } + + /// ### Purpose: + /// Convenience function to configure whether or not to use USB serial to + /// communicate to the Maestro. + pub fn using_usb_serial(self, flag: bool) -> Self { + Self { + using_usb_serial: flag, + ..self + } + } } impl TryFrom for Maestro { @@ -71,15 +89,26 @@ impl TryFrom for Maestro { Builder { baudrate, block_duration, + using_usb_serial, }: Builder, ) -> Result { let baudrate = baudrate.ok_or_else(|| Error::Uninitialized)? as u32; - let mut uart = Uart::new( - baudrate, - Parity::None, - internals::DATA_BITS, - internals::STOP_BITS, - )?; + let mut uart = if using_usb_serial { + Uart::with_path( + VIRTUAL_COMMAND_PORT, + baudrate, + Parity::None, + internals::DATA_BITS, + internals::STOP_BITS, + )?; + } else { + Uart::new( + baudrate, + Parity::None, + internals::DATA_BITS, + internals::STOP_BITS, + )?; + }; let block_duration = block_duration.unwrap_or_default(); uart.set_read_mode(0u8, block_duration)?; let read_buf = [0u8; internals::BUFFER_SIZE]; diff --git a/src/maestro/constants/mod.rs b/src/maestro/constants/mod.rs index d70eb10..021030a 100644 --- a/src/maestro/constants/mod.rs +++ b/src/maestro/constants/mod.rs @@ -29,10 +29,16 @@ pub const MIN_QTR_PWM: u16 = 3968u16; /// error. pub const MAX_QTR_PWM: u16 = 8000u16; +/// ### Purpose: +/// When connected via USB, the Maestro creates two virtual serial ports +/// /dev/ttyACM0 for commands and /dev/ttyACM1 for communications. +pub const VIRTUAL_COMMAND_PORT: &str = "/dev/ttyACM0"; + /// ### Purpose: /// Maximum number of channels on the Maestro. const CHANNEL_COUNT: u8 = 6u8; + /// ### Purpose: /// All available channels to send commands to. #[derive(Copy, Clone, PartialEq)]