use crate::{
orientation::Orientation,
vector::{Component, Vector, VectorExt},
};
use micromath::generic_array::typenum::U3;
#[allow(unused_imports)]
use crate::accelerometer::Accelerometer;
#[allow(unused_imports)]
use micromath::F32Ext;
pub struct Tracker {
threshold: f32,
last_orientation: Orientation,
}
impl Tracker {
pub fn new(threshold: impl Into<f32>) -> Self {
Self {
threshold: threshold.into(),
last_orientation: Orientation::Unknown,
}
}
pub fn update<V, C>(&mut self, acceleration: V) -> Orientation
where
V: Vector<Axes = U3, Component = C> + VectorExt,
C: Component + Into<f32>,
{
let components = acceleration.to_array();
let x: f32 = components[0].into();
let y: f32 = components[1].into();
let z: f32 = components[2].into();
let result = if x.abs() > self.threshold {
if x >= 0.0 {
Orientation::LandscapeUp
} else {
Orientation::LandscapeDown
}
} else if y.abs() > self.threshold {
if y >= 0.0 {
Orientation::PortraitUp
} else {
Orientation::PortraitDown
}
} else if z.abs() > self.threshold {
if z <= 0.0 {
Orientation::FaceUp
} else {
Orientation::FaceDown
}
} else {
Orientation::Unknown
};
if result != Orientation::Unknown {
self.last_orientation = result;
}
result
}
pub fn orientation(&self) -> Orientation {
self.last_orientation
}
}