ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
infrared.h
Go to the documentation of this file.
1#pragma once
2
3// WARNING: This component is EXPERIMENTAL. The API may change at any time
4// without following the normal breaking changes policy. Use at your own risk.
5// Once the API is considered stable, this warning will be removed.
6
10
11#include <vector>
12
13namespace esphome::infrared {
14
16enum InfraredCapability : uint32_t {
17 CAPABILITY_TRANSMITTER = 1 << 0, // Can transmit signals
18 CAPABILITY_RECEIVER = 1 << 1, // Can receive signals
19};
20
22class Infrared;
23
26 public:
27 explicit InfraredCall(Infrared *parent) : parent_(parent) {}
28
31
32 // ===== Raw Timings Methods =====
33 // All set_raw_timings_* methods store pointers/references to external data.
34 // The referenced data must remain valid until perform() completes.
35 // Safe pattern: call.set_raw_timings_xxx(data); call.perform(); // synchronous
36 // Unsafe pattern: call.set_raw_timings_xxx(data); defer([call]() { call.perform(); }); // data may be gone!
37
41 InfraredCall &set_raw_timings(const std::vector<int32_t> &timings);
42
47 InfraredCall &set_raw_timings_base64url(const std::string &base64url);
48
52 InfraredCall &set_raw_timings_packed(const uint8_t *data, uint16_t length, uint16_t count);
53
55 InfraredCall &set_repeat_count(uint32_t count);
56
58 void perform();
59
63 const std::vector<int32_t> &get_raw_timings() const { return *this->raw_timings_; }
65 bool has_raw_timings() const {
66 return this->raw_timings_ != nullptr || this->packed_data_ != nullptr || this->base64url_ptr_ != nullptr;
67 }
69 bool is_packed() const { return this->packed_data_ != nullptr; }
71 bool is_base64url() const { return this->base64url_ptr_ != nullptr; }
73 const std::string &get_base64url_data() const { return *this->base64url_ptr_; }
75 const uint8_t *get_packed_data() const { return this->packed_data_; }
76 uint16_t get_packed_length() const { return this->packed_length_; }
77 uint16_t get_packed_count() const { return this->packed_count_; }
79 uint32_t get_repeat_count() const { return this->repeat_count_; }
80
81 protected:
82 uint32_t repeat_count_{1};
85 // Pointer to vector-based timings (caller-owned, must outlive perform())
86 const std::vector<int32_t> *raw_timings_{nullptr};
87 // Pointer to base64url-encoded string (caller-owned, must outlive perform())
88 const std::string *base64url_ptr_{nullptr};
89 // Pointer to packed protobuf buffer (caller-owned, must outlive perform())
90 const uint8_t *packed_data_{nullptr};
91 uint16_t packed_length_{0};
92 uint16_t packed_count_{0};
93};
94
97 public:
98 bool get_supports_transmitter() const { return this->supports_transmitter_; }
99 void set_supports_transmitter(bool supports) { this->supports_transmitter_ = supports; }
100
101 bool get_supports_receiver() const { return this->supports_receiver_; }
102 void set_supports_receiver(bool supports) { this->supports_receiver_ = supports; }
103
104 protected:
107};
108
111 public:
112 Infrared() = default;
113
114 void setup() override;
115 void dump_config() override;
116 float get_setup_priority() const override { return setup_priority::AFTER_CONNECTION; }
117
119 void set_receiver(remote_base::RemoteReceiverBase *receiver) { this->receiver_ = receiver; }
121 void set_transmitter(remote_base::RemoteTransmitterBase *transmitter) { this->transmitter_ = transmitter; }
122
124 bool has_transmitter() const { return this->transmitter_ != nullptr; }
126 bool has_receiver() const { return this->receiver_ != nullptr; }
127
129 InfraredTraits &get_traits() { return this->traits_; }
130 const InfraredTraits &get_traits() const { return this->traits_; }
131
134
136 uint32_t get_capability_flags() const;
137
139 bool on_receive(remote_base::RemoteReceiveData data) override;
140
141 protected:
142 friend class InfraredCall;
143
145 virtual void control(const InfraredCall &call);
146
147 // Underlying hardware components
150
151 // Traits describing capabilities
153};
154
155} // namespace esphome::infrared
uint16_le_t frequency
Definition bl0942.h:6
InfraredCall - Builder pattern for transmitting infrared signals.
Definition infrared.h:25
InfraredCall & set_raw_timings(const std::vector< int32_t > &timings)
Set the raw timings from a vector (positive = mark, negative = space)
Definition infrared.cpp:19
bool is_packed() const
Check if using packed data format.
Definition infrared.h:69
uint32_t get_repeat_count() const
Get the repeat count.
Definition infrared.h:79
InfraredCall & set_carrier_frequency(uint32_t frequency)
Set the carrier frequency in Hz.
Definition infrared.cpp:14
InfraredCall & set_repeat_count(uint32_t count)
Set the number of times to repeat transmission (1 = transmit once, 2 = transmit twice,...
Definition infrared.cpp:42
bool has_raw_timings() const
Check if raw timings have been set (any format)
Definition infrared.h:65
void perform()
Perform the transmission.
Definition infrared.cpp:47
InfraredCall & set_raw_timings_packed(const uint8_t *data, uint16_t length, uint16_t count)
Set the raw timings from packed protobuf sint32 data (zigzag + varint encoded)
Definition infrared.cpp:33
const uint8_t * get_packed_data() const
Get packed data (only valid if set via set_raw_timings_packed)
Definition infrared.h:75
const optional< uint32_t > & get_carrier_frequency() const
Get the carrier frequency.
Definition infrared.h:61
const std::vector< int32_t > & get_raw_timings() const
Get the raw timings (only valid if set via set_raw_timings)
Definition infrared.h:63
uint16_t get_packed_count() const
Definition infrared.h:77
const std::string * base64url_ptr_
Definition infrared.h:88
optional< uint32_t > carrier_frequency_
Definition infrared.h:84
const uint8_t * packed_data_
Definition infrared.h:90
uint16_t get_packed_length() const
Definition infrared.h:76
InfraredCall & set_raw_timings_base64url(const std::string &base64url)
Set the raw timings from base64url-encoded little-endian int32 data.
Definition infrared.cpp:26
const std::string & get_base64url_data() const
Get the base64url data string.
Definition infrared.h:73
const std::vector< int32_t > * raw_timings_
Definition infrared.h:86
InfraredCall(Infrared *parent)
Definition infrared.h:27
bool is_base64url() const
Check if using base64url data format.
Definition infrared.h:71
Infrared - Base class for infrared remote control implementations.
Definition infrared.h:110
void dump_config() override
Definition infrared.cpp:66
remote_base::RemoteReceiverBase * receiver_
Definition infrared.h:148
bool on_receive(remote_base::RemoteReceiveData data) override
Called when IR data is received (from RemoteReceiverListener)
Definition infrared.cpp:149
void set_transmitter(remote_base::RemoteTransmitterBase *transmitter)
Set the remote transmitter component.
Definition infrared.h:121
InfraredCall make_call()
Create a call object for transmitting.
Definition infrared.cpp:75
float get_setup_priority() const override
Definition infrared.h:116
virtual void control(const InfraredCall &call)
Perform the actual transmission (called by InfraredCall)
Definition infrared.cpp:77
remote_base::RemoteTransmitterBase * transmitter_
Definition infrared.h:149
const InfraredTraits & get_traits() const
Definition infrared.h:130
bool has_transmitter() const
Check if this infrared has a transmitter configured.
Definition infrared.h:124
bool has_receiver() const
Check if this infrared has a receiver configured.
Definition infrared.h:126
InfraredTraits & get_traits()
Get the traits for this infrared implementation.
Definition infrared.h:129
uint32_t get_capability_flags() const
Get capability flags for this infrared instance.
Definition infrared.cpp:137
void set_receiver(remote_base::RemoteReceiverBase *receiver)
Set the remote receiver component.
Definition infrared.h:119
InfraredTraits - Describes the capabilities of an infrared implementation.
Definition infrared.h:96
bool get_supports_transmitter() const
Definition infrared.h:98
void set_supports_transmitter(bool supports)
Definition infrared.h:99
void set_supports_receiver(bool supports)
Definition infrared.h:102
InfraredCapability
Capability flags for individual infrared instances.
Definition infrared.h:16
constexpr float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.h:43
uint16_t length
Definition tt21100.cpp:0