ESPHome 2026.1.0b1
Loading...
Searching...
No Matches
infrared.cpp
Go to the documentation of this file.
1#include "infrared.h"
2#include "esphome/core/log.h"
3
4#ifdef USE_API
6#endif
7
9
10static const char *const TAG = "infrared";
11
12// ========== InfraredCall ==========
13
18
19InfraredCall &InfraredCall::set_raw_timings(const std::vector<int32_t> &timings) {
20 this->raw_timings_ = &timings;
21 this->packed_data_ = nullptr; // Clear packed if vector is set
22 return *this;
23}
24
25InfraredCall &InfraredCall::set_raw_timings_packed(const uint8_t *data, uint16_t length, uint16_t count) {
26 this->packed_data_ = data;
27 this->packed_length_ = length;
28 this->packed_count_ = count;
29 this->raw_timings_ = nullptr; // Clear vector if packed is set
30 return *this;
31}
32
34 this->repeat_count_ = count;
35 return *this;
36}
37
39 if (this->parent_ != nullptr) {
40 this->parent_->control(*this);
41 }
42}
43
44// ========== Infrared ==========
45
47 // Set up traits based on configuration
50
51 // Register as listener for received IR data
52 if (this->receiver_ != nullptr) {
53 this->receiver_->register_listener(this);
54 }
55}
56
58 ESP_LOGCONFIG(TAG,
59 "Infrared '%s'\n"
60 " Supports Transmitter: %s\n"
61 " Supports Receiver: %s",
62 this->get_name().c_str(), YESNO(this->traits_.get_supports_transmitter()),
63 YESNO(this->traits_.get_supports_receiver()));
64}
65
67
69 if (this->transmitter_ == nullptr) {
70 ESP_LOGW(TAG, "No transmitter configured");
71 return;
72 }
73
74 if (!call.has_raw_timings()) {
75 ESP_LOGE(TAG, "No raw timings provided");
76 return;
77 }
78
79 // Create transmit data object
80 auto transmit_call = this->transmitter_->transmit();
81 auto *transmit_data = transmit_call.get_data();
82
83 // Set carrier frequency
84 if (call.get_carrier_frequency().has_value()) {
85 transmit_data->set_carrier_frequency(call.get_carrier_frequency().value());
86 }
87
88 // Set timings based on format
89 if (call.is_packed()) {
90 // Zero-copy from packed protobuf data
91 transmit_data->set_data_from_packed_sint32(call.get_packed_data(), call.get_packed_length(),
92 call.get_packed_count());
93 ESP_LOGD(TAG, "Transmitting packed raw timings: count=%u, repeat=%u", call.get_packed_count(),
94 call.get_repeat_count());
95 } else {
96 // From vector (lambdas/automations)
97 transmit_data->set_data(call.get_raw_timings());
98 ESP_LOGD(TAG, "Transmitting raw timings: count=%zu, repeat=%u", call.get_raw_timings().size(),
99 call.get_repeat_count());
100 }
101
102 // Set repeat count
103 if (call.get_repeat_count() > 0) {
104 transmit_call.set_send_times(call.get_repeat_count());
105 }
106
107 // Perform transmission
108 transmit_call.perform();
109}
110
112 uint32_t flags = 0;
113
114 // Add transmit/receive capability based on traits
117 if (this->traits_.get_supports_receiver())
119
120 return flags;
121}
122
124 // Forward received IR data to API server
125#if defined(USE_API) && defined(USE_IR_RF)
126 if (api::global_api_server != nullptr) {
127#ifdef USE_DEVICES
128 uint32_t device_id = this->get_device_id();
129#else
130 uint32_t device_id = 0;
131#endif
133 }
134#endif
135 return false; // Don't consume the event, allow other listeners to process it
136}
137
138} // namespace esphome::infrared
uint16_le_t frequency
Definition bl0942.h:6
ESPDEPRECATED("object_id mangles names and all object_id methods are planned for removal " "(see https://github.com/esphome/backlog/issues/76). " "Now is the time to stop using object_id. If still needed, use get_object_id_to() " "which will remain available longer. get_object_id() will be removed in 2026.7.0", "2025.12.0") std uint32_t get_object_id_hash()
const StringRef & get_name() const
uint32_t get_device_id() const
void send_infrared_rf_receive_event(uint32_t device_id, uint32_t key, const std::vector< int32_t > *timings)
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 (positive = mark, negative = space) Note: The timings vector must outlive the Inf...
Definition infrared.cpp:19
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:33
void perform()
Perform the transmission.
Definition infrared.cpp:38
InfraredCall & set_raw_timings_packed(const uint8_t *data, uint16_t length, uint16_t count)
Set the raw timings from packed protobuf sint32 data (zero-copy from wire) Note: The data must outliv...
Definition infrared.cpp:25
optional< uint32_t > carrier_frequency_
Definition infrared.h:61
const uint8_t * packed_data_
Definition infrared.h:65
const std::vector< int32_t > * raw_timings_
Definition infrared.h:63
void dump_config() override
Definition infrared.cpp:57
remote_base::RemoteReceiverBase * receiver_
Definition infrared.h:123
bool on_receive(remote_base::RemoteReceiveData data) override
Called when IR data is received (from RemoteReceiverListener)
Definition infrared.cpp:123
InfraredCall make_call()
Create a call object for transmitting.
Definition infrared.cpp:66
virtual void control(const InfraredCall &call)
Perform the actual transmission (called by InfraredCall)
Definition infrared.cpp:68
remote_base::RemoteTransmitterBase * transmitter_
Definition infrared.h:124
bool has_transmitter() const
Check if this infrared has a transmitter configured.
Definition infrared.h:99
bool has_receiver() const
Check if this infrared has a receiver configured.
Definition infrared.h:101
uint32_t get_capability_flags() const
Get capability flags for this infrared instance.
Definition infrared.cpp:111
bool get_supports_transmitter() const
Definition infrared.h:73
void set_supports_transmitter(bool supports)
Definition infrared.h:74
void set_supports_receiver(bool supports)
Definition infrared.h:77
const RawTimings & get_raw_data() const
Definition remote_base.h:54
void register_listener(RemoteReceiverListener *listener)
uint16_t flags
APIServer * global_api_server
uint16_t length
Definition tt21100.cpp:0