mlx90393 0.1.2

MLX90393 magnetometer driver
Documentation
// no_std support
#[allow(unused_imports)]
#[allow(dead_code)]
// use libm::{exp, round, trunc};
use log::debug;
use bitfield::bitfield;

#[allow(unused_imports)] // for no_std use


/// A raw (non-scaled) measurement result from the sensor.
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct RawMeasurements {
    /// temperature
    pub t: u16,
    /// x raw field strength
    pub x: u16,
    /// y raw field strength
    pub y: u16,
    /// z raw field strength
    pub z: u16,
}

/// ScaleMeasurements after converting from RawMeasurements
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ScaledMeasurements {
    /// temperature C
    pub t: f32,
    /// x axis field strength in uT
    pub x: f32,
    /// y axis field strength in uT 
    pub y: f32,
    /// z axis field strength in uT 
    pub z: f32,
}


/// Measurement Mode
#[derive(Debug, PartialEq, Clone, Copy, Default)]
#[repr(u8)]
pub enum MeasurementMode {
    Burst =  0x00,
    WakeOnChange = 0x01,
    #[default]  // default power on mode
    Single = 0x03,
}

impl From<u8> for MeasurementMode {
    fn from(v: u8) -> MeasurementMode {
        match v {
            0x00 => Self::Burst,
            0x01 => Self::WakeOnChange,
            0x03 => Self::Single,
            _    => unreachable!(),
        }
    }
}



bitfield! {
    /// STATUS bits
    pub struct Status(u8);
    impl Debug;

    pub bool, get_burst_mode, _: 7;    // burst mode when == 1
    pub bool, get_woc_mode, _: 6;      // wake on change mode when == 1
    pub bool, get_single_mode, _: 5;   // single measure mode when == 1
    pub bool, get_error, _: 4;         // command error or uncorrectable memory error
    pub bool, get_sed, _: 3;           // single bit error detected
    pub bool, get_rs, _: 2;            // caused by reset
    pub u8, get_data_length, _: 1,0;  // data length in words (2 bytes) + 2 for 

}

/// x, y and z resolution.
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Resolution {
    pub x: u8,
    pub y: u8,
    pub z: u8,
}

/// x, y, z offset
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct XYZOffset {
    pub x: u16,
    pub y: u16,
    pub z: u16,
}

bitfield! {
    /// register 0x02:  resolution, digital filter, OSR, OSR
    pub struct Register_0x02(u16);
    impl Debug;
    pub u8, get_OSR2, set_OSR2: 12,11;          // OSR2
    pub u8, get_z_res, set_z_res: 10, 9;        // RES_Z
    pub u8, get_y_res, set_y_res: 8, 7;         // RES_Y
    pub u8, get_x_res, set_x_res: 6, 5;         // RES_X
    pub u8, get_dig_filt, set_dig_filt: 4,2;    // digital filter value
    pub u8, get_OSR, set_OSR: 1,0;              // OSR
}

bitfield! {
    /// register 0x01:  Burst_self, Burst_data_rate, trig_int, comm_mode, WOC_DIFF, ext_trg, tcmp_en, burst_sel_again
    pub struct Register_0x01(u16);
    impl Debug;
    pub bool, get_trig_int, set_trig_int: 15;       // TRIG_INT
    pub u8,   get_comm_mode, set_comm_mode: 14,13;  // COMM_MODE
    pub bool, get_woc_diff, set_woc_diff: 12;       // WOC_DIFF
    pub bool, get_ext_trg, set_ext_trig: 11;        // EXT_TRIG
    pub bool, get_tcmp_en, set_tcmp_en: 10;         // TCMP_EN
    pub u8,   get_burst_sel2, set_burst_sel2: 9,8;  // BURST_SEL again?
    pub u8,   get_burst_sel, set_burst_sel: 7,6;    // BURST_SEL
    pub u8,   get_burst_data_rate, set_burst_data_rate: 5, 0;  // BURST DATA RATE * 20 msec
}

bitfield! {
    /// register 0x00:  Z-Series, GAIN_Sel, HallConf, ANA_RESERVED_LOW and BIST
    pub struct Register_0x00(u16);
    impl Debug;
    pub bool,  get_z_series, set_z_series: 7;     // Z-SERIES
    pub u8,    get_gain_sel, set_gain_sel: 6,4;   // GAIN_SEL
    pub u8,    get_hallconf, set_hallconf: 3, 0;  // HALLCONF
    pub u8,    get_ana_reserved_low, set_ana_reserved_low: 15, 9; // ANA_RESERVED_LOW
    pub bool,  get_bist,     set_bist: 8;         // BIST
}