ESPHome 2025.10.3
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)
6
7namespace esphome {
8namespace remote_transmitter {
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
25void RemoteTransmitterComponent::calculate_on_off_time_(uint32_t carrier_frequency, uint32_t *on_time_period,
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#if defined(USE_LIBRETINY)
44 // busy loop for libretiny is required (see the comment inside micros() in wiring.c)
45 while ((int32_t) (this->target_time_ - micros()) > 0)
46 ;
47#else
48 delayMicroseconds(this->target_time_ - current_time);
49#endif
50 }
51}
52
53void RemoteTransmitterComponent::mark_(uint32_t on_time, uint32_t off_time, uint32_t usec) {
54 this->await_target_time_();
55 this->pin_->digital_write(true);
56
57 const uint32_t target = this->target_time_ + usec;
58 if (this->carrier_duty_percent_ < 100 && (on_time > 0 || off_time > 0)) {
59 while (true) { // Modulate with carrier frequency
60 this->target_time_ += on_time;
61 if ((int32_t) (this->target_time_ - target) >= 0)
62 break;
63 this->await_target_time_();
64 this->pin_->digital_write(false);
65
66 this->target_time_ += off_time;
67 if ((int32_t) (this->target_time_ - target) >= 0)
68 break;
69 this->await_target_time_();
70 this->pin_->digital_write(true);
71 }
72 }
73 this->target_time_ = target;
74}
75
77 this->await_target_time_();
78 this->pin_->digital_write(false);
79 this->target_time_ += usec;
80}
81
83
84void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t send_wait) {
85 ESP_LOGD(TAG, "Sending remote code");
86 uint32_t on_time, off_time;
87 this->calculate_on_off_time_(this->temp_.get_carrier_frequency(), &on_time, &off_time);
88 this->target_time_ = 0;
90 for (uint32_t i = 0; i < send_times; i++) {
91 InterruptLock lock;
92 for (int32_t item : this->temp_.get_data()) {
93 if (item > 0) {
94 const auto length = uint32_t(item);
95 this->mark_(on_time, off_time, length);
96 } else {
97 const auto length = uint32_t(-item);
98 this->space_(length);
99 }
100 App.feed_wdt();
101 }
102 this->await_target_time_(); // wait for duration of last pulse
103 this->pin_->digital_write(false);
104
105 if (i + 1 < send_times)
106 this->target_time_ += send_wait;
107 }
108 this->complete_trigger_->trigger();
109}
110
111} // namespace remote_transmitter
112} // namespace esphome
113
114#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:788
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition automation.h:145
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)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:32
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:31
Application App
Global storage of Application pointer - only one Application can exist.
uint16_t length
Definition tt21100.cpp:0