2019-04-17 12:06:00 +02:00
|
|
|
#include "remote_transmitter.h"
|
|
|
|
#include "esphome/core/log.h"
|
|
|
|
#include "esphome/core/application.h"
|
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
#ifdef USE_ESP32
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace remote_transmitter {
|
|
|
|
|
2021-06-10 22:19:44 +02:00
|
|
|
static const char *const TAG = "remote_transmitter";
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2021-09-24 18:02:28 +02:00
|
|
|
void RemoteTransmitterComponent::setup() { this->configure_rmt_(); }
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
void RemoteTransmitterComponent::dump_config() {
|
|
|
|
ESP_LOGCONFIG(TAG, "Remote Transmitter...");
|
|
|
|
ESP_LOGCONFIG(TAG, " Channel: %d", this->channel_);
|
2020-05-01 03:59:51 +02:00
|
|
|
ESP_LOGCONFIG(TAG, " RMT memory blocks: %d", this->mem_block_num_);
|
2019-04-17 12:06:00 +02:00
|
|
|
ESP_LOGCONFIG(TAG, " Clock divider: %u", this->clock_divider_);
|
|
|
|
LOG_PIN(" Pin: ", this->pin_);
|
|
|
|
|
|
|
|
if (this->current_carrier_frequency_ != 0 && this->carrier_duty_percent_ != 100) {
|
|
|
|
ESP_LOGCONFIG(TAG, " Carrier Duty: %u%%", this->carrier_duty_percent_);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->is_failed()) {
|
|
|
|
ESP_LOGE(TAG, "Configuring RMT driver failed: %s", esp_err_to_name(this->error_code_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 18:02:28 +02:00
|
|
|
void RemoteTransmitterComponent::configure_rmt_() {
|
2019-04-17 12:06:00 +02:00
|
|
|
rmt_config_t c{};
|
|
|
|
|
2020-05-01 03:59:51 +02:00
|
|
|
this->config_rmt(c);
|
2019-04-17 12:06:00 +02:00
|
|
|
c.rmt_mode = RMT_MODE_TX;
|
|
|
|
c.gpio_num = gpio_num_t(this->pin_->get_pin());
|
|
|
|
c.tx_config.loop_en = false;
|
|
|
|
|
|
|
|
if (this->current_carrier_frequency_ == 0 || this->carrier_duty_percent_ == 100) {
|
|
|
|
c.tx_config.carrier_en = false;
|
|
|
|
} else {
|
|
|
|
c.tx_config.carrier_en = true;
|
|
|
|
c.tx_config.carrier_freq_hz = this->current_carrier_frequency_;
|
|
|
|
c.tx_config.carrier_duty_percent = this->carrier_duty_percent_;
|
|
|
|
}
|
|
|
|
|
|
|
|
c.tx_config.idle_output_en = true;
|
|
|
|
if (!this->pin_->is_inverted()) {
|
|
|
|
c.tx_config.carrier_level = RMT_CARRIER_LEVEL_HIGH;
|
|
|
|
c.tx_config.idle_level = RMT_IDLE_LEVEL_LOW;
|
|
|
|
} else {
|
|
|
|
c.tx_config.carrier_level = RMT_CARRIER_LEVEL_LOW;
|
|
|
|
c.tx_config.idle_level = RMT_IDLE_LEVEL_HIGH;
|
2021-07-26 09:20:02 +02:00
|
|
|
this->inverted_ = true;
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t error = rmt_config(&c);
|
|
|
|
if (error != ESP_OK) {
|
|
|
|
this->error_code_ = error;
|
|
|
|
this->mark_failed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->initialized_) {
|
|
|
|
error = rmt_driver_install(this->channel_, 0, 0);
|
|
|
|
if (error != ESP_OK) {
|
|
|
|
this->error_code_ = error;
|
|
|
|
this->mark_failed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->initialized_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t send_wait) {
|
|
|
|
if (this->is_failed())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (this->current_carrier_frequency_ != this->temp_.get_carrier_frequency()) {
|
|
|
|
this->current_carrier_frequency_ = this->temp_.get_carrier_frequency();
|
2021-09-24 18:02:28 +02:00
|
|
|
this->configure_rmt_();
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this->rmt_temp_.clear();
|
|
|
|
this->rmt_temp_.reserve((this->temp_.get_data().size() + 1) / 2);
|
|
|
|
uint32_t rmt_i = 0;
|
|
|
|
rmt_item32_t rmt_item;
|
|
|
|
|
|
|
|
for (int32_t val : this->temp_.get_data()) {
|
|
|
|
bool level = val >= 0;
|
|
|
|
if (!level)
|
|
|
|
val = -val;
|
2021-09-24 18:02:28 +02:00
|
|
|
val = this->from_microseconds_(static_cast<uint32_t>(val));
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
do {
|
2021-09-22 20:13:24 +02:00
|
|
|
int32_t item = std::min(val, int32_t(32767));
|
2019-04-17 12:06:00 +02:00
|
|
|
val -= item;
|
|
|
|
|
|
|
|
if (rmt_i % 2 == 0) {
|
2021-07-26 09:20:02 +02:00
|
|
|
rmt_item.level0 = static_cast<uint32_t>(level ^ this->inverted_);
|
2019-04-17 12:06:00 +02:00
|
|
|
rmt_item.duration0 = static_cast<uint32_t>(item);
|
|
|
|
} else {
|
2021-07-26 09:20:02 +02:00
|
|
|
rmt_item.level1 = static_cast<uint32_t>(level ^ this->inverted_);
|
2019-04-17 12:06:00 +02:00
|
|
|
rmt_item.duration1 = static_cast<uint32_t>(item);
|
|
|
|
this->rmt_temp_.push_back(rmt_item);
|
|
|
|
}
|
|
|
|
rmt_i++;
|
|
|
|
} while (val != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rmt_i % 2 == 1) {
|
|
|
|
rmt_item.level1 = 0;
|
|
|
|
rmt_item.duration1 = 0;
|
|
|
|
this->rmt_temp_.push_back(rmt_item);
|
|
|
|
}
|
|
|
|
|
2022-08-23 03:26:36 +02:00
|
|
|
if ((this->rmt_temp_.data() == nullptr) || this->rmt_temp_.empty()) {
|
|
|
|
ESP_LOGE(TAG, "Empty data");
|
|
|
|
return;
|
|
|
|
}
|
2021-12-01 21:03:51 +01:00
|
|
|
for (uint32_t i = 0; i < send_times; i++) {
|
2019-04-17 12:06:00 +02:00
|
|
|
esp_err_t error = rmt_write_items(this->channel_, this->rmt_temp_.data(), this->rmt_temp_.size(), true);
|
|
|
|
if (error != ESP_OK) {
|
|
|
|
ESP_LOGW(TAG, "rmt_write_items failed: %s", esp_err_to_name(error));
|
|
|
|
this->status_set_warning();
|
|
|
|
} else {
|
|
|
|
this->status_clear_warning();
|
|
|
|
}
|
2021-11-10 04:22:00 +01:00
|
|
|
if (i + 1 < send_times)
|
|
|
|
delayMicroseconds(send_wait);
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace remote_transmitter
|
|
|
|
} // namespace esphome
|
|
|
|
|
|
|
|
#endif
|