M5Unit-INFRARED 0.3.0 git rev:2148adc
Loading...
Searching...
No Matches
unit_IR.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_INFRARED_UNIT_IR_HPP
11#define M5_UNIT_INFRARED_UNIT_IR_HPP
12
13#include <M5UnitComponent.hpp>
15
16namespace m5 {
17namespace unit {
18
81class UnitIR : public Component {
82 M5_UNIT_COMPONENT_HPP_BUILDER(UnitIR, 0x00);
83
84public:
89 struct config_t {
91 uint32_t tick_ns{1000};
93 uint16_t rx_ring_buffer_size{1024};
97 uint16_t rx_idle_threshold{20000};
100 uint16_t rx_filter_threshold{200};
108 bool rx_invert_level{true};
112 gpio::RxPull rx_pull{gpio::RxPull::None};
113 };
114
116 UnitIR() : Component(0x00)
117 {
118 }
119
125 {
126 return _cfg;
127 }
132 void config(const config_t& cfg)
133 {
134 _cfg = cfg;
135 }
136
143 bool begin() override;
148 void update(const bool force = false) override;
149
152
157 {
158 return _codec ? *_codec : static_cast<ir::IRCodec&>(_default_codec);
159 }
164 const ir::IRCodec& codec() const
165 {
166 return _codec ? static_cast<const ir::IRCodec&>(*_codec) : static_cast<const ir::IRCodec&>(_default_codec);
167 }
175 {
176 _codec = (&codec == &_default_codec) ? nullptr : &codec;
177 }
180 {
181 _codec = nullptr;
182 }
188 {
189 return _default_codec;
190 }
192
195
208 bool send(uint16_t address, uint16_t command, uint8_t frames = 0);
209
217 bool sendRaw(const gpio::m5_rmt_item_t* items, uint32_t num);
219
222
228 size_t available() const
229 {
230 return _rx_available ? 1 : 0;
231 }
236 bool empty() const
237 {
238 return !_rx_available;
239 }
246 {
247 return _latest_result;
248 }
250 void flush();
255 const gpio::m5_rmt_item_t* rawItems() const
256 {
257 return _raw_items;
258 }
263 uint32_t rawItemCount() const
264 {
265 return _raw_item_count;
266 }
268
271
275 bool hasTX() const
276 {
277 return _has_tx;
278 }
283 bool hasRX() const
284 {
285 return _has_rx;
286 }
288
289private:
290 bool read_rx();
291 bool apply_carrier();
292
293 ir::AutoDetectCodec _default_codec{};
294 ir::IRCodec* _codec{}; // nullptr: use _default_codec (no self-pointer, so the defaulted move stays valid)
295 config_t _cfg{};
296 ir::DecodeResult _latest_result{};
297 const gpio::m5_rmt_item_t* _raw_items{};
298 uint32_t _raw_item_count{};
299 bool _rx_available{};
300 bool _has_tx{};
301 bool _has_rx{};
302
303 // RX buffer (4-byte aligned for RMT v2)
304 struct FreeDeleter {
305 void operator()(uint8_t* p) const
306 {
307 free(p);
308 }
309 };
310 std::unique_ptr<uint8_t[], FreeDeleter> _rx_buffer{};
311 size_t _rx_buffer_size{};
312};
313
314} // namespace unit
315} // namespace m5
316#endif
Auto-detecting multi-protocol IR codec.
uint32_t rawItemCount() const
Number of raw RMT items from last reception.
Definition unit_IR.hpp:263
void update(const bool force=false) override
Poll the RX ringbuffer and decode incoming frames.
Definition unit_IR.cpp:147
ir::IRCodec & codec()
Get current codec.
Definition unit_IR.hpp:156
void resetCodec()
Reset to built-in AutoDetectCodec.
Definition unit_IR.hpp:179
bool begin() override
Initialize the RMT channels (TX and/or RX) based on the adapter pins.
Definition unit_IR.cpp:41
bool hasTX() const
True if TX pin is configured and RMT TX channel initialized.
Definition unit_IR.hpp:275
void flush()
Clear received data (both DecodeResult and raw items)
Definition unit_IR.cpp:223
const ir::DecodeResult & latest() const
Get latest received result.
Definition unit_IR.hpp:245
void config(const config_t &cfg)
Set the config values.
Definition unit_IR.hpp:132
size_t available() const
Number of received frames available since last update.
Definition unit_IR.hpp:228
bool sendRaw(const gpio::m5_rmt_item_t *items, uint32_t num)
Send raw RMT items directly.
Definition unit_IR.cpp:205
bool hasRX() const
True if RX pin is configured and RMT RX channel initialized.
Definition unit_IR.hpp:283
const ir::IRCodec & codec() const
Get current codec (const)
Definition unit_IR.hpp:164
config_t config() const
Gets the config values.
Definition unit_IR.hpp:124
UnitIR()
Constructor.
Definition unit_IR.hpp:116
ir::AutoDetectCodec & defaultCodec()
Get built-in AutoDetectCodec (for accessing individual protocol codecs)
Definition unit_IR.hpp:187
bool send(uint16_t address, uint16_t command, uint8_t frames=0)
Send IR command using current codec.
Definition unit_IR.cpp:165
const gpio::m5_rmt_item_t * rawItems() const
Get raw RMT items from last reception.
Definition unit_IR.hpp:255
void setCodec(ir::IRCodec &codec)
Set protocol codec.
Definition unit_IR.hpp:174
bool empty() const
True if no received frames.
Definition unit_IR.hpp:236
Auto-detecting multi-protocol IR decoder.
Definition auto_detect_codec.hpp:40
Abstract base class for IR protocol encoding/decoding.
Definition ir_codec.hpp:61
Top level namespace of M5Stack.
Unit-related namespace.
Settings for begin.
Definition unit_IR.hpp:89
uint16_t rx_filter_threshold
Definition unit_IR.hpp:100
uint32_t tick_ns
RMT tick resolution in nanoseconds (default: 1000 = 1us)
Definition unit_IR.hpp:91
uint16_t rx_idle_threshold
Definition unit_IR.hpp:97
uint16_t rx_ring_buffer_size
RX ring buffer size in bytes.
Definition unit_IR.hpp:93
gpio::RxPull rx_pull
Definition unit_IR.hpp:112
uint8_t rx_min_item_count
Definition unit_IR.hpp:104
bool rx_invert_level
Definition unit_IR.hpp:108
Result of decoding a received IR frame.
Definition ir_codec.hpp:43