bme68x_rust/
interface.rs

1use crate::bme68x::*;
2
3/// BME68X Errors
4#[derive(Debug)]
5pub enum Error {
6    /// Null pointer
7    NullPointer,
8    /// Communication failure
9    CommunicationFailure,
10    /// Incorrect length parameter
11    IncorrectLengthParameter,
12    /// Device not found
13    DeviceNotFound,
14    /// Self test error
15    SelfTestError,
16    /// No new data found
17    NoNewDataFound,
18    /// Unknown error code
19    Unknown,
20}
21
22pub fn check_rslt(rslt: i8) -> Result<(), Error> {
23    match rslt {
24        0 => Ok(()),
25        -1 => Err(Error::NullPointer),
26        -2 => Err(Error::CommunicationFailure),
27        -3 => Err(Error::DeviceNotFound),
28        -4 => Err(Error::IncorrectLengthParameter),
29        -5 => Err(Error::SelfTestError),
30        2 => Err(Error::NoNewDataFound),
31        _ => Err(Error::Unknown),
32    }
33}
34
35pub trait Interface {
36    /// Communication to the bme68x device occurs over SPI or I2C.
37    fn interface_type(&self) -> CommInterface;
38
39    /// Function for reading the sensor's registers through SPI bus.
40    fn read(&self, _reg_addr: u8, _reg_data: &mut [u8]) -> Result<(), Error>;
41
42    /// Function for writing the sensor's registers through SPI bus.
43    fn write(&self, _reg_addr: u8, _reg_data: &[u8]) -> Result<(), Error>;
44
45    /// Function for delaying in Microseconds.
46    fn delay(&self, _us: u32);
47
48    #[doc(hidden)]
49    unsafe fn read_raw(&self, reg_addr: u8, reg_data: *mut u8, len: u32) -> i8 {
50        let reg_slice: &mut [u8] =
51            &mut *core::ptr::slice_from_raw_parts_mut(reg_data, len as usize);
52        if self.read(reg_addr, reg_slice).is_ok() {
53            0
54        } else {
55            -1
56        }
57    }
58
59    #[doc(hidden)]
60    unsafe fn write_raw(&self, reg_addr: u8, reg_data: *const u8, len: u32) -> i8 {
61        let reg_slice: &[u8] = &*core::ptr::slice_from_raw_parts(reg_data, len as usize);
62        if self.write(reg_addr, reg_slice).is_ok() {
63            0
64        } else {
65            -1
66        }
67    }
68}