Skip to content

Commit 6c12cc8

Browse files
committed
Re-add channel-specific types.
1 parent 5f797bc commit 6c12cc8

File tree

11 files changed

+129
-94
lines changed

11 files changed

+129
-94
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ Please find additional examples using hardware in this repository: [driver-examp
8080
use linux_embedded_hal::I2cdev;
8181
use nb::block;
8282

83-
use ads1x1x::{Ads1x1x, SlaveAddr, ChannelSelection};
83+
use ads1x1x::{channel, Ads1x1x, SlaveAddr};
8484

8585
fn main() {
8686
let dev = I2cdev::new("/dev/i2c-1").unwrap();
8787
let address = SlaveAddr::default();
8888
let mut adc = Ads1x1x::new_ads1013(dev, address);
89-
let value = block!(adc.read(ChannelSelection::DifferentialA0A1)).unwrap();
89+
let value = block!(adc.read(channel::DifferentialA0A1)).unwrap();
9090
println!("Measurement: {}", value);
9191
// get I2C device back
9292
let _dev = adc.destroy_ads1013();

examples/all_channels.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use linux_embedded_hal::I2cdev;
22
use nb::block;
33

4-
use ads1x1x::{Ads1x1x, ChannelSelection, SlaveAddr};
4+
use ads1x1x::{channel, Ads1x1x, SlaveAddr};
55

66
fn main() {
77
let dev = I2cdev::new("/dev/i2c-1").unwrap();
88
let address = SlaveAddr::default();
99
let mut adc = Ads1x1x::new_ads1015(dev, address);
1010
let values = [
11-
block!(adc.read(ChannelSelection::SingleA0)).unwrap(),
12-
block!(adc.read(ChannelSelection::SingleA1)).unwrap(),
13-
block!(adc.read(ChannelSelection::SingleA2)).unwrap(),
14-
block!(adc.read(ChannelSelection::SingleA3)).unwrap(),
11+
block!(adc.read(channel::SingleA0)).unwrap(),
12+
block!(adc.read(channel::SingleA1)).unwrap(),
13+
block!(adc.read(channel::SingleA2)).unwrap(),
14+
block!(adc.read(channel::SingleA3)).unwrap(),
1515
];
1616
for (channel, value) in values.iter().enumerate() {
1717
println!("Channel {}: {}", channel, value);

examples/linux.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use linux_embedded_hal::I2cdev;
22
use nb::block;
33

4-
use ads1x1x::{Ads1x1x, ChannelSelection, SlaveAddr};
4+
use ads1x1x::{channel, Ads1x1x, SlaveAddr};
55

66
fn main() {
77
let dev = I2cdev::new("/dev/i2c-1").unwrap();
88
let address = SlaveAddr::default();
99
let mut adc = Ads1x1x::new_ads1013(dev, address);
10-
let value = block!(adc.read(ChannelSelection::DifferentialA0A1)).unwrap();
10+
let value = block!(adc.read(channel::DifferentialA0A1)).unwrap();
1111
println!("Measurement: {}", value);
1212
// get I2C device back
1313
let _dev = adc.destroy_ads1013();

examples/typed.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ use linux_embedded_hal::I2cdev;
55
use nb::block;
66

77
use ads1x1x::{
8+
channel,
89
ic::{Ads1115, Resolution16Bit},
910
interface::I2cInterface,
10-
Ads1x1x, ChannelSelection, SlaveAddr,
11+
Ads1x1x, SlaveAddr,
1112
};
1213

1314
/// Type alias
@@ -16,7 +17,7 @@ type Adc = Ads1x1x<I2cInterface<I2cdev>, Ads1115, Resolution16Bit, ads1x1x::mode
1617
/// Read a single value from channel A.
1718
/// Returns 0 on Error.
1819
pub fn read(adc: &mut Adc) -> i16 {
19-
block!(adc.read(ChannelSelection::SingleA0)).unwrap_or(0)
20+
block!(adc.read(channel::SingleA0)).unwrap_or(0)
2021
}
2122

2223
fn main() {

src/channel.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//! ADC input channels
2+
use crate::{ic, Ads1x1x, BitFlags as BF, Config};
3+
4+
use private::ChannelSelection;
5+
6+
/// Marker type for an ADC input channel.
7+
pub trait Channel<T> {
8+
/// Get the channel.
9+
fn channel(self) -> ChannelSelection;
10+
}
11+
12+
macro_rules! impl_channels {
13+
($(#[doc = $doc:expr] $CH:ident => [$($IC:ident),+]),+ $(,)?) => {
14+
mod private {
15+
#[derive(Debug, Clone, Copy)]
16+
/// ADC input channel selection.
17+
pub enum ChannelSelection {
18+
$(
19+
#[doc = $doc]
20+
$CH,
21+
)+
22+
}
23+
}
24+
25+
$(
26+
#[doc = $doc]
27+
pub struct $CH;
28+
29+
$(
30+
impl<DI, CONV, MODE> Channel<Ads1x1x<DI, ic::$IC, CONV, MODE>> for $CH {
31+
fn channel(self) -> ChannelSelection {
32+
ChannelSelection::$CH
33+
}
34+
}
35+
)+
36+
)+
37+
};
38+
}
39+
40+
impl_channels!(
41+
/// Measure signal on input channel 0 differentially to signal on input channel 1.
42+
DifferentialA0A1 => [Ads1013, Ads1014, Ads1015, Ads1113, Ads1114, Ads1115],
43+
/// Measure signal on input channel 0 differentially to signal on input channel 3.
44+
DifferentialA0A3 => [Ads1015, Ads1115],
45+
/// Measure signal on input channel 1 differentially to signal on input channel 3.
46+
DifferentialA1A3 => [Ads1015, Ads1115],
47+
/// Measure signal on input channel 3 differentially to signal on input channel 3.
48+
DifferentialA2A3 => [Ads1015, Ads1115],
49+
/// Measure single-ended signal on input channel 0.
50+
SingleA0 => [Ads1015, Ads1115],
51+
/// Measure single-ended signal on input channel 1.
52+
SingleA1 => [Ads1015, Ads1115],
53+
/// Measure single-ended signal on input channel 2.
54+
SingleA2 => [Ads1015, Ads1115],
55+
/// Measure single-ended signal on input channel 3.
56+
SingleA3 => [Ads1015, Ads1115]
57+
);
58+
59+
impl Config {
60+
pub(crate) fn with_mux_bits(&self, ch: ChannelSelection) -> Self {
61+
match ch {
62+
ChannelSelection::DifferentialA0A1 => self
63+
.with_low(BF::MUX2)
64+
.with_low(BF::MUX1)
65+
.with_low(BF::MUX0),
66+
ChannelSelection::DifferentialA0A3 => self
67+
.with_low(BF::MUX2)
68+
.with_low(BF::MUX1)
69+
.with_high(BF::MUX0),
70+
ChannelSelection::DifferentialA1A3 => self
71+
.with_low(BF::MUX2)
72+
.with_high(BF::MUX1)
73+
.with_low(BF::MUX0),
74+
ChannelSelection::DifferentialA2A3 => self
75+
.with_low(BF::MUX2)
76+
.with_high(BF::MUX1)
77+
.with_high(BF::MUX0),
78+
ChannelSelection::SingleA0 => self
79+
.with_high(BF::MUX2)
80+
.with_low(BF::MUX1)
81+
.with_low(BF::MUX0),
82+
ChannelSelection::SingleA1 => self
83+
.with_high(BF::MUX2)
84+
.with_low(BF::MUX1)
85+
.with_high(BF::MUX0),
86+
ChannelSelection::SingleA2 => self
87+
.with_high(BF::MUX2)
88+
.with_high(BF::MUX1)
89+
.with_low(BF::MUX0),
90+
ChannelSelection::SingleA3 => self
91+
.with_high(BF::MUX2)
92+
.with_high(BF::MUX1)
93+
.with_high(BF::MUX0),
94+
}
95+
}
96+
}

src/channels.rs

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/devices/mode/continuous.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Continuous measurement mode
22
33
use crate::{
4-
channels::ChannelSelection, conversion, devices::OperatingMode, interface, mode, Ads1x1x,
5-
Error, ModeChangeError, Register,
4+
conversion, devices::OperatingMode, interface, mode, Ads1x1x, Channel, Error, ModeChangeError,
5+
Register,
66
};
77
use core::marker::PhantomData;
88

@@ -40,8 +40,8 @@ where
4040
/// Note that when changing the channel in continuous conversion mode, the
4141
/// ongoing conversion will be completed.
4242
/// The following conversions will use the new channel configuration.
43-
pub fn select_channel(&mut self, channel: ChannelSelection) -> Result<(), Error<E>> {
44-
let config = self.config.with_mux_bits(channel);
43+
pub fn select_channel<CH: Channel<Self>>(&mut self, channel: CH) -> Result<(), Error<E>> {
44+
let config = self.config.with_mux_bits(channel.channel());
4545
self.iface.write_register(Register::CONFIG, config.bits)?;
4646
self.config = config;
4747
Ok(())

src/devices/mode/oneshot.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Common functions
22
use crate::{
3-
conversion, devices::OperatingMode, interface, mode, Ads1x1x, BitFlags, ChannelSelection,
4-
Config, Error, ModeChangeError, Register,
3+
conversion, devices::OperatingMode, interface, mode, Ads1x1x, BitFlags, Channel, Config, Error,
4+
ModeChangeError, Register,
55
};
66
use core::marker::PhantomData;
77

@@ -52,14 +52,15 @@ where
5252
/// In case a measurement was requested and after is it is finished a
5353
/// measurement on a different channel is requested, a new measurement on
5454
/// using the new channel selection is triggered.
55-
pub fn read(&mut self, channel: ChannelSelection) -> nb::Result<i16, Error<E>> {
55+
pub fn read<CH: Channel<Self>>(&mut self, channel: CH) -> nb::Result<i16, Error<E>> {
5656
if self
5757
.is_measurement_in_progress()
5858
.map_err(nb::Error::Other)?
5959
{
6060
return Err(nb::Error::WouldBlock);
6161
}
62-
let same_channel = self.config == self.config.with_mux_bits(channel);
62+
let config = self.config.with_mux_bits(channel.channel());
63+
let same_channel = self.config == config;
6364
if self.a_conversion_was_started && same_channel {
6465
// result is ready
6566
let value = self
@@ -69,7 +70,6 @@ where
6970
self.a_conversion_was_started = false;
7071
return Ok(CONV::convert_measurement(value));
7172
}
72-
let config = self.config.with_mux_bits(channel);
7373
self.trigger_measurement(&config)
7474
.map_err(nb::Error::Other)?;
7575
self.config = config;

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@
128128
//!
129129
//! ### Make a one-shot measurement
130130
//! ```no_run
131-
//! use ads1x1x::{Ads1x1x, SlaveAddr, ChannelSelection};
131+
//! use ads1x1x::{channel, Ads1x1x, SlaveAddr};
132132
//! use linux_embedded_hal::I2cdev;
133133
//! use nb::block;
134134
//!
135135
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
136136
//! let mut adc = Ads1x1x::new_ads1013(dev, SlaveAddr::default());
137-
//! let measurement = block!(adc.read(ChannelSelection::DifferentialA0A1)).unwrap();
137+
//! let measurement = block!(adc.read(channel::DifferentialA0A1)).unwrap();
138138
//! println!("Measurement: {}", measurement);
139139
//! let _dev = adc.destroy_ads1013(); // get I2C device back
140140
//! ```
@@ -234,8 +234,8 @@ impl BitFlags {
234234
const COMP_QUE0: u16 = 0b0000_0000_0000_0001;
235235
}
236236

237-
mod channels;
238-
pub use crate::channels::ChannelSelection;
237+
pub mod channel;
238+
pub use channel::Channel;
239239
mod construction;
240240
mod conversion;
241241
pub use crate::conversion::{ConvertMeasurement, ConvertThreshold};

tests/mux.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ads1x1x::ChannelSelection;
1+
use ads1x1x::channel;
22
use embedded_hal_mock::eh1::i2c::Transaction as I2cTrans;
33
use nb::block;
44

@@ -32,7 +32,7 @@ macro_rules! mux_test {
3232
I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00]),
3333
];
3434
let mut dev = new(&transactions);
35-
let measurement = block!(dev.read(ChannelSelection::$CS)).unwrap();
35+
let measurement = block!(dev.read(channel::$CS)).unwrap();
3636
assert_eq!(-2048, measurement);
3737
destroy(dev);
3838
}
@@ -66,8 +66,8 @@ macro_rules! mux_test {
6666
I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00]),
6767
];
6868
let mut dev = new(&transactions);
69-
assert_would_block!(dev.read(ChannelSelection::$CS));
70-
let measurement = block!(dev.read(ChannelSelection::$other_CS)).unwrap();
69+
assert_would_block!(dev.read(channel::$CS));
70+
let measurement = block!(dev.read(channel::$other_CS)).unwrap();
7171
assert_eq!(-2048, measurement);
7272
destroy(dev);
7373
}
@@ -88,7 +88,7 @@ macro_rules! mux_test {
8888
];
8989
let dev = new(&transactions);
9090
let mut dev = dev.into_continuous().ok().unwrap();
91-
dev.select_channel(ChannelSelection::$CS).unwrap();
91+
dev.select_channel(channel::$CS).unwrap();
9292
destroy(dev);
9393
}
9494
}

0 commit comments

Comments
 (0)