diff options
author | Markus Reiter <me@reitermark.us> | 2024-02-13 09:38:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-13 09:38:01 +0100 |
commit | 0ff4c7b448fd388015d4903b774c63991356b954 (patch) | |
tree | efadd00a0534ed0c86e456c9b65d67e6c63c5fae | |
parent | 3c2805fdb499202d4f52d0775de8837cded97131 (diff) | |
download | ads1x1x-async-0ff4c7b448fd388015d4903b774c63991356b954.tar.gz ads1x1x-async-0ff4c7b448fd388015d4903b774c63991356b954.tar.xz ads1x1x-async-0ff4c7b448fd388015d4903b774c63991356b954.zip |
Simplify `SlaveAddr`. (#21)
* Simplify `SlaveAddr`.
* Update changelog.
* Update CHANGELOG.md
---------
Co-authored-by: Diego Barrios Romero <eldruin@gmail.com>
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | examples/all_channels.rs | 3 | ||||
-rw-r--r-- | examples/linux.rs | 3 | ||||
-rw-r--r-- | examples/trait.rs | 3 | ||||
-rw-r--r-- | examples/typed.rs | 3 | ||||
-rw-r--r-- | src/construction.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 34 | ||||
-rw-r--r-- | src/types.rs | 85 |
9 files changed, 42 insertions, 99 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a0587c..cb23a44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Add default for `ComparatorQueue`. ### Changed -- Updated `embedded-hal` to version `1`, `read` in one-shot mode is therefore only an inherent method. +- [breaking-change] Updated `embedded-hal` to version `1`, `read` in one-shot mode is therefore only an inherent method. +- [breaking-change] Simplified `SlaveAddr` enum. - Raised MSRV to 1.62.0. ### Removed @@ -85,8 +85,7 @@ use ads1x1x::{channel, Ads1x1x, SlaveAddr}; fn main() { let dev = I2cdev::new("/dev/i2c-1").unwrap(); - let address = SlaveAddr::default(); - let mut adc = Ads1x1x::new_ads1013(dev, address); + let mut adc = Ads1x1x::new_ads1013(dev, SlaveAddr::default()); let value = block!(adc.read(channel::DifferentialA0A1)).unwrap(); println!("Measurement: {}", value); // get I2C device back diff --git a/examples/all_channels.rs b/examples/all_channels.rs index ddf8326..39687ca 100644 --- a/examples/all_channels.rs +++ b/examples/all_channels.rs @@ -5,8 +5,7 @@ use ads1x1x::{channel, Ads1x1x, SlaveAddr}; fn main() { let dev = I2cdev::new("/dev/i2c-1").unwrap(); - let address = SlaveAddr::default(); - let mut adc = Ads1x1x::new_ads1015(dev, address); + let mut adc = Ads1x1x::new_ads1015(dev, SlaveAddr::default()); let values = [ block!(adc.read(channel::SingleA0)).unwrap(), block!(adc.read(channel::SingleA1)).unwrap(), diff --git a/examples/linux.rs b/examples/linux.rs index 55eab64..c3c9d54 100644 --- a/examples/linux.rs +++ b/examples/linux.rs @@ -5,8 +5,7 @@ use ads1x1x::{channel, Ads1x1x, SlaveAddr}; fn main() { let dev = I2cdev::new("/dev/i2c-1").unwrap(); - let address = SlaveAddr::default(); - let mut adc = Ads1x1x::new_ads1013(dev, address); + let mut adc = Ads1x1x::new_ads1013(dev, SlaveAddr::default()); let value = block!(adc.read(channel::DifferentialA0A1)).unwrap(); println!("Measurement: {}", value); // get I2C device back diff --git a/examples/trait.rs b/examples/trait.rs index 070d89c..3eff5b5 100644 --- a/examples/trait.rs +++ b/examples/trait.rs @@ -14,8 +14,7 @@ pub fn read<E, A: DynamicOneShot<Error = E>>(adc: &mut A) -> i16 { fn main() { let dev = I2cdev::new("/dev/i2c-1").unwrap(); - let address = SlaveAddr::default(); - let mut adc = Ads1x1x::new_ads1115(dev, address); + let mut adc = Ads1x1x::new_ads1115(dev, SlaveAddr::default()); let value = read(&mut adc); println!("Measurement: {}", value); diff --git a/examples/typed.rs b/examples/typed.rs index 572754f..797b69e 100644 --- a/examples/typed.rs +++ b/examples/typed.rs @@ -21,8 +21,7 @@ pub fn read(adc: &mut Adc) -> i16 { fn main() { let dev = I2cdev::new("/dev/i2c-1").unwrap(); - let address = SlaveAddr::default(); - let mut adc = Ads1x1x::new_ads1115(dev, address); + let mut adc = Ads1x1x::new_ads1115(dev, SlaveAddr::default()); let value = read(&mut adc); println!("Measurement: {}", value); diff --git a/src/construction.rs b/src/construction.rs index 7ac128f..13a2350 100644 --- a/src/construction.rs +++ b/src/construction.rs @@ -1,6 +1,6 @@ //! Constructor/destructor functions. -use crate::{ic, mode, Ads1x1x, Config, FullScaleRange, SlaveAddr, DEVICE_BASE_ADDRESS}; +use crate::{ic, mode, Ads1x1x, Config, FullScaleRange, SlaveAddr}; use core::marker::PhantomData; macro_rules! impl_new_destroy { @@ -13,7 +13,7 @@ macro_rules! impl_new_destroy { pub fn $create(i2c: I2C, address: SlaveAddr) -> Self { Ads1x1x { i2c, - address: address.addr(DEVICE_BASE_ADDRESS), + address: address.bits(), config: Config::default(), fsr: FullScaleRange::default(), a_conversion_was_started: false, @@ -84,46 +84,28 @@ //! //! [driver-examples]: https://github.com/eldruin/driver-examples //! -//! ### Create a driver instance for the ADS1013 +//! ### Create a driver instance for an ADS1013 with the default address. //! //! ```no_run //! use linux_embedded_hal::I2cdev; //! use ads1x1x::{Ads1x1x, SlaveAddr}; //! //! let dev = I2cdev::new("/dev/i2c-1").unwrap(); -//! let address = SlaveAddr::default(); -//! let adc = Ads1x1x::new_ads1013(dev, address); +//! let adc = Ads1x1x::new_ads1013(dev, SlaveAddr::default()); //! // do something... //! //! // get the I2C device back //! let dev = adc.destroy_ads1013(); //! ``` //! -//! ### Create a driver instance for the ADS1013 with an alternative address (method 1) +//! ### Create a driver instance for an ADS1013 with the ADDR pin connected to SDA. //! //! ```no_run //! use linux_embedded_hal::I2cdev; //! use ads1x1x::{Ads1x1x, SlaveAddr}; //! //! let dev = I2cdev::new("/dev/i2c-1").unwrap(); -//! let (bit1, bit0) = (true, false); // last two bits of address -//! let address = SlaveAddr::Alternative(bit1, bit0); -//! let adc = Ads1x1x::new_ads1013(dev, address); -//! ``` - -//! ### Create a driver instance for the ADS1013 with an alternative address (method 2) -//! -//! Using helper `SlaveAddr` creation method depending on the connection of -//! the `ADDR` pin. -//! -//! ```no_run -//! use linux_embedded_hal::I2cdev; -//! use ads1x1x::{Ads1x1x, SlaveAddr}; -//! -//! let dev = I2cdev::new("/dev/i2c-1").unwrap(); -//! // `ADDR` pin connected to SDA results in the 0x4A effective address -//! let address = SlaveAddr::new_sda(); -//! let adc = Ads1x1x::new_ads1013(dev, address); +//! let adc = Ads1x1x::new_ads1013(dev, SlaveAddr::Sda); //! ``` //! //! ### Make a one-shot measurement @@ -149,8 +131,7 @@ //! use ads1x1x::{Ads1x1x, ModeChangeError, SlaveAddr}; //! //! let dev = I2cdev::new("/dev/i2c-1").unwrap(); -//! let address = SlaveAddr::default(); -//! let adc = Ads1x1x::new_ads1013(dev, address); +//! let adc = Ads1x1x::new_ads1013(dev, SlaveAddr::default()); //! match adc.into_continuous() { //! Err(ModeChangeError::I2C(e, adc)) => /* mode change failed handling */ panic!(), //! Ok(mut adc) => { @@ -170,8 +151,7 @@ //! use ads1x1x::{Ads1x1x, DataRate16Bit, SlaveAddr}; //! //! let dev = I2cdev::new("/dev/i2c-1").unwrap(); -//! let address = SlaveAddr::default(); -//! let mut adc = Ads1x1x::new_ads1115(dev, address); +//! let mut adc = Ads1x1x::new_ads1115(dev, SlaveAddr::default()); //! adc.set_data_rate(DataRate16Bit::Sps860).unwrap(); //! ``` //! @@ -204,8 +184,6 @@ #![deny(missing_docs)] #![no_std] -const DEVICE_BASE_ADDRESS: u8 = 0b100_1000; - struct Register; impl Register { const CONVERSION: u8 = 0x00; diff --git a/src/types.rs b/src/types.rs index 1ec4601..31b4cdb 100644 --- a/src/types.rs +++ b/src/types.rs @@ -162,56 +162,35 @@ pub enum FullScaleRange { Within0_256V, } -/// Possible slave addresses +/// A slave address. +/// +/// See [Table 4 in the datasheet](https://www.ti.com/lit/ds/symlink/ads1115.pdf#%5B%7B%22num%22%3A716%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C0%2C602.2%2C0%5D). #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)] pub enum SlaveAddr { + /// Address when the ADDR pin is connected to GND. (default) #[default] - /// Default slave address - Default, - /// Alternative slave address using the provided values - /// for the last two bits (A1, A0) - Alternative(bool, bool), + Gnd, + /// Address when the ADDR pin is connected to VDD. + Vdd, + /// Address when the ADDR pin is connected to SDA. + /// + /// If SDA is used as the device address, hold the SDA line low for at + /// least 100 ns after the SCL line goes low to make sure the device + /// decodes the address correctly during I²C communication. + Sda, + /// Address when the ADDR pin is connected to SCL. + Scl, } impl SlaveAddr { - pub(crate) fn addr(self, default: u8) -> u8 { + pub(crate) const fn bits(self) -> u8 { match self { - SlaveAddr::Default => default, - SlaveAddr::Alternative(a1, a0) => default | ((a1 as u8) << 1) | a0 as u8, + Self::Gnd => 0b1001000, + Self::Vdd => 0b1001001, + Self::Sda => 0b1001010, + Self::Scl => 0b1001011, } } - - /// Create `SlaveAddr` instance corresponding to the address - /// effective when connecting the pin `ADDR` to GND (0x48). - /// - /// See [Table 4 in the datasheet](https://www.ti.com/lit/ds/symlink/ads1115.pdf#%5B%7B%22num%22%3A716%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C0%2C602.2%2C0%5D). - pub fn new_gnd() -> Self { - SlaveAddr::default() - } - - /// Create `SlaveAddr` instance corresponding to the address - /// effective when connecting the pin `ADDR` to VDD (0x49). - /// - /// See [Table 4 in the datasheet](https://www.ti.com/lit/ds/symlink/ads1115.pdf#%5B%7B%22num%22%3A716%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C0%2C602.2%2C0%5D). - pub fn new_vdd() -> Self { - SlaveAddr::Alternative(false, true) - } - - /// Create `SlaveAddr` instance corresponding to the address - /// effective when connecting the pin `ADDR` to SDA (0x4A). - /// - /// See [Table 4 in the datasheet](https://www.ti.com/lit/ds/symlink/ads1115.pdf#%5B%7B%22num%22%3A716%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C0%2C602.2%2C0%5D). - pub fn new_sda() -> Self { - SlaveAddr::Alternative(true, false) - } - - /// Create `SlaveAddr` instance corresponding to the address - /// effective when connecting the pin `ADDR` to SCL (0x4B). - /// - /// See [Table 4 in the datasheet](https://www.ti.com/lit/ds/symlink/ads1115.pdf#%5B%7B%22num%22%3A716%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C0%2C602.2%2C0%5D). - pub fn new_scl() -> Self { - SlaveAddr::Alternative(true, true) - } } #[derive(Debug, Clone, PartialEq)] @@ -267,29 +246,19 @@ pub trait DynamicOneShot: private::Sealed { #[cfg(test)] mod tests { - use crate::DEVICE_BASE_ADDRESS as ADDR; use crate::{FullScaleRange, SlaveAddr}; #[test] - fn can_get_default_address() { - let addr = SlaveAddr::default(); - assert_eq!(ADDR, addr.addr(ADDR)); - } - - #[test] - fn can_generate_alternative_addresses() { - assert_eq!(0b100_1000, SlaveAddr::Alternative(false, false).addr(ADDR)); - assert_eq!(0b100_1001, SlaveAddr::Alternative(false, true).addr(ADDR)); - assert_eq!(0b100_1010, SlaveAddr::Alternative(true, false).addr(ADDR)); - assert_eq!(0b100_1011, SlaveAddr::Alternative(true, true).addr(ADDR)); + fn slave_addr_default() { + assert_eq!(0b100_1000, SlaveAddr::default().bits()); } #[test] - fn can_generate_alternative_addresses_using_helper_constructors() { - assert_eq!(0b100_1000, SlaveAddr::new_gnd().addr(ADDR)); - assert_eq!(0b100_1001, SlaveAddr::new_vdd().addr(ADDR)); - assert_eq!(0b100_1010, SlaveAddr::new_sda().addr(ADDR)); - assert_eq!(0b100_1011, SlaveAddr::new_scl().addr(ADDR)); + fn slave_addr_bits() { + assert_eq!(0b100_1000, SlaveAddr::Gnd.bits()); + assert_eq!(0b100_1001, SlaveAddr::Vdd.bits()); + assert_eq!(0b100_1010, SlaveAddr::Sda.bits()); + assert_eq!(0b100_1011, SlaveAddr::Scl.bits()); } #[test] |