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,8 +14,15 @@ established, or if testing of that connection fails.
use b15f::B15F;
let drv = B15F::new().unwrap();
sourcepub fn digital_write<const PORT: usize>(
&mut self,
value: u8
) -> Result<(), Error>
pub fn digital_write<const PORT: usize>(
&mut self,
value: u8
) -> Result<(), Error>
Sets the value of the specified port
+sourcepub fn reconnect(&mut self) -> Result<(), Error>
pub fn reconnect(&mut self) -> Result<(), Error>
Tries to reestablish a connection to the B15
+sourcepub fn self_test(&mut self) -> Result<(), Error>
pub fn self_test(&mut self) -> Result<(), Error>
Enables the self test mode of the B15
+IMPORTANT: Nothing must be connected to the B15 during this self check +routine.
Errors
+This function returns an error::Error
when communication with
+the board or the self check fails.
sourcepub fn digital_write<const PORT: usize>(
&mut self,
value: u8
) -> Result<(), Error>
pub fn digital_write<const PORT: usize>(
&mut self,
value: u8
) -> Result<(), Error>
Sets 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.
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
+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.
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
+sourcepub fn read_dip_switch(&mut self) -> Result<u8, Error>
pub fn read_dip_switch(&mut self) -> Result<u8, Error>
Reads the value of the DIP switch (S7)
+Returns
+A bitfield representing the value of all 8 DIP switches. The least +significant bit represents switch 1.
+Errors
+When communication with the board fails an error::Error
is returned.
Example
+use b15f::B15F;
+
+fn main() -> Result<(), String> {
+ let mut drv = B15F::new()?;
+
+ println!("{}", drv.read_dip_switch()?);
+
+ 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
+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
use b15f::B15F;
@@ -69,16 +92,16 @@ 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
@@ -91,7 +114,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.
U::from(self)
.","Creates a new instance of the B15","Reads the value of the DIP switch (S7)","Tries to reestablish a connection to the B15","Enables the self test mode 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,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,[2,0]]]],[1,[[3,[0]]]],[1,[[3,[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/search.js b/docs/search.js
similarity index 100%
rename from doc/search.js
rename to docs/search.js
diff --git a/doc/settings.css b/docs/settings.css
similarity index 100%
rename from doc/settings.css
rename to docs/settings.css
diff --git a/doc/settings.html b/docs/settings.html
similarity index 100%
rename from doc/settings.html
rename to docs/settings.html
diff --git a/doc/settings.js b/docs/settings.js
similarity index 100%
rename from doc/settings.js
rename to docs/settings.js
diff --git a/doc/source-files.js b/docs/source-files.js
similarity index 86%
rename from doc/source-files.js
rename to docs/source-files.js
index b008575..b04802a 100644
--- a/doc/source-files.js
+++ b/docs/source-files.js
@@ -1,4 +1,4 @@
var sourcesIndex = JSON.parse('{\
-"b15f":["",[],["assert.rs","b15f.rs","error.rs","lib.rs","request.rs"]]\
+"b15f":["",[],["assert.rs","b15f.rs","error.rs","lib.rs","request.rs","usart.rs"]]\
}');
createSourceSidebar();
diff --git a/doc/source-script.js b/docs/source-script.js
similarity index 100%
rename from doc/source-script.js
rename to docs/source-script.js
diff --git a/doc/src/b15f/assert.rs.html b/docs/src/b15f/assert.rs.html
similarity index 100%
rename from doc/src/b15f/assert.rs.html
rename to docs/src/b15f/assert.rs.html
diff --git a/doc/src/b15f/b15f.rs.html b/docs/src/b15f/b15f.rs.html
similarity index 84%
rename from doc/src/b15f/b15f.rs.html
rename to docs/src/b15f/b15f.rs.html
index 173dca6..79a391c 100644
--- a/doc/src/b15f/b15f.rs.html
+++ b/docs/src/b15f/b15f.rs.html
@@ -372,6 +372,64 @@
372
373
374
+375
+376
+377
+378
+379
+380
+381
+382
+383
+384
+385
+386
+387
+388
+389
+390
+391
+392
+393
+394
+395
+396
+397
+398
+399
+400
+401
+402
+403
+404
+405
+406
+407
+408
+409
+410
+411
+412
+413
+414
+415
+416
+417
+418
+419
+420
+421
+422
+423
+424
+425
+426
+427
+428
+429
+430
+431
+432
//! 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.
@@ -380,7 +438,7 @@
use rand::Rng;
use serialport::SerialPort;
-use crate::{assert::assert_in_range, error::Error, request::Request, build_request};
+use crate::{assert::assert_in_range, error::Error, request::Request, usart::read_sized, build_request};
macro_rules! log {
($text: literal, $($arg:tt)*) => (println!(concat!("[B15F] ", $text), $($arg)*));
@@ -489,6 +547,42 @@
Ok(port)
}
+ /// Tries to reestablish a connection to the B15
+ pub fn reconnect(&mut self) -> Result<(), Error> {
+ let tries = 3;
+ while tries > 0 {
+ sleep(Duration::from_millis(64));
+ self.discard()?;
+
+ match self.test_connection() {
+ Ok(_) => return Ok(()),
+ Err(_) => { }
+ };
+
+ }
+
+ Err("Connection could not be repaired".into())
+ }
+
+ /// Enables the self test mode of the B15
+ ///
+ /// IMPORTANT: Nothing must be connected to the B15 during this self check
+ /// routine.
+ ///
+ /// # Errors
+ /// This function returns an `error::Error` when communication with
+ /// the board or the self check fails.
+ pub fn self_test(&mut self) -> Result<(), Error> {
+ self.usart.write(build_request!(Request::SelfTest))?;
+
+ let aw = read_sized::<1>(&mut self.usart)?;
+ if aw[0] != B15F::MSG_OK {
+ return Err("Self test failed".into())
+ }
+
+ Ok(())
+ }
+
/// Sets the value of the specified port
///
/// # Errors
@@ -524,9 +618,7 @@
self.usart.write(build_request![request, reversed])?;
- let mut aw: [u8; 1] = [0; 1];
- self.usart.read(&mut aw)?;
-
+ let aw = read_sized::<1>(&mut self.usart)?;
if aw[0] != B15F::MSG_OK {
return Err(format!("Setting Port {} failed", PORT).into());
}
@@ -569,12 +661,39 @@
self.usart.clear(serialport::ClearBuffer::Input)?;
self.usart.write(build_request![request])?;
- let mut aw: [u8; 1] = [0; 1];
- self.usart.read(&mut aw)?;
-
+ let aw = read_sized::<1>(&mut self.usart)?;
Ok(u8::reverse_bits(aw[0]))
}
+ /// Reads the value of the DIP switch (S7)
+ ///
+ /// # Returns
+ /// A bitfield representing the value of all 8 DIP switches. The least
+ /// significant bit represents switch 1.
+ ///
+ /// # Errors
+ /// When communication with the board fails an `error::Error` is returned.
+ ///
+ /// # Example
+ /// ```
+ /// use b15f::B15F;
+ ///
+ /// fn main() -> Result<(), String> {
+ /// let mut drv = B15F::new()?;
+ ///
+ /// println!("{}", drv.read_dip_switch()?);
+ ///
+ /// Ok(())
+ /// }
+ /// ```
+ pub fn read_dip_switch(&mut self) -> Result<u8, Error> {
+ self.usart.clear(serialport::ClearBuffer::Input)?;
+ self.usart.write(build_request!(Request::ReadDipSwitch))?;
+
+ let aw = read_sized::<1>(&mut self.usart)?;
+ Ok(aw[0].reverse_bits())
+ }
+
/// Yields information about the installed firmware on the B15
///
/// Returns an array of strings, where each string contains a piece
@@ -625,9 +744,7 @@
data_count[0] -= 1;
}
- let mut aw: [u8; 1] = [0; 1];
- self.usart.read(&mut aw)?;
-
+ let aw = read_sized::<1>(&mut self.usart)?;
if aw[0] != B15F::MSG_OK {
return Err(format!("Board info is faulty: code {}", aw[0]).into());
}
@@ -659,9 +776,8 @@
self.usart.write(build_request!(Request::IntTest, dummy & 0xFF, dummy >> 8))?;
- let mut aw: [u8; 2] = [0; 2];
- self.usart.read(&mut aw)?;
-
+ let aw = read_sized::<2>(&mut self.usart)?;
+
let result = u16::from_le_bytes(aw);
if result != dummy * 3 {
return Err("Int conversion failed".into());
diff --git a/doc/src/b15f/error.rs.html b/docs/src/b15f/error.rs.html
similarity index 100%
rename from doc/src/b15f/error.rs.html
rename to docs/src/b15f/error.rs.html
diff --git a/doc/src/b15f/lib.rs.html b/docs/src/b15f/lib.rs.html
similarity index 99%
rename from doc/src/b15f/lib.rs.html
rename to docs/src/b15f/lib.rs.html
index 7dc4593..9481665 100644
--- a/doc/src/b15f/lib.rs.html
+++ b/docs/src/b15f/lib.rs.html
@@ -22,7 +22,6 @@
#![deny(missing_docs,
missing_debug_implementations,
trivial_casts,
- unsafe_code,
unstable_features,
unused_import_braces, unused_qualifications)]
@@ -35,6 +34,7 @@
//! [`original`]: https://github.com/devfix/b15f
pub mod b15f;
+mod usart;
mod request;
mod error;
mod assert;
diff --git a/doc/src/b15f/request.rs.html b/docs/src/b15f/request.rs.html
similarity index 96%
rename from doc/src/b15f/request.rs.html
rename to docs/src/b15f/request.rs.html
index cd90f5c..543b3ea 100644
--- a/doc/src/b15f/request.rs.html
+++ b/docs/src/b15f/request.rs.html
@@ -27,6 +27,7 @@
27
28
29
+30
//! This module contains the request data used to communicate
//! with the B15 via USART.
//!
@@ -50,10 +51,11 @@
Test = 1,
Info = 2,
IntTest = 3,
-
+ SelfTest = 4,
DigitalWrite0 = 5,
DigitalWrite1 = 6,
DigitalRead0 = 7,
- DigitalRead1 = 8
+ DigitalRead1 = 8,
+ ReadDipSwitch = 9
}
1 +2 +3 +4 +5 +6 +7 +8 +9 +
use serialport::SerialPort;
+use crate::error::Error;
+
+pub fn read_sized<const N: usize> (usart: &mut Box<dyn SerialPort>) -> Result<[u8; N], Error> {
+ let mut buf: [u8; N] = [0; N];
+
+ usart.read(&mut buf)?;
+ Ok(buf)
+}