1111#![ cfg_attr( not( test) , no_std) ]
1212
1313use crate :: Reserved0 :: Res0 ;
14+ use embedded_hal_async:: delay:: DelayNs ;
1415use embedded_hal_async:: i2c:: I2c ;
1516
1617pub mod registers;
@@ -39,31 +40,36 @@ impl From<SA0> for u8 {
3940 }
4041}
4142
42- pub struct Lis2dw12 < I2C : I2c > {
43+ pub struct Lis2dw12 < I2C : I2c , DELAY : DelayNs > {
4344 i2c : I2C ,
45+ delay : DELAY ,
4446 addr : u8 ,
4547}
4648
4749// Bit field masks
4850const TAP_THRESHOLD_MASK : u8 = 0x1F ;
4951const SELF_TEST_MODE_MASK : u8 = 0b1100_0000 ;
5052
51- impl < I2C : embedded_hal_async:: i2c:: I2c > Lis2dw12 < I2C > {
53+ impl < I2C : embedded_hal_async:: i2c:: I2c , DELAY : embedded_hal_async :: delay :: DelayNs > Lis2dw12 < I2C , DELAY > {
5254 /// Create a new LIS2DW12 instance. Address determined by connection to SA0
53- pub fn new ( i2c : I2C , sa0 : SA0 ) -> Self {
54- Self { i2c, addr : sa0. into ( ) }
55+ pub fn new ( i2c : I2C , delay : DELAY , sa0 : SA0 ) -> Self {
56+ Self {
57+ i2c,
58+ delay,
59+ addr : sa0. into ( ) ,
60+ }
5561 }
5662
5763 /// Create a new LIS2DW12 instance with SA0 tied to GND, resulting in an
5864 /// instance responding to address `0x18`.
59- pub fn new_with_sa0_gnd ( i2c : I2C ) -> Self {
60- Self :: new ( i2c, SA0 :: Gnd )
65+ pub fn new_with_sa0_gnd ( i2c : I2C , delay : DELAY ) -> Self {
66+ Self :: new ( i2c, delay , SA0 :: Gnd )
6167 }
6268
6369 /// Create a new LIS2DW12 instance with SA0 tied to V+, resulting in an
6470 /// instance responding to address `0x19`.
65- pub fn new_with_sa0_vplus ( i2c : I2C ) -> Self {
66- Self :: new ( i2c, SA0 :: Vplus )
71+ pub fn new_with_sa0_vplus ( i2c : I2C , delay : DELAY ) -> Self {
72+ Self :: new ( i2c, delay , SA0 :: Vplus )
6773 }
6874
6975 /// Destroy the driver instance, return the I2C bus instance.
@@ -145,7 +151,9 @@ impl<I2C: embedded_hal_async::i2c::I2c> Lis2dw12<I2C> {
145151
146152 /// Reads the current temperature and returns the value in degrees Celsius
147153 pub async fn temp_celsius ( & mut self ) -> Result < f32 , I2C :: Error > {
148- Ok ( Lis2dw12 :: < I2C > :: convert_temp_reg_to_celsius ( self . temp_12bit ( ) . await ?) )
154+ Ok ( Lis2dw12 :: < I2C , DELAY > :: convert_temp_reg_to_celsius (
155+ self . temp_12bit ( ) . await ?,
156+ ) )
149157 }
150158
151159 /// Reads the device acceleration register in the X axis
@@ -185,9 +193,9 @@ impl<I2C: embedded_hal_async::i2c::I2c> Lis2dw12<I2C> {
185193 let full_scale = self . full_scale_range ( ) . await ?;
186194 let ( accx, accy, accz) = self . acc ( ) . await ?;
187195 Ok ( (
188- Lis2dw12 :: < I2C > :: convert_acc_to_gs ( accx, full_scale) ,
189- Lis2dw12 :: < I2C > :: convert_acc_to_gs ( accy, full_scale) ,
190- Lis2dw12 :: < I2C > :: convert_acc_to_gs ( accz, full_scale) ,
196+ Lis2dw12 :: < I2C , DELAY > :: convert_acc_to_gs ( accx, full_scale) ,
197+ Lis2dw12 :: < I2C , DELAY > :: convert_acc_to_gs ( accy, full_scale) ,
198+ Lis2dw12 :: < I2C , DELAY > :: convert_acc_to_gs ( accz, full_scale) ,
191199 ) )
192200 }
193201
@@ -196,9 +204,9 @@ impl<I2C: embedded_hal_async::i2c::I2c> Lis2dw12<I2C> {
196204 let full_scale = self . full_scale_range ( ) . await ?;
197205 let ( accx, accy, accz) = self . acc ( ) . await ?;
198206 Ok ( (
199- Lis2dw12 :: < I2C > :: convert_acc_to_mgs ( accx, full_scale) ,
200- Lis2dw12 :: < I2C > :: convert_acc_to_mgs ( accy, full_scale) ,
201- Lis2dw12 :: < I2C > :: convert_acc_to_mgs ( accz, full_scale) ,
207+ Lis2dw12 :: < I2C , DELAY > :: convert_acc_to_mgs ( accx, full_scale) ,
208+ Lis2dw12 :: < I2C , DELAY > :: convert_acc_to_mgs ( accy, full_scale) ,
209+ Lis2dw12 :: < I2C , DELAY > :: convert_acc_to_mgs ( accz, full_scale) ,
202210 ) )
203211 }
204212
@@ -207,9 +215,9 @@ impl<I2C: embedded_hal_async::i2c::I2c> Lis2dw12<I2C> {
207215 let full_scale = self . full_scale_range ( ) . await ?;
208216 let ( accx, accy, accz) = self . acc ( ) . await ?;
209217 Ok ( (
210- Lis2dw12 :: < I2C > :: convert_acc_to_ugs ( accx, full_scale) ,
211- Lis2dw12 :: < I2C > :: convert_acc_to_ugs ( accy, full_scale) ,
212- Lis2dw12 :: < I2C > :: convert_acc_to_ugs ( accz, full_scale) ,
218+ Lis2dw12 :: < I2C , DELAY > :: convert_acc_to_ugs ( accx, full_scale) ,
219+ Lis2dw12 :: < I2C , DELAY > :: convert_acc_to_ugs ( accy, full_scale) ,
220+ Lis2dw12 :: < I2C , DELAY > :: convert_acc_to_ugs ( accz, full_scale) ,
213221 ) )
214222 }
215223
@@ -367,7 +375,7 @@ impl<I2C: embedded_hal_async::i2c::I2c> Lis2dw12<I2C> {
367375 . await ?;
368376
369377 // 2. Record unbiased accelerometer samples
370- embassy_time :: Timer :: after_millis ( TEST_STAGE_SLEEP_MS as u64 ) . await ;
378+ self . delay . delay_ms ( TEST_STAGE_SLEEP_MS as u32 ) . await ;
371379 self . flush_samples ( ) . await ?;
372380
373381 let avg_unbiased: ( f32 , f32 , f32 ) = match self
@@ -385,7 +393,7 @@ impl<I2C: embedded_hal_async::i2c::I2c> Lis2dw12<I2C> {
385393 . set_self_test_mode ( registers:: Control3SelfTest :: PositiveSign )
386394 . await ?;
387395
388- embassy_time :: Timer :: after_millis ( TEST_STAGE_SLEEP_MS as u64 ) . await ;
396+ self . delay . delay_ms ( TEST_STAGE_SLEEP_MS as u32 ) . await ;
389397 self . flush_samples ( ) . await ?;
390398
391399 // 4. Record positive accelerometer samples
@@ -404,7 +412,7 @@ impl<I2C: embedded_hal_async::i2c::I2c> Lis2dw12<I2C> {
404412 . set_self_test_mode ( registers:: Control3SelfTest :: NegativeSign )
405413 . await ?;
406414
407- embassy_time :: Timer :: after_millis ( TEST_STAGE_SLEEP_MS as u64 ) . await ;
415+ self . delay . delay_ms ( TEST_STAGE_SLEEP_MS as u32 ) . await ;
408416 self . flush_samples ( ) . await ?;
409417
410418 // 6. Record negative self-test accelerometer samples
@@ -482,7 +490,7 @@ impl<I2C: embedded_hal_async::i2c::I2c> Lis2dw12<I2C> {
482490 avg. 2 += sample. 2 ;
483491 count += 1 ;
484492 }
485- embassy_time :: Timer :: after_millis ( sample_period_ms as u64 ) . await ;
493+ self . delay . delay_ms ( sample_period_ms as u32 ) . await ;
486494 attempts += 1 ;
487495 }
488496 if count == 0 {
@@ -551,6 +559,7 @@ impl<I2C: embedded_hal_async::i2c::I2c> Lis2dw12<I2C> {
551559#[ cfg( test) ]
552560mod tests {
553561 use crate :: { Lis2dw12 , Register } ;
562+ use embedded_hal_mock:: eh1:: delay:: StdSleep ;
554563 use embedded_hal_mock:: eh1:: i2c:: { Mock , Transaction } ;
555564 const SA0_GND_ADDR : u8 = 0x18 ;
556565
@@ -565,7 +574,9 @@ mod tests {
565574 vec![ wud_reg, ff_reg] ,
566575 ) ] ;
567576 let i2c = Mock :: new ( & expectations) ;
568- let mut accel = Lis2dw12 :: new_with_sa0_gnd ( i2c) ;
577+ let delay = StdSleep :: new ( ) ;
578+ let mut accel: Lis2dw12 < embedded_hal_mock:: common:: Generic < Transaction > , StdSleep > =
579+ Lis2dw12 :: new_with_sa0_gnd ( i2c, delay) ;
569580 let ff_dur: u8 = accel. free_fall_duration ( ) . await . unwrap ( ) ;
570581
571582 // Verify the stitched value
0 commit comments