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
//! Nec

use core::{convert::TryInto, marker::PhantomData};

use crate::{cmd::Protocol, Command};

pub mod receiver;
#[cfg(test)]
mod tests;

#[doc(inline)]
pub use receiver::Nec;

/// Standard Nec protocol
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct NecStandard;
/// Nec protocol with Samsung timings
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct NecSamsung;
/// Nec with 16 bit address, 8 bit command
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Nec16;

#[derive(Debug, Copy, Clone, PartialEq)]
/// Nec Command
pub struct NecCommand<VARIANT: NecVariant + ?Sized = NecStandard> {
    pub addr: u16,
    pub cmd: u8,
    var: PhantomData<VARIANT>,
}

impl<V: NecVariant> NecCommand<V> {
    pub fn new(addr: u16, cmd: u8) -> Self {
        NecCommand {
            addr,
            cmd,
            var: PhantomData,
        }
    }
}

impl<VARIANT: NecVariant> Command for NecCommand<VARIANT> {
    fn construct(addr: u32, cmd: u32) -> Option<Self> {
        Some(NecCommand::new(addr.try_into().ok()?, cmd.try_into().ok()?))
    }

    fn address(&self) -> u32 {
        self.addr.into()
    }

    fn data(&self) -> u32 {
        self.cmd.into()
    }

    fn protocol(&self) -> Protocol {
        Protocol::Nec
    }

    fn pulses(&self, b: &mut [u16]) -> usize {
        b[0] = 0;
        b[1] = VARIANT::TIMING.hh as u16;
        b[2] = VARIANT::TIMING.hl as u16;

        let bits = VARIANT::cmd_to_bits(self);

        let mut bi = 3;

        for i in 0..32 {
            let one = (bits >> i) & 1 != 0;
            b[bi] = VARIANT::TIMING.dh as u16;
            if one {
                b[bi + 1] = VARIANT::TIMING.ol as u16;
            } else {
                b[bi + 1] = VARIANT::TIMING.zl as u16;
            }
            bi += 2;
        }

        bi
    }
}

pub trait NecVariant {
    const TIMING: &'static NecTiming;

    fn cmd_to_bits(cmd: &NecCommand<Self>) -> u32;
    fn cmd_from_bits(bits: u32) -> NecCommand<Self>;
    fn cmd_is_valid(bits: u32) -> bool;
}

impl NecVariant for NecStandard {
    const TIMING: &'static NecTiming = &STANDARD_TIMING;

    // Encode to bit
    fn cmd_to_bits(cmd: &NecCommand) -> u32 {
        let addr = cmd.addr;
        let cmd = cmd.cmd;

        let addr = u32::from(addr) | (u32::from(!addr) & 0xFF) << 8;
        let cmd = u32::from(cmd) << 16 | u32::from(!cmd) << 24;
        addr | cmd
    }

    fn cmd_from_bits(bits: u32) -> NecCommand<NecStandard> {
        let addr = ((bits) & 0xFF) as u16;
        let cmd = ((bits >> 16) & 0xFF) as u8;
        NecCommand {
            addr,
            cmd,
            var: PhantomData,
        }
    }

    fn cmd_is_valid(bits: u32) -> bool {
        ((bits >> 24) ^ (bits >> 16)) & 0xFF == 0xFF && ((bits >> 8) ^ bits) & 0xFF == 0xFF
    }
}

impl NecVariant for Nec16 {
    const TIMING: &'static NecTiming = &STANDARD_TIMING;

    fn cmd_to_bits(cmd: &NecCommand<Self>) -> u32 {
        let addr = u32::from(cmd.addr);
        let cmd = u32::from(cmd.cmd) << 16 | u32::from(!cmd.cmd) << 24;
        addr | cmd
    }

    fn cmd_from_bits(bits: u32) -> NecCommand<Nec16> {
        let addr = ((bits) & 0xFFFF) as u16;
        let cmd = ((bits >> 16) & 0xFF) as u8;
        NecCommand {
            addr,
            cmd,
            var: PhantomData,
        }
    }

    fn cmd_is_valid(bits: u32) -> bool {
        ((bits >> 24) ^ (bits >> 16)) & 0xFF == 0xFF
    }
}

impl NecVariant for NecSamsung {
    const TIMING: &'static NecTiming = &NecTiming {
        hh: 4500,
        hl: 4500,
        rl: 2250,
        zl: 560,
        dh: 560,
        ol: 1690,
    };

    fn cmd_to_bits(cmd: &NecCommand<Self>) -> u32 {
        let addr = u32::from(cmd.addr) | u32::from(cmd.addr) << 8;
        let cmd = u32::from(cmd.cmd) << 16 | u32::from(!cmd.cmd) << 24;
        addr | cmd
    }

    fn cmd_from_bits(bits: u32) -> NecCommand<NecSamsung> {
        let addr = ((bits) & 0xFF) as u16;
        let cmd = ((bits >> 16) & 0xFF) as u8;
        NecCommand {
            addr,
            cmd,
            var: PhantomData,
        }
    }

    fn cmd_is_valid(bits: u32) -> bool {
        ((bits >> 24) ^ (bits >> 16)) & 0xFF == 0xFF && ((bits >> 8) ^ bits) & 0xFF == 0
    }
}

/// High and low times for Nec-like protocols. In us.
pub struct NecTiming {
    /// Header high
    hh: u32,
    /// Header low
    hl: u32,
    /// Repeat low
    rl: u32,
    /// Data high
    dh: u32,
    /// Zero low
    zl: u32,
    /// One low
    ol: u32,
}

const STANDARD_TIMING: NecTiming = NecTiming {
    hh: 9000,
    hl: 4500,
    rl: 2250,
    dh: 560,
    zl: 560,
    ol: 1690,
};