[−][src]Crate infrared
Infrared
Rust library for using Infrared hardware decoders (For example a Vishay TSOP* decoder), enabling remote control support for embedded project.
This library aims for to be useful with the any MCU hal that implements the embedded-hal traits, and at the same time provide functionality for using it with more efficient implementation such as input capture, and be useful in host applications (such as Blipper).
Design
Receivers
+------------------------+---------------------+
| hal::PeriodicReceiver | hal::EventReceiver | Embedded hal based receivers
|------------------------+---------------------|
| PeriodicReceiver | EventReceiver | Generic Receivers
|------+--------+--------+---+-----+-----+-----|
| Nec | Nec16 | NecSamsung | Rc5 | Rc6 | Sbp | Protocol state machines
+------+--------+------------+-----+-----+-----+
Using Infrared with embedded-hal
Polled
Right now, this is the easiest way to setup Infrared to work with any embedded-hal based board.
-
Setup a CountDown-timer at a frequency of something like 20 kHz. How to setup the timer and enable interrupts is HAL-specific but most HALs have examples showing you how to do it.
-
Create a
hal::PeriodicReceiver
with the desired Decoder state machine.
Example:
use infrared::{hal::PeriodicReceiver, protocols::Rc5} use embedded_hal::digital::v2::InputPin; const SAMPLERATE: u32 = 20_000; let pin = ... // Setup the input pin connected to the infrared receiver let mut recv: PeriodicReceiver<Rc5, PINTYPE> = PeriodicReceiver::new(pin, SAMPLERATE);
- In the timer interrupt handler for the timer
poll
the receiver and wait for it to successfully detect a command
if let Ok(Some(cmd)) = recv.poll() { rprintln!("{} {}", cmd.address(), cmd.data()); }
There is also support for receiving and decoding the pulse train to a known remote control
use infrared::{remotecontrol::Button, remotes::rc5::Rc5CdPlayer}; if let Ok(Some(button)) = recv.poll_button::<Rc5CdPlayer>() { match button { Button::Play => ... // Handle play, Button::Stop => ... // Handle stop ... } }
Evented
The library could also be used with external interrupt if you have a way of keeping track time between the interrupts.
The receiver_exti
example, shows a way of doing it using a timer method found in
the stm32f1xx-hal (non embedded-hal). Another way would be to have a
monotonic timer running and using that for keeping track of the time between the edges.
Examples
In the examples directory in the github repo, there are fully working examples of different kind of receivers
receiver
: HAL Periodic receiver examplereceiver_exti
: HAL EventReceiver using external interruptsmultireceiver
: receiver for multiple protocolsmediakeyboard
: USB hid media keyboard, RTIC based Media keyboard controlled by remote control.sender
: Send example
Modules
hal | Embedded-hal based Receiver types |
protocols | Infrared protocols |
remotes | Library with some example remote controls |
sender | Transmitter state machine |
Structs
BufferedReceiver | Receiver for decoding a captured pulse train |
EventReceiver | Event driven receiver |
PeriodicReceiver | Receiver to use with periodic polling |
Enums
Button | Extensive list of all buttons ever found on a remote control ;-) |
DeviceType | Device type that the remote control controls |
Protocol | Protocol |
Traits
Command | Remote control command trait |
ReceiverSM | Receiver state machine |
RemoteControl | A trait describing a Remote Control |