diff --git a/doc/b15f/b15f/index.html b/doc/b15f/b15f/index.html index 1f04cbc..00afab9 100644 --- a/doc/b15f/b15f/index.html +++ b/doc/b15f/b15f/index.html @@ -1,4 +1,4 @@ -b15f::b15f - Rust

Module b15f::b15f

source · []
Expand description

This module contains all the structures and functions related to +b15f::b15f - Rust

Module b15f::b15f

source · []
Expand description

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.

Structs

Structure representing the driver for the board 15
\ No newline at end of file diff --git a/doc/b15f/b15f/struct.B15F.html b/doc/b15f/b15f/struct.B15F.html index 633881f..9d095ad 100644 --- a/doc/b15f/b15f/struct.B15F.html +++ b/doc/b15f/b15f/struct.B15F.html @@ -1,17 +1,34 @@ -B15F in b15f::b15f - Rust
pub struct B15F { /* private fields */ }
Expand description

Structure representing the driver for the board 15

-

Implementations

Creates a new instance of the B15

+B15F in b15f::b15f - Rust
pub struct B15F { /* private fields */ }
Expand description

Structure representing the driver for the board 15

+

Implementations

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 behaviour.

+
Returns
+

A new B15F object is returned. It contains an already active USART connection, +so calling this function multiple times will create an Error

+
Errors
+

An error::Error is generated if the connection to the board cannot be +established, or if testing of that connection fails.

Examples
use b15f::B15F;
  
 let drv = B15F::new().unwrap();
-

Yields information about the installed firmware on the B15

+

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.

+
Examples

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

-
Examples
+
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
+

An error::Error is generated if the communication with the board fails.

+
Examples
use b15f::B15F;
  
 let mut drv = B15F::new().unwrap();
@@ -21,12 +38,19 @@ of information stored on the B15

.unwrap() .iter() .for_each(|info| println!("{info}"));
-

Clears data in the USART buffers on this device and on the B15

-

Tests the connetion to the B15

+

Clears data in the USART buffers on this device and on the B15

+

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.

+

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.

-
Examples
+
Errors
+

An error::Error is returned if the test fails, or if the +communication itself fails.

+
Examples
use b15f::B15F;
  
 fn main() {
@@ -36,7 +60,7 @@ board returns that value the connection is working correctly.

panic!("Connection is not working: {err}"); } }
-

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

+

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

diff --git a/doc/search-index.js b/doc/search-index.js index 4326c42..0903a4e 100644 --- a/doc/search-index.js +++ b/doc/search-index.js @@ -1,5 +1,5 @@ var searchIndex = JSON.parse('{\ -"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],"n":["B15F","b15f","build_request","B15F","borrow","borrow_mut","discard","fmt","from","get_board_info","into","new","test_connection","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","","","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","","","",""],"i":[0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1],"f":[0,0,0,0,[[]],[[]],[1,[[2,[0]]]],[[1,3],4],[[]],[1,[[2,[[6,[5]],0]]]],[[]],[[],[[2,[1,0]]]],[1,[[2,[0]]]],[[],2],[[],2],[[],7],[[]]],"p":[[3,"B15F"],[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],"n":["B15F","b15f","build_request","B15F","borrow","borrow_mut","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","","","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],"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"]]}\ }'); if (typeof window !== 'undefined' && window.initSearch) {window.initSearch(searchIndex)}; if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex}; diff --git a/doc/src/b15f/b15f.rs.html b/doc/src/b15f/b15f.rs.html index fa9c047..004a9f0 100644 --- a/doc/src/b15f/b15f.rs.html +++ b/doc/src/b15f/b15f.rs.html @@ -241,6 +241,79 @@ 241 242 243 +244 +245 +246 +247 +248 +249 +250 +251 +252 +253 +254 +255 +256 +257 +258 +259 +260 +261 +262 +263 +264 +265 +266 +267 +268 +269 +270 +271 +272 +273 +274 +275 +276 +277 +278 +279 +280 +281 +282 +283 +284 +285 +286 +287 +288 +289 +290 +291 +292 +293 +294 +295 +296 +297 +298 +299 +300 +301 +302 +303 +304 +305 +306 +307 +308 +309 +310 +311 +312 +313 +314 +315 +316
//! 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.
@@ -282,6 +355,14 @@
 	/// program; calling `B15F::new()` more than once might lead to unexpected
 	/// behaviour.
 	/// 
+	/// # Returns
+	/// A new B15F object is returned. It contains an already active USART connection,
+	/// so calling this function multiple times will create an Error
+	/// 
+	/// # Errors
+	/// An `error::Error` is generated if the connection to the board cannot be
+	/// established, or if testing of that connection fails.
+	/// 
 	/// # Examples
 	/// ```
 	/// use b15f::B15F;
@@ -305,6 +386,11 @@
 				Err(_) => {} // Do nothing
 			};
 
+			match drv.test_int_conv() {
+				Ok(()) => break,
+				Err(_) => {}
+			}
+
 			tries -= 1;
 		}
 
@@ -346,11 +432,46 @@
 		Ok(port)
 	}
 
+	/// 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.
+	/// 
+	/// # Examples
+	/// 
+	pub fn digital_write<const port: u8> (&mut self, value: u8) -> Result<(), Error> {
+		assert!(port == 0 || port == 1);
+
+		let reversed = value.reverse_bits();
+		let request = if port == 0 { Request::DigitalWrite0 } else { Request::DigitalWrite1 };
+
+		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());
+		}
+
+		Ok(())
+	}
+
 	/// 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
+	/// An `error::Error` is generated if the communication with the board fails.
+	/// 
 	/// # Examples
 	/// ```
 	/// use b15f::B15F;
@@ -412,13 +533,38 @@
 		Ok(())
 	}
 
+	/// 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.
+	pub fn test_int_conv(&mut self) -> Result<(), Error> {
+		let dummy: u16 = rand::thread_rng().gen_range(0x0000..=(0xFFFF / 3));
+		
+		self.usart.write(build_request!(Request::IntTest, dummy & 0xFF, dummy >> 8))?;
+
+		let mut aw: [u8; 2] = [0; 2];
+		self.usart.read(&mut aw)?;
+
+		let result = u16::from_le_bytes(aw);
+		if result != dummy * 3 {
+			return Err("Int conversion failed".into());
+		}
+
+		Ok(())
+	}
+	
 	/// 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.
 	/// 
-	/// ## Examples
+	/// # Errors
+	/// An `error::Error` is returned if the test fails, or if the 
+	/// communication itself fails.
+	/// 
+	/// # Examples
 	/// ```
 	/// use b15f::B15F;
 	/// 
diff --git a/doc/src/b15f/request.rs.html b/doc/src/b15f/request.rs.html
index 07114ae..49544b3 100644
--- a/doc/src/b15f/request.rs.html
+++ b/doc/src/b15f/request.rs.html
@@ -21,6 +21,10 @@
 21
 22
 23
+24
+25
+26
+27
 
//! This module contains the request data used to communicate
 //! with the B15 via USART. 
 //! 
@@ -40,8 +44,12 @@
 
 #[repr(u8)]
 pub enum Request {
-	Discard = 0,
-	Test 	= 1,
-	Info 	= 2
-}
+ Discard = 0, + Test = 1, + Info = 2, + IntTest = 3, + + DigitalWrite0 = 5, + DigitalWrite1 = 6, +}
\ No newline at end of file diff --git a/examples/nightrider.rs b/examples/nightrider.rs new file mode 100644 index 0000000..ece6934 --- /dev/null +++ b/examples/nightrider.rs @@ -0,0 +1,21 @@ +use std::{thread::sleep, time::Duration}; + +use b15f::b15f::B15F; + +fn main() { + let mut drv = B15F::new().unwrap(); + + let mut position = 0; + let mut direction = 1; + + loop { + drv.digital_write::<0>(1 << position); + + position += direction; + if position >= 7 || position <= 0 { + direction *= -1; + } + + sleep(Duration::from_millis(50)); + } +} \ No newline at end of file diff --git a/src/b15f.rs b/src/b15f.rs index a2d0333..807aa20 100644 --- a/src/b15f.rs +++ b/src/b15f.rs @@ -39,6 +39,14 @@ impl B15F { /// program; calling `B15F::new()` more than once might lead to unexpected /// behaviour. /// + /// # Returns + /// A new B15F object is returned. It contains an already active USART connection, + /// so calling this function multiple times will create an Error + /// + /// # Errors + /// An `error::Error` is generated if the connection to the board cannot be + /// established, or if testing of that connection fails. + /// /// # Examples /// ``` /// use b15f::B15F; @@ -62,6 +70,11 @@ impl B15F { Err(_) => {} // Do nothing }; + match drv.test_int_conv() { + Ok(()) => break, + Err(_) => {} + } + tries -= 1; } @@ -103,11 +116,46 @@ impl B15F { Ok(port) } + /// 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. + /// + /// # Examples + /// + pub fn digital_write (&mut self, value: u8) -> Result<(), Error> { + assert!(port == 0 || port == 1); + + let reversed = value.reverse_bits(); + let request = if port == 0 { Request::DigitalWrite0 } else { Request::DigitalWrite1 }; + + 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()); + } + + Ok(()) + } + /// 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 + /// An `error::Error` is generated if the communication with the board fails. + /// /// # Examples /// ``` /// use b15f::B15F; @@ -169,13 +217,38 @@ impl B15F { Ok(()) } + /// 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. + pub fn test_int_conv(&mut self) -> Result<(), Error> { + let dummy: u16 = rand::thread_rng().gen_range(0x0000..=(0xFFFF / 3)); + + self.usart.write(build_request!(Request::IntTest, dummy & 0xFF, dummy >> 8))?; + + let mut aw: [u8; 2] = [0; 2]; + self.usart.read(&mut aw)?; + + let result = u16::from_le_bytes(aw); + if result != dummy * 3 { + return Err("Int conversion failed".into()); + } + + Ok(()) + } + /// 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. /// - /// ## Examples + /// # Errors + /// An `error::Error` is returned if the test fails, or if the + /// communication itself fails. + /// + /// # Examples /// ``` /// use b15f::B15F; /// diff --git a/src/request.rs b/src/request.rs index 25e624f..d268c91 100644 --- a/src/request.rs +++ b/src/request.rs @@ -17,7 +17,11 @@ macro_rules! build_request { #[repr(u8)] pub enum Request { - Discard = 0, - Test = 1, - Info = 2 + Discard = 0, + Test = 1, + Info = 2, + IntTest = 3, + + DigitalWrite0 = 5, + DigitalWrite1 = 6, } \ No newline at end of file