From a665dbd7a7a465102a00c5de45dc12e211a0d986 Mon Sep 17 00:00:00 2001 From: Omelia Iliffe Date: Sun, 1 Sep 2024 17:31:21 +1200 Subject: [PATCH] Added `std`, `serial2` as default features. moved `crate::serial2` re-export to `crate::system::serial_port::serial2`. fixed module `system` not being public. updated the dynamixelcli crate --- CHANGELOG | 8 ++++++++ Cargo.toml | 6 ++++-- dynamixel2-cli/src/bin/dynamixel2/main.rs | 5 +++-- src/bus.rs | 22 ++++++++++++++++------ src/instructions/bulk_write.rs | 6 ++++-- src/lib.rs | 5 +---- src/systems/mod.rs | 7 +++++++ src/systems/serial_port.rs | 7 +++++++ src/systems/std.rs | 3 +++ 9 files changed, 53 insertions(+), 16 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 3eeba96..97bd814 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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`. diff --git a/Cargo.toml b/Cargo.toml index ab960d0..098f873 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/dynamixel2-cli/src/bin/dynamixel2/main.rs b/dynamixel2-cli/src/bin/dynamixel2/main.rs index 6d57e9a..6dd88b5 100644 --- a/dynamixel2-cli/src/bin/dynamixel2/main.rs +++ b/dynamixel2-cli/src/bin/dynamixel2/main.rs @@ -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; @@ -151,7 +152,7 @@ fn do_main(options: Options) -> Result<(), ()> { Ok(()) } -fn open_bus(options: &Options) -> Result, Vec>, ()> { +fn open_bus(options: &Options) -> Result, Vec, StdSystem>, ()> { 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!( diff --git a/src/bus.rs b/src/bus.rs index f0bb5bc..d41964f 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -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; @@ -34,7 +38,11 @@ pub struct Bus { write_buffer: WriteBuffer, } // -impl core::fmt::Debug for Bus where S: System, T: Transport + core::fmt::Debug { +impl core::fmt::Debug for Bus +where + S: System, + T: Transport + core::fmt::Debug, +{ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Bus") .field("transport", &self.transport) @@ -43,7 +51,8 @@ impl core::fmt::Debug for Bus, Vec, StdSystem> { +#[cfg(feature = "serial2")] +impl Bus, Vec, StdSystem> { /// Open a serial port with the given baud rate. /// /// This will allocate a new read and write buffer of 128 bytes each. @@ -67,7 +76,8 @@ impl Bus, Vec, StdSystem> { } } -impl Bus +#[cfg(feature = "serial2")] +impl Bus> where ReadBuffer: AsRef<[u8]> + AsMut<[u8]>, WriteBuffer: AsRef<[u8]> + AsMut<[u8]>, diff --git a/src/instructions/bulk_write.rs b/src/instructions/bulk_write.rs index a43072a..f4dd2d9 100644 --- a/src/instructions/bulk_write.rs +++ b/src/instructions/bulk_write.rs @@ -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`. /// @@ -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, StdSystem>) -> Result<(), Box> { + fn bulk_write_accepts_vec_ref_no_clone( + bus: &mut Bus, Vec, StdSystem>, + ) -> Result<(), Box> { /// Non-clonable wrapper around `&[u8]` to ensure `bulk_write` doesn't clone data from vec references. struct Data<'a> { data: &'a [u8], diff --git a/src/lib.rs b/src/lib.rs index e770ad3..e056795 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 08807e2..7c8b339 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -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; diff --git a/src/systems/serial_port.rs b/src/systems/serial_port.rs index a7be975..80164d0 100644 --- a/src/systems/serial_port.rs +++ b/src/systems/serial_port.rs @@ -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 } } diff --git a/src/systems/std.rs b/src/systems/std.rs index 7711acb..e4b7411 100644 --- a/src/systems/std.rs +++ b/src/systems/std.rs @@ -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 { _transport: T,