Skip to content

Commit

Permalink
Make Client::new() and Device::new() available for all serial ports.
Browse files Browse the repository at this point in the history
  • Loading branch information
de-vri-es committed Dec 30, 2024
1 parent a195785 commit 42ed81a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
33 changes: 17 additions & 16 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,6 @@ impl<'a> Client<'a, serial2::SerialPort> {
Ok(Self { bus })
}

/// Create a new client using an open serial port.
///
/// The serial port must already be configured in raw mode with the correct baud rate,
/// character size (8), parity (disabled) and stop bits (1).
///
/// This will allocate a new read and write buffer of 128 bytes each.
/// Use [`Self::with_buffers()`] if you want to use a custom buffers.
pub fn new(serial_port: serial2::SerialPort) -> std::io::Result<Self> {
let bus = Bus::with_buffers(
serial_port,
vec![0; 128],
vec![0; 128],
)?;
Ok(Self { bus })
}

/// Open a serial port with the given baud rate.
///
/// This will allocate a new read and write buffer of 128 bytes each.
Expand All @@ -82,6 +66,23 @@ impl<'a, SerialPort> Client<'a, SerialPort>
where
SerialPort: crate::SerialPort,
{
/// Create a new client using an open serial port.
///
/// The serial port must already be configured in raw mode with the correct baud rate,
/// character size (8), parity (disabled) and stop bits (1).
///
/// This will allocate a new read and write buffer of 128 bytes each.
/// Use [`Self::with_buffers()`] if you want to use a custom buffers.
#[cfg(feature = "alloc")]
pub fn new(serial_port: SerialPort) -> Result<Self, SerialPort::Error> {
let bus = Bus::with_buffers(
serial_port,
vec![0; 128],
vec![0; 128],
)?;
Ok(Self { bus })
}

/// Create a new client using pre-allocated buffers.
///
/// The serial port must already be configured in raw mode with the correct baud rate,
Expand Down
33 changes: 17 additions & 16 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,6 @@ impl<'a> Device<'a, serial2::SerialPort> {
Ok(Self { bus })
}

/// Create a new device for an open serial port.
///
/// The serial port must already be configured in raw mode with the correct baud rate,
/// character size (8), parity (disabled) and stop bits (1).
///
/// This will allocate a new read and write buffer of 128 bytes each.
/// Use [`Self::with_buffers()`] if you want to use a custom buffers.
pub fn new(serial_port: serial2::SerialPort) -> std::io::Result<Self> {
let bus = Bus::with_buffers(
serial_port,
vec![0; 128],
vec![0; 128],
)?;
Ok(Self { bus })
}

/// Open a serial port with the given baud rate.
///
/// This will allocate a new read and write buffer of 128 bytes each.
Expand All @@ -77,6 +61,23 @@ impl<'a, SerialPort> Device<'a, SerialPort>
where
SerialPort: crate::SerialPort,
{
/// Create a new device for an open serial port.
///
/// The serial port must already be configured in raw mode with the correct baud rate,
/// character size (8), parity (disabled) and stop bits (1).
///
/// This will allocate a new read and write buffer of 128 bytes each.
/// Use [`Self::with_buffers()`] if you want to use a custom buffers.
#[cfg(feature = "alloc")]
pub fn new(serial_port: SerialPort) -> Result<Self, SerialPort::Error> {
let bus = Bus::with_buffers(
serial_port,
vec![0; 128],
vec![0; 128],
)?;
Ok(Self { bus })
}

/// Create a new device using pre-allocated buffers.
pub fn with_buffers(
serial_port: SerialPort,
Expand Down
8 changes: 3 additions & 5 deletions tests/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ use mock_serial_port::MockSerialPort;

fn setup_bus() -> (Client<'static, MockSerialPort>, Device<'static, MockSerialPort>) {
let serial_port = MockSerialPort::new(56700);
let device_serial_port = serial_port.device_port();
(
Client::with_buffers(serial_port, vec![0; 1024], vec![0; 1024]).unwrap(),
Device::with_buffers(device_serial_port, vec![0; 1024], vec![0; 1024]).unwrap(),
)
let_assert!(Ok(device) = Device::new(serial_port.device_port()));
let_assert!(Ok(client) = Client::new(serial_port));
(client, device)
}

const DEVICE_ID: u8 = 1;
Expand Down

0 comments on commit 42ed81a

Please sign in to comment.