summaryrefslogtreecommitdiffstats
path: root/src/channels.rs
blob: a4c806c8f532fc8f2cf575e9aecefe20f39baf27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//! ADC input channels
use crate::{ic, Ads1x1x, BitFlags as BF, Config};
use embedded_hal::adc;

/// ADC input channel selection
#[allow(dead_code)]
pub mod channel {
    /// Measure single-ended signal on input channel 0
    pub struct SingleA0;
    /// Measure single-ended signal on input channel 1
    pub struct SingleA1;
    /// Measure single-ended signal on input channel 2
    pub struct SingleA2;
    /// Measure single-ended signal on input channel 3
    pub struct SingleA3;
    /// Measure signal on input channel 0 differentially to signal on input channel 1
    pub struct DifferentialA0A1;
    /// Measure signal on input channel 0 differentially to signal on input channel 3
    pub struct DifferentialA0A3;
    /// Measure signal on input channel 1 differentially to signal on input channel 3
    pub struct DifferentialA1A3;
    /// Measure signal on input channel 3 differentially to signal on input channel 3
    pub struct DifferentialA2A3;
}

/// ADC input channel selection
#[derive(Debug, Clone, Copy)]
pub enum ChannelSelection {
    /// Measure single-ended signal on input channel 0
    SingleA0,
    /// Measure single-ended signal on input channel 1
    SingleA1,
    /// Measure single-ended signal on input channel 2
    SingleA2,
    /// Measure single-ended signal on input channel 3
    SingleA3,
    /// Measure signal on input channel 0 differentially to signal on input channel 1
    DifferentialA0A1,
    /// Measure signal on input channel 0 differentially to signal on input channel 3
    DifferentialA0A3,
    /// Measure signal on input channel 1 differentially to signal on input channel 3
    DifferentialA1A3,
    /// Measure signal on input channel 2 differentially to signal on input channel 3
    DifferentialA2A3,
}

macro_rules! impl_channel {
    ( $IC:ident, $CH:ident ) => {
        impl<DI, CONV, MODE> adc::Channel<Ads1x1x<DI, ic::$IC, CONV, MODE>> for channel::$CH {
            type ID = ChannelSelection;

            fn channel() -> Self::ID {
                ChannelSelection::$CH
            }
        }
    };
}

impl_channel!(Ads1013, DifferentialA0A1);
impl_channel!(Ads1113, DifferentialA0A1);

impl_channel!(Ads1014, DifferentialA0A1);
impl_channel!(Ads1114, DifferentialA0A1);

impl_channel!(Ads1015, DifferentialA0A1);
impl_channel!(Ads1015, DifferentialA0A3);
impl_channel!(Ads1015, DifferentialA1A3);
impl_channel!(Ads1015, DifferentialA2A3);
impl_channel!(Ads1015, SingleA0);
impl_channel!(Ads1015, SingleA1);
impl_channel!(Ads1015, SingleA2);
impl_channel!(Ads1015, SingleA3);

impl_channel!(Ads1115, DifferentialA0A1);
impl_channel!(Ads1115, DifferentialA0A3);
impl_channel!(Ads1115, DifferentialA1A3);
impl_channel!(Ads1115, DifferentialA2A3);
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),
        }
    }
}