diff options
author | Diego Barrios Romero <eldruin@gmail.com> | 2018-11-07 20:21:18 +0100 |
---|---|---|
committer | Diego Barrios Romero <eldruin@gmail.com> | 2018-11-07 20:21:18 +0100 |
commit | 42cc99c1835ff2838a25218ae57e47649846af9d (patch) | |
tree | 4282fe03896f200c1502634914d0891080974541 | |
parent | 41325641f087f6e01e31d7d5bd741becf0154e77 (diff) | |
download | ads1x1x-async-42cc99c1835ff2838a25218ae57e47649846af9d.tar.gz ads1x1x-async-42cc99c1835ff2838a25218ae57e47649846af9d.tar.xz ads1x1x-async-42cc99c1835ff2838a25218ae57e47649846af9d.zip |
Change interface to handle registers as u16
-rw-r--r-- | src/interface.rs | 75 | ||||
-rw-r--r-- | src/lib.rs | 4 |
2 files changed, 21 insertions, 58 deletions
diff --git a/src/interface.rs b/src/interface.rs index 2bf14f2..74d175f 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -1,10 +1,9 @@ //! I2C/SPI interfaces -#![deny(missing_docs)] - extern crate embedded_hal as hal; use hal::blocking; -use Error; +use { private, Error }; + /// I2C interface #[derive(Debug, Default)] @@ -21,13 +20,11 @@ pub struct SpiInterface<SPI, CS> { } /// Write data -pub trait WriteData { +pub trait WriteData : private::Sealed { /// Error type type Error; /// Write to an u8 register - fn write_register(&mut self, register: u8, data: u8) -> Result<(), Error<Self::Error>>; - /// Write data. The first element corresponds to the starting address. - fn write_data(&mut self, payload: &mut [u8]) -> Result<(), Error<Self::Error>>; + fn write_register(&mut self, register: u8, data: u16) -> Result<(), Error<Self::Error>>; } impl<I2C, E> WriteData for I2cInterface<I2C> @@ -35,14 +32,8 @@ where I2C: blocking::i2c::Write<Error = E> { type Error = E; - fn write_register(&mut self, register: u8, data: u8) -> Result<(), Error<E>> { - let payload: [u8; 2] = [register, data]; - self.i2c - .write(self.address, &payload) - .map_err(Error::Comm) - } - - fn write_data(&mut self, payload: &mut [u8]) -> Result<(), Error<Self::Error>> { + fn write_register(&mut self, register: u8, data: u16) -> Result<(), Error<E>> { + let payload: [u8; 3] = [register, (data >> 8) as u8, data as u8]; self.i2c .write(self.address, &payload) .map_err(Error::Comm) @@ -55,21 +46,10 @@ where CS: hal::digital::OutputPin { type Error = E; - fn write_register(&mut self, register: u8, data: u8) -> Result<(), Error<E>> { + fn write_register(&mut self, register: u8, data: u16) -> Result<(), Error<E>> { self.cs.set_low(); - let payload: [u8; 2] = [register + 0x80, data]; - let result = self.spi - .write(&payload) - .map_err(Error::Comm); - - self.cs.set_high(); - result - } - - fn write_data(&mut self, payload: &mut [u8]) -> Result<(), Error<Self::Error>> { - self.cs.set_low(); - payload[0] += 0x80; + let payload: [u8; 3] = [register + 0x80, (data >> 8) as u8, data as u8]; let result = self.spi .write(&payload) .map_err(Error::Comm); @@ -81,13 +61,11 @@ where /// Read data -pub trait ReadData { +pub trait ReadData : private::Sealed { /// Error type type Error; /// Read an u8 register - fn read_register(&mut self, register: u8) -> Result<u8, Error<Self::Error>>; - /// Read some data. The first element corresponds to the starting address. - fn read_data(&mut self, payload: &mut [u8]) -> Result<(), Error<Self::Error>>; + fn read_register(&mut self, register: u8) -> Result<u16, Error<Self::Error>>; } impl<I2C, E> ReadData for I2cInterface<I2C> @@ -95,19 +73,12 @@ where I2C: blocking::i2c::WriteRead<Error = E> { type Error = E; - fn read_register(&mut self, register: u8) -> Result<u8, Error<E>> { - let mut data = [0]; + fn read_register(&mut self, register: u8) -> Result<u16, Error<E>> { + let mut data = [0, 0]; self.i2c .write_read(self.address, &[register], &mut data) .map_err(Error::Comm) - .and(Ok(data[0])) - } - - fn read_data(&mut self, payload: &mut [u8]) -> Result<(), Error<Self::Error>> { - let len = payload.len(); - self.i2c - .write_read(self.address, &[payload[0]], &mut payload[1..=(len-1)]) - .map_err(Error::Comm) + .and(Ok(((data[0] as u16) << 8) | data[1] as u16)) } } @@ -117,26 +88,14 @@ where CS: hal::digital::OutputPin { type Error = E; - fn read_register(&mut self, register: u8) -> Result<u8, Error<E>> { + fn read_register(&mut self, register: u8) -> Result<u16, Error<E>> { self.cs.set_low(); - let mut data = [register, 0]; + let mut data = [register, 0, 0]; let result = self.spi .transfer(&mut data) .map_err(Error::Comm); self.cs.set_high(); - match result { - Ok(result) => Ok(result[1]), - Err(e) => Err(e) - } - } - - fn read_data(&mut self, mut payload: &mut [u8]) -> Result<(), Error<Self::Error>> { - self.cs.set_low(); - let result = self.spi - .transfer(&mut payload) - .map_err(Error::Comm); - self.cs.set_high(); - result?; - Ok(()) + let result = result?; + Ok(((result[0] as u16) << 8) | result[1] as u16) } } @@ -159,6 +159,10 @@ pub use devices::channel; mod private { pub trait Sealed {} + + impl<I2C> Sealed for super::interface::I2cInterface<I2C> {} + impl<SPI, CS> Sealed for super::interface::SpiInterface<SPI, CS> {} + impl Sealed for super::devices::ic::Ads1013 {} impl Sealed for super::devices::ic::Ads1113 {} } |