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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use core::ops::Range;

use crate::nec::Pulsedistance;
use crate::{Receiver, ReceiverState, ProtocolId};
#[cfg(feature = "protocol-dev")]
use crate::ReceiverDebug;
use crate::protocols::nec::{NecTypeTrait, NecCommand};


#[derive(Debug, Clone, Copy)]
/// Error when receiving
pub enum NecError {
    /// Couldn't determine the type of message
    CommandType(u32),
    /// Receiving data but failed to read bit
    Data,
}

pub type NecResult = ReceiverState<NecCommand, NecError>;

pub struct NecTypeReceiver<NECTYPE> {
    // State
    state: NecState,
    pub bitbuf: u32,
    prev_sampletime: u32,
    prev_pinval: bool,
    // Timing and tolerances
    tolerance: Tolerances,
    lastcommand: u32,
    nectype: core::marker::PhantomData<NECTYPE>,

    #[cfg(feature = "protocol-dev")]
    pub debug: ReceiverDebug<NecState, Tolerances>,
}

#[derive(Debug, Copy, Clone)]
// Internal receiver state
pub enum NecState {
    // Waiting for first pulse
    Init,
    // Receiving data
    Receiving(u32),
    // Command received
    Done,
    // Repeat command received
    RepeatDone,
    // In error state
    Err(NecError),
    // Disabled
    Disabled,
}

impl<NECTYPE: NecTypeTrait> NecTypeReceiver<NECTYPE> {
    pub fn new(samplerate: u32) -> Self {
        let timing = &NECTYPE::PULSEDISTANCE;
        Self::new_from_timing(samplerate, timing)
    }

    fn new_from_timing(samplerate: u32, timing: &Pulsedistance) -> Self {
        let tol = Tolerances::from_timing(timing, samplerate);
        Self {
            state: NecState::Init,
            prev_sampletime: 0,
            prev_pinval: false,
            bitbuf: 0,
            lastcommand: 0,
            nectype: core::marker::PhantomData,
            #[cfg(feature = "protocol-dev")]
            debug: ReceiverDebug {
                state: NecState::Init,
                state_new: NecState::Init,
                delta: 0,
                extra: tol.clone(),
            },
            tolerance: tol,
        }
    }

    fn receiver_state(&self) -> NecResult {
        use ReceiverState::*;
        // Internalstate to ReceiverState
        match self.state {
            NecState::Init => Idle,
            NecState::Done => Done(NECTYPE::decode_command(self.bitbuf)),
            NecState::RepeatDone => Done(NECTYPE::decode_command(self.lastcommand)),
            NecState::Err(e) => Error(e),
            NecState::Disabled => Disabled,
            _ => Receiving,
        }
    }
}

impl<NECTYPE: NecTypeTrait> Receiver for NecTypeReceiver<NECTYPE> {
    type Cmd = NecCommand;
    type Err = NecError;
    const PROTOCOL_ID: ProtocolId = NECTYPE::PROTOCOL;

    fn sample(&mut self, pinval: bool, timestamp: u32) -> ReceiverState<NecCommand, NecError> {

        let rising_edge = pinval && !self.prev_pinval;
        self.prev_pinval = pinval;

        if rising_edge {
            return self.sample_edge(true, timestamp);
        }

        self.receiver_state()
    }

    fn sample_edge(&mut self, rising: bool, sampletime: u32) -> ReceiverState<Self::Cmd, Self::Err> {
        use NecState::*;
        use PulseWidth::*;

        if rising {

            let mut delta = sampletime.wrapping_sub(self.prev_sampletime);

            if delta >= core::u16::MAX.into() {
                delta = 0;
            }

            self.prev_sampletime = sampletime;

            let pulsewidth = self.tolerance.pulsewidth(delta);

            let newstate = match (self.state, pulsewidth) {
                (Init,            Sync)     => Receiving(0),
                (Init,            Repeat)   => RepeatDone,
                (Init,            _)        => Init,

                (Receiving(31),   One)      => {self.bitbuf |= 1 << 31; Done},
                (Receiving(31),   Zero)     => Done,

                (Receiving(bit),  One)      => {self.bitbuf |= 1 << bit; Receiving(bit + 1)},
                (Receiving(bit),  Zero)     => Receiving(bit + 1),

                (Receiving(_),    _)        => Err(NecError::Data),

                (Done,            _)        => Done,
                (RepeatDone,      _)        => RepeatDone,
                (Err(err),        _)        => Err(err),
                (Disabled,        _)        => Disabled,
            };

            #[cfg(feature = "protocol-dev")]
            {
                self.debug.state = self.state;
                self.debug.state_new = newstate;
                self.debug.delta = delta as u16;
            }

            self.state = newstate;
        }

        self.receiver_state()
    }

    fn reset(&mut self) {
        self.state = NecState::Init;
        self.prev_sampletime = 0;
        self.prev_pinval = false;
        self.lastcommand = if self.bitbuf == 0 {self.lastcommand} else {self.bitbuf};
        self.bitbuf = 0;
    }

    fn disable(&mut self) {
        self.state = NecState::Disabled;
    }
}

#[derive(Debug, Clone)]
pub struct Tolerances {
    pub sync: Range<u32>,
    pub repeat: Range<u32>,
    pub zero: Range<u32>,
    pub one: Range<u32>,
}

pub enum PulseWidth {
    Sync,
    Repeat,
    Zero,
    One,
    NotAPulseWidth,
}

impl Tolerances {
    pub const fn from_timing(timing: &Pulsedistance, samplerate: u32) -> Self {
        let per: u32 = 1000 / (samplerate / 1000);
        Tolerances {
            sync: sample_range((timing.header_high + timing.header_low) / per, 5),
            repeat: sample_range((timing.header_high + timing.repeat_low) / per, 5),
            zero: sample_range((timing.data_high + timing.zero_low) / per, 10),
            one: sample_range((timing.data_high + timing.one_low) / per, 10),
        }
    }

    pub fn pulsewidth(&self, samples: u32) -> PulseWidth {
        if self.sync.contains(&samples) {
            return PulseWidth::Sync;
        }
        if self.repeat.contains(&samples) {
            return PulseWidth::Repeat;
        }
        if self.one.contains(&samples) {
            return PulseWidth::One;
        }
        if self.zero.contains(&samples) {
            return PulseWidth::Zero;
        }
        PulseWidth::NotAPulseWidth
    }
}

const fn sample_range(units: u32, percent: u32) -> Range<u32> {
    let tol = (units * percent) / 100;
    (units - tol..units + tol)
}