diff options
author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2024-12-08 12:01:31 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2024-12-08 12:01:31 +0000 |
commit | cd403a91e6078956445aeb21d6509e863b0592ae (patch) | |
tree | b1bb59ed526081be31494544f8920c39f0588711 /tests | |
parent | 0edd5527161809dfbc0c76e39c462e3a4f00beb7 (diff) | |
download | ads1x1x-async-async.tar.gz ads1x1x-async-async.tar.xz ads1x1x-async-async.zip |
Modify to use asyncasync
A changeover from embedded_hal::i2c::I2c to embedded_hal_async::i2c::I2c
including changes to all the relevant functions into async functions.
Tests have been updated to work using futures-test and embedded-hal-mock
with the embedded-hal-async feature.
Examples have been kept the same meaning they no longer compile.
Currently it doesn't _seem_ like the linux embedded hal can do async
i2c so maybe these should be re-written to use embassy?
Diffstat (limited to 'tests')
-rw-r--r-- | tests/mux.rs | 22 | ||||
-rw-r--r-- | tests/tier1.rs | 58 | ||||
-rw-r--r-- | tests/tier2.rs | 28 |
3 files changed, 55 insertions, 53 deletions
diff --git a/tests/mux.rs b/tests/mux.rs index ac335b5..1b2575b 100644 --- a/tests/mux.rs +++ b/tests/mux.rs @@ -13,8 +13,8 @@ macro_rules! mux_test { mod $name { use super::*; - #[test] - fn can_read() { + #[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 = [ @@ -32,13 +32,13 @@ macro_rules! mux_test { I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00]), ]; let mut dev = new(&transactions); - let measurement = block!(dev.read(channel::$CS)).unwrap(); + let measurement = block!(dev.read(channel::$CS).await).unwrap(); assert_eq!(-2048, measurement); destroy(dev); } - #[test] - fn read_then_read_different_triggers_new_measurement() { + #[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); @@ -66,14 +66,14 @@ macro_rules! mux_test { I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00]), ]; let mut dev = new(&transactions); - assert_would_block!(dev.read(channel::$CS)); - let measurement = block!(dev.read(channel::$other_CS)).unwrap(); + assert_would_block!(dev.read(channel::$CS).await); + let measurement = block!(dev.read(channel::$other_CS).await).unwrap(); assert_eq!(-2048, measurement); destroy(dev); } - #[test] - fn continuous_can_select_channel() { + #[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 = [ @@ -87,8 +87,8 @@ macro_rules! mux_test { ), ]; let dev = new(&transactions); - let mut dev = dev.into_continuous().ok().unwrap(); - dev.select_channel(channel::$CS).unwrap(); + let mut dev = dev.into_continuous().await.ok().unwrap(); + dev.select_channel(channel::$CS).await.unwrap(); destroy(dev); } } diff --git a/tests/tier1.rs b/tests/tier1.rs index bbe9d6f..fe5f1b4 100644 --- a/tests/tier1.rs +++ b/tests/tier1.rs @@ -16,8 +16,8 @@ macro_rules! measure_tests { mod would_block { use super::*; - #[test] - fn read_if_measurement_in_progress() { + #[futures_test::test] + async fn read_if_measurement_in_progress() { let config = Config::default().with_low(BF::OS); let transactions = [I2cTrans::write_read( DEV_ADDR, @@ -25,13 +25,13 @@ macro_rules! measure_tests { vec![config.msb(), config.lsb()], )]; let mut dev = $create(&transactions); - assert_would_block!(dev.read(channel::DifferentialA0A1)); + assert_would_block!(dev.read(channel::DifferentialA0A1).await); $destroy(dev); } } - #[test] - fn can_measure() { + #[futures_test::test] + async fn can_measure() { let default_config = Config::default(); let config_with_os = Config::default().with_high(BF::OS); let transactions = [ @@ -52,21 +52,21 @@ macro_rules! measure_tests { I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00]), ]; let mut dev = $create(&transactions); - let measurement = block!(dev.read(channel::DifferentialA0A1)).unwrap(); + let measurement = block!(dev.read(channel::DifferentialA0A1).await).unwrap(); assert_eq!($expected, measurement); $destroy(dev); } - #[test] - fn can_measure_continuous() { + #[futures_test::test] + async 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(); + let mut dev = dev.into_continuous().await.ok().unwrap(); + let measurement = dev.read().await.unwrap(); assert_eq!($expected, measurement); $destroy(dev); } @@ -82,14 +82,14 @@ mod data_rate_12bit { macro_rules! test { ($name:ident, $variant:ident, $config:expr) => { - #[test] - fn $name() { + #[futures_test::test] + async 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(); + dev.set_data_rate(DataRate12Bit::$variant).await.unwrap(); destroy_ads1013(dev); } }; @@ -158,14 +158,14 @@ mod data_rate_16bit { macro_rules! test { ($name:ident, $variant:ident, $config:expr) => { - #[test] - fn $name() { + #[futures_test::test] + async 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(); + dev.set_data_rate(DataRate16Bit::$variant).await.unwrap(); destroy_ads1113(dev); } }; @@ -237,8 +237,8 @@ mod data_rate_16bit { ); } -#[test] -fn can_read_measurement_in_progress() { +#[futures_test::test] +async fn can_read_measurement_in_progress() { let config_os = Config::default().with_low(BF::OS); let transactions = [I2cTrans::write_read( DEV_ADDR, @@ -246,12 +246,12 @@ fn can_read_measurement_in_progress() { vec![config_os.msb(), config_os.lsb()], )]; let mut dev = new_ads1013(&transactions); - assert!(dev.is_measurement_in_progress().unwrap()); + assert!(dev.is_measurement_in_progress().await.unwrap()); destroy_ads1013(dev); } -#[test] -fn can_read_measurement_not_in_progress() { +#[futures_test::test] +async fn can_read_measurement_not_in_progress() { let config_os = Config::default().with_high(BF::OS); let transactions = [I2cTrans::write_read( DEV_ADDR, @@ -259,24 +259,24 @@ fn can_read_measurement_not_in_progress() { vec![config_os.msb(), config_os.lsb()], )]; let mut dev = new_ads1013(&transactions); - assert!(!dev.is_measurement_in_progress().unwrap()); + assert!(!dev.is_measurement_in_progress().await.unwrap()); destroy_ads1013(dev); } -#[test] -fn can_convert_to_continuous() { +#[futures_test::test] +async 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(); + let dev = dev.into_continuous().await.ok().unwrap(); destroy_ads1013(dev); } -#[test] -fn can_convert_to_one_shot() { +#[futures_test::test] +async fn can_convert_to_one_shot() { let config_cont = Config::default().with_low(BF::OP_MODE); let config_os = Config::default(); let transactions = [ @@ -290,7 +290,7 @@ fn can_convert_to_one_shot() { ), ]; let dev = new_ads1013(&transactions); - let dev = dev.into_continuous().ok().unwrap(); - let dev = dev.into_one_shot().ok().unwrap(); + let dev = dev.into_continuous().await.ok().unwrap(); + let dev = dev.into_one_shot().await.ok().unwrap(); destroy_ads1013(dev); } diff --git a/tests/tier2.rs b/tests/tier2.rs index e29b67b..270d3ce 100644 --- a/tests/tier2.rs +++ b/tests/tier2.rs @@ -10,11 +10,11 @@ use crate::common::{ macro_rules! set_value_test { ($name:ident, $method:ident, $value:expr, $reg:ident, $msb:expr, $lsb:expr) => { - #[test] - fn $name() { + #[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).unwrap(); + dev.$method($value).await.unwrap(); destroy_ads1014(dev); } }; @@ -80,8 +80,8 @@ mod can_set_comparator_latching { ); } -#[test] -fn can_disable_comparator() { +#[futures_test::test] +async fn can_disable_comparator() { let config = Config::default() .with_high(BF::COMP_QUE1) .with_high(BF::COMP_QUE0); @@ -90,7 +90,7 @@ fn can_disable_comparator() { vec![Register::CONFIG, config.msb(), config.lsb()], )]; let mut dev = new_ads1014(&transactions); - dev.disable_comparator().unwrap(); + dev.disable_comparator().await.unwrap(); destroy_ads1014(dev); } @@ -122,19 +122,19 @@ mod can_set_comparator_queue { ); } -#[test] -fn can_use_alert_rdy_pin_as_rdy_does_not_disable_comparator_if_already_disabled() { +#[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().unwrap(); + dev.use_alert_rdy_pin_as_ready().await.unwrap(); destroy_ads1014(dev); } -#[test] -fn can_use_alert_rdy_pin_as_rdy_disabled_comparator() { +#[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); @@ -155,8 +155,10 @@ fn can_use_alert_rdy_pin_as_rdy_disabled_comparator() { I2cTrans::write(DEV_ADDR, vec![Register::LOW_TH, 0, 0]), ]; let mut dev = new_ads1014(&transactions); - dev.set_comparator_queue(ComparatorQueue::One).unwrap(); - dev.use_alert_rdy_pin_as_ready().unwrap(); + dev.set_comparator_queue(ComparatorQueue::One) + .await + .unwrap(); + dev.use_alert_rdy_pin_as_ready().await.unwrap(); destroy_ads1014(dev); } |