diff --git a/examples/read.rs b/examples/read.rs index 87e2434..77bbc58 100644 --- a/examples/read.rs +++ b/examples/read.rs @@ -3,6 +3,9 @@ use b15f::b15f::B15F; fn main() -> Result<(), String> { let mut drv = B15F::new()?; - println!("{}", drv.digital_read::<0>().unwrap()); + println!("BA-0: {:b}", drv.digital_read::<0>()?); + println!("BA-1: {:b}", drv.digital_read::<1>()?); + println!("DIP : {:b}", drv.read_dip_switch()?); + Ok(()) } \ No newline at end of file diff --git a/src/b15f.rs b/src/b15f.rs index 62aa1a0..2a95e6e 100644 --- a/src/b15f.rs +++ b/src/b15f.rs @@ -233,6 +233,35 @@ impl B15F { 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 { + 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 diff --git a/src/request.rs b/src/request.rs index a8748d6..50610a4 100644 --- a/src/request.rs +++ b/src/request.rs @@ -25,5 +25,6 @@ pub enum Request { DigitalWrite0 = 5, DigitalWrite1 = 6, DigitalRead0 = 7, - DigitalRead1 = 8 + DigitalRead1 = 8, + ReadDipSwitch = 9 } \ No newline at end of file