summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDiego Barrios Romero <eldruin@gmail.com>2019-03-30 13:21:57 +0200
committerDiego Barrios Romero <eldruin@gmail.com>2019-03-31 13:41:26 +0200
commitc8301c630051d0960e9738a6ab768b8836065e25 (patch)
treed0c1adcebd9d7b64d17fc24fb579aede47d7a477
parent281e2eb97c694f4978e6d28eb51c5b8642224960 (diff)
downloadads1x1x-async-c8301c630051d0960e9738a6ab768b8836065e25.tar.gz
ads1x1x-async-c8301c630051d0960e9738a6ab768b8836065e25.tar.xz
ads1x1x-async-c8301c630051d0960e9738a6ab768b8836065e25.zip
Code simplifications
-rw-r--r--src/devices/features/tier1.rs44
-rw-r--r--src/devices/features/tier2.rs72
2 files changed, 64 insertions, 52 deletions
diff --git a/src/devices/features/tier1.rs b/src/devices/features/tier1.rs
index 6bd1a99..9e5cb77 100644
--- a/src/devices/features/tier1.rs
+++ b/src/devices/features/tier1.rs
@@ -9,16 +9,16 @@ where
/// Set data rate
pub fn set_data_rate(&mut self, rate: DataRate12Bit) -> Result<(), Error<E>> {
use DataRate12Bit as DR;
- let config;
- match rate {
- DR::Sps128 => config = self.config.with_low( BF::DR2).with_low( BF::DR1).with_low( BF::DR0),
- DR::Sps250 => config = self.config.with_low( BF::DR2).with_low( BF::DR1).with_high(BF::DR0),
- DR::Sps490 => config = self.config.with_low( BF::DR2).with_high(BF::DR1).with_low( BF::DR0),
- DR::Sps920 => config = self.config.with_low( BF::DR2).with_high(BF::DR1).with_high(BF::DR0),
- DR::Sps1600 => config = self.config.with_high(BF::DR2).with_low( BF::DR1).with_low( BF::DR0),
- DR::Sps2400 => config = self.config.with_high(BF::DR2).with_low( BF::DR1).with_high(BF::DR0),
- DR::Sps3300 => config = self.config.with_high(BF::DR2).with_high(BF::DR1).with_low( BF::DR0),
- }
+ let cfg = self.config.clone();
+ let config = match rate {
+ DR::Sps128 => cfg.with_low(BF::DR2).with_low(BF::DR1).with_low(BF::DR0),
+ DR::Sps250 => cfg.with_low(BF::DR2).with_low(BF::DR1).with_high(BF::DR0),
+ DR::Sps490 => cfg.with_low(BF::DR2).with_high(BF::DR1).with_low(BF::DR0),
+ DR::Sps920 => cfg.with_low(BF::DR2).with_high(BF::DR1).with_high(BF::DR0),
+ DR::Sps1600 => cfg.with_high(BF::DR2).with_low(BF::DR1).with_low(BF::DR0),
+ DR::Sps2400 => cfg.with_high(BF::DR2).with_low(BF::DR1).with_high(BF::DR0),
+ DR::Sps3300 => cfg.with_high(BF::DR2).with_high(BF::DR1).with_low(BF::DR0),
+ };
self.iface.write_register(Register::CONFIG, config.bits)?;
self.config = config;
Ok(())
@@ -27,22 +27,22 @@ where
impl<DI, IC, MODE, E> Ads1x1x<DI, IC, ic::Resolution16Bit, MODE>
where
- DI: interface::WriteData<Error = E>
+ DI: interface::WriteData<Error = E>,
{
/// Set data rate
pub fn set_data_rate(&mut self, rate: DataRate16Bit) -> Result<(), Error<E>> {
use DataRate16Bit as DR;
- let config;
- match rate {
- DR::Sps8 => config = self.config.with_low( BF::DR2).with_low( BF::DR1).with_low( BF::DR0),
- DR::Sps16 => config = self.config.with_low( BF::DR2).with_low( BF::DR1).with_high(BF::DR0),
- DR::Sps32 => config = self.config.with_low( BF::DR2).with_high(BF::DR1).with_low( BF::DR0),
- DR::Sps64 => config = self.config.with_low( BF::DR2).with_high(BF::DR1).with_high(BF::DR0),
- DR::Sps128 => config = self.config.with_high(BF::DR2).with_low( BF::DR1).with_low( BF::DR0),
- DR::Sps250 => config = self.config.with_high(BF::DR2).with_low( BF::DR1).with_high(BF::DR0),
- DR::Sps475 => config = self.config.with_high(BF::DR2).with_high(BF::DR1).with_low( BF::DR0),
- DR::Sps860 => config = self.config.with_high(BF::DR2).with_high(BF::DR1).with_high(BF::DR0),
- }
+ let cfg = self.config.clone();
+ let config = match rate {
+ DR::Sps8 => cfg.with_low(BF::DR2).with_low(BF::DR1).with_low(BF::DR0),
+ DR::Sps16 => cfg.with_low(BF::DR2).with_low(BF::DR1).with_high(BF::DR0),
+ DR::Sps32 => cfg.with_low(BF::DR2).with_high(BF::DR1).with_low(BF::DR0),
+ DR::Sps64 => cfg.with_low(BF::DR2).with_high(BF::DR1).with_high(BF::DR0),
+ DR::Sps128 => cfg.with_high(BF::DR2).with_low(BF::DR1).with_low(BF::DR0),
+ DR::Sps250 => cfg.with_high(BF::DR2).with_low(BF::DR1).with_high(BF::DR0),
+ DR::Sps475 => cfg.with_high(BF::DR2).with_high(BF::DR1).with_low(BF::DR0),
+ DR::Sps860 => cfg.with_high(BF::DR2).with_high(BF::DR1).with_high(BF::DR0),
+ };
self.iface.write_register(Register::CONFIG, config.bits)?;
self.config = config;
Ok(())
diff --git a/src/devices/features/tier2.rs b/src/devices/features/tier2.rs
index 3f8d7b4..48e4eb9 100644
--- a/src/devices/features/tier2.rs
+++ b/src/devices/features/tier2.rs
@@ -17,15 +17,31 @@ where
///
/// This configures the programmable gain amplifier and determines the measurable input voltage range.
pub fn set_full_scale_range(&mut self, range: FullScaleRange) -> Result<(), Error<E>> {
- let config;
- match range {
- FullScaleRange::Within6_144V => config = self.config.with_low( BF::PGA2).with_low( BF::PGA1).with_low( BF::PGA0),
- FullScaleRange::Within4_096V => config = self.config.with_low( BF::PGA2).with_low( BF::PGA1).with_high(BF::PGA0),
- FullScaleRange::Within2_048V => config = self.config.with_low( BF::PGA2).with_high(BF::PGA1).with_low( BF::PGA0),
- FullScaleRange::Within1_024V => config = self.config.with_low( BF::PGA2).with_high(BF::PGA1).with_high(BF::PGA0),
- FullScaleRange::Within0_512V => config = self.config.with_high(BF::PGA2).with_low( BF::PGA1).with_low( BF::PGA0),
- FullScaleRange::Within0_256V => config = self.config.with_high(BF::PGA2).with_low( BF::PGA1).with_high(BF::PGA0),
- }
+ use FullScaleRange as FSR;
+ let cfg = self.config.clone();
+ let config = match range {
+ FSR::Within6_144V => cfg.with_low(BF::PGA2).with_low(BF::PGA1).with_low(BF::PGA0),
+ FSR::Within4_096V => cfg
+ .with_low(BF::PGA2)
+ .with_low(BF::PGA1)
+ .with_high(BF::PGA0),
+ FSR::Within2_048V => cfg
+ .with_low(BF::PGA2)
+ .with_high(BF::PGA1)
+ .with_low(BF::PGA0),
+ FSR::Within1_024V => cfg
+ .with_low(BF::PGA2)
+ .with_high(BF::PGA1)
+ .with_high(BF::PGA0),
+ FSR::Within0_512V => cfg
+ .with_high(BF::PGA2)
+ .with_low(BF::PGA1)
+ .with_low(BF::PGA0),
+ FSR::Within0_256V => cfg
+ .with_high(BF::PGA2)
+ .with_low(BF::PGA1)
+ .with_high(BF::PGA0),
+ };
self.iface.write_register(Register::CONFIG, config.bits)?;
self.config = config;
Ok(())
@@ -55,11 +71,10 @@ where
/// Set comparator mode
pub fn set_comparator_mode(&mut self, mode: ComparatorMode) -> Result<(), Error<E>> {
- let config;
- match mode {
- ComparatorMode::Traditional => config = self.config.with_low( BF::COMP_MODE),
- ComparatorMode::Window => config = self.config.with_high(BF::COMP_MODE),
- }
+ let config = match mode {
+ ComparatorMode::Traditional => self.config.with_low(BF::COMP_MODE),
+ ComparatorMode::Window => self.config.with_high(BF::COMP_MODE),
+ };
self.iface.write_register(Register::CONFIG, config.bits)?;
self.config = config;
Ok(())
@@ -70,11 +85,10 @@ where
&mut self,
polarity: ComparatorPolarity,
) -> Result<(), Error<E>> {
- let config;
- match polarity {
- ComparatorPolarity::ActiveLow => config = self.config.with_low( BF::COMP_POL),
- ComparatorPolarity::ActiveHigh => config = self.config.with_high(BF::COMP_POL),
- }
+ let config = match polarity {
+ ComparatorPolarity::ActiveLow => self.config.with_low(BF::COMP_POL),
+ ComparatorPolarity::ActiveHigh => self.config.with_high(BF::COMP_POL),
+ };
self.iface.write_register(Register::CONFIG, config.bits)?;
self.config = config;
Ok(())
@@ -85,11 +99,10 @@ where
&mut self,
latching: ComparatorLatching,
) -> Result<(), Error<E>> {
- let config;
- match latching {
- ComparatorLatching::Nonlatching => config = self.config.with_low( BF::COMP_LAT),
- ComparatorLatching::Latching => config = self.config.with_high(BF::COMP_LAT),
- }
+ let config = match latching {
+ ComparatorLatching::Nonlatching => self.config.with_low(BF::COMP_LAT),
+ ComparatorLatching::Latching => self.config.with_high(BF::COMP_LAT),
+ };
self.iface.write_register(Register::CONFIG, config.bits)?;
self.config = config;
Ok(())
@@ -99,12 +112,11 @@ where
///
/// The comparator can be disabled with [`disable_comparator()`](struct.Ads1x1x.html#method.disable_comparator)
pub fn set_comparator_queue(&mut self, queue: ComparatorQueue) -> Result<(), Error<E>> {
- let config;
- match queue {
- ComparatorQueue::One => config = self.config.with_low( BF::COMP_QUE1).with_low( BF::COMP_QUE0),
- ComparatorQueue::Two => config = self.config.with_low( BF::COMP_QUE1).with_high(BF::COMP_QUE0),
- ComparatorQueue::Four => config = self.config.with_high(BF::COMP_QUE1).with_low( BF::COMP_QUE0),
- }
+ let config = match queue {
+ ComparatorQueue::One => self.config.with_low(BF::COMP_QUE1).with_low(BF::COMP_QUE0),
+ ComparatorQueue::Two => self.config.with_low(BF::COMP_QUE1).with_high(BF::COMP_QUE0),
+ ComparatorQueue::Four => self.config.with_high(BF::COMP_QUE1).with_low(BF::COMP_QUE0),
+ };
self.iface.write_register(Register::CONFIG, config.bits)?;
self.config = config;
Ok(())