ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
remote_transmitter.cpp
Go to the documentation of this file.
2#include <optional>
3#include "esphome/core/log.h"
5
6#if (defined(USE_LIBRETINY) && !defined(USE_RTL87XX) && !defined(REMOTE_TRANSMITTER_BK_PWM)) || \
7 defined(USE_ESP8266) || defined(USE_RP2) || (defined(USE_ESP32) && !SOC_RMT_SUPPORTED)
8
10
11ESPHOME_LOG_TAG(TAG, "remote_transmitter");
12// Holding interrupts off about 1 ms makes the ESP8266 Wi-Fi SDK hang until a watchdog resets the chip, so a frame
13// is not locked whole there: interrupts are on until EDGE_LOCK_US before each edge. Elsewhere micros() can jump
14// when interrupts come back on, so the whole frame stays locked.
15#ifdef USE_ESP8266
16static constexpr bool LOCK_PER_EDGE = true;
17#else
18static constexpr bool LOCK_PER_EDGE = false;
19#endif
20static constexpr int32_t EDGE_LOCK_US = 50;
21
23 this->pin_->setup();
24 this->pin_->digital_write(false);
25}
26
28 ESP_LOGCONFIG(TAG,
29 "Remote Transmitter:\n"
30 " Carrier Duty: %u%%",
32 LOG_PIN(" Pin: ", this->pin_);
33}
34
36 uint32_t *off_time_period) {
37 if (carrier_frequency == 0) {
38 *on_time_period = 0;
39 *off_time_period = 0;
40 return;
41 }
42 uint32_t period = (1000000UL + carrier_frequency / 2) / carrier_frequency; // round(1000000/freq)
43 period = std::max(uint32_t(1), period);
44 *on_time_period = (period * this->carrier_duty_percent_) / 100;
45 *off_time_period = period - *on_time_period;
46}
47
49 const uint32_t current_time = micros();
50 if (this->target_time_ == 0) {
51 this->target_time_ = current_time;
52 } else if ((int32_t) (this->target_time_ - current_time) > lead_us) {
53 // busy loop, as interrupts can be disabled here and delayMicroseconds()
54 // may not work correctly in interrupt-disabled contexts on all platforms
55 while ((int32_t) (this->target_time_ - micros()) > lead_us)
56 ;
57 }
58}
59
61 std::optional<InterruptLock> edge_lock;
62 if (LOCK_PER_EDGE) {
63 // The first edge anchors the frame, so it takes the time under the lock, right before the write
64 if (this->target_time_ != 0)
65 this->await_target_time_(EDGE_LOCK_US);
66 edge_lock.emplace();
67 }
68 this->await_target_time_();
69 this->pin_->digital_write(value);
70}
71
73 this->write_at_target_(true);
74
75 const uint32_t target = this->target_time_ + usec;
76 if (this->carrier_duty_percent_ < 100 && (on_time > 0 || off_time > 0)) {
77 while (true) { // Modulate with carrier frequency
78 // When an interrupt made the falling edge late by a whole period or more, skip the periods it used up
79 // instead of catching up with runt pulses. Computed once from one timestamp; lateness under a period
80 // (call overhead at a fast carrier) and an on time that rounds to 0 are left alone. With a frame lock no
81 // interrupt can delay an edge, so the check is compiled out there.
82 if (LOCK_PER_EDGE && on_time > 0) {
83 const uint32_t period = on_time + off_time;
84 const uint32_t late = micros() - (this->target_time_ + on_time);
85 if ((int32_t) late >= (int32_t) period)
86 this->target_time_ += (late / period + 1) * period;
87 }
88 this->target_time_ += on_time;
89 if ((int32_t) (this->target_time_ - target) >= 0)
90 break;
91 this->write_at_target_(false);
92
93 this->target_time_ += off_time;
94 if ((int32_t) (this->target_time_ - target) >= 0)
95 break;
96 this->write_at_target_(true);
97 }
98 }
99 this->target_time_ = target;
100}
101
103 this->write_at_target_(false);
104 this->target_time_ += usec;
105}
106
108
110 ESP_LOGD(TAG, "Sending remote code");
111 uint32_t on_time, off_time;
112 this->calculate_on_off_time_(this->temp_.get_carrier_frequency(), &on_time, &off_time);
114 for (uint32_t i = 0; i < send_times; i++) {
115 {
116 std::optional<InterruptLock> frame_lock;
117 if (!LOCK_PER_EDGE)
118 frame_lock.emplace();
119 // Re-anchor every iteration: outside ESP8266 timing must never span a lock boundary, as micros() can
120 // jump when interrupts are re-enabled between repeats (e.g. LibreTiny's Beken micros()
121 // discards its interrupt-lock correction, stretching the repeat gap by the lock duration).
122 // ESP8266 micros() does not jump, so there the lock is taken per edge in write_at_target_().
123 this->target_time_ = 0;
124 for (int32_t item : this->temp_.get_data()) {
125 if (item > 0) {
126 const auto length = uint32_t(item);
127 this->mark_(on_time, off_time, length);
128 } else {
129 const auto length = uint32_t(-item);
130 this->space_(length);
131 }
132 App.feed_wdt();
133 }
134 this->write_at_target_(false); // wait for duration of last pulse
135 }
136
137 if (i + 1 < send_times) {
138 // Wait out the repeat gap with interrupts enabled: wait_time is unbounded user config
139 // (previously this spin ran inside the next iteration's lock, disabling interrupts for
140 // the whole gap). Anchoring after the lock release keeps it exact on all platforms.
141 const uint32_t gap_end = micros() + send_wait;
142 while ((int32_t) (gap_end - micros()) > 0) {
143 App.feed_wdt();
144 }
145 }
146 }
147 this->fire_complete_(send_times != 0); // same answer as the ISR backend for a frame sent zero times
148}
149
150} // namespace esphome::remote_transmitter
151
152#endif
void feed_wdt()
Feed the task watchdog.
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:557
void digital_write(bool value) override
Definition gpio.cpp:215
const RawTimings & get_data() const
Definition remote_base.h:66
RemoteTransmitData temp_
Use same vector for all transmits, avoids many allocations.
void send_internal(uint32_t send_times, uint32_t send_wait) override
void calculate_on_off_time_(uint32_t carrier_frequency, uint32_t *on_time_period, uint32_t *off_time_period)
void mark_(uint32_t on_time, uint32_t off_time, uint32_t usec)
ESPHOME_LOG_TAG(TAG, "remote_transmitter")
uint32_t value
Definition hal.cpp:41
uint32_t IRAM_ATTR HOT micros()
Definition hal.cpp:43
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t