98 lines
3.2 KiB
Rust
98 lines
3.2 KiB
Rust
use crate::Gain;
|
|
use thiserror::Error;
|
|
|
|
/// Observable state of one non-motorized absolute control.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum PickupState {
|
|
/// No live target is available, such as while offline or disconnected.
|
|
Unavailable,
|
|
/// Input is inhibited until the physical position reaches the live target.
|
|
Waiting,
|
|
/// Physical movement is allowed to emit volume commands.
|
|
Tracking,
|
|
}
|
|
|
|
/// Invalid pickup tolerance.
|
|
#[derive(Clone, Copy, Debug, Error, PartialEq)]
|
|
#[error("pickup tolerance {0} must be finite and within 0..=1")]
|
|
pub struct PickupError(f64);
|
|
|
|
/// Pickup state for one non-motorized absolute control.
|
|
///
|
|
/// Arming deliberately forgets the prior physical sample. This prevents an
|
|
/// external change, reconnect, rebind, or another surface's write from using a
|
|
/// stale position to cross a new target.
|
|
#[derive(Clone, Debug)]
|
|
pub struct Pickup {
|
|
tolerance: f64,
|
|
target: Option<Gain>,
|
|
previous: Option<Gain>,
|
|
state: PickupState,
|
|
}
|
|
|
|
impl Pickup {
|
|
/// Constructs pickup with a normalized crossing tolerance.
|
|
pub fn new(tolerance: f64) -> Result<Self, PickupError> {
|
|
if !tolerance.is_finite() || !(0.0..=1.0).contains(&tolerance) {
|
|
return Err(PickupError(tolerance));
|
|
}
|
|
Ok(Self {
|
|
tolerance,
|
|
target: None,
|
|
previous: None,
|
|
state: PickupState::Unavailable,
|
|
})
|
|
}
|
|
|
|
/// Arms against the newest observed target without emitting a command.
|
|
/// Targets above the physical range are reachable at its upper endpoint.
|
|
pub fn arm(&mut self, observed: Gain) {
|
|
self.target = Some(if observed > Gain::UNITY {
|
|
Gain::UNITY
|
|
} else {
|
|
observed
|
|
});
|
|
self.previous = None;
|
|
self.state = PickupState::Waiting;
|
|
}
|
|
|
|
/// Makes input unavailable and discards all physical history.
|
|
pub fn disconnect(&mut self) {
|
|
self.target = None;
|
|
self.previous = None;
|
|
self.state = PickupState::Unavailable;
|
|
}
|
|
|
|
/// Processes one normalized physical position.
|
|
///
|
|
/// Returns a command value only after entering the tolerance window or
|
|
/// crossing the target between two consecutive samples.
|
|
pub fn input(&mut self, position: Gain) -> Option<Gain> {
|
|
match self.state {
|
|
PickupState::Unavailable => None,
|
|
PickupState::Tracking => Some(position),
|
|
PickupState::Waiting => {
|
|
let target = self.target?;
|
|
let within = (position.get() - target.get()).abs() <= self.tolerance;
|
|
let crossed = self.previous.is_some_and(|previous| {
|
|
(previous < target && position > target)
|
|
|| (previous > target && position < target)
|
|
});
|
|
self.previous = Some(position);
|
|
if within || crossed {
|
|
self.state = PickupState::Tracking;
|
|
Some(position)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Current pickup state for TUI and feedback views.
|
|
#[must_use]
|
|
pub const fn state(&self) -> PickupState {
|
|
self.state
|
|
}
|
|
}
|