use crate::error::Result;
use crate::interface::Adxl372Interface;
use crate::params::{FifoFormat, FifoMode};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Sample {
pub x: Option<i16>,
pub y: Option<i16>,
pub z: Option<i16>,
pub is_peak: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FifoSettings {
pub watermark: u16,
pub mode: FifoMode,
pub format: FifoFormat,
}
impl FifoSettings {
pub const fn new(watermark: u16, mode: FifoMode, format: FifoFormat) -> Self {
Self {
watermark,
mode,
format,
}
}
}
impl Default for Sample {
fn default() -> Self {
Self {
x: None,
y: None,
z: None,
is_peak: false,
}
}
}
pub fn read_fifo_raw<IFACE>(interface: &mut IFACE, buf: &mut [u8]) -> Result<usize, IFACE::Error>
where
IFACE: Adxl372Interface,
{
if buf.is_empty() {
return Ok(0);
}
interface.read_many(crate::registers::REG_STATUS, buf)?;
Ok(buf.len())
}
pub fn read_fifo_samples<IFACE>(
interface: &mut IFACE,
samples: &mut [Sample],
) -> Result<usize, IFACE::Error>
where
IFACE: Adxl372Interface,
{
let mut raw = [0u8; 6];
let mut count = 0;
for sample in samples.iter_mut() {
let bytes_used = read_fifo_raw(interface, &mut raw)?;
if bytes_used < 2 {
break;
}
sample.x = Some(i16::from_be_bytes([raw[0], raw[1]]));
sample.y = Some(i16::from_be_bytes([raw[2], raw[3]]));
sample.z = Some(i16::from_be_bytes([raw[4], raw[5]]));
sample.is_peak = false;
count += 1;
}
Ok(count)
}