M5Unit-INFRARED 0.2.0 git rev:76ad9e1
Loading...
Searching...
No Matches
m5::unit::UnitIR Class Reference

IR remote control transceiver unit. More...

#include <unit_IR.hpp>

Inheritance diagram for m5::unit::UnitIR:

Classes

struct  config_t
 Settings for begin. More...
 

Public Member Functions

 UnitIR ()
 Constructor.
 
config_t config () const
 Gets the config values.
 
void config (const config_t &cfg)
 Set the config values.
 
bool begin () override
 Initialize the RMT channels (TX and/or RX) based on the adapter pins.
 
void update (const bool force=false) override
 Poll the RX ringbuffer and decode incoming frames.
 
Codec
ir::IRCodeccodec ()
 Get current codec.
 
void setCodec (ir::IRCodec &codec)
 Set protocol codec.
 
void resetCodec ()
 Reset to built-in AutoDetectCodec.
 
ir::AutoDetectCodecdefaultCodec ()
 Get built-in AutoDetectCodec (for accessing individual protocol codecs)
 
TX
bool send (uint16_t address, uint16_t command, uint8_t frames=0)
 Send IR command using current codec.
 
bool sendRaw (const gpio::m5_rmt_item_t *items, uint32_t num)
 Send raw RMT items directly.
 
RX
size_t available () const
 Number of decoded messages available since last update.
 
bool empty () const
 True if no decoded messages.
 
const ir::DecodeResultlatest () const
 Get latest decoded result.
 
void flush ()
 Clear received data (both DecodeResult and raw items)
 
const gpio::m5_rmt_item_t * rawItems () const
 Get raw RMT items from last reception.
 
uint32_t rawItemCount () const
 Number of raw RMT items from last reception.
 
State
bool hasTX () const
 True if TX pin is configured and RMT TX channel initialized.
 
bool hasRX () const
 True if RX pin is configured and RMT RX channel initialized.
 

Detailed Description

IR remote control transceiver unit.

Supports Unit IR (SKU: U002) via Grove and built-in IR transmitters (e.g., M5StickC Plus2 GPIO 9). Uses ESP32 RMT peripheral for precise carrier modulation (TX) and timing capture (RX).

Usage (Unit IR U002 via Grove)
m5::unit::UnitUnified Units;
// Port B preferred, fallback to Port A
auto pin_rx = M5.getPin(m5::pin_name_t::port_b_in);
auto pin_tx = M5.getPin(m5::pin_name_t::port_b_out);
if (pin_rx < 0 || pin_tx < 0) {
pin_rx = M5.getPin(m5::pin_name_t::port_a_pin1);
pin_tx = M5.getPin(m5::pin_name_t::port_a_pin2);
}
Units.add(ir, pin_rx, pin_tx);
Units.begin();
ir.send(0x00, 0x1F); // NEC send
IR remote control transceiver unit.
bool begin() override
Initialize the RMT channels (TX and/or RX) based on the adapter pins.
Definition unit_IR.cpp:34
bool send(uint16_t address, uint16_t command, uint8_t frames=0)
Send IR command using current codec.
Definition unit_IR.cpp:151
Usage (Built-in IR, TX only, e.g. M5StickC Plus2)
m5::unit::UnitUnified Units;
if (!Units.add(ir, -1, 19) || !Units.begin()) { // CPlus2: TX=G19
// error
}
ir.send(0x00, 0x1F);
Usage (Built-in IR, TX+RX, e.g. M5StickS3)
m5::unit::UnitUnified Units;
if (!Units.add(ir, 42, 46) || !Units.begin()) { // StickS3: RX=G42, TX=G46
// error
}
Built-in IR pin assignments
Device TX GPIO RX GPIO
M5StickC 9 -
M5StickC Plus 9 -
M5StickC Plus2 19 -
M5StickS3 46 42
Atom Lite 12 -
Atom Matrix 12 -
AtomU 12 -
AtomS3 4 -
AtomS3 Lite 4 -
AtomS3U 12 -
AtomS3R 47 -
Atom VoiceS3R 47 -
M5Capsule 4 -
NanoC6 3 -
NessoN1 9 -
Cardputer 44 -
Cardputer ADV 44 -

Member Function Documentation

◆ available()

size_t m5::unit::UnitIR::available ( ) const
inline

Number of decoded messages available since last update.

Returns
1 if a decoded frame is pending, 0 otherwise

◆ begin()

bool m5::unit::UnitIR::begin ( )
override

Initialize the RMT channels (TX and/or RX) based on the adapter pins.

Returns
True on success, false if neither TX nor RX is configured or RMT init fails
Note
config_t::tick_ns must be 1000; other values are rejected because codec timing constants assume 1us per tick.

◆ codec()

ir::IRCodec & m5::unit::UnitIR::codec ( )
inline

Get current codec.

Returns
Reference to the active codec (default: built-in AutoDetectCodec)

◆ config() [1/2]

config_t m5::unit::UnitIR::config ( ) const
inline

Gets the config values.

Returns
Current configuration (by value)

◆ config() [2/2]

void m5::unit::UnitIR::config ( const config_t & cfg)
inline

Set the config values.

Parameters
cfgNew configuration to apply on next begin()

◆ defaultCodec()

ir::AutoDetectCodec & m5::unit::UnitIR::defaultCodec ( )
inline

Get built-in AutoDetectCodec (for accessing individual protocol codecs)

Returns
Reference to the internal AutoDetectCodec instance

◆ empty()

bool m5::unit::UnitIR::empty ( ) const
inline

True if no decoded messages.

Returns
True when no frame has been decoded since the last flush/update

◆ hasRX()

bool m5::unit::UnitIR::hasRX ( ) const
inline

True if RX pin is configured and RMT RX channel initialized.

Returns
True if receive is available

◆ hasTX()

bool m5::unit::UnitIR::hasTX ( ) const
inline

True if TX pin is configured and RMT TX channel initialized.

Returns
True if transmit is available

◆ latest()

const ir::DecodeResult & m5::unit::UnitIR::latest ( ) const
inline

Get latest decoded result.

Returns
Reference to the most recent DecodeResult (valid until next update)

◆ rawItemCount()

uint32_t m5::unit::UnitIR::rawItemCount ( ) const
inline

Number of raw RMT items from last reception.

Returns
Count of valid items in rawItems(), 0 if none

◆ rawItems()

const gpio::m5_rmt_item_t * m5::unit::UnitIR::rawItems ( ) const
inline

Get raw RMT items from last reception.

Returns
Pointer to the raw item array (valid until next update()), or nullptr if none

◆ send()

bool m5::unit::UnitIR::send ( uint16_t address,
uint16_t command,
uint8_t frames = 0 )

Send IR command using current codec.

Parameters
addressDevice address
commandCommand code
framesFrame count per keypress. 0 (default) uses the codec's minFrames() (NEC=1, Mitsubishi=2, Sony SIRC=3). Any value ≥ 1 overrides and sends exactly that many frames.
Returns
True if successful
Note
Multiple frames are sent tightly in a single RMT burst with codec.frameGapUs() between them. To simulate press-and-hold (NEC repeat frame at ~110 ms intervals, etc.), loop externally using encode(addr, cmd, true) + sendRaw().
Carrier frequency and duty cycle are determined by the current codec.

◆ sendRaw()

bool m5::unit::UnitIR::sendRaw ( const gpio::m5_rmt_item_t * items,
uint32_t num )

Send raw RMT items directly.

Parameters
itemsRMT items
numNumber of items
Returns
True if successful
Note
Uses carrier settings from the current codec.

◆ setCodec()

void m5::unit::UnitIR::setCodec ( ir::IRCodec & codec)
inline

Set protocol codec.

Parameters
codecCodec instance (must outlive UnitIR; typically a global/static variable)
Note
Default codec is a built-in AutoDetectCodec. Call resetCodec() to restore it.

◆ update()

void m5::unit::UnitIR::update ( const bool force = false)
override

Poll the RX ringbuffer and decode incoming frames.

Parameters
forceUnused (reserved for future periodic-update support)