summaryrefslogtreecommitdiffstats
path: root/tests/tier1.rs
blob: 89a60e0bb1b23ba8bdf39951dd0ba5fe87a00dbc (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#[macro_use(block)]
extern crate nb;
extern crate embedded_hal;
extern crate embedded_hal_mock as hal;
use hal::i2c::Transaction as I2cTrans;
extern crate ads1x1x;
use ads1x1x::{channel, DataRate12Bit, DataRate16Bit};

mod common;
use common::{
    destroy_ads1013, destroy_ads1113, new_ads1013, new_ads1113, BitFlags as BF, Config, Register,
    DEVICE_ADDRESS as DEV_ADDR,
};

macro_rules! measure_tests {
    ($IC:ident, $create:ident, $destroy:ident, $expected:expr) => {
        mod $IC {
            use super::*;
            use embedded_hal::adc::OneShot;

            mod would_block {
                use super::*;

                #[test]
                fn read_if_measurement_in_progress() {
                    let config = Config::default().with_low(BF::OS);
                    let transactions = [I2cTrans::write_read(
                        DEV_ADDR,
                        vec![Register::CONFIG],
                        vec![config.msb(), config.lsb()],
                    )];
                    let mut dev = $create(&transactions);
                    assert_would_block!(dev.read(&mut channel::DifferentialA0A1));
                    $destroy(dev);
                }
            }

            #[test]
            fn can_measure() {
                let default_config = Config::default();
                let config_with_os = Config::default().with_high(BF::OS);
                let transactions = [
                    I2cTrans::write_read(
                        DEV_ADDR,
                        vec![Register::CONFIG],
                        vec![default_config.msb(), default_config.lsb()],
                    ),
                    I2cTrans::write(
                        DEV_ADDR,
                        vec![Register::CONFIG, config_with_os.msb(), config_with_os.lsb()],
                    ),
                    I2cTrans::write_read(
                        DEV_ADDR,
                        vec![Register::CONFIG],
                        vec![config_with_os.msb(), config_with_os.lsb()],
                    ),
                    I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00]),
                ];
                let mut dev = $create(&transactions);
                let measurement = block!(dev.read(&mut channel::DifferentialA0A1)).unwrap();
                assert_eq!($expected, measurement);
                $destroy(dev);
            }

            #[test]
            fn can_measure_continuous() {
                let config = Config::default().with_low(BF::OP_MODE);
                let transactions = [
                    I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]),
                    I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00]),
                ];
                let dev = $create(&transactions);
                let mut dev = dev.into_continuous().ok().unwrap();
                let measurement = dev.read().unwrap();
                assert_eq!($expected, measurement);
                $destroy(dev);
            }
        }
    };
}

measure_tests!(ads1013, new_ads1013, destroy_ads1013, -2048);
measure_tests!(ads1113, new_ads1113, destroy_ads1113, -32768);

mod data_rate_12bit {
    use super::*;

    macro_rules! test {
        ($name:ident, $variant:ident, $config:expr) => {
            #[test]
            fn $name() {
                let transactions = [I2cTrans::write(
                    DEV_ADDR,
                    vec![Register::CONFIG, $config.msb(), $config.lsb()],
                )];
                let mut dev = new_ads1013(&transactions);
                dev.set_data_rate(DataRate12Bit::$variant).unwrap();
                destroy_ads1013(dev);
            }
        };
    }

    test!(
        sps128,
        Sps128,
        Config::default()
            .with_low(BF::DR2)
            .with_low(BF::DR1)
            .with_low(BF::DR0)
    );
    test!(
        sps250,
        Sps250,
        Config::default()
            .with_low(BF::DR2)
            .with_low(BF::DR1)
            .with_high(BF::DR0)
    );
    test!(
        sps490,
        Sps490,
        Config::default()
            .with_low(BF::DR2)
            .with_high(BF::DR1)
            .with_low(BF::DR0)
    );
    test!(
        sps920,
        Sps920,
        Config::default()
            .with_low(BF::DR2)
            .with_high(BF::DR1)
            .with_high(BF::DR0)
    );
    test!(
        sps1600,
        Sps1600,
        Config::default()
            .with_high(BF::DR2)
            .with_low(BF::DR1)
            .with_low(BF::DR0)
    );
    test!(
        sps2400,
        Sps2400,
        Config::default()
            .with_high(BF::DR2)
            .with_low(BF::DR1)
            .with_high(BF::DR0)
    );
    test!(
        sps3300,
        Sps3300,
        Config::default()
            .with_high(BF::DR2)
            .with_high(BF::DR1)
            .with_low(BF::DR0)
    );
}

mod data_rate_16bit {
    use super::*;

    macro_rules! test {
        ($name:ident, $variant:ident, $config:expr) => {
            #[test]
            fn $name() {
                let transactions = [I2cTrans::write(
                    DEV_ADDR,
                    vec![Register::CONFIG, $config.msb(), $config.lsb()],
                )];
                let mut dev = new_ads1113(&transactions);
                dev.set_data_rate(DataRate16Bit::$variant).unwrap();
                destroy_ads1113(dev);
            }
        };
    }

    test!(
        sps8,
        Sps8,
        Config::default()
            .with_low(BF::DR2)
            .with_low(BF::DR1)
            .with_low(BF::DR0)
    );
    test!(
        sps16,
        Sps16,
        Config::default()
            .with_low(BF::DR2)
            .with_low(BF::DR1)
            .with_high(BF::DR0)
    );
    test!(
        sps32,
        Sps32,
        Config::default()
            .with_low(BF::DR2)
            .with_high(BF::DR1)
            .with_low(BF::DR0)
    );
    test!(
        sps64,
        Sps64,
        Config::default()
            .with_low(BF::DR2)
            .with_high(BF::DR1)
            .with_high(BF::DR0)
    );
    test!(
        sps128,
        Sps128,
        Config::default()
            .with_high(BF::DR2)
            .with_low(BF::DR1)
            .with_low(BF::DR0)
    );
    test!(
        sps250,
        Sps250,
        Config::default()
            .with_high(BF::DR2)
            .with_low(BF::DR1)
            .with_high(BF::DR0)
    );
    test!(
        sps475,
        Sps475,
        Config::default()
            .with_high(BF::DR2)
            .with_high(BF::DR1)
            .with_low(BF::DR0)
    );
    test!(
        sps860,
        Sps860,
        Config::default()
            .with_high(BF::DR2)
            .with_high(BF::DR1)
            .with_high(BF::DR0)
    );
}

#[test]
fn can_read_measurement_in_progress() {
    let config_os = Config::default().with_low(BF::OS);
    let transactions = [I2cTrans::write_read(
        DEV_ADDR,
        vec![Register::CONFIG],
        vec![config_os.msb(), config_os.lsb()],
    )];
    let mut dev = new_ads1013(&transactions);
    assert!(dev.is_measurement_in_progress().unwrap());
    destroy_ads1013(dev);
}

#[test]
fn can_read_measurement_not_in_progress() {
    let config_os = Config::default().with_high(BF::OS);
    let transactions = [I2cTrans::write_read(
        DEV_ADDR,
        vec![Register::CONFIG],
        vec![config_os.msb(), config_os.lsb()],
    )];
    let mut dev = new_ads1013(&transactions);
    assert!(!dev.is_measurement_in_progress().unwrap());
    destroy_ads1013(dev);
}

#[test]
fn can_convert_to_continuous() {
    let config = Config::default().with_low(BF::OP_MODE);
    let transactions = [I2cTrans::write(
        DEV_ADDR,
        vec![Register::CONFIG, config.msb(), config.lsb()],
    )];
    let dev = new_ads1013(&transactions);
    let dev = dev.into_continuous().ok().unwrap();
    destroy_ads1013(dev);
}

#[test]
fn can_convert_to_one_shot() {
    let config_cont = Config::default().with_low(BF::OP_MODE);
    let config_os = Config::default();
    let transactions = [
        I2cTrans::write(
            DEV_ADDR,
            vec![Register::CONFIG, config_cont.msb(), config_cont.lsb()],
        ),
        I2cTrans::write(
            DEV_ADDR,
            vec![Register::CONFIG, config_os.msb(), config_os.lsb()],
        ),
    ];
    let dev = new_ads1013(&transactions);
    let dev = dev.into_continuous().ok().unwrap();
    let dev = dev.into_one_shot().ok().unwrap();
    destroy_ads1013(dev);
}