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
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use crate::receiver::{
    ConstDecodeStateMachine, DecoderState, DefaultInput, Error, Event, PinInput, Poll, Status,
};

#[cfg(feature = "embedded-hal")]
use embedded_hal::digital::v2::InputPin;

/// # ConstReceiver
///
///```
/// use infrared::{
///     receiver::Builder,
/// };
///
/// let recv = Builder::new()
///     .rc6()
///     .event_driven()
///     .build_const::<40_000>();
/// ```
///
pub struct ConstReceiver<SM, MD, IN, const R: u32>
where
    SM: ConstDecodeStateMachine<R>,
{
    state: SM::State,
    data: MD,
    input: IN,
}

impl<SM, MD, IN, const R: u32> ConstReceiver<SM, MD, IN, R>
where
    SM: ConstDecodeStateMachine<R>,
    MD: Default,
{
    pub fn with_input(input: IN) -> Self {
        ConstReceiver {
            state: SM::state(),
            data: MD::default(),
            input,
        }
    }
}

#[cfg(feature = "embedded-hal")]
impl<SM, MD, PIN, const R: u32> ConstReceiver<SM, MD, PinInput<PIN>, R>
where
    SM: ConstDecodeStateMachine<R>,
    MD: Default,
    PIN: InputPin,
{
    pub fn with_pin(pin: PIN) -> ConstReceiver<SM, MD, PinInput<PIN>, R> {
        ConstReceiver {
            state: SM::state(),
            data: MD::default(),
            input: PinInput(pin),
        }
    }
}

#[cfg(feature = "embedded-hal")]
impl<SM, PIN, const R: u32> ConstReceiver<SM, Event, PinInput<PIN>, R>
where
    SM: ConstDecodeStateMachine<R>,
    PIN: InputPin,
{
    #[inline]
    pub fn event(&mut self, dt: u32) -> Result<Option<SM::Cmd>, Error<PIN::Error>> {
        let edge = self.input.0.is_low().map_err(Error::Hal)?;
        let state: Status = SM::event(&mut self.state, dt, edge).into();

        match state {
            Status::Done => {
                let cmd = SM::command(&self.state);
                self.state.reset();
                Ok(cmd)
            }
            Status::Error(err) => {
                self.state.reset();
                Err(err.into())
            }
            Status::Idle | Status::Receiving => Ok(None),
        }
    }
    pub fn pin(&mut self) -> &mut PIN {
        &mut self.input.0
    }

    pub fn release(self) -> PIN {
        self.input.0
    }
}

impl<SM, const R: u32> ConstReceiver<SM, Event, DefaultInput, R>
where
    SM: ConstDecodeStateMachine<R>,
{
    #[inline]
    pub fn event(&mut self, dt: u32, edge: bool) -> Option<()> {
        SM::event(&mut self.state, dt, edge);
        None
    }
}

#[cfg(feature = "embedded-hal")]
impl<SM, PIN, const R: u32> ConstReceiver<SM, Poll, PinInput<PIN>, R>
where
    SM: ConstDecodeStateMachine<R>,
    PIN: InputPin,
{
    #[inline]
    pub fn poll(&mut self) -> Result<(), PIN::Error> {
        let edge = self.input.0.is_low()?;
        self.data.clock += 1;
        SM::event(&mut self.state, self.data.clock, edge);
        Ok(())
    }
}