M5Unit-INFRARED 0.3.0 git rev:2148adc
Loading...
Searching...
No Matches
ir_codec.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_IR_CODEC_HPP
11#define M5_UNIT_INFRARED_IR_CODEC_HPP
12
13#include <M5UnitComponent.hpp>
14#include <cstdint>
15#include <vector>
16
17namespace m5 {
18namespace unit {
19namespace ir {
20
26enum class CodecType : uint8_t {
27 NEC = 0,
28 SIRC,
29 RC5,
30 RC6,
31 Panasonic,
33 Raw,
34 // Built-in protocol IDs 7..253 are reserved.
35 Unknown = 254,
36 Custom = 255,
37};
38
44 CodecType protocol{CodecType::Unknown};
45 uint16_t address{};
46 uint16_t command{};
47 uint64_t raw{};
48 uint8_t bits{};
49 bool repeat{};
50 bool toggle{};
51};
52
53using item_container_type = std::vector<m5::unit::gpio::m5_rmt_item_t>;
54
61class IRCodec {
62public:
67 explicit IRCodec(const CodecType t) : _type(t)
68 {
69 }
71 virtual ~IRCodec() = default;
72
78 {
79 return _type;
80 }
81
84
91 virtual item_container_type encode(uint16_t address, uint16_t command, bool repeat = false) = 0;
92
97 virtual uint32_t carrierFrequencyHz() const = 0;
98
103 virtual float carrierDuty() const
104 {
105 return 0.33f;
106 }
107
113 virtual uint8_t minFrames() const
114 {
115 return 1;
116 }
117
123 virtual uint16_t frameGapUs() const
124 {
125 return 0;
126 }
128
131
138 virtual bool decode(const m5::unit::gpio::m5_rmt_item_t* items, uint32_t num, DecodeResult& result) = 0;
140
141private:
142 CodecType _type;
143};
144
145} // namespace ir
146} // namespace unit
147} // namespace m5
148#endif
Abstract base class for IR protocol encoding/decoding.
Definition ir_codec.hpp:61
virtual float carrierDuty() const
Carrier duty cycle (0.0 - 1.0)
Definition ir_codec.hpp:103
virtual uint8_t minFrames() const
Number of frames that should be transmitted per keypress for this protocol.
Definition ir_codec.hpp:113
virtual item_container_type encode(uint16_t address, uint16_t command, bool repeat=false)=0
Encode IR command into RMT items.
virtual ~IRCodec()=default
Destructor.
virtual bool decode(const m5::unit::gpio::m5_rmt_item_t *items, uint32_t num, DecodeResult &result)=0
Try to decode RMT items into an IR command.
virtual uint32_t carrierFrequencyHz() const =0
Carrier frequency for this protocol in Hz.
virtual uint16_t frameGapUs() const
Inter-frame gap (microseconds) when transmitting multiple frames in one burst.
Definition ir_codec.hpp:123
CodecType type() const
Get codec type for safe downcasting.
Definition ir_codec.hpp:77
IRCodec(const CodecType t)
Constructor.
Definition ir_codec.hpp:67
std::vector< m5::unit::gpio::m5_rmt_item_t > item_container_type
RMT item container.
Definition ir_codec.hpp:53
CodecType
Identifies the IR protocol codec implementation.
Definition ir_codec.hpp:26
@ SIRC
Sony SIRC (12/15/20-bit)
@ Mitsubishi
Mitsubishi (16-bit, no leader, sent twice)
@ Panasonic
Panasonic / Kaseikyo (48-bit, customer code + data)
@ RC5
Philips RC5 / RC5X.
@ Raw
Raw mark/space (no protocol)
@ NEC
NEC / Extended NEC.
@ Unknown
Unknown protocol.
@ Custom
User-defined custom protocol.
@ RC6
Philips RC6 Mode 0.
Top level namespace of M5Stack.
Unit-related namespace.
Result of decoding a received IR frame.
Definition ir_codec.hpp:43
uint64_t raw
Raw encoded value (protocol-specific, up to 64 bits)
Definition ir_codec.hpp:47
uint8_t bits
Total bit count (NEC=32, SIRC=12/15/20, RC5=14, RC6=21)
Definition ir_codec.hpp:48
uint16_t command
Command code.
Definition ir_codec.hpp:46
uint16_t address
Device address.
Definition ir_codec.hpp:45
CodecType protocol
Detected protocol.
Definition ir_codec.hpp:44
bool toggle
Toggle bit (RC5/RC6 only)
Definition ir_codec.hpp:50
bool repeat
True if repeat frame (NEC repeat code / re-sent frame)
Definition ir_codec.hpp:49