use crate::data::{FifoFrameFormat, Scales};
use crate::registers::{
AccelOdr, AccelRange, Ctrl2, Ctrl3, Ctrl5, Ctrl7, FifoMode, FifoSize, GyroRange, LpfMode, Odr,
};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Config {
pub accel: Option<AccelConfig>,
pub gyro: Option<GyroConfig>,
pub sync_sample: bool,
}
impl Default for Config {
fn default() -> Self {
Self {
accel: Some(AccelConfig::default()),
gyro: Some(GyroConfig::default()),
sync_sample: false,
}
}
}
impl Config {
#[must_use]
pub const fn new() -> Self {
Self {
accel: Some(AccelConfig::default_const()),
gyro: Some(GyroConfig::default_const()),
sync_sample: false,
}
}
#[must_use]
pub const fn with_accel(mut self, accel: AccelConfig) -> Self {
self.accel = Some(accel);
self
}
#[must_use]
pub const fn without_accel(mut self) -> Self {
self.accel = None;
self
}
#[must_use]
pub const fn with_gyro(mut self, gyro: GyroConfig) -> Self {
self.gyro = Some(gyro);
self
}
#[must_use]
pub const fn without_gyro(mut self) -> Self {
self.gyro = None;
self
}
#[must_use]
pub const fn with_sync_sample(mut self, enabled: bool) -> Self {
self.sync_sample = enabled;
self
}
pub(crate) const fn validate(&self, current_fifo_mode: FifoMode) -> Result<(), &'static str> {
if self.accel.is_none() && self.gyro.is_none() {
return Err("at least one sensor must be enabled");
}
if self.gyro.is_some() {
if let Some(accel) = &self.accel {
if !accel.odr.is_six_dof() {
return Err(
"accelerometer standalone/low-power ODR requires the gyroscope disabled",
);
}
}
}
if self.sync_sample && !matches!(current_fifo_mode, FifoMode::Bypass) {
return Err("SyncSample mode requires the FIFO bypassed; bypass the FIFO first");
}
Ok(())
}
pub(crate) const fn ctrl5(&self) -> Ctrl5 {
let mut ctrl5 = Ctrl5 {
gyro_lpf_mode: LpfMode::Pct2_66,
gyro_lpf_en: false,
accel_lpf_mode: LpfMode::Pct2_66,
accel_lpf_en: false,
};
if let Some(accel) = &self.accel {
if let Some(mode) = accel.lpf {
ctrl5.accel_lpf_mode = mode;
ctrl5.accel_lpf_en = true;
}
}
if let Some(gyro) = &self.gyro {
if let Some(mode) = gyro.lpf {
ctrl5.gyro_lpf_mode = mode;
ctrl5.gyro_lpf_en = true;
}
}
ctrl5
}
pub(crate) const fn ctrl7(&self) -> Ctrl7 {
Ctrl7 {
sync_sample: self.sync_sample,
drdy_dis: false,
gyro_snooze: false,
gyro_en: self.gyro.is_some(),
accel_en: self.accel.is_some(),
}
}
pub(crate) const fn scales(&self) -> Scales {
Scales {
accel: match &self.accel {
Some(a) => a.range,
None => AccelRange::G4,
},
gyro: match &self.gyro {
Some(g) => g.range,
None => GyroRange::Dps512,
},
}
}
pub(crate) const fn fifo_format(&self) -> FifoFrameFormat {
match (self.accel.is_some(), self.gyro.is_some()) {
(true, true) => FifoFrameFormat::AccelAndGyro,
(false, true) => FifoFrameFormat::GyroOnly,
_ => FifoFrameFormat::AccelOnly,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct AccelConfig {
pub range: AccelRange,
pub odr: AccelOdr,
pub lpf: Option<LpfMode>,
}
impl AccelConfig {
pub(crate) const fn default_const() -> Self {
Self {
range: AccelRange::G4,
odr: AccelOdr::SixDof(Odr::Hz224),
lpf: None,
}
}
#[must_use]
pub const fn new(range: AccelRange, odr: AccelOdr) -> Self {
Self {
range,
odr,
lpf: None,
}
}
#[must_use]
pub const fn with_lpf(mut self, mode: LpfMode) -> Self {
self.lpf = Some(mode);
self
}
#[must_use]
pub const fn without_lpf(mut self) -> Self {
self.lpf = None;
self
}
pub(crate) const fn ctrl2(&self) -> Ctrl2 {
Ctrl2 {
self_test: false,
range: self.range,
odr_bits: self.odr.bits(),
}
}
}
impl Default for AccelConfig {
fn default() -> Self {
Self::default_const()
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct GyroConfig {
pub range: GyroRange,
pub odr: Odr,
pub lpf: Option<LpfMode>,
}
impl GyroConfig {
pub(crate) const fn default_const() -> Self {
Self {
range: GyroRange::Dps512,
odr: Odr::Hz224,
lpf: None,
}
}
#[must_use]
pub const fn new(range: GyroRange, odr: Odr) -> Self {
Self {
range,
odr,
lpf: None,
}
}
#[must_use]
pub const fn with_lpf(mut self, mode: LpfMode) -> Self {
self.lpf = Some(mode);
self
}
#[must_use]
pub const fn without_lpf(mut self) -> Self {
self.lpf = None;
self
}
pub(crate) const fn ctrl3(&self) -> Ctrl3 {
Ctrl3 {
self_test: false,
range: self.range,
odr: self.odr,
}
}
}
impl Default for GyroConfig {
fn default() -> Self {
Self::default_const()
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct FifoConfig {
pub mode: FifoMode,
pub size: FifoSize,
pub watermark: u8,
}
impl Default for FifoConfig {
fn default() -> Self {
Self {
mode: FifoMode::Stream,
size: FifoSize::Samples128,
watermark: 1,
}
}
}
impl FifoConfig {
#[must_use]
pub const fn new() -> Self {
Self {
mode: FifoMode::Stream,
size: FifoSize::Samples128,
watermark: 1,
}
}
#[must_use]
pub const fn with_mode(mut self, mode: FifoMode) -> Self {
self.mode = mode;
self
}
#[must_use]
pub const fn with_size(mut self, size: FifoSize) -> Self {
self.size = size;
self
}
#[must_use]
pub const fn with_watermark(mut self, watermark: u8) -> Self {
self.watermark = watermark;
self
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct InterruptConfig {
pub int1_enable: bool,
pub int2_enable: bool,
pub fifo_interrupt_on_int1: bool,
pub activity_interrupt_on_int1: bool,
pub ctrl9_handshake_on_int1: bool,
pub data_ready_enable: bool,
}
impl Default for InterruptConfig {
fn default() -> Self {
Self {
int1_enable: true,
int2_enable: true,
fifo_interrupt_on_int1: false,
activity_interrupt_on_int1: false,
ctrl9_handshake_on_int1: false,
data_ready_enable: true,
}
}
}
impl InterruptConfig {
#[must_use]
pub const fn new() -> Self {
Self {
int1_enable: true,
int2_enable: true,
fifo_interrupt_on_int1: false,
activity_interrupt_on_int1: false,
ctrl9_handshake_on_int1: false,
data_ready_enable: true,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum TapPriority {
#[default]
Xyz = 0,
Xzy = 1,
Yxz = 2,
Yzx = 3,
Zxy = 4,
Zyx = 5,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct TapConfig {
pub priority: TapPriority,
pub peak_window: u8,
pub tap_window: u16,
pub double_tap_window: u16,
pub alpha: u8,
pub gamma: u8,
pub peak_mag_thr: u16,
pub udm_thr: u16,
}
impl Default for TapConfig {
fn default() -> Self {
Self {
priority: TapPriority::Xyz,
peak_window: 20,
tap_window: 50,
double_tap_window: 250,
alpha: 8,
gamma: 32,
peak_mag_thr: 0x0320,
udm_thr: 0x0190,
}
}
}
impl TapConfig {
#[must_use]
pub const fn new() -> Self {
Self {
priority: TapPriority::Xyz,
peak_window: 20,
tap_window: 50,
double_tap_window: 250,
alpha: 8,
gamma: 32,
peak_mag_thr: 0x0320,
udm_thr: 0x0190,
}
}
pub(crate) const fn encode_call1(&self) -> [u8; 8] {
[
self.peak_window,
self.priority as u8,
self.tap_window.to_le_bytes()[0],
self.tap_window.to_le_bytes()[1],
self.double_tap_window.to_le_bytes()[0],
self.double_tap_window.to_le_bytes()[1],
0,
0x01,
]
}
pub(crate) const fn encode_call2(&self) -> [u8; 8] {
[
self.alpha,
self.gamma,
self.peak_mag_thr.to_le_bytes()[0],
self.peak_mag_thr.to_le_bytes()[1],
self.udm_thr.to_le_bytes()[0],
self.udm_thr.to_le_bytes()[1],
0,
0x02,
]
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct MotionEngines {
pub any_motion: bool,
pub no_motion: bool,
pub significant_motion: bool,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum AxisLogic {
#[default]
Or,
And,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct MotionConfig {
pub any_threshold: [u8; 3],
pub no_threshold: [u8; 3],
pub any_window: u8,
pub no_window: u8,
pub sig_wait_window: u16,
pub sig_confirm_window: u16,
pub any_axes: [bool; 3],
pub no_axes: [bool; 3],
pub any_logic: AxisLogic,
pub no_logic: AxisLogic,
}
impl Default for MotionConfig {
fn default() -> Self {
Self {
any_threshold: [16, 16, 16],
no_threshold: [4, 4, 4],
any_window: 5,
no_window: 50,
sig_wait_window: 100,
sig_confirm_window: 500,
any_axes: [true, true, true],
no_axes: [true, true, true],
any_logic: AxisLogic::Or,
no_logic: AxisLogic::Or,
}
}
}
impl MotionConfig {
#[must_use]
pub const fn new() -> Self {
Self {
any_threshold: [16, 16, 16],
no_threshold: [4, 4, 4],
any_window: 5,
no_window: 50,
sig_wait_window: 100,
sig_confirm_window: 500,
any_axes: [true, true, true],
no_axes: [true, true, true],
any_logic: AxisLogic::Or,
no_logic: AxisLogic::Or,
}
}
const fn mode_ctrl(&self) -> u8 {
((self.no_logic as u8) << 7)
| ((self.no_axes[2] as u8) << 6)
| ((self.no_axes[1] as u8) << 5)
| ((self.no_axes[0] as u8) << 4)
| ((self.any_logic as u8) << 3)
| ((self.any_axes[2] as u8) << 2)
| ((self.any_axes[1] as u8) << 1)
| (self.any_axes[0] as u8)
}
pub(crate) const fn encode_call1(&self) -> [u8; 8] {
[
self.any_threshold[0],
self.any_threshold[1],
self.any_threshold[2],
self.no_threshold[0],
self.no_threshold[1],
self.no_threshold[2],
self.mode_ctrl(),
0x01,
]
}
pub(crate) const fn encode_call2(&self) -> [u8; 8] {
[
self.any_window,
self.no_window,
self.sig_wait_window.to_le_bytes()[0],
self.sig_wait_window.to_le_bytes()[1],
self.sig_confirm_window.to_le_bytes()[0],
self.sig_confirm_window.to_le_bytes()[1],
0,
0x02,
]
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct PedometerConfig {
pub sample_cnt: u16,
pub fix_peak2peak: u16,
pub fix_peak: u16,
pub time_up: u16,
pub time_low: u8,
pub cnt_entry: u8,
pub fix_precision: u8,
pub sig_count: u8,
}
impl Default for PedometerConfig {
fn default() -> Self {
Self {
sample_cnt: 50,
fix_peak2peak: 0x00CC,
fix_peak: 0x0066,
time_up: 200,
time_low: 20,
cnt_entry: 10,
fix_precision: 0,
sig_count: 4,
}
}
}
impl PedometerConfig {
#[must_use]
pub const fn new() -> Self {
Self {
sample_cnt: 50,
fix_peak2peak: 0x00CC,
fix_peak: 0x0066,
time_up: 200,
time_low: 20,
cnt_entry: 10,
fix_precision: 0,
sig_count: 4,
}
}
pub(crate) const fn encode_call1(&self) -> [u8; 8] {
[
self.sample_cnt.to_le_bytes()[0],
self.sample_cnt.to_le_bytes()[1],
self.fix_peak2peak.to_le_bytes()[0],
self.fix_peak2peak.to_le_bytes()[1],
self.fix_peak.to_le_bytes()[0],
self.fix_peak.to_le_bytes()[1],
0,
0x01,
]
}
pub(crate) const fn encode_call2(&self) -> [u8; 8] {
[
self.time_up.to_le_bytes()[0],
self.time_up.to_le_bytes()[1],
self.time_low,
self.cnt_entry,
self.fix_precision,
self.sig_count,
0,
0x02,
]
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum WomInterrupt {
#[default]
Int1,
Int2,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct WomConfig {
pub threshold_mg: u8,
pub blanking_samples: u8,
pub interrupt: WomInterrupt,
pub initial_level_high: bool,
}
impl Default for WomConfig {
fn default() -> Self {
Self {
threshold_mg: 50,
blanking_samples: 0,
interrupt: WomInterrupt::Int1,
initial_level_high: false,
}
}
}
impl WomConfig {
#[must_use]
pub const fn new() -> Self {
Self {
threshold_mg: 50,
blanking_samples: 0,
interrupt: WomInterrupt::Int1,
initial_level_high: false,
}
}
pub(crate) const fn encode(&self) -> [u8; 2] {
let pin_bit = match self.interrupt {
WomInterrupt::Int1 => 0,
WomInterrupt::Int2 => 1,
};
[
self.threshold_mg,
((self.initial_level_high as u8) << 7)
| (pin_bit << 6)
| (self.blanking_samples & 0x3F),
]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn const_config_builders() {
const CFG: Config = Config::new()
.with_accel(
AccelConfig::new(AccelRange::G2, AccelOdr::SixDof(Odr::Hz112))
.with_lpf(LpfMode::Pct5_39),
)
.without_gyro()
.with_sync_sample(true);
assert_eq!(CFG.accel.map(|a| a.range), Some(AccelRange::G2));
assert_eq!(CFG.accel.and_then(|a| a.lpf), Some(LpfMode::Pct5_39));
assert!(CFG.gyro.is_none() && CFG.sync_sample);
assert_eq!(Config::default(), Config::new());
}
#[test]
fn tap_encoder_layout() {
let cfg = TapConfig {
priority: TapPriority::Zyx,
peak_window: 20,
tap_window: 50,
double_tap_window: 250,
alpha: 8,
gamma: 32,
peak_mag_thr: 0x0320,
udm_thr: 0x0190,
};
assert_eq!(cfg.encode_call1(), [20, 5, 50, 0, 250, 0, 0, 0x01]);
assert_eq!(cfg.encode_call2(), [8, 32, 0x20, 0x03, 0x90, 0x01, 0, 0x02]);
}
#[test]
fn motion_encoder_layout() {
let cfg = MotionConfig {
any_threshold: [1, 2, 3],
no_threshold: [4, 5, 6],
any_window: 7,
no_window: 8,
sig_wait_window: 0x1234,
sig_confirm_window: 0x5678,
any_axes: [true, false, true],
no_axes: [false, true, false],
any_logic: AxisLogic::And,
no_logic: AxisLogic::Or,
};
assert_eq!(cfg.encode_call1(), [1, 2, 3, 4, 5, 6, 0b0010_1101, 0x01]);
assert_eq!(cfg.encode_call2(), [7, 8, 0x34, 0x12, 0x78, 0x56, 0, 0x02]);
}
#[test]
fn pedometer_encoder_layout() {
let cfg = PedometerConfig::default();
assert_eq!(
cfg.encode_call1(),
[0x32, 0x00, 0xCC, 0x00, 0x66, 0x00, 0, 0x01]
);
assert_eq!(cfg.encode_call2(), [0xC8, 0x00, 20, 10, 0, 4, 0, 0x02]);
}
#[test]
fn wom_encoder_layout() {
let base = WomConfig {
threshold_mg: 100,
blanking_samples: 0x7F,
..WomConfig::new()
};
assert_eq!(base.encode(), [100, 0x3F]);
let int2_high = WomConfig {
interrupt: WomInterrupt::Int2,
initial_level_high: true,
..base
};
assert_eq!(int2_high.encode(), [100, 0xFF]);
let int2_low = WomConfig {
interrupt: WomInterrupt::Int2,
initial_level_high: false,
blanking_samples: 0,
..base
};
assert_eq!(int2_low.encode(), [100, 0x40]);
}
}