Skip to main content

oxstack_cardputer/
rmt.rs

1//! The RMT pulse generator and the two LEDs it drives: the WS2812-class RGB
2//! LED (GPIO21) and the IR transmitter (GPIO44).
3//!
4//! Both need an RMT channel from one `Rmt` instance at one clock, so a
5//! single [`RmtResources::init`] sets up the peripheral and returns both
6//! drivers:
7//!
8//! ```ignore
9//! let (mut led, mut ir) = board.rmt.init()?;
10//! led.set(0, 32, 0).await?;
11//! ir.send_nec(0x04, 0x08).await?;
12//! ```
13//!
14//! Channels 2–3 stay free; build your own from the group's fields if needed
15//! (see [`crate::led::RgbLed::new`] for the pattern).
16
17use esp_hal::peripherals::{GPIO21, GPIO44, RMT};
18use esp_hal::rmt::{ConfigError, Rmt};
19use esp_hal::time::Rate;
20
21use crate::ir::IrTx;
22use crate::led::{RMT_MHZ, RgbLed};
23
24/// The RMT clock both drivers' timings assume.
25pub const CLOCK: Rate = Rate::from_mhz(RMT_MHZ);
26
27/// The RMT peripheral and the pins of the LEDs it drives.
28#[allow(missing_docs)]
29pub struct RmtResources<'d> {
30    pub rmt: RMT<'d>,
31    pub rgb_led: GPIO21<'d>,
32    pub ir_tx: GPIO44<'d>,
33}
34reborrow_group!(RmtResources {
35    rmt,
36    rgb_led,
37    ir_tx
38});
39
40impl<'d> RmtResources<'d> {
41    /// Starts the RMT at [`CLOCK`] and configures channel 0 for the RGB LED
42    /// and channel 1 for the IR LED.
43    pub fn init(&mut self) -> Result<(RgbLed<'_>, IrTx<'_>), ConfigError> {
44        let rmt = Rmt::new(self.rmt.reborrow(), CLOCK)?.into_async();
45        let led = RgbLed::new(rmt.channel0, self.rgb_led.reborrow())?;
46        let ir = IrTx::new(rmt.channel1, self.ir_tx.reborrow())?;
47        Ok((led, ir))
48    }
49}