From 28b16cc364cc4f4112aca2195df389ea6f007048 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Wed, 7 Nov 2018 20:19:06 +0100 Subject: Convert between modes through transformation of the struct --- src/devices/ads1013.rs | 28 ------------------ src/devices/construction/i2c.rs | 41 ++++++++++++++++++++++++++ src/devices/construction/mod.rs | 3 ++ src/devices/ic.rs | 22 ++++++++++++++ src/devices/mod.rs | 23 ++++----------- src/lib.rs | 64 +++++++++++++++++++++++++++++++++++++++-- 6 files changed, 133 insertions(+), 48 deletions(-) delete mode 100644 src/devices/ads1013.rs create mode 100644 src/devices/construction/i2c.rs create mode 100644 src/devices/construction/mod.rs create mode 100644 src/devices/ic.rs (limited to 'src') diff --git a/src/devices/ads1013.rs b/src/devices/ads1013.rs deleted file mode 100644 index b4628b5..0000000 --- a/src/devices/ads1013.rs +++ /dev/null @@ -1,28 +0,0 @@ -//! Functions exclusive of ADS1013 - -extern crate embedded_hal as hal; -use hal::blocking; -use core::marker::PhantomData; -use { Ads1x1x, DEVICE_BASE_ADDRESS, SlaveAddr, ic }; -use interface::I2cInterface; - -impl Ads1x1x, ic::ADS1013> -where - I2C: blocking::i2c::Write + blocking::i2c::WriteRead -{ - /// Create a new instance of the ADS1013 device. - pub fn new_ads1013(i2c: I2C, address: SlaveAddr) -> Self { - Ads1x1x { - iface: I2cInterface { - i2c, - address: address.addr(DEVICE_BASE_ADDRESS) - }, - _ic: PhantomData - } - } - - /// Destroy driver instance, return I²C bus instance. - pub fn destroy_ads1013(self) -> I2C { - self.iface.i2c - } -} diff --git a/src/devices/construction/i2c.rs b/src/devices/construction/i2c.rs new file mode 100644 index 0000000..900a5fe --- /dev/null +++ b/src/devices/construction/i2c.rs @@ -0,0 +1,41 @@ +//! Functions exclusive of ADS1013 + +extern crate embedded_hal as hal; +use hal::blocking; +use core::marker::PhantomData; +use { Ads1x1x, DEVICE_BASE_ADDRESS, SlaveAddr, ic, Config, mode }; +use interface::I2cInterface; + + +macro_rules! impl_new_destroy { + ( $IC:ident, $create:ident, $destroy:ident ) => { + impl Ads1x1x, ic::$IC, mode::OneShot> + where + I2C: blocking::i2c::Write + blocking::i2c::WriteRead + { + /// Create a new instance of the device in OneShot mode. + pub fn $create(i2c: I2C, address: SlaveAddr) -> Self { + Ads1x1x { + iface: I2cInterface { + i2c, + address: address.addr(DEVICE_BASE_ADDRESS) + }, + config: Config::default(), + a_conversion_was_started: false, + _ic: PhantomData, + _mode: PhantomData + } + } + } + impl Ads1x1x, ic::$IC, MODE> + { + /// Destroy driver instance, return I²C bus instance. + pub fn $destroy(self) -> I2C { + self.iface.i2c + } + } + } +} + +impl_new_destroy!(Ads1013, new_ads1013, destroy_ads1013); +impl_new_destroy!(Ads1113, new_ads1113, destroy_ads1113); \ No newline at end of file diff --git a/src/devices/construction/mod.rs b/src/devices/construction/mod.rs new file mode 100644 index 0000000..0873d88 --- /dev/null +++ b/src/devices/construction/mod.rs @@ -0,0 +1,3 @@ +//! Construction / destruction functions + +mod i2c; diff --git a/src/devices/ic.rs b/src/devices/ic.rs new file mode 100644 index 0000000..9795de6 --- /dev/null +++ b/src/devices/ic.rs @@ -0,0 +1,22 @@ +/// ICs +#[derive(PartialEq)] +pub enum ResolutionBits { + _12, + _16 +} + +pub trait Resolution : super::private::Sealed { + const BITS : ResolutionBits; +} + +/// ADS1013 IC marker +pub struct Ads1013(()); +impl Resolution for Ads1013 { + const BITS: ResolutionBits = ResolutionBits::_12; +} + +/// ADS1113 IC marker +pub struct Ads1113(()); +impl Resolution for Ads1113 { + const BITS: ResolutionBits = ResolutionBits::_16; +} \ No newline at end of file diff --git a/src/devices/mod.rs b/src/devices/mod.rs index 529db04..e35196c 100644 --- a/src/devices/mod.rs +++ b/src/devices/mod.rs @@ -1,19 +1,6 @@ -/// IC markers -pub mod ic { - /// ADS1013 IC marker - pub struct ADS1013; - /// ADS1014 IC marker - pub struct ADS1014; - /// ADS1015 IC marker - pub struct ADS1015; - /// ADS1113 IC marker - pub struct ADS1113; - /// ADS1114 IC marker - pub struct ADS1114; - /// ADS1115 IC marker - pub struct ADS1115; - /// ADS1118 IC marker - pub struct ADS1118; -} +use super::private; -mod ads1013; +#[doc(hidden)] +pub mod ic; +mod ads1x1x; +mod construction; diff --git a/src/lib.rs b/src/lib.rs index 23c36bb..a141840 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,6 +65,15 @@ pub enum Error { const DEVICE_BASE_ADDRESS : u8 = 0b100_1000; +/// Mode marker types +pub mod mode { + /// One-shot operating mode / power-down state (default) + pub struct OneShot(()); + + /// Continuous conversion mode + pub struct Continuous(()); +} + /// Possible slave addresses #[derive(Debug, Clone)] pub enum SlaveAddr { @@ -92,16 +101,67 @@ impl SlaveAddr { } } +struct Register; + +impl Register { + const CONVERSION : u8 = 0x00; + const CONFIG : u8 = 0x01; + //const LOW_TH : u8 = 0x02; + //const HIGH_TH : u8 = 0x03; +} + +struct BitFlags; +impl BitFlags { + const OP_MODE : u16 = 0b0000_0001_0000_0000; + const OS : u16 = 0b1000_0000_0000_0000; +} + + +#[derive(Debug, Clone)] +struct Config { + bits: u16 +} + +impl Config { + fn is_high(&self, mask : u16) -> bool { + (self.bits & mask) != 0 + } + + fn with_high(&self, mask: u16) -> Self { + Config { bits: self.bits | mask } + } + fn with_low(&self, mask: u16) -> Self { + Config { bits: self.bits & !mask } + } +} + +impl Default for Config { + fn default() -> Self { + Config { bits: 0x8583 } + } +} + /// ADS1x1x ADC driver #[derive(Debug, Default)] -pub struct Ads1x1x { +pub struct Ads1x1x { iface: DI, - _ic: PhantomData + config: Config, + a_conversion_was_started: bool, + _ic: PhantomData, + _mode: PhantomData } +#[doc(hidden)] pub mod interface; mod devices; pub use devices::ic; +pub use devices::channel; + +mod private { + pub trait Sealed {} + impl Sealed for super::devices::ic::Ads1013 {} + impl Sealed for super::devices::ic::Ads1113 {} +} #[cfg(test)] mod tests { -- cgit v1.2.3-54-g00ecf