diff options
author | Diego Barrios Romero <eldruin@gmail.com> | 2018-11-11 07:51:02 +0100 |
---|---|---|
committer | Diego Barrios Romero <eldruin@gmail.com> | 2018-11-11 07:51:02 +0100 |
commit | 8b9e9ecdb85a30ac1c57fe6b96498cae6ad9f7cc (patch) | |
tree | 1c1464f861566df450b607f5f8024e41caea26c0 /src/devices | |
parent | f28b8e1379f5a2c2f0283a280c58fef61629f1f6 (diff) | |
download | ads1x1x-async-8b9e9ecdb85a30ac1c57fe6b96498cae6ad9f7cc.tar.gz ads1x1x-async-8b9e9ecdb85a30ac1c57fe6b96498cae6ad9f7cc.tar.xz ads1x1x-async-8b9e9ecdb85a30ac1c57fe6b96498cae6ad9f7cc.zip |
Add support for channel selection
Diffstat (limited to 'src/devices')
-rw-r--r-- | src/devices/ads1x1x/mode/oneshot.rs | 13 | ||||
-rw-r--r-- | src/devices/channels.rs | 18 |
2 files changed, 25 insertions, 6 deletions
diff --git a/src/devices/ads1x1x/mode/oneshot.rs b/src/devices/ads1x1x/mode/oneshot.rs index 0da4a7b..7ff9ac9 100644 --- a/src/devices/ads1x1x/mode/oneshot.rs +++ b/src/devices/ads1x1x/mode/oneshot.rs @@ -3,7 +3,8 @@ use core::marker::PhantomData; use { Ads1x1x, mode, Error, Register, BitFlags, Config, ic }; use { interface, hal, nb }; -use super::super::OperatingMode; +use devices::ads1x1x::OperatingMode; +use devices::channels::ChannelSelection; use super::convert_measurement; impl<DI, IC, E> Ads1x1x<DI, IC, mode::OneShot> @@ -30,8 +31,8 @@ where Ok(!config.is_high(BitFlags::OS)) } - fn trigger_measurement(&mut self) -> Result<(), Error<E>> { - let config = self.config.with_high(BitFlags::OS); + fn trigger_measurement(&mut self, config: &Config) -> Result<(), Error<E>> { + let config = config.with_high(BitFlags::OS); self.iface.write_register(Register::CONFIG, config.bits) } } @@ -40,7 +41,7 @@ impl<DI, IC, E, CH> hal::adc::OneShot<Ads1x1x<DI, IC, mode::OneShot>, i16, CH> f where DI: interface::ReadData<Error = E> + interface::WriteData<Error = E>, IC: ic::Resolution, - CH: hal::adc::Channel<Ads1x1x<DI, IC, mode::OneShot>> + CH: hal::adc::Channel<Ads1x1x<DI, IC, mode::OneShot>, ID = ChannelSelection> { type Error = Error<E>; @@ -56,7 +57,9 @@ where self.a_conversion_was_started = false; return Ok(convert_measurement::<IC>(value)); } - self.trigger_measurement().map_err(nb::Error::Other)?; + let config = self.config.with_mux_bits(CH::channel()); + self.trigger_measurement(&config).map_err(nb::Error::Other)?; + self.config = config; self.a_conversion_was_started = true; Err(nb::Error::WouldBlock) } diff --git a/src/devices/channels.rs b/src/devices/channels.rs index 2482df5..c202ad3 100644 --- a/src/devices/channels.rs +++ b/src/devices/channels.rs @@ -1,5 +1,5 @@ //! ADC input channels -use { Ads1x1x, ic, hal }; +use { Ads1x1x, ic, hal, Config, BitFlags as BF }; /// ADC input channel selection #[allow(dead_code)] @@ -68,3 +68,19 @@ impl_channel!(Ads1115, SingleA0); impl_channel!(Ads1115, SingleA1); impl_channel!(Ads1115, SingleA2); impl_channel!(Ads1115, SingleA3); + +impl Config { + pub(crate) fn with_mux_bits(&self, ch: ChannelSelection) -> Self { + use self::ChannelSelection as CS; + match ch { + CS::DifferentialA0A1 => self.with_low( BF::MUX2).with_low( BF::MUX1).with_low( BF::MUX0), + CS::DifferentialA0A3 => self.with_low( BF::MUX2).with_low( BF::MUX1).with_high(BF::MUX0), + CS::DifferentialA1A3 => self.with_low( BF::MUX2).with_high(BF::MUX1).with_low( BF::MUX0), + CS::DifferentialA2A3 => self.with_low( BF::MUX2).with_high(BF::MUX1).with_high(BF::MUX0), + CS::SingleA0 => self.with_high(BF::MUX2).with_low( BF::MUX1).with_low( BF::MUX0), + CS::SingleA1 => self.with_high(BF::MUX2).with_low( BF::MUX1).with_high(BF::MUX0), + CS::SingleA2 => self.with_high(BF::MUX2).with_high(BF::MUX1).with_low( BF::MUX0), + CS::SingleA3 => self.with_high(BF::MUX2).with_high(BF::MUX1).with_high(BF::MUX0), + } + } +} |