Skip to content

Commit

Permalink
Added std, serial2 as default features.
Browse files Browse the repository at this point in the history
moved `crate::serial2` re-export to `crate::system::serial_port::serial2`.
fixed module `system` not being public.
updated the dynamixelcli crate
  • Loading branch information
omelia-iliffe committed Sep 1, 2024
1 parent 37de7ee commit a665dbd
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 16 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Unreleased
- [major][add] Added `System` and `Transport` traits.
- [major][add] Added `StdSystem` & `SerialPort` structs.
- [major][add] Added `std` and `serial2` as default features
- [major][change] Changed `bus` to be generic over `System/Transport`
- [major][change] Changed errors types to be generic over `Transport::Error`
- [major][change] Moved `crate::serial2` re-export to `crate::system::serial_port::serial2`.

# Version 0.9.1 - 2024-07-31
- [minor][add] Add missing `Error` impl for `InitializeError`.

Expand Down
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ publish = ["crates-io"]

[dependencies]
log = { version = "0.4.8", optional = true }
serial2 = "0.2.24"
serial2 = { version = "0.2.24", optional = true }

[dev-dependencies]
assert2 = "0.3.3"
env_logger = "0.11.5"

[features]
rs4xx = ["serial2/rs4xx"]
default = ["std", "serial2"]
std = []
rs4xx = ["std", "serial2/rs4xx"]

[workspace]
members = ["dynamixel2-cli"]
5 changes: 3 additions & 2 deletions dynamixel2-cli/src/bin/dynamixel2/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use dynamixel2::systems::serial_port::SerialPort;
use dynamixel2::systems::std::StdSystem;
use std::path::Path;
use std::time::{Duration, Instant};

mod logging;
mod options;

Expand Down Expand Up @@ -151,7 +152,7 @@ fn do_main(options: Options) -> Result<(), ()> {
Ok(())
}

fn open_bus(options: &Options) -> Result<dynamixel2::Bus<Vec<u8>, Vec<u8>>, ()> {
fn open_bus(options: &Options) -> Result<dynamixel2::Bus<Vec<u8>, Vec<u8>, StdSystem<SerialPort>>, ()> {

Check warning on line 155 in dynamixel2-cli/src/bin/dynamixel2/main.rs

View workflow job for this annotation

GitHub Actions / Build and test

very complex type used. Consider factoring parts into `type` definitions

Check warning on line 155 in dynamixel2-cli/src/bin/dynamixel2/main.rs

View workflow job for this annotation

GitHub Actions / Build and test

very complex type used. Consider factoring parts into `type` definitions
let bus = dynamixel2::Bus::open(&options.serial_port, options.baud_rate)
.map_err(|e| log::error!("Failed to open serial port: {}: {}", options.serial_port.display(), e))?;
log::debug!(
Expand Down
22 changes: 16 additions & 6 deletions src/bus.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use std::path::Path;
use std::time::{Duration, Instant};

use crate::bytestuff;
use crate::checksum::calculate_checksum;
use crate::endian::{read_u16_le, read_u32_le, read_u8_le, write_u16_le};
use crate::systems::serial_port::SerialPort;
use crate::systems::std::StdSystem;
use crate::systems::{System, Transport};
use crate::{ReadError, TransferError, WriteError};

#[cfg(feature = "serial2")]
use crate::systems::serial_port::SerialPort;
#[cfg(feature = "std")]
use crate::systems::std::StdSystem;
#[cfg(feature = "serial2")]
use std::path::Path;

const HEADER_PREFIX: [u8; 4] = [0xFF, 0xFF, 0xFD, 0x00];
const HEADER_SIZE: usize = 8;
const STATUS_HEADER_SIZE: usize = 9;
Expand All @@ -34,7 +38,11 @@ pub struct Bus<ReadBuffer, WriteBuffer, S: System> {
write_buffer: WriteBuffer,
}
//
impl<ReadBuffer, WriteBuffer, S, T> core::fmt::Debug for Bus<ReadBuffer, WriteBuffer, S> where S: System<Transport = T>, T: Transport + core::fmt::Debug {
impl<ReadBuffer, WriteBuffer, S, T> core::fmt::Debug for Bus<ReadBuffer, WriteBuffer, S>
where
S: System<Transport = T>,
T: Transport + core::fmt::Debug,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Bus")
.field("transport", &self.transport)
Expand All @@ -43,7 +51,8 @@ impl<ReadBuffer, WriteBuffer, S, T> core::fmt::Debug for Bus<ReadBuffer, WriteBu
}
}

impl Bus<Vec<u8>, Vec<u8>, StdSystem> {
#[cfg(feature = "serial2")]
impl Bus<Vec<u8>, Vec<u8>, StdSystem<SerialPort>> {
/// Open a serial port with the given baud rate.
///
/// This will allocate a new read and write buffer of 128 bytes each.
Expand All @@ -67,7 +76,8 @@ impl Bus<Vec<u8>, Vec<u8>, StdSystem> {
}
}

impl<ReadBuffer, WriteBuffer> Bus<ReadBuffer, WriteBuffer, StdSystem>
#[cfg(feature = "serial2")]
impl<ReadBuffer, WriteBuffer> Bus<ReadBuffer, WriteBuffer, StdSystem<SerialPort>>
where
ReadBuffer: AsRef<[u8]> + AsMut<[u8]>,
WriteBuffer: AsRef<[u8]> + AsMut<[u8]>,
Expand Down
6 changes: 4 additions & 2 deletions src/instructions/bulk_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ where

#[cfg(test)]
mod tests {
use super::*;
use crate::systems::serial_port::SerialPort;
use crate::systems::std::StdSystem;
use super::*;

/// Ensure that `bulk_write` accepts a slice of `BulkWriteData`.
///
Expand Down Expand Up @@ -140,7 +140,9 @@ mod tests {
///
/// This is a compile test. It only tests that the test code compiles.
#[allow(dead_code)]
fn bulk_write_accepts_vec_ref_no_clone(bus: &mut Bus<Vec<u8>, Vec<u8>, StdSystem<SerialPort>>) -> Result<(), Box<dyn std::error::Error>> {
fn bulk_write_accepts_vec_ref_no_clone(
bus: &mut Bus<Vec<u8>, Vec<u8>, StdSystem<SerialPort>>,
) -> Result<(), Box<dyn std::error::Error>> {
/// Non-clonable wrapper around `&[u8]` to ensure `bulk_write` doesn't clone data from vec references.
struct Data<'a> {
data: &'a [u8],
Expand Down
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ mod bus;
pub use bus::*;

mod error;
mod systems;
pub mod systems;
pub use systems::System;
pub use systems::Transport;

pub use error::*;

/// Re-exported `serial2` crate in case you need to modify serial port settings.
pub use serial2;
7 changes: 7 additions & 0 deletions src/systems/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
//! Traits to support different systems and transports.
//!
//! Use the [`System`] trait to define the system-specific types
//! and the [`Transport`] trait to define the serial interface used to communicate with the hardware.
#[cfg(feature = "serial2")]
pub mod serial_port;
#[cfg(feature = "std")]
pub mod std;

use crate::ReadError;
Expand Down
7 changes: 7 additions & 0 deletions src/systems/serial_port.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
//! Serial port transport implementation using the `serial2` crate.
use crate::ReadError;
use std::fmt::Formatter;
use std::time::Duration;

/// Re-exported `serial2` crate in case you need to modify serial port settings.
pub use serial2;

/// A wrapper around a `serial2::SerialPort` that implements the `Transport` trait.
pub struct SerialPort {
port: serial2::SerialPort,
}

impl SerialPort {
/// Create a new `SerialPort` from a `serial2::SerialPort`.
pub fn new(port: serial2::SerialPort) -> Self {
Self { port }
}
Expand Down
3 changes: 3 additions & 0 deletions src/systems/std.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! The system implementation used by default when the `std` feature is enabled.
use crate::Transport;

/// System implementation using the standard library.
#[derive(Debug)]
pub struct StdSystem<T> {
_transport: T,
Expand Down

0 comments on commit a665dbd

Please sign in to comment.