infrared/remotecontrol/
mod.rs

1//! Library with some example remote controls
2
3pub mod mapper;
4
5#[cfg(feature = "nec")]
6pub mod nec;
7#[cfg(feature = "rc5")]
8pub mod rc5;
9#[cfg(feature = "rc6")]
10pub mod rc6;
11#[cfg(feature = "sbp")]
12pub mod sbp;
13
14pub use mapper::Button;
15
16use crate::{cmd::AddressCommand, ProtocolId};
17
18/// A trait describing a Remote Control
19pub trait RemoteControlModel: Default {
20    /// Remote control model
21    const MODEL: &'static str = "<NONAME>";
22    /// Type of device that this remote controls
23    const DEVTYPE: DeviceType = DeviceType::Generic;
24    /// Protocol
25    const PROTOCOL: ProtocolId;
26    /// Device address
27    const ADDRESS: u32;
28    /// The type of command
29    type Cmd: AddressCommand;
30    /// command byte to action mapping
31    const BUTTONS: &'static [(u32, Action)] = &[];
32
33    /// Try to map a command into a Action for this remote
34    fn decode(cmd: &Self::Cmd) -> Option<Action> {
35        // Check address
36        if Self::ADDRESS != cmd.address() {
37            return None;
38        }
39        Self::BUTTONS
40            .iter()
41            .find(|(c, _)| *c == cmd.command())
42            .map(|(_, b)| *b)
43    }
44
45    /// Encode a button into a command
46    fn encode(button: &Action) -> Option<Self::Cmd> {
47        Self::BUTTONS
48            .iter()
49            .find(|(_, b)| b == button)
50            .and_then(|(c, _)| Self::Cmd::create(Self::ADDRESS, *c as u32))
51    }
52}
53
54#[derive(Debug)]
55#[cfg_attr(feature = "defmt", derive(defmt::Format))]
56/// Device type that the remote control controls
57pub enum DeviceType {
58    Generic,
59    TV,
60    DVDPlayer,
61    CDPlayer,
62    BluRayPlayer,
63}
64
65#[allow(non_camel_case_types)]
66#[derive(Debug, Copy, Clone, Eq, PartialEq)]
67#[cfg_attr(feature = "defmt", derive(defmt::Format))]
68#[non_exhaustive]
69/// Remote control actions
70pub enum Action {
71    Power,
72    Source,
73    One,
74    Two,
75    Three,
76    Four,
77    Five,
78    Six,
79    Seven,
80    Eight,
81    Nine,
82    Zero,
83
84    Teletext,
85    ChannelPrev,
86    VolumeUp,
87    VolumeDown,
88    VolumeMute,
89    ChannelList,
90    ChannelListNext,
91    ChannelListPrev,
92
93    Tools,
94    Info,
95    Return,
96    Exit,
97    Enter,
98    Up,
99    Down,
100    Left,
101    Right,
102    Red,
103    Green,
104    Yellow,
105    Blue,
106    Emanual,
107    PictureSize,
108    Subtitle,
109    Stop,
110    Rewind,
111    Play,
112    Paus,
113    Play_Pause,
114    Play_Pause2,
115
116    Forward,
117    Mode,
118    Shuffle,
119    U_SD,
120    Plus,
121    Minus,
122    Next,
123    Prev,
124    Eq,
125    Mute,
126
127    Random,
128    Repeat,
129    Time,
130    Setup,
131    Menu,
132
133    PitchReset,
134    PitchPlus,
135    PitchMinus,
136    Prog,
137
138    BatteryLow,
139}
140
141impl Action {
142    pub fn to_str(&self) -> &str {
143        match self {
144            Action::Power => "power",
145            Action::Source => "source",
146            Action::One => "one",
147            Action::Two => "two",
148            Action::Three => "three",
149            Action::Four => "four",
150            Action::Five => "five",
151            Action::Six => "six",
152            Action::Seven => "seven",
153            Action::Eight => "eight",
154            Action::Nine => "nine",
155            Action::Zero => "zero",
156            Action::Teletext => "teletext",
157            Action::ChannelPrev => "channel_prev",
158            Action::VolumeUp => "volume_up",
159            Action::VolumeDown => "volume_down",
160            Action::VolumeMute => "volume_mute",
161            Action::ChannelList => "channellist",
162            Action::ChannelListNext => "channellist_next",
163            Action::ChannelListPrev => "channellist_prev",
164            Action::Tools => "tools",
165            Action::Info => "info",
166            Action::Return => "return",
167            Action::Exit => "exit",
168            Action::Enter => "enter",
169            Action::Up => "up",
170            Action::Down => "down",
171            Action::Left => "left",
172            Action::Right => "right",
173            Action::Red => "red",
174            Action::Green => "green",
175            Action::Yellow => "yellow",
176            Action::Blue => "blue",
177            Action::Emanual => "e_manual",
178            Action::PictureSize => "picture_size",
179            Action::Subtitle => "subtitle",
180            Action::Stop => "stop",
181            Action::Rewind => "rewind",
182            Action::Play => "play",
183            Action::Paus => "pause",
184            Action::Play_Pause => "play_pause",
185            Action::Play_Pause2 => "play_pause2",
186            Action::Forward => "forward",
187            Action::Mode => "mode",
188            Action::Shuffle => "shuffle",
189            Action::U_SD => "u_sd",
190            Action::Plus => "plus",
191            Action::Minus => "minus",
192            Action::Next => "next",
193            Action::Prev => "prev",
194            Action::Eq => "equalizer",
195            Action::Mute => "mute",
196            Action::Random => "random",
197            Action::Repeat => "repeat",
198            Action::Time => "time",
199            Action::Setup => "setup",
200            Action::Menu => "menu",
201            Action::PitchReset => "pitch_reset",
202            Action::PitchPlus => "pitch_plus",
203            Action::PitchMinus => "pitch_minus",
204            Action::Prog => "program",
205            Action::BatteryLow => "battery_low",
206        }
207    }
208}