1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use crate::receiver::DecodingError;
use crate::Protocol;

/// Protocol decode state machine
pub trait DecoderStateMachine: Protocol {
    /// Decoder state
    type State: DecoderState;
    /// The pulsewidth ranges
    type RangeData;
    /// Internal State
    type InternalStatus: Into<Status>;

    /// Create the resources
    fn state() -> Self::State;

    /// Create the timer dependent ranges
    /// `resolution`: Timer resolution
    fn ranges(resolution: usize) -> Self::RangeData;

    /// Notify the state machine of a new event
    /// * `edge`: true = positive edge, false = negative edge
    /// * `dt` : Time in micro seconds since last transition
    fn event_full(
        res: &mut Self::State,
        rd: &Self::RangeData,
        edge: bool,
        delta_t: usize,
    ) -> Self::InternalStatus;

    /// Get the command
    /// Returns the data if State == Done, otherwise None
    fn command(state: &Self::State) -> Option<Self::Cmd>;
}

pub trait ConstDecodeStateMachine<const R: usize>: DecoderStateMachine {
    const RANGES: Self::RangeData;

    fn event(res: &mut Self::State, delta_samples: usize, edge: bool) -> Self::InternalStatus {
        Self::event_full(res, &Self::RANGES, edge, delta_samples)
    }
}

pub trait DecoderState {
    fn reset(&mut self);
}

#[derive(PartialEq, Eq, Copy, Clone)]
/// Protocol decoder status
pub enum Status {
    /// Idle
    Idle,
    /// Receiving data
    Receiving,
    /// Command successfully decoded
    Done,
    /// Error while decoding
    Error(DecodingError),
}