Skip to content

Commit

Permalink
modify builder to enable using_usb_serial configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
kumarpit committed Aug 8, 2024
1 parent 77a054d commit b5df68e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
41 changes: 35 additions & 6 deletions src/maestro/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,8 +45,14 @@ pub struct Builder {
/// ### Purpose:
/// How long to wait for a response before quitting and returning.
pub block_duration: Option<Duration>,

/// ### 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.
Expand All @@ -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<Builder> for Maestro {
Expand All @@ -71,15 +89,26 @@ impl TryFrom<Builder> for Maestro {
Builder {
baudrate,
block_duration,
using_usb_serial,
}: Builder,
) -> Result<Self, Self::Error> {
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];
Expand Down
6 changes: 6 additions & 0 deletions src/maestro/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit b5df68e

Please sign in to comment.