summaryrefslogtreecommitdiffstats
path: root/tests/ads1x1x_i2c.rs
blob: 2d8e7e249c84b820befddce94a797d7e0488902f (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
extern crate embedded_hal;
extern crate embedded_hal_mock as hal;
use hal::i2c::Transaction as I2cTrans;
extern crate ads1x1x;
use ads1x1x::DataRate;

#[macro_use]
mod common;
use common::{ new_ads1013, destroy_ads1013,
              DEVICE_ADDRESS as DEV_ADDR, Register, BitFlags, Config };

#[test]
fn can_set_low_threshold() {
    let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::LOW_TH, 0x7F, 0xF0]) ];
    let mut dev = new_ads1013(&transactions);
    dev.set_low_threshold(2047).unwrap();
    destroy_ads1013(dev);
}

#[test]
fn can_set_high_threshold() {
    let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::HIGH_TH, 0x7F, 0xF0]) ];
    let mut dev = new_ads1013(&transactions);
    dev.set_high_threshold(2047).unwrap();
    destroy_ads1013(dev);
}


macro_rules! test_set_data_rate {
    ($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(DataRate::$variant).unwrap();
            destroy_ads1013(dev);
        }
    }
}

mod data_rate {
    use super::*;
    test_set_data_rate!(sps128,  Sps128,  Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0));
    test_set_data_rate!(sps250,  Sps250,  Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0));
    test_set_data_rate!(sps490,  Sps490,  Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0));
    test_set_data_rate!(sps920,  Sps920,  Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0));
    test_set_data_rate!(sps1600, Sps1600, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0));
    test_set_data_rate!(sps2400, Sps2400, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0));
    test_set_data_rate!(sps3300, Sps3300, Config::default().with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0));
}