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
use core::marker::PhantomData;

use crate::{
    receiver::{
        time::{InfraMonotonic, PulseSpans},
        DecoderFactory, ProtocolDecoder, State,
    },
    Protocol,
};

impl<Mono: InfraMonotonic> DecoderFactory<Mono> for Capture<Mono> {
    type Decoder = CaptureDecoder<Mono>;
    fn decoder(_freq: u32) -> Self::Decoder {
        CaptureDecoder {
            ts: [Mono::ZERO_DURATION; 96],
            pos: 0,
        }
    }
}

pub struct Capture<Mono> {
    dur: PhantomData<Mono>,
}

pub struct CaptureDecoder<Mono: InfraMonotonic> {
    pub ts: [Mono::Duration; 96],
    pub pos: usize,
}

impl<Mono: InfraMonotonic> Protocol for Capture<Mono> {
    type Cmd = [Mono::Duration; 96];
}

impl<Mono: InfraMonotonic> ProtocolDecoder<Mono, [Mono::Duration; 96]> for CaptureDecoder<Mono> {
    fn event(&mut self, _edge: bool, dur: Mono::Duration) -> State {
        if self.pos >= self.ts.len() {
            return State::Done;
        }

        self.ts[self.pos] = dur;
        self.pos += 1;

        State::Receiving
    }

    fn command(&self) -> Option<[Mono::Duration; 96]> {
        Some(self.ts)
    }

    fn reset(&mut self) {
        self.pos = 0;
    }

    fn spans(&self) -> &PulseSpans<Mono> {
        todo!()
    }
}