use crate::registers::*;
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Status {
pub fifo_threshold: bool,
pub wake_up_event: bool,
pub sleep_event: bool,
pub double_tap_event: bool,
pub single_tap_event: bool,
pub position_change_event: bool,
pub free_fall_event: bool,
pub data_ready: bool,
}
impl From<u8> for Status {
fn from(value: u8) -> Self {
Self {
fifo_threshold: value & FIFO_THS != 0,
wake_up_event: value & WU_IA != 0,
sleep_event: value & SLEEP_STATE != 0,
double_tap_event: value & DOUBLE_TAP != 0,
single_tap_event: value & SINGLE_TAP != 0,
position_change_event: value & D6D_IA != 0,
free_fall_event: value & FF_IA != 0,
data_ready: value & DRDY != 0,
}
}
}
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct EventStatus {
pub fifo_overrun: bool,
pub temperature_data_ready: bool,
pub sleep_event: bool,
pub double_tap_event: bool,
pub single_tap_event: bool,
pub position_change_event: bool,
pub free_fall_event: bool,
pub data_ready: bool,
}
impl From<u8> for EventStatus {
fn from(value: u8) -> Self {
Self {
fifo_overrun: value & OVR != 0,
temperature_data_ready: value & DRDY_T != 0,
sleep_event: value & SLEEP_STATE_IA != 0,
double_tap_event: value & DOUBLE_TAP != 0,
single_tap_event: value & SINGLE_TAP != 0,
position_change_event: value & D6D_IA != 0,
free_fall_event: value & FF_IA != 0,
data_ready: value & DRDY != 0,
}
}
}
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct AccelerationData {
pub x: f32,
pub y: f32,
pub z: f32,
}
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct RawAccelerationData {
pub x: i16,
pub y: i16,
pub z: i16,
}
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct FifoSamplesStatus {
pub threshold: bool,
pub overrun: bool,
pub samples: u8,
}
impl From<u8> for FifoSamplesStatus {
fn from(value: u8) -> Self {
Self {
threshold: value & FIFO_FTH != 0,
overrun: value & FIFO_OVR != 0,
samples: value & FIFO_DIFF,
}
}
}
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct WakeUpSource {
pub free_fall_event: bool,
pub sleep_event: bool,
pub wake_up_event: bool,
pub x_wake_up_event: bool,
pub y_wake_up_event: bool,
pub z_wake_up_event: bool,
}
impl From<u8> for WakeUpSource {
fn from(value: u8) -> Self {
Self {
free_fall_event: value & WAKE_UP_FF_IA != 0,
sleep_event: value & WAKE_UP_SLEEP_STATE_IA != 0,
wake_up_event: value & WAKE_UP_WU_IA != 0,
x_wake_up_event: value & X_WU != 0,
y_wake_up_event: value & Y_WU != 0,
z_wake_up_event: value & Z_WU != 0,
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Sign {
Positive,
Negative,
}
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct TapSource {
pub tap_event: bool,
pub single_tap_event: bool,
pub double_tap_event: bool,
pub tap_sign: Sign,
pub x_tap_event: bool,
pub y_tap_event: bool,
pub z_tap_event: bool,
}
impl From<u8> for TapSource {
fn from(value: u8) -> Self {
Self {
tap_event: value & TAP_IA != 0,
single_tap_event: value & TAP_SRC_SINGLE_TAP != 0,
double_tap_event: value & TAP_SRC_DOUBLE_TAP != 0,
tap_sign: if value & TAP_SIGN == 0 {
Sign::Positive
} else {
Sign::Negative
},
x_tap_event: value & X_TAP != 0,
y_tap_event: value & Y_TAP != 0,
z_tap_event: value & Z_TAP != 0,
}
}
}
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct SixDSource {
pub position_change_event: bool,
pub zh_over_threshold: bool,
pub zl_over_threshold: bool,
pub yh_over_threshold: bool,
pub yl_over_threshold: bool,
pub xh_over_threshold: bool,
pub xl_over_threshold: bool,
}
impl From<u8> for SixDSource {
fn from(value: u8) -> Self {
Self {
position_change_event: value & IA_6D != 0,
zh_over_threshold: value & ZH != 0,
zl_over_threshold: value & ZL != 0,
yh_over_threshold: value & YH != 0,
yl_over_threshold: value & YL != 0,
xh_over_threshold: value & XH != 0,
xl_over_threshold: value & XL != 0,
}
}
}
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct AllInterruptSources {
pub sleep_change_interrupt: bool,
pub six_d_interrupt: bool,
pub double_tap_interrupt: bool,
pub single_tap_interrupt: bool,
pub wake_up_interrupt: bool,
pub free_fall_interrupt: bool,
}
impl From<u8> for AllInterruptSources {
fn from(value: u8) -> Self {
Self {
sleep_change_interrupt: value & ALL_INT_SLEEP_CHANGE_IA != 0,
six_d_interrupt: value & ALL_INT_6D_IA != 0,
double_tap_interrupt: value & ALL_INT_DOUBLE_TAP != 0,
single_tap_interrupt: value & ALL_INT_SINGLE_TAP != 0,
wake_up_interrupt: value & ALL_INT_WU_IA != 0,
free_fall_interrupt: value & ALL_INT_FF_IA != 0,
}
}
}
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct AllSources {
pub event_status: EventStatus,
pub wake_up_source: WakeUpSource,
pub tap_source: TapSource,
pub six_d_source: SixDSource,
pub all_interrupt_sources: AllInterruptSources,
}
impl From<[u8; 5]> for AllSources {
fn from(value: [u8; 5]) -> Self {
Self {
event_status: EventStatus::from(value[0]),
wake_up_source: WakeUpSource::from(value[1]),
tap_source: TapSource::from(value[2]),
six_d_source: SixDSource::from(value[3]),
all_interrupt_sources: AllInterruptSources::from(value[4]),
}
}
}