lis2dw12/
lib.rs

1#![no_std]
2
3mod reg;
4pub use crate::reg::*;
5
6#[cfg(feature = "non_blocking")]
7mod non_blocking;
8
9#[cfg(feature = "non_blocking")]
10pub use non_blocking::*;
11
12#[cfg(not(feature = "non_blocking"))]
13mod blocking;
14
15#[cfg(not(feature = "non_blocking"))]
16pub use blocking::*;
17
18use core::fmt::Debug;
19
20#[cfg(feature = "out_f32")]
21pub use accelerometer::{vector::F32x3, Accelerometer};
22
23#[derive(Debug)]
24pub enum Error<SpiError, PinError> {
25    /// SPI communication error
26    Spi(SpiError),
27    /// CS output pin error
28    Pin(PinError),
29    InvalidWhoAmI(u8),
30}
31
32impl<SpiError, PinError> From<SpiError> for Error<SpiError, PinError> {
33    fn from(err: SpiError) -> Self {
34        Self::Spi(err)
35    }
36}
37
38pub struct Lis2dw12<SPI, CS> {
39    spi: SPI,
40    cs: CS,
41    #[cfg(feature = "out_f32")]
42    scale: FullScaleSelection,
43    #[cfg(feature = "out_f32")]
44    #[cfg(not(feature = "non_blocking"))]
45    operating_mode: OperatingMode,
46}
47
48impl<SPI, CS> Lis2dw12<SPI, CS> {
49    pub fn new(spi: SPI, cs: CS) -> Self {
50        Self {
51            spi,
52            cs,
53            #[cfg(feature = "out_f32")]
54            scale: FullScaleSelection::PlusMinus2G,
55            #[cfg(feature = "out_f32")]
56            #[cfg(not(feature = "non_blocking"))]
57            operating_mode: OperatingMode::LowPower,
58        }
59    }
60
61    // destroy the instance and return the spi bus and its cs pin
62    pub fn destroy(self) -> (SPI, CS) {
63        (self.spi, self.cs)
64    }
65}