diff --git a/Cargo.toml b/Cargo.toml index e87964c..cd77aa7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,5 +11,4 @@ path = "src/lib.rs" [dependencies] serialport = "4.2.0" -rand = "0.8.5" - +rand = "0.8.5" \ No newline at end of file diff --git a/examples/nightrider.rs b/examples/nightrider.rs index ece6934..8972314 100644 --- a/examples/nightrider.rs +++ b/examples/nightrider.rs @@ -16,6 +16,6 @@ fn main() { direction *= -1; } - sleep(Duration::from_millis(50)); + sleep(Duration::from_millis(40)); } } \ No newline at end of file diff --git a/src/assert.rs b/src/assert.rs new file mode 100644 index 0000000..cb7f223 --- /dev/null +++ b/src/assert.rs @@ -0,0 +1,9 @@ +struct AssertInRange; + +impl AssertInRange { + const OK: () = assert!(MIN <= VALUE && VALUE <= MAX); +} + +pub fn assert_in_range () { + let () = AssertInRange::::OK; +} \ No newline at end of file diff --git a/src/b15f.rs b/src/b15f.rs index 807aa20..1a1955c 100644 --- a/src/b15f.rs +++ b/src/b15f.rs @@ -5,9 +5,8 @@ use std::{process::Command, time::Duration, fmt::Debug, thread::sleep}; use rand::Rng; use serialport::SerialPort; -use crate::error::Error; -use crate::{request::Request, build_request}; +use crate::{assert::assert_in_range, error::Error, request::Request, build_request}; macro_rules! log { ($text: literal, $($arg:tt)*) => (println!(concat!("[B15F] ", $text), $($arg)*)); @@ -125,11 +124,11 @@ impl B15F { /// /// # Examples /// - pub fn digital_write (&mut self, value: u8) -> Result<(), Error> { - assert!(port == 0 || port == 1); + pub fn digital_write (&mut self, value: u8) -> Result<(), Error> { + assert_in_range::(); let reversed = value.reverse_bits(); - let request = if port == 0 { Request::DigitalWrite0 } else { Request::DigitalWrite1 }; + let request = if PORT == 0 { Request::DigitalWrite0 } else { Request::DigitalWrite1 }; self.usart.write(build_request![request, reversed])?; @@ -137,7 +136,7 @@ impl B15F { self.usart.read(&mut aw)?; if aw[0] != B15F::MSG_OK { - return Err(format!("Setting Port {} failed", port).into()); + return Err(format!("Setting Port {} failed", PORT).into()); } Ok(()) diff --git a/src/lib.rs b/src/lib.rs index b4df429..84b63dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,5 +16,6 @@ pub mod b15f; mod request; mod error; +mod assert; pub use crate::b15f::B15F; \ No newline at end of file