M5Unit-INFRARED 0.2.0 git rev:76ad9e1
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};
109 };
110
112 UnitIR() : Component(0x00)
113 {
114 }
115
121 {
122 return _cfg;
123 }
128 void config(const config_t& cfg)
129 {
130 _cfg = cfg;
131 }
132
139 bool begin() override;
144 void update(const bool force = false) override;
145
148
153 {
154 return *_codec;
155 }
162 {
163 _codec = &codec;
164 }
167 {
168 _codec = &_default_codec;
169 }
175 {
176 return _default_codec;
177 }
179
182
195 bool send(uint16_t address, uint16_t command, uint8_t frames = 0);
196
204 bool sendRaw(const gpio::m5_rmt_item_t* items, uint32_t num);
206
209
213 size_t available() const
214 {
215 return _rx_available ? 1 : 0;
216 }
221 bool empty() const
222 {
223 return !_rx_available;
224 }
230 {
231 return _latest_result;
232 }
234 void flush();
239 const gpio::m5_rmt_item_t* rawItems() const
240 {
241 return _raw_items;
242 }
247 uint32_t rawItemCount() const
248 {
249 return _raw_item_count;
250 }
252
255
259 bool hasTX() const
260 {
261 return _has_tx;
262 }
267 bool hasRX() const
268 {
269 return _has_rx;
270 }
272
273private:
274 bool read_rx();
275 bool apply_carrier();
276
277 ir::AutoDetectCodec _default_codec{};
278 ir::IRCodec* _codec{&_default_codec};
279 config_t _cfg{};
280 ir::DecodeResult _latest_result{};
281 const gpio::m5_rmt_item_t* _raw_items{};
282 uint32_t _raw_item_count{};
283 bool _rx_available{};
284 bool _has_tx{};
285 bool _has_rx{};
286
287 // RX buffer (4-byte aligned for RMT v2)
288 struct FreeDeleter {
289 void operator()(uint8_t* p) const
290 {
291 free(p);
292 }
293 };
294 std::unique_ptr<uint8_t[], FreeDeleter> _rx_buffer{};
295 size_t _rx_buffer_size{};
296};
297
298} // namespace unit
299} // namespace m5
300#endif
Auto-detecting multi-protocol IR codec.
uint32_t rawItemCount() const
Number of raw RMT items from last reception.
Definition unit_IR.hpp:247
void update(const bool force=false) override
Poll the RX ringbuffer and decode incoming frames.
Definition unit_IR.cpp:133
ir::IRCodec & codec()
Get current codec.
Definition unit_IR.hpp:152
void resetCodec()
Reset to built-in AutoDetectCodec.
Definition unit_IR.hpp:166
bool begin() override
Initialize the RMT channels (TX and/or RX) based on the adapter pins.
Definition unit_IR.cpp:34
bool hasTX() const
True if TX pin is configured and RMT TX channel initialized.
Definition unit_IR.hpp:259
void flush()
Clear received data (both DecodeResult and raw items)
Definition unit_IR.cpp:209
const ir::DecodeResult & latest() const
Get latest decoded result.
Definition unit_IR.hpp:229
void config(const config_t &cfg)
Set the config values.
Definition unit_IR.hpp:128
size_t available() const
Number of decoded messages available since last update.
Definition unit_IR.hpp:213
bool sendRaw(const gpio::m5_rmt_item_t *items, uint32_t num)
Send raw RMT items directly.
Definition unit_IR.cpp:191
bool hasRX() const
True if RX pin is configured and RMT RX channel initialized.
Definition unit_IR.hpp:267
config_t config() const
Gets the config values.
Definition unit_IR.hpp:120
UnitIR()
Constructor.
Definition unit_IR.hpp:112
ir::AutoDetectCodec & defaultCodec()
Get built-in AutoDetectCodec (for accessing individual protocol codecs)
Definition unit_IR.hpp:174
bool send(uint16_t address, uint16_t command, uint8_t frames=0)
Send IR command using current codec.
Definition unit_IR.cpp:151
const gpio::m5_rmt_item_t * rawItems() const
Get raw RMT items from last reception.
Definition unit_IR.hpp:239
void setCodec(ir::IRCodec &codec)
Set protocol codec.
Definition unit_IR.hpp:161
bool empty() const
True if no decoded messages.
Definition unit_IR.hpp:221
Auto-detecting multi-protocol IR decoder.
Definition auto_detect_codec.hpp:40
Abstract base class for IR protocol encoding/decoding.
Definition ir_codec.hpp:62
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
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:44