ESPHome 2026.3.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_ESP8266) || defined(USE_RP2040) || (defined(USE_ESP32) && !SOC_RMT_SUPPORTED)
6
8
9static const char *const TAG = "remote_transmitter";
10
12 this->pin_->setup();
13 this->pin_->digital_write(false);
14}
15
17 ESP_LOGCONFIG(TAG,
18 "Remote Transmitter:\n"
19 " Carrier Duty: %u%%",
21 LOG_PIN(" Pin: ", this->pin_);
22}
23
24void RemoteTransmitterComponent::calculate_on_off_time_(uint32_t carrier_frequency, uint32_t *on_time_period,
25 uint32_t *off_time_period) {
26 if (carrier_frequency == 0) {
27 *on_time_period = 0;
28 *off_time_period = 0;
29 return;
30 }
31 uint32_t period = (1000000UL + carrier_frequency / 2) / carrier_frequency; // round(1000000/freq)
32 period = std::max(uint32_t(1), period);
33 *on_time_period = (period * this->carrier_duty_percent_) / 100;
34 *off_time_period = period - *on_time_period;
35}
36
38 const uint32_t current_time = micros();
39 if (this->target_time_ == 0) {
40 this->target_time_ = current_time;
41 } else if ((int32_t) (this->target_time_ - current_time) > 0) {
42 // busy loop is required as interrupts are disabled and delayMicroseconds()
43 // may not work correctly in interrupt-disabled contexts on all platforms
44 while ((int32_t) (this->target_time_ - micros()) > 0)
45 ;
46 }
47}
48
49void RemoteTransmitterComponent::mark_(uint32_t on_time, uint32_t off_time, uint32_t usec) {
50 this->await_target_time_();
51 this->pin_->digital_write(true);
52
53 const uint32_t target = this->target_time_ + usec;
54 if (this->carrier_duty_percent_ < 100 && (on_time > 0 || off_time > 0)) {
55 while (true) { // Modulate with carrier frequency
56 this->target_time_ += on_time;
57 if ((int32_t) (this->target_time_ - target) >= 0)
58 break;
59 this->await_target_time_();
60 this->pin_->digital_write(false);
61
62 this->target_time_ += off_time;
63 if ((int32_t) (this->target_time_ - target) >= 0)
64 break;
65 this->await_target_time_();
66 this->pin_->digital_write(true);
67 }
68 }
69 this->target_time_ = target;
70}
71
73 this->await_target_time_();
74 this->pin_->digital_write(false);
75 this->target_time_ += usec;
76}
77
79
80void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t send_wait) {
81 ESP_LOGD(TAG, "Sending remote code");
82 uint32_t on_time, off_time;
83 this->calculate_on_off_time_(this->temp_.get_carrier_frequency(), &on_time, &off_time);
84 this->target_time_ = 0;
86 for (uint32_t i = 0; i < send_times; i++) {
87 InterruptLock lock;
88 for (int32_t item : this->temp_.get_data()) {
89 if (item > 0) {
90 const auto length = uint32_t(item);
91 this->mark_(on_time, off_time, length);
92 } else {
93 const auto length = uint32_t(-item);
94 this->space_(length);
95 }
96 App.feed_wdt();
97 }
98 this->await_target_time_(); // wait for duration of last pulse
99 this->pin_->digital_write(false);
100
101 if (i + 1 < send_times)
102 this->target_time_ += send_wait;
103 }
105}
106
107} // namespace esphome::remote_transmitter
108
109#endif
void feed_wdt(uint32_t time=0)
virtual void setup()=0
virtual void digital_write(bool value)=0
Helper class to disable interrupts.
Definition helpers.h:1694
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:325
const RawTimings & get_data() const
Definition remote_base.h:32
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 core.cpp:28
Application App
Global storage of Application pointer - only one Application can exist.
uint16_t length
Definition tt21100.cpp:0