ESPHome  2024.12.2
remote_transmitter_esp32.cpp
Go to the documentation of this file.
1 #include "remote_transmitter.h"
2 #include "esphome/core/log.h"
4 
5 #ifdef USE_ESP32
6 
7 namespace esphome {
8 namespace remote_transmitter {
9 
10 static const char *const TAG = "remote_transmitter";
11 
13 
15  ESP_LOGCONFIG(TAG, "Remote Transmitter...");
16  ESP_LOGCONFIG(TAG, " Channel: %d", this->channel_);
17  ESP_LOGCONFIG(TAG, " RMT memory blocks: %d", this->mem_block_num_);
18  ESP_LOGCONFIG(TAG, " Clock divider: %u", this->clock_divider_);
19  LOG_PIN(" Pin: ", this->pin_);
20 
21  if (this->current_carrier_frequency_ != 0 && this->carrier_duty_percent_ != 100) {
22  ESP_LOGCONFIG(TAG, " Carrier Duty: %u%%", this->carrier_duty_percent_);
23  }
24 
25  if (this->is_failed()) {
26  ESP_LOGE(TAG, "Configuring RMT driver failed: %s (%s)", esp_err_to_name(this->error_code_),
27  this->error_string_.c_str());
28  }
29 }
30 
32  rmt_config_t c{};
33 
34  this->config_rmt(c);
35  c.rmt_mode = RMT_MODE_TX;
36  c.gpio_num = gpio_num_t(this->pin_->get_pin());
37  c.tx_config.loop_en = false;
38 
39  if (this->current_carrier_frequency_ == 0 || this->carrier_duty_percent_ == 100) {
40  c.tx_config.carrier_en = false;
41  } else {
42  c.tx_config.carrier_en = true;
43  c.tx_config.carrier_freq_hz = this->current_carrier_frequency_;
44  c.tx_config.carrier_duty_percent = this->carrier_duty_percent_;
45  }
46 
47  c.tx_config.idle_output_en = true;
48  if (!this->pin_->is_inverted()) {
49  c.tx_config.carrier_level = RMT_CARRIER_LEVEL_HIGH;
50  c.tx_config.idle_level = RMT_IDLE_LEVEL_LOW;
51  } else {
52  c.tx_config.carrier_level = RMT_CARRIER_LEVEL_LOW;
53  c.tx_config.idle_level = RMT_IDLE_LEVEL_HIGH;
54  this->inverted_ = true;
55  }
56 
57  esp_err_t error = rmt_config(&c);
58  if (error != ESP_OK) {
59  this->error_code_ = error;
60  this->error_string_ = "in rmt_config";
61  this->mark_failed();
62  return;
63  }
64 
65  if (!this->initialized_) {
66  error = rmt_driver_install(this->channel_, 0, 0);
67  if (error != ESP_OK) {
68  this->error_code_ = error;
69  if (error == ESP_ERR_INVALID_STATE) {
70  this->error_string_ = str_sprintf("RMT channel %i is already in use by another component", this->channel_);
71  } else {
72  this->error_string_ = "in rmt_driver_install";
73  }
74  this->mark_failed();
75  return;
76  }
77  this->initialized_ = true;
78  }
79 }
80 
81 void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t send_wait) {
82  if (this->is_failed())
83  return;
84 
87  this->configure_rmt_();
88  }
89 
90  this->rmt_temp_.clear();
91  this->rmt_temp_.reserve((this->temp_.get_data().size() + 1) / 2);
92  uint32_t rmt_i = 0;
93  rmt_item32_t rmt_item;
94 
95  for (int32_t val : this->temp_.get_data()) {
96  bool level = val >= 0;
97  if (!level)
98  val = -val;
99  val = this->from_microseconds_(static_cast<uint32_t>(val));
100 
101  do {
102  int32_t item = std::min(val, int32_t(32767));
103  val -= item;
104 
105  if (rmt_i % 2 == 0) {
106  rmt_item.level0 = static_cast<uint32_t>(level ^ this->inverted_);
107  rmt_item.duration0 = static_cast<uint32_t>(item);
108  } else {
109  rmt_item.level1 = static_cast<uint32_t>(level ^ this->inverted_);
110  rmt_item.duration1 = static_cast<uint32_t>(item);
111  this->rmt_temp_.push_back(rmt_item);
112  }
113  rmt_i++;
114  } while (val != 0);
115  }
116 
117  if (rmt_i % 2 == 1) {
118  rmt_item.level1 = 0;
119  rmt_item.duration1 = 0;
120  this->rmt_temp_.push_back(rmt_item);
121  }
122 
123  if ((this->rmt_temp_.data() == nullptr) || this->rmt_temp_.empty()) {
124  ESP_LOGE(TAG, "Empty data");
125  return;
126  }
127  this->transmit_trigger_->trigger();
128  for (uint32_t i = 0; i < send_times; i++) {
129  esp_err_t error = rmt_write_items(this->channel_, this->rmt_temp_.data(), this->rmt_temp_.size(), true);
130  if (error != ESP_OK) {
131  ESP_LOGW(TAG, "rmt_write_items failed: %s", esp_err_to_name(error));
132  this->status_set_warning();
133  } else {
134  this->status_clear_warning();
135  }
136  if (i + 1 < send_times)
137  delayMicroseconds(send_wait);
138  }
139  this->complete_trigger_->trigger();
140 }
141 
142 } // namespace remote_transmitter
143 } // namespace esphome
144 
145 #endif
RemoteTransmitData temp_
Use same vector for all transmits, avoids many allocations.
Definition: remote_base.h:173
mopeka_std_values val[4]
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
virtual uint8_t get_pin() const =0
std::string str_sprintf(const char *fmt,...)
Definition: helpers.cpp:320
void send_internal(uint32_t send_times, uint32_t send_wait) override
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition: core.cpp:28
const RawTimings & get_data() const
Definition: remote_base.h:36
virtual bool is_inverted() const =0