Skip to main content

Module ir

Module ir 

Source
Expand description

A device abstraction for infrared receivers using the NEC protocol.

This page provides the primary documentation and examples for receiving NEC infrared input on ESP devices. It covers raw address/command events, mapped application keys, and Kepler remote keys. Traits define the shared API; macros generate concrete device types. Choose ir! for raw NEC events, ir_mapping! when mapping to your own enum, and ir_kepler! for the SunFounder Kepler remote.

After reading the examples below, see also:

§Example: Read Raw NEC Events

In this example, the generated Ir7 type emits raw NEC press events with address and command bytes.

use device_envoy_esp::{Result, init_and_start, init_and_start::rmt_mode, ir, ir::{Ir as _, IrEvent}};
ir! {
    Ir7 { pin: GPIO7 }
}

async fn example(spawner: embassy_executor::Spawner) -> Result<core::convert::Infallible> {
    init_and_start!(p, rmt80: rmt80, mode: rmt_mode::Async);
    esp_println::logger::init_logger(log::LevelFilter::Info);

    #[cfg(target_arch = "xtensa")]
    let channel_creator = rmt80.channel4;
    #[cfg(not(target_arch = "xtensa"))]
    let channel_creator = rmt80.channel2;

    let ir7 = Ir7::new(p.GPIO7, channel_creator, spawner)?;

    loop {
        let IrEvent::Press { addr, cmd } = ir7.wait_for_press().await;
        info!("IR press: addr=0x{:04X}, cmd=0x{:02X}", addr, cmd);
    }
}

§Example: Map NEC Events to App Keys

In this example, the generated IrMapping7 type maps raw NEC address/command pairs into an application-defined enum.

use device_envoy_esp::{Result, init_and_start, init_and_start::rmt_mode, ir::IrMapping as _, ir_mapping};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum RemoteKeys {
    Power,
    Plus,
    Minus,
}

ir_mapping! {
    IrMapping7 {
        pin: GPIO7,
        button: RemoteKeys,
        capacity: 3,
    }
}

const REMOTE_KEYS_MAP: [(u16, u8, RemoteKeys); 3] = [
    (0x0000, 0x45, RemoteKeys::Power),
    (0x0000, 0x09, RemoteKeys::Plus),
    (0x0000, 0x15, RemoteKeys::Minus),
];

async fn example(spawner: embassy_executor::Spawner) -> Result<core::convert::Infallible> {
    init_and_start!(p, rmt80: rmt80, mode: rmt_mode::Async);

    #[cfg(target_arch = "xtensa")]
    let channel_creator = rmt80.channel4;
    #[cfg(not(target_arch = "xtensa"))]
    let channel_creator = rmt80.channel2;

    let ir_mapping7 = IrMapping7::new(p.GPIO7, channel_creator, &REMOTE_KEYS_MAP, spawner)?;

    loop {
        let remote_key = ir_mapping7.wait_for_press().await;
        match remote_key {
            RemoteKeys::Power => {}
            RemoteKeys::Plus => {}
            RemoteKeys::Minus => {}
        }
    }
}

§Example: Read Kepler Remote Keys

In this example, the generated IrKepler7 type returns typed keys from the SunFounder Kepler remote key mapping.

use device_envoy_esp::{Result, init_and_start, init_and_start::rmt_mode, ir::IrKepler as _, ir::KeplerKeys, ir_kepler};
ir_kepler! {
    IrKepler7 { pin: GPIO7 }
}

async fn example(spawner: embassy_executor::Spawner) -> Result<core::convert::Infallible> {
    init_and_start!(p, rmt80: rmt80, mode: rmt_mode::Async);
    esp_println::logger::init_logger(log::LevelFilter::Info);

    #[cfg(target_arch = "xtensa")]
    let channel_creator = rmt80.channel4;
    #[cfg(not(target_arch = "xtensa"))]
    let channel_creator = rmt80.channel2;

    let ir_kepler7 = IrKepler7::new(p.GPIO7, channel_creator, spawner)?;

    loop {
        let kepler_key = ir_kepler7.wait_for_press().await;
        match kepler_key {
            KeplerKeys::Power => info!("Power"),
            KeplerKeys::PlayPause => info!("PlayPause"),
            _ => info!("Other: {:?}", kepler_key),
        }
    }
}

Modules§

ir_generated
Module containing sample struct types generated by IR macros: IrGenerated, IrMappingGenerated, and IrKeplerGenerated.

Macros§

ir
Macro to generate an IR receiver struct type (includes syntax details).
ir_kepler
Macro to generate a Kepler IR struct type (includes syntax details).
ir_keplers
Macro to generate multiple Kepler IR struct types (includes syntax details).
ir_mapping
Macro to generate an IR mapping struct type (includes syntax details).
ir_mappings
Macro to generate multiple IR mapping struct types (includes syntax details).
irs
Macro to generate multiple IR receiver struct types (includes syntax details).

Enums§

IrEvent
Events received from the infrared receiver.
KeplerKeys
Button types for the SunFounder Kepler Kit remote control.

Traits§

Ir
Platform-agnostic infrared receiver device contract.
IrKepler
Platform-agnostic SunFounder Kepler IR device contract.
IrMapping
Platform-agnostic IR button mapper device contract.