ls7366/
mdr1.rs

1//! Secondary 8 bit configuration register.
2//! Holds configurations for the [Counter Size], and the various occurrence flags. See datasheet for
3//! details.
4//!
5//! [Counter Size]: ./enum.CounterMode.html
6use bitfield::bitfield;
7
8use crate::errors::EncoderError;
9use crate::traits::{Decodable, Encodable};
10
11#[derive(Debug)]
12/// Programmable size of the [`Cntr`] register.
13///
14/// [`Cntr`]: ../ir/enum.Target.html#variant.Cntr
15pub enum CounterMode {
16    /// 4 byte counter mode.
17    Byte4,
18    /// 3 byte counter mode.
19    Byte3,
20    /// 2 byte counter mode.
21    Byte2,
22    /// 1 byte counter mode.
23    Byte1,
24}
25
26#[derive(Debug)]
27/// Extended configuration options, mainly used for occurrence flags. (See datasheet).
28pub struct Mdr1 {
29    /// programmed size of the counter([`Cntr`]) register.
30    ///
31    /// [`Cntr`]: ../ir/enum.Target.html#variant.Cntr
32
33    pub counter_mode: CounterMode,
34    /// Controls whether counting is enabled (false) or not (true)
35    pub disable_counting: bool,
36    pub flag_on_idx: bool,
37    pub flag_on_cmp: bool,
38    pub flag_on_bw: bool,
39    pub flag_on_cy: bool,
40}
41
42bitfield! {
43    struct Payload(u8);
44    impl Debug;
45    pub counter_mode, set_counter_mode: 1,0;
46    pub counting_enabled, set_counting_enabled: 2;
47    // bit 3 is unused
48    pub flag_on_idx, set_flag_on_idx: 4;
49    pub flag_on_cmp, set_flag_on_cmp: 5;
50    pub flag_on_bw, set_flag_on_bw: 6;
51    pub flag_on_cy, set_flag_on_cy: 7;
52}
53impl Encodable for CounterMode {
54    fn encode(&self) -> u8 {
55        match self {
56            CounterMode::Byte4 => 0b00,
57            CounterMode::Byte3 => 0b01,
58            CounterMode::Byte2 => 0b10,
59            CounterMode::Byte1 => 0b11,
60        }
61    }
62}
63
64impl Decodable for CounterMode {
65    fn decode(raw: u8) -> Result<Self, EncoderError> {
66        match raw
67        {
68            0b00 => Ok(CounterMode::Byte4),
69            0b01 => Ok(CounterMode::Byte3),
70            0b10 => Ok(CounterMode::Byte2),
71            0b11 => Ok(CounterMode::Byte1),
72            _ => Err(EncoderError::FailedDecode)
73        }
74    }
75}
76
77impl Encodable for Mdr1 {
78    fn encode(&self) -> u8 {
79        let mut payload = Payload(0x00);
80        payload.set_counter_mode(self.counter_mode.encode());
81        payload.set_counting_enabled(self.disable_counting);
82        payload.set_flag_on_idx(self.flag_on_idx);
83        payload.set_flag_on_cmp(self.flag_on_cmp);
84        payload.set_flag_on_bw(self.flag_on_bw);
85        payload.set_flag_on_cy(self.flag_on_cy);
86
87        payload.0
88    }
89}
90
91impl Decodable for Mdr1 {
92    fn decode(raw: u8) -> Result<Self, EncoderError> {
93        let payload = Payload(raw);
94
95        Ok(Self {
96            counter_mode: CounterMode::decode(payload.counter_mode())?,
97            disable_counting: payload.counting_enabled(),
98            flag_on_idx: payload.flag_on_idx(),
99            flag_on_cmp: payload.flag_on_cmp(),
100            flag_on_bw: payload.flag_on_bw(),
101            flag_on_cy: payload.flag_on_cy(),
102        })
103    }
104}