use crate::fsm_engine::Fsm;
use crate::key::types::ButtonPhase;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum State {
Released,
PressCandidate,
Pressed,
ReleaseCandidate,
}
impl State {
pub(crate) const fn stable_pressed(self) -> bool {
matches!(self, Self::Pressed | Self::ReleaseCandidate)
}
pub(crate) const fn candidate_phase(self) -> Option<ButtonPhase> {
match self {
Self::PressCandidate => Some(ButtonPhase::Down),
Self::ReleaseCandidate => Some(ButtonPhase::Up),
Self::Released | Self::Pressed => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum Event {
SampleReleased,
SamplePressed,
CandidateStable,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Motion {
None,
ArmCandidate,
CancelCandidate,
Confirm(ButtonPhase),
}
pub(crate) fn new(initially_pressed: bool) -> Fsm<State, Event, Motion> {
use Event::{CandidateStable, SamplePressed, SampleReleased};
use Motion::{ArmCandidate, CancelCandidate, Confirm, None};
use State::{PressCandidate, Pressed, ReleaseCandidate, Released};
Fsm::builder()
.initial_state(if initially_pressed { Pressed } else { Released })
.on(Released, SampleReleased, Released, None)
.on(Released, SamplePressed, PressCandidate, ArmCandidate)
.on(PressCandidate, SamplePressed, PressCandidate, None)
.on(PressCandidate, SampleReleased, Released, CancelCandidate)
.on(
PressCandidate,
CandidateStable,
Pressed,
Confirm(ButtonPhase::Down),
)
.on(Pressed, SamplePressed, Pressed, None)
.on(Pressed, SampleReleased, ReleaseCandidate, ArmCandidate)
.on(ReleaseCandidate, SampleReleased, ReleaseCandidate, None)
.on(ReleaseCandidate, SamplePressed, Pressed, CancelCandidate)
.on(
ReleaseCandidate,
CandidateStable,
Released,
Confirm(ButtonPhase::Up),
)
.build()
.expect("the debounce transition table is valid")
}