1use crate::bme68x::*;
2
3#[derive(Debug)]
5pub enum Error {
6 NullPointer,
8 CommunicationFailure,
10 IncorrectLengthParameter,
12 DeviceNotFound,
14 SelfTestError,
16 NoNewDataFound,
18 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 fn interface_type(&self) -> CommInterface;
38
39 fn read(&self, _reg_addr: u8, _reg_data: &mut [u8]) -> Result<(), Error>;
41
42 fn write(&self, _reg_addr: u8, _reg_data: &[u8]) -> Result<(), Error>;
44
45 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}