tof_control/rb_control/
rb_mag.rs

1use crate::constant::*;
2use crate::helper::rb_type::{RBMag, RBError};
3use crate::device::{lis3mdltr, pca9548a};
4
5impl RBMag {
6    pub fn new() -> Self {
7        match Self::read_mag() {
8            Ok(rb_mag) => {
9                rb_mag
10            }
11            Err(_) => {
12                Self {
13                    mag_xyz: [f32::MAX; 3],
14                }
15            }
16        }
17    }
18    pub fn read_mag() -> Result<RBMag, RBError> {
19        let i2c_mux = pca9548a::PCA9548A::new(I2C_BUS, RB_PCA9548A_ADDRESS_1);
20        i2c_mux.select(RB_LIS3MDLTR_CHANNEL)?;
21
22        let lis3mdltr = lis3mdltr::LIS3MDLTR::new(I2C_BUS, RB_LIS3MDLTR_ADDRESS);
23        lis3mdltr.configure()?;
24        let mag_xyz = lis3mdltr.read_mag()?;
25
26        i2c_mux.reset()?;
27
28        Ok(
29            RBMag {
30                mag_xyz,
31            }
32        )
33    }
34}
35
36pub fn config_mag() -> Result<(), RBError> {
37    let i2c_mux = pca9548a::PCA9548A::new(I2C_BUS, RB_PCA9548A_ADDRESS_1);
38    i2c_mux.select(RB_LIS3MDLTR_CHANNEL)?;
39    let lis3mdltr = lis3mdltr::LIS3MDLTR::new(I2C_BUS, RB_LIS3MDLTR_ADDRESS);
40    lis3mdltr.configure()?;
41
42    i2c_mux.reset()?;
43
44    Ok(())
45}