summaryrefslogtreecommitdiffstats
path: root/tests/mux.rs
blob: 1b2575b81a4634c54b9a599559c8fbddd3a94283 (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
use ads1x1x::channel;
use embedded_hal_mock::eh1::i2c::Transaction as I2cTrans;
use nb::block;

mod common;
use crate::common::{
    destroy_ads1015 as destroy, new_ads1015 as new, BitFlags as BF, Config, Register,
    DEVICE_ADDRESS as DEV_ADDR,
};

macro_rules! mux_test {
    ($name:ident, $CS:ident, $config_bits:expr, $other_CS:ident, $other_config_bits:expr) => {
        mod $name {
            use super::*;

            #[futures_test::test]
            async fn can_read() {
                let default_config = Config::default();
                let config = Config::default().with_high(BF::OS).with_high($config_bits);
                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.msb(), config.lsb()]),
                    I2cTrans::write_read(
                        DEV_ADDR,
                        vec![Register::CONFIG],
                        vec![config.msb(), config.lsb()],
                    ),
                    I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00]),
                ];
                let mut dev = new(&transactions);
                let measurement = block!(dev.read(channel::$CS).await).unwrap();
                assert_eq!(-2048, measurement);
                destroy(dev);
            }

            #[futures_test::test]
            async fn read_then_read_different_triggers_new_measurement() {
                let default_config = Config::default();
                let config = Config::default().with_high(BF::OS).with_high($config_bits);
                let other_config = Config::default().with_high($other_config_bits);
                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.msb(), config.lsb()]),
                    I2cTrans::write_read(
                        DEV_ADDR,
                        vec![Register::CONFIG],
                        vec![config.msb(), config.lsb()],
                    ),
                    I2cTrans::write(
                        DEV_ADDR,
                        vec![Register::CONFIG, other_config.msb(), other_config.lsb()],
                    ),
                    I2cTrans::write_read(
                        DEV_ADDR,
                        vec![Register::CONFIG],
                        vec![other_config.msb(), other_config.lsb()],
                    ),
                    I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00]),
                ];
                let mut dev = new(&transactions);
                assert_would_block!(dev.read(channel::$CS).await);
                let measurement = block!(dev.read(channel::$other_CS).await).unwrap();
                assert_eq!(-2048, measurement);
                destroy(dev);
            }

            #[futures_test::test]
            async fn continuous_can_select_channel() {
                let config1 = Config::default().with_low(BF::OP_MODE);
                let config2 = config1.with_high($config_bits);
                let transactions = [
                    I2cTrans::write(
                        DEV_ADDR,
                        vec![Register::CONFIG, config1.msb(), config1.lsb()],
                    ),
                    I2cTrans::write(
                        DEV_ADDR,
                        vec![Register::CONFIG, config2.msb(), config2.lsb()],
                    ),
                ];
                let dev = new(&transactions);
                let mut dev = dev.into_continuous().await.ok().unwrap();
                dev.select_channel(channel::$CS).await.unwrap();
                destroy(dev);
            }
        }
    };
}

mux_test!(diffa0a1, DifferentialA0A1, 0, SingleA0, BF::MUX2);
mux_test!(diffa0a3, DifferentialA0A3, BF::MUX0, SingleA0, BF::MUX2);
mux_test!(diffa1a3, DifferentialA1A3, BF::MUX1, SingleA0, BF::MUX2);
mux_test!(
    diffa2a3,
    DifferentialA2A3,
    BF::MUX1 | BF::MUX0,
    SingleA0,
    BF::MUX2
);
mux_test!(singlea0, SingleA0, BF::MUX2, DifferentialA0A1, 0);
mux_test!(singlea1, SingleA1, BF::MUX2 | BF::MUX0, SingleA0, BF::MUX2);
mux_test!(singlea2, SingleA2, BF::MUX2 | BF::MUX1, SingleA0, BF::MUX2);
mux_test!(
    singlea3,
    SingleA3,
    BF::MUX2 | BF::MUX1 | BF::MUX0,
    SingleA0,
    BF::MUX2
);