summaryrefslogtreecommitdiffstats
path: root/tests/tier2.rs
blob: 270d3ced35137b81bd156c6b438ac75f74557301 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use ads1x1x::{
    ComparatorLatching, ComparatorMode, ComparatorPolarity, ComparatorQueue, FullScaleRange,
};
use embedded_hal_mock::eh1::i2c::Transaction as I2cTrans;

mod common;
use crate::common::{
    destroy_ads1014, new_ads1014, BitFlags as BF, Config, Register, DEVICE_ADDRESS as DEV_ADDR,
};

macro_rules! set_value_test {
    ($name:ident, $method:ident, $value:expr, $reg:ident, $msb:expr, $lsb:expr) => {
        #[futures_test::test]
        async fn $name() {
            let transactions = [I2cTrans::write(DEV_ADDR, vec![Register::$reg, $msb, $lsb])];
            let mut dev = new_ads1014(&transactions);
            dev.$method($value).await.unwrap();
            destroy_ads1014(dev);
        }
    };
}

macro_rules! config_test {
    ($name:ident, $method:ident, $value:expr, $config:expr) => {
        set_value_test!($name, $method, $value, CONFIG, $config.msb(), $config.lsb());
    };
}

mod can_set_comparator_thresholds {
    use super::*;
    set_value_test!(low, set_low_threshold_raw, 2047, LOW_TH, 0x7F, 0xF0);
    set_value_test!(high, set_high_threshold_raw, 2047, HIGH_TH, 0x7F, 0xF0);
}

mod can_set_comparator_mode {
    use super::*;
    config_test!(
        traditional,
        set_comparator_mode,
        ComparatorMode::Traditional,
        Config::default().with_low(BF::COMP_MODE)
    );
    config_test!(
        window,
        set_comparator_mode,
        ComparatorMode::Window,
        Config::default().with_high(BF::COMP_MODE)
    );
}

mod can_set_comparator_polarity {
    use super::*;
    config_test!(
        low,
        set_comparator_polarity,
        ComparatorPolarity::ActiveLow,
        Config::default().with_low(BF::COMP_POL)
    );
    config_test!(
        high,
        set_comparator_polarity,
        ComparatorPolarity::ActiveHigh,
        Config::default().with_high(BF::COMP_POL)
    );
}

mod can_set_comparator_latching {
    use super::*;
    config_test!(
        non,
        set_comparator_latching,
        ComparatorLatching::Nonlatching,
        Config::default().with_low(BF::COMP_LAT)
    );
    config_test!(
        lat,
        set_comparator_latching,
        ComparatorLatching::Latching,
        Config::default().with_high(BF::COMP_LAT)
    );
}

#[futures_test::test]
async fn can_disable_comparator() {
    let config = Config::default()
        .with_high(BF::COMP_QUE1)
        .with_high(BF::COMP_QUE0);
    let transactions = [I2cTrans::write(
        DEV_ADDR,
        vec![Register::CONFIG, config.msb(), config.lsb()],
    )];
    let mut dev = new_ads1014(&transactions);
    dev.disable_comparator().await.unwrap();
    destroy_ads1014(dev);
}

mod can_set_comparator_queue {
    use super::*;
    config_test!(
        one,
        set_comparator_queue,
        ComparatorQueue::One,
        Config::default()
            .with_low(BF::COMP_QUE1)
            .with_low(BF::COMP_QUE0)
    );
    config_test!(
        two,
        set_comparator_queue,
        ComparatorQueue::Two,
        Config::default()
            .with_low(BF::COMP_QUE1)
            .with_high(BF::COMP_QUE0)
    );
    config_test!(
        four,
        set_comparator_queue,
        ComparatorQueue::Four,
        Config::default()
            .with_high(BF::COMP_QUE1)
            .with_low(BF::COMP_QUE0)
    );
}

#[futures_test::test]
async fn can_use_alert_rdy_pin_as_rdy_does_not_disable_comparator_if_already_disabled() {
    let transactions = [
        I2cTrans::write(DEV_ADDR, vec![Register::HIGH_TH, 0b1000_0000, 0]),
        I2cTrans::write(DEV_ADDR, vec![Register::LOW_TH, 0, 0]),
    ];
    let mut dev = new_ads1014(&transactions);
    dev.use_alert_rdy_pin_as_ready().await.unwrap();
    destroy_ads1014(dev);
}

#[futures_test::test]
async fn can_use_alert_rdy_pin_as_rdy_disabled_comparator() {
    let config = Config::default()
        .with_low(BF::COMP_QUE1)
        .with_low(BF::COMP_QUE0);
    let config_disabled_comp = Config::default()
        .with_high(BF::COMP_QUE1)
        .with_low(BF::COMP_QUE0);
    let transactions = [
        I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]),
        I2cTrans::write(
            DEV_ADDR,
            vec![
                Register::CONFIG,
                config_disabled_comp.msb(),
                config_disabled_comp.lsb(),
            ],
        ),
        I2cTrans::write(DEV_ADDR, vec![Register::HIGH_TH, 0b1000_0000, 0]),
        I2cTrans::write(DEV_ADDR, vec![Register::LOW_TH, 0, 0]),
    ];
    let mut dev = new_ads1014(&transactions);
    dev.set_comparator_queue(ComparatorQueue::One)
        .await
        .unwrap();
    dev.use_alert_rdy_pin_as_ready().await.unwrap();
    destroy_ads1014(dev);
}

mod can_set_full_scale_range {
    use super::*;
    config_test!(
        fsr6,
        set_full_scale_range,
        FullScaleRange::Within6_144V,
        Config::default()
            .with_low(BF::PGA2)
            .with_low(BF::PGA1)
            .with_low(BF::PGA0)
    );
    config_test!(
        fsr4,
        set_full_scale_range,
        FullScaleRange::Within4_096V,
        Config::default()
            .with_low(BF::PGA2)
            .with_low(BF::PGA1)
            .with_high(BF::PGA0)
    );
    config_test!(
        fsr2,
        set_full_scale_range,
        FullScaleRange::Within2_048V,
        Config::default()
            .with_low(BF::PGA2)
            .with_high(BF::PGA1)
            .with_low(BF::PGA0)
    );
    config_test!(
        fsr1,
        set_full_scale_range,
        FullScaleRange::Within1_024V,
        Config::default()
            .with_low(BF::PGA2)
            .with_high(BF::PGA1)
            .with_high(BF::PGA0)
    );
    config_test!(
        fsr0_5,
        set_full_scale_range,
        FullScaleRange::Within0_512V,
        Config::default()
            .with_high(BF::PGA2)
            .with_low(BF::PGA1)
            .with_low(BF::PGA0)
    );
    config_test!(
        fsr0_2,
        set_full_scale_range,
        FullScaleRange::Within0_256V,
        Config::default()
            .with_high(BF::PGA2)
            .with_low(BF::PGA1)
            .with_high(BF::PGA0)
    );
}