summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorDiego Barrios Romero <eldruin@gmail.com>2018-11-07 20:23:54 +0100
committerDiego Barrios Romero <eldruin@gmail.com>2018-11-07 20:23:54 +0100
commitfddc058847ecc4dc1571ae4be8e3c31698f315dc (patch)
tree6d1f5409c24f640991e3e79357dec9b306afd96c /tests
parent72c3a3d5cd5fb90452640ad2d218f8cdac596f68 (diff)
downloadads1x1x-async-fddc058847ecc4dc1571ae4be8e3c31698f315dc.tar.gz
ads1x1x-async-fddc058847ecc4dc1571ae4be8e3c31698f315dc.tar.xz
ads1x1x-async-fddc058847ecc4dc1571ae4be8e3c31698f315dc.zip
Add tests for ADS1x13 devices
Diffstat (limited to 'tests')
-rw-r--r--tests/ads1x13.rs55
-rw-r--r--tests/common/mod.rs76
2 files changed, 131 insertions, 0 deletions
diff --git a/tests/ads1x13.rs b/tests/ads1x13.rs
new file mode 100644
index 0000000..6a2b558
--- /dev/null
+++ b/tests/ads1x13.rs
@@ -0,0 +1,55 @@
+#[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;
+
+mod common;
+use common::{ new_ads1013, destroy_ads1013, new_ads1113, destroy_ads1113,
+ DEVICE_ADDRESS as DEV_ADDR, Register, BitFlags, Config };
+
+macro_rules! impl_tests {
+ ($IC:ident, $create:ident, $destroy:ident, $expected:expr) => {
+ mod $IC {
+ use embedded_hal::adc::OneShot;
+ use super::*;
+ #[test]
+ fn can_create() {
+ let dev = $create(&[]);
+ $destroy(dev);
+ }
+
+ mod would_block {
+ use super::*;
+
+ #[test]
+ fn read_if_measurement_in_progress() {
+ let config = Config::default().with_low(BitFlags::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::A0));
+ $destroy(dev);
+ }
+ }
+
+ #[test]
+ fn can_measure() {
+ let default_config = Config::default();
+ let config_with_os = Config::default().with_high(BitFlags::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::A0{})).unwrap();
+ assert_eq!($expected, measurement);
+ $destroy(dev);
+ }
+ }
+ }
+}
+
+impl_tests!(ads1013, new_ads1013, destroy_ads1013, -2048);
+impl_tests!(ads1113, new_ads1113, destroy_ads1113, -32768);
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
new file mode 100644
index 0000000..8a4f1dd
--- /dev/null
+++ b/tests/common/mod.rs
@@ -0,0 +1,76 @@
+use hal::i2c::{ Mock as I2cMock, Transaction as I2cTrans };
+extern crate ads1x1x;
+use self::ads1x1x::{ Ads1x1x, interface, ic, SlaveAddr, mode };
+
+pub const DEVICE_ADDRESS : u8 = 0b100_1000;
+
+pub struct Register;
+
+impl Register {
+ pub const CONVERSION : u8 = 0x00;
+ pub const CONFIG : u8 = 0x01;
+ //const LOW_TH : u8 = 0x02;
+ //const HIGH_TH : u8 = 0x03;
+}
+
+pub struct BitFlags;
+impl BitFlags {
+ //pub const OP_MODE : u16 = 0b0000_0001_0000_0000;
+ pub const OS : u16 = 0b1000_0000_0000_0000;
+ //pub const DR2 : u16 = 0b0000_0000_1000_0000;
+ //pub const DR1 : u16 = 0b0000_0000_0100_0000;
+ //pub const DR0 : u16 = 0b0000_0000_0010_0000;
+}
+
+pub struct Config {
+ pub bits: u16
+}
+
+impl Config {
+ pub fn with_high(&self, mask: u16) -> Self {
+ Config { bits: self.bits | mask }
+ }
+ pub fn with_low(&self, mask: u16) -> Self {
+ Config { bits: self.bits & !mask }
+ }
+
+ pub fn msb(&self) -> u8 {
+ (self.bits >> 8) as u8
+ }
+
+ pub fn lsb(&self) -> u8 {
+ self.bits as u8
+ }
+}
+
+impl Default for Config {
+ fn default() -> Self {
+ Config { bits: 0x8583 }
+ }
+}
+
+macro_rules! impl_new_destroy {
+ ($ic:ident, $create:ident, $destroy:ident, $trans:ty, $iface:ty) => {
+ pub fn $create(transactions: &[$trans]) -> Ads1x1x<$iface, ic::$ic, mode::OneShot> {
+ Ads1x1x::$create(I2cMock::new(&transactions), SlaveAddr::default())
+ }
+
+ pub fn $destroy<MODE>(dev: Ads1x1x<$iface, ic::$ic, MODE>) {
+ dev.$destroy().done();
+ }
+ }
+}
+
+impl_new_destroy!(Ads1013, new_ads1013, destroy_ads1013, I2cTrans, interface::I2cInterface<I2cMock>);
+impl_new_destroy!(Ads1113, new_ads1113, destroy_ads1113, I2cTrans, interface::I2cInterface<I2cMock>);
+
+#[macro_export]
+macro_rules! assert_would_block {
+ ($result: expr) => {
+ match $result {
+ Err(nb::Error::WouldBlock) => (),
+ _ => panic!("Would not block.")
+ }
+ }
+}
+