ESPHome  2024.4.1
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", esp_err_to_name(this->error_code_));
27  }
28 }
29 
31  rmt_config_t c{};
32 
33  this->config_rmt(c);
34  c.rmt_mode = RMT_MODE_TX;
35  c.gpio_num = gpio_num_t(this->pin_->get_pin());
36  c.tx_config.loop_en = false;
37 
38  if (this->current_carrier_frequency_ == 0 || this->carrier_duty_percent_ == 100) {
39  c.tx_config.carrier_en = false;
40  } else {
41  c.tx_config.carrier_en = true;
42  c.tx_config.carrier_freq_hz = this->current_carrier_frequency_;
43  c.tx_config.carrier_duty_percent = this->carrier_duty_percent_;
44  }
45 
46  c.tx_config.idle_output_en = true;
47  if (!this->pin_->is_inverted()) {
48  c.tx_config.carrier_level = RMT_CARRIER_LEVEL_HIGH;
49  c.tx_config.idle_level = RMT_IDLE_LEVEL_LOW;
50  } else {
51  c.tx_config.carrier_level = RMT_CARRIER_LEVEL_LOW;
52  c.tx_config.idle_level = RMT_IDLE_LEVEL_HIGH;
53  this->inverted_ = true;
54  }
55 
56  esp_err_t error = rmt_config(&c);
57  if (error != ESP_OK) {
58  this->error_code_ = error;
59  this->mark_failed();
60  return;
61  }
62 
63  if (!this->initialized_) {
64  error = rmt_driver_install(this->channel_, 0, 0);
65  if (error != ESP_OK) {
66  this->error_code_ = error;
67  this->mark_failed();
68  return;
69  }
70  this->initialized_ = true;
71  }
72 }
73 
74 void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t send_wait) {
75  if (this->is_failed())
76  return;
77 
80  this->configure_rmt_();
81  }
82 
83  this->rmt_temp_.clear();
84  this->rmt_temp_.reserve((this->temp_.get_data().size() + 1) / 2);
85  uint32_t rmt_i = 0;
86  rmt_item32_t rmt_item;
87 
88  for (int32_t val : this->temp_.get_data()) {
89  bool level = val >= 0;
90  if (!level)
91  val = -val;
92  val = this->from_microseconds_(static_cast<uint32_t>(val));
93 
94  do {
95  int32_t item = std::min(val, int32_t(32767));
96  val -= item;
97 
98  if (rmt_i % 2 == 0) {
99  rmt_item.level0 = static_cast<uint32_t>(level ^ this->inverted_);
100  rmt_item.duration0 = static_cast<uint32_t>(item);
101  } else {
102  rmt_item.level1 = static_cast<uint32_t>(level ^ this->inverted_);
103  rmt_item.duration1 = static_cast<uint32_t>(item);
104  this->rmt_temp_.push_back(rmt_item);
105  }
106  rmt_i++;
107  } while (val != 0);
108  }
109 
110  if (rmt_i % 2 == 1) {
111  rmt_item.level1 = 0;
112  rmt_item.duration1 = 0;
113  this->rmt_temp_.push_back(rmt_item);
114  }
115 
116  if ((this->rmt_temp_.data() == nullptr) || this->rmt_temp_.empty()) {
117  ESP_LOGE(TAG, "Empty data");
118  return;
119  }
120  for (uint32_t i = 0; i < send_times; i++) {
121  esp_err_t error = rmt_write_items(this->channel_, this->rmt_temp_.data(), this->rmt_temp_.size(), true);
122  if (error != ESP_OK) {
123  ESP_LOGW(TAG, "rmt_write_items failed: %s", esp_err_to_name(error));
124  this->status_set_warning();
125  } else {
126  this->status_clear_warning();
127  }
128  if (i + 1 < send_times)
129  delayMicroseconds(send_wait);
130  }
131 }
132 
133 } // namespace remote_transmitter
134 } // namespace esphome
135 
136 #endif
RemoteTransmitData temp_
Use same vector for all transmits, avoids many allocations.
Definition: remote_base.h:146
mopeka_std_values val[4]
virtual uint8_t get_pin() const =0
void send_internal(uint32_t send_times, uint32_t send_wait) override
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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:31
virtual bool is_inverted() const =0