ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
remote_transmitter.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
4
5#if (defined(USE_LIBRETINY) && !defined(USE_RTL87XX) && !defined(REMOTE_TRANSMITTER_BK_PWM)) || \
6 defined(USE_ESP8266) || defined(USE_RP2) || (defined(USE_ESP32) && !SOC_RMT_SUPPORTED)
7
9
10static const char *const TAG = "remote_transmitter";
11
13 this->pin_->setup();
14 this->pin_->digital_write(false);
15}
16
18 ESP_LOGCONFIG(TAG,
19 "Remote Transmitter:\n"
20 " Carrier Duty: %u%%",
22 LOG_PIN(" Pin: ", this->pin_);
23}
24
26 uint32_t *off_time_period) {
27 if (carrier_frequency == 0) {
28 *on_time_period = 0;
29 *off_time_period = 0;
30 return;
31 }
32 uint32_t period = (1000000UL + carrier_frequency / 2) / carrier_frequency; // round(1000000/freq)
33 period = std::max(uint32_t(1), period);
34 *on_time_period = (period * this->carrier_duty_percent_) / 100;
35 *off_time_period = period - *on_time_period;
36}
37
39 const uint32_t current_time = micros();
40 if (this->target_time_ == 0) {
41 this->target_time_ = current_time;
42 } else if ((int32_t) (this->target_time_ - current_time) > 0) {
43 // busy loop is required as interrupts are disabled and delayMicroseconds()
44 // may not work correctly in interrupt-disabled contexts on all platforms
45 while ((int32_t) (this->target_time_ - micros()) > 0)
46 ;
47 }
48}
49
51 this->await_target_time_();
52 this->pin_->digital_write(true);
53
54 const uint32_t target = this->target_time_ + usec;
55 if (this->carrier_duty_percent_ < 100 && (on_time > 0 || off_time > 0)) {
56 while (true) { // Modulate with carrier frequency
57 this->target_time_ += on_time;
58 if ((int32_t) (this->target_time_ - target) >= 0)
59 break;
60 this->await_target_time_();
61 this->pin_->digital_write(false);
62
63 this->target_time_ += off_time;
64 if ((int32_t) (this->target_time_ - target) >= 0)
65 break;
66 this->await_target_time_();
67 this->pin_->digital_write(true);
68 }
69 }
70 this->target_time_ = target;
71}
72
74 this->await_target_time_();
75 this->pin_->digital_write(false);
76 this->target_time_ += usec;
77}
78
80
82 ESP_LOGD(TAG, "Sending remote code");
83 uint32_t on_time, off_time;
84 this->calculate_on_off_time_(this->temp_.get_carrier_frequency(), &on_time, &off_time);
86 for (uint32_t i = 0; i < send_times; i++) {
87 {
89 // Re-anchor every iteration: timing must never span a lock boundary, as micros() can
90 // jump when interrupts are re-enabled between repeats (e.g. LibreTiny's Beken micros()
91 // discards its interrupt-lock correction, stretching the repeat gap by the lock duration)
92 this->target_time_ = 0;
93 for (int32_t item : this->temp_.get_data()) {
94 if (item > 0) {
95 const auto length = uint32_t(item);
96 this->mark_(on_time, off_time, length);
97 } else {
98 const auto length = uint32_t(-item);
99 this->space_(length);
100 }
101 App.feed_wdt();
102 }
103 this->await_target_time_(); // wait for duration of last pulse
104 this->pin_->digital_write(false);
105 }
106
107 if (i + 1 < send_times) {
108 // Wait out the repeat gap with interrupts enabled: wait_time is unbounded user config
109 // (previously this spin ran inside the next iteration's lock, disabling interrupts for
110 // the whole gap). Anchoring after the lock release keeps it exact on all platforms.
111 const uint32_t gap_end = micros() + send_wait;
112 while ((int32_t) (gap_end - micros()) > 0) {
113 App.feed_wdt();
114 }
115 }
116 }
118}
119
120} // namespace esphome::remote_transmitter
121
122#endif
void feed_wdt()
Feed the task watchdog.
virtual void setup()=0
virtual void digital_write(bool value)=0
Helper class to disable interrupts.
Definition helpers.h:2001
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:461
const RawTimings & get_data() const
Definition remote_base.h:31
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)
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
uint16_t length
Definition tt21100.cpp:0
SemaphoreHandle_t lock