Expand description
This module contains all the structures and functions related to +
pub struct B15F { /* private fields */ }
Expand description
Structure representing the driver for the board 15
-Implementations
sourceimpl B15F
impl B15F
sourcepub fn new() -> Result<B15F, Error>
pub fn new() -> Result<B15F, Error>
Creates a new instance of the B15
+pub struct B15F { /* private fields */ }
Expand description
Structure representing the driver for the board 15
+Implementations
sourceimpl B15F
impl B15F
sourcepub fn new() -> Result<B15F, Error>
pub fn new() -> Result<B15F, Error>
Creates a new instance of the B15
This function will establish a connection to a connected B15 and return
a handle to interact with it. Only one such instance should exist per
program; calling B15F::new()
more than once might lead to unexpected
@@ -14,21 +14,52 @@ established, or if testing of that connection fails.
use b15f::B15F;
let drv = B15F::new().unwrap();
sourcepub fn digital_write<const port: u8>(&mut self, value: u8) -> Result<(), Error>
pub fn digital_write<const port: u8>(&mut self, value: u8) -> Result<(), Error>
Sets the value of the specified port
+sourcepub fn get_board_info(&mut self) -> Result<Vec<String>, Error>
pub fn get_board_info(&mut self) -> Result<Vec<String>, Error>
Yields information about the installed firmware on the B15
+Examples
+use b15f::B15F;
+
+fn main() -> Result<(), String> {
+ let mut drv = B15F::new()?;
+
+ drv.digital_write::<0>(0xFF)?; // Turn on all bits of port 0
+ drv.digital_write::<1>(0x0F)?; // Turn on bits 0-4 of port 1
+
+ // drv.digital_write::<2>(0xFF); // Compiler error
+
+ Ok(())
+}
sourcepub fn digital_read<const PORT: usize>(&mut self) -> Result<u8, Error>
pub fn digital_read<const PORT: usize>(&mut self) -> Result<u8, Error>
Reads the value of the specified port
+Errors
+PORT
can either be 0 or 1, other values will cause a compile-time
+error. Otherwise an error::Error
is generated if communication
+with the B15 fails.
Examples
+use b15f::B15F;
+
+fn main() -> Result<(), String> {
+ let mut drv = B15F::new()?;
+
+ let _ = drv.digital_read::<0>()?; // Read inputs of port 0
+ let _ = drv.digital_read::<1>()?; // Read inputs of port 1
+
+ // drv.digital_read::<2>(); // Compiler error
+
+ Ok(())
+}
sourcepub fn get_board_info(&mut self) -> Result<Vec<String>, Error>
pub fn get_board_info(&mut self) -> Result<Vec<String>, Error>
Yields information about the installed firmware on the B15
Returns an array of strings, where each string contains a piece of information stored on the B15
Returns
A list of strings where each string contains a piece of information about the board. What string contains what information is determined, but not explicitly listed.
-Errors
+Errors
An error::Error
is generated if the communication with the board fails.
Examples
+Examples
use b15f::B15F;
let mut drv = B15F::new().unwrap();
@@ -38,19 +69,19 @@ but not explicitly listed.
.unwrap()
.iter()
.for_each(|info| println!("{info}"));
sourcepub fn discard(&mut self) -> Result<(), Error>
pub fn discard(&mut self) -> Result<(), Error>
Clears data in the USART buffers on this device and on the B15
-sourcepub fn test_int_conv(&mut self) -> Result<(), Error>
pub fn test_int_conv(&mut self) -> Result<(), Error>
Test the integer conversion of the USART connection
-Errors
+sourcepub fn discard(&mut self) -> Result<(), Error>
pub fn discard(&mut self) -> Result<(), Error>
Clears data in the USART buffers on this device and on the B15
+sourcepub fn test_int_conv(&mut self) -> Result<(), Error>
pub fn test_int_conv(&mut self) -> Result<(), Error>
Test the integer conversion of the USART connection
+Errors
If an error occurs in the conversion or the communication with the
board, an error::Error
will be returned.
sourcepub fn test_connection(&mut self) -> Result<(), Error>
pub fn test_connection(&mut self) -> Result<(), Error>
Tests the connetion to the B15
+sourcepub fn test_connection(&mut self) -> Result<(), Error>
pub fn test_connection(&mut self) -> Result<(), Error>
Tests the connetion to the B15
To test the connection a Request::Test
request will be sent
to the board together with a randomly generated value. If the
board returns that value the connection is working correctly.
Errors
+Errors
An error::Error
is returned if the test fails, or if the
communication itself fails.
Examples
+Examples
use b15f::B15F;
fn main() {
@@ -60,7 +91,7 @@ communication itself fails.
panic!("Connection is not working: {err}");
}
}
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for B15F
impl Send for B15F
impl !Sync for B15F
impl Unpin for B15F
impl !UnwindSafe for B15F
Blanket Implementations
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for B15F
impl Send for B15F
impl !Sync for B15F
impl Unpin for B15F
impl !UnwindSafe for B15F
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<T, U> Into<U> for Twhere
U: From<T>,
impl<T, U> Into<U> for Twhere
U: From<T>,
const: unstable · sourcefn into(self) -> U
fn into(self) -> U
Calls U::from(self)
.
That is, this conversion is whatever the implementation of
From<T> for U
chooses to do.
Expand description
Driver code for the board 15
+Expand description
Driver code for the board 15
It aims to be syntactically similar to the original
implementation.
The original repository is no longer maintained, and this library doesn’t maintain it either,
it simply provides the same functionality in another language.
U::from(self)
.","Creates a new instance of the B15","Tests the connetion to the B15","Test the integer conversion of the USART connection","","","",""],"i":[0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],"f":[0,0,0,0,[[]],[[]],[[1,2],[[3,[0]]]],[1,[[3,[0]]]],[[1,4],5],[[]],[1,[[3,[[7,[6]],0]]]],[[]],[[],[[3,[1,0]]]],[1,[[3,[0]]]],[1,[[3,[0]]]],[[],3],[[],3],[[],8],[[]]],"p":[[3,"B15F"],[15,"u8"],[4,"Result"],[3,"Formatter"],[6,"Result"],[3,"String"],[3,"Vec"],[3,"TypeId"]]}\
+"b15f":{"doc":"Driver code for the board 15","t":[2,0,14,3,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11],"n":["B15F","b15f","build_request","B15F","borrow","borrow_mut","digital_read","digital_write","discard","fmt","from","get_board_info","into","new","test_connection","test_int_conv","try_from","try_into","type_id","vzip"],"q":["b15f","","","b15f::b15f","","","","","","","","","","","","","","","",""],"d":["","This module contains all the structures and functions …","Builds a new request buffer from the given data","Structure representing the driver for the board 15","","","Reads the value of the specified port","Sets the value of the specified port","Clears data in the USART buffers on this device and on the …","","Returns the argument unchanged.","Yields information about the installed firmware on the B15","Calls U::from(self)
.","Creates a new instance of the B15","Tests the connetion to the B15","Test the integer conversion of the USART connection","","","",""],"i":[0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],"f":[0,0,0,0,[[]],[[]],[1,[[3,[2,0]]]],[[1,2],[[3,[0]]]],[1,[[3,[0]]]],[[1,4],5],[[]],[1,[[3,[[7,[6]],0]]]],[[]],[[],[[3,[1,0]]]],[1,[[3,[0]]]],[1,[[3,[0]]]],[[],3],[[],3],[[],8],[[]]],"p":[[3,"B15F"],[15,"u8"],[4,"Result"],[3,"Formatter"],[6,"Result"],[3,"String"],[3,"Vec"],[3,"TypeId"]]}\
}');
if (typeof window !== 'undefined' && window.initSearch) {window.initSearch(searchIndex)};
if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
diff --git a/doc/source-files.js b/doc/source-files.js
index 5669024..b008575 100644
--- a/doc/source-files.js
+++ b/doc/source-files.js
@@ -1,4 +1,4 @@
var sourcesIndex = JSON.parse('{\
-"b15f":["",[],["b15f.rs","error.rs","lib.rs","request.rs"]]\
+"b15f":["",[],["assert.rs","b15f.rs","error.rs","lib.rs","request.rs"]]\
}');
createSourceSidebar();
diff --git a/doc/src/b15f/assert.rs.html b/doc/src/b15f/assert.rs.html
new file mode 100644
index 0000000..d8dbe2c
--- /dev/null
+++ b/doc/src/b15f/assert.rs.html
@@ -0,0 +1,19 @@
+1 +2 +3 +4 +5 +6 +7 +8 +9 +
struct AssertInRange<const VALUE: usize, const MIN: usize, const MAX: usize>;
+
+impl<const VALUE: usize, const MIN: usize, const MAX: usize> AssertInRange<VALUE, MIN, MAX> {
+ const OK: () = assert!(MIN <= VALUE && VALUE <= MAX);
+}
+
+pub fn assert_in_range<const VALUE: usize, const MIN: usize, const MAX: usize> () {
+ let () = AssertInRange::<VALUE, MIN, MAX>::OK;
+}
//! This module contains all the structures and functions related to
//! interacting with the B15 on a high level. If you are writing code
//! for the B15, this is the module you want to use.
@@ -321,9 +379,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)*));
@@ -435,30 +492,89 @@
/// Sets the value of the specified port
///
/// # Errors
- /// `port` can either be 0 or 1, other values will cause a compile-time
+ /// `PORT` can either be 0 or 1, other values will cause a compile-time
/// error. Otherwise an `error::Error` is generated if communication
/// with the B15 fails.
///
/// # Examples
+ /// ```
+ /// use b15f::B15F;
///
- pub fn digital_write<const port: u8> (&mut self, value: u8) -> Result<(), Error> {
- assert!(port == 0 || port == 1);
+ /// fn main() -> Result<(), String> {
+ /// let mut drv = B15F::new()?;
+ ///
+ /// drv.digital_write::<0>(0xFF)?; // Turn on all bits of port 0
+ /// drv.digital_write::<1>(0x0F)?; // Turn on bits 0-4 of port 1
+ ///
+ /// // drv.digital_write::<2>(0xFF); // Compiler error
+ ///
+ /// Ok(())
+ /// }
+ /// ```
+ ///
+ pub fn digital_write<const PORT: usize> (&mut self, value: u8) -> Result<(), Error> {
+ assert_in_range::<PORT, 0, 1>();
let reversed = value.reverse_bits();
- let request = if port == 0 { Request::DigitalWrite0 } else { Request::DigitalWrite1 };
-
+ let request = match PORT {
+ 0 => Request::DigitalWrite0,
+ 1 => Request::DigitalWrite1,
+ _ => panic!("Report this issue to someone, this should not ever be visible.")
+ };
+
self.usart.write(build_request![request, reversed])?;
let mut aw: [u8; 1] = [0; 1];
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(())
}
+ /// Reads the value of the specified port
+ ///
+ /// # Errors
+ /// `PORT` can either be 0 or 1, other values will cause a compile-time
+ /// error. Otherwise an `error::Error` is generated if communication
+ /// with the B15 fails.
+ ///
+ /// # Examples
+ /// ```
+ /// use b15f::B15F;
+ ///
+ /// fn main() -> Result<(), String> {
+ /// let mut drv = B15F::new()?;
+ ///
+ /// let _ = drv.digital_read::<0>()?; // Read inputs of port 0
+ /// let _ = drv.digital_read::<1>()?; // Read inputs of port 1
+ ///
+ /// // drv.digital_read::<2>(); // Compiler error
+ ///
+ /// Ok(())
+ /// }
+ /// ```
+ ///
+ pub fn digital_read<const PORT: usize> (&mut self) -> Result<u8, Error> {
+ assert_in_range::<PORT, 0, 1>();
+
+ let request = match PORT {
+ 0 => Request::DigitalRead0,
+ 1 => Request::DigitalRead1,
+ _ => panic!("Report this issue to someone, this should not ever be visible.")
+ };
+
+ self.usart.clear(serialport::ClearBuffer::Input)?;
+ self.usart.write(build_request![request])?;
+
+ let mut aw: [u8; 1] = [0; 1];
+ self.usart.read(&mut aw)?;
+
+ Ok(u8::reverse_bits(aw[0]))
+ }
+
/// Yields information about the installed firmware on the B15
///
/// Returns an array of strings, where each string contains a piece
diff --git a/doc/src/b15f/error.rs.html b/doc/src/b15f/error.rs.html
index 1310124..775510a 100644
--- a/doc/src/b15f/error.rs.html
+++ b/doc/src/b15f/error.rs.html
@@ -40,6 +40,12 @@
40
41
42
+43
+44
+45
+46
+47
+48
use std::fmt::Display;
#[derive(Debug)]
@@ -77,6 +83,12 @@
}
}
+impl From<Error> for String {
+ fn from(err: Error) -> Self {
+ err.message
+ }
+}
+
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)
diff --git a/doc/src/b15f/lib.rs.html b/doc/src/b15f/lib.rs.html
index c99c952..7dc4593 100644
--- a/doc/src/b15f/lib.rs.html
+++ b/doc/src/b15f/lib.rs.html
@@ -18,6 +18,7 @@
18
19
20
+21
#![deny(missing_docs,
missing_debug_implementations,
trivial_casts,
@@ -36,6 +37,7 @@
pub mod b15f;
mod request;
mod error;
+mod assert;
pub use crate::b15f::B15F;
//! This module contains the request data used to communicate
//! with the B15 via USART.
//!
@@ -51,5 +53,7 @@
DigitalWrite0 = 5,
DigitalWrite1 = 6,
-}