ir_remote/ir_signal/
mod.rs

1use std::fmt::Debug;
2use std::time::Duration;
3
4use crate::is_around::IsAround;
5
6pub mod decode;
7pub mod encode;
8
9#[derive(Debug, Clone, Copy)]
10pub struct Event {
11    pub is_on: bool,
12    pub duration: Duration,
13}
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
17pub enum RemoteType {
18    Generic,
19    Samsung,
20}
21
22const FIRST_HIGH_GENERIC_DURATION: f64 = 0.009108749;
23const FIRST_HIGH_SAMSUNG_DURATION: f64 = 0.004413791;
24const FIRST_LOW_DURATION: f64 = 0.004424661;
25
26const HIGH_DURATION: f64 = 0.000627288;
27const LOW_0_DURATION: f64 = 0.000503018;
28const LOW_1_DURATION: f64 = 0.001632658;
29
30impl RemoteType {
31    pub fn decode_first_high(duration: Duration) -> Option<Self> {
32        let acceptable_error: f64 = 0.2;
33        match duration {
34            duration
35                if duration.is_around(
36                    Duration::from_secs_f64(FIRST_HIGH_GENERIC_DURATION),
37                    acceptable_error,
38                ) =>
39            {
40                Some(Self::Generic)
41            }
42            duration
43                if duration.is_around(
44                    Duration::from_secs_f64(FIRST_HIGH_SAMSUNG_DURATION),
45                    acceptable_error,
46                ) =>
47            {
48                Some(Self::Samsung)
49            }
50            _ => None,
51        }
52    }
53
54    pub fn get_first_high_duration(&self) -> Duration {
55        match self {
56            Self::Generic => Duration::from_secs_f64(FIRST_HIGH_GENERIC_DURATION),
57            Self::Samsung => Duration::from_secs_f64(FIRST_HIGH_SAMSUNG_DURATION),
58        }
59    }
60}
61
62#[derive(Debug, Clone, Copy)]
63#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
64pub struct Repeat {
65    /// Total number of times to send the message
66    pub times: usize,
67    pub duration_between: Duration,
68}
69
70#[derive(PartialEq, Eq, Clone, Copy)]
71#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
72pub struct IrPacket {
73    pub remote_type: RemoteType,
74    pub receiver_id: u16,
75    pub button: u8,
76}
77
78impl Debug for IrPacket {
79    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80        Debug::fmt(&IrPacketDebug::from(self), f)
81    }
82}
83
84#[derive(Debug)]
85#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
86pub struct IrSignal {
87    pub packet: IrPacket,
88    pub repeat: Option<Repeat>,
89}
90
91#[derive(Debug)]
92pub struct IrPacketDebug {
93    pub remote_type: RemoteType,
94    pub receiver_id: String,
95    pub button: String,
96}
97
98impl From<&IrPacket> for IrPacketDebug {
99    fn from(value: &IrPacket) -> Self {
100        Self {
101            remote_type: value.remote_type,
102            receiver_id: format!("{:#06X}", value.receiver_id),
103            button: format!("{:#04X}", value.button),
104        }
105    }
106}