1
0
mirror of https://github.com/esphome/esphome.git synced 2025-02-24 13:58:14 +00:00
esphome/esphome/components/remote_receiver/remote_receiver_esp32.cpp
Otto Winter 6682c43dfa
🏗 Merge C++ into python codebase (#504)
## Description:

Move esphome-core codebase into esphome (and a bunch of other refactors). See https://github.com/esphome/feature-requests/issues/97

Yes this is a shit ton of work and no there's no way to automate it :( But it will be worth it 👍

Progress:
- Core support (file copy etc): 80%
- Base Abstractions (light, switch): ~50%
- Integrations: ~10%
- Working? Yes, (but only with ported components).

Other refactors:
- Moves all codegen related stuff into a single class: `esphome.codegen` (imported as `cg`)
- Rework coroutine syntax
- Move from `component/platform.py` to `domain/component.py` structure as with HA
- Move all defaults out of C++ and into config validation.
- Remove `make_...` helpers from Application class. Reason: Merge conflicts with every single new integration.
- Pointer Variables are stored globally instead of locally in setup(). Reason: stack size limit.

Future work:
- Rework const.py - Move all `CONF_...` into a conf class (usage `conf.UPDATE_INTERVAL` vs `CONF_UPDATE_INTERVAL`). Reason: Less convoluted import block
- Enable loading from `custom_components` folder.

**Related issue (if applicable):** https://github.com/esphome/feature-requests/issues/97

**Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#<esphome-docs PR number goes here>

## Checklist:
  - [ ] The code change is tested and works locally.
  - [ ] Tests have been added to verify that the new code works (under `tests/` folder).

If user exposed functionality or configuration variables are added/changed:
  - [ ] Documentation added/updated in [esphomedocs](https://github.com/OttoWinter/esphomedocs).
2019-04-17 12:06:00 +02:00

155 lines
4.6 KiB
C++

#include "remote_receiver.h"
#include "esphome/core/log.h"
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace remote_receiver {
static const char *TAG = "remote_receiver.esp32";
void RemoteReceiverComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up Remote Receiver...");
rmt_config_t rmt{};
rmt.channel = this->channel_;
rmt.gpio_num = gpio_num_t(this->pin_->get_pin());
rmt.clk_div = this->clock_divider_;
rmt.mem_block_num = 1;
rmt.rmt_mode = RMT_MODE_RX;
if (this->filter_us_ == 0) {
rmt.rx_config.filter_en = false;
} else {
rmt.rx_config.filter_en = true;
rmt.rx_config.filter_ticks_thresh = this->from_microseconds(this->filter_us_);
}
rmt.rx_config.idle_threshold = this->from_microseconds(this->idle_us_);
esp_err_t error = rmt_config(&rmt);
if (error != ESP_OK) {
this->error_code_ = error;
this->mark_failed();
return;
}
error = rmt_driver_install(this->channel_, this->buffer_size_, 0);
if (error != ESP_OK) {
this->error_code_ = error;
this->mark_failed();
return;
}
error = rmt_get_ringbuf_handle(this->channel_, &this->ringbuf_);
if (error != ESP_OK) {
this->error_code_ = error;
this->mark_failed();
return;
}
error = rmt_rx_start(this->channel_, true);
if (error != ESP_OK) {
this->error_code_ = error;
this->mark_failed();
return;
}
}
void RemoteReceiverComponent::dump_config() {
ESP_LOGCONFIG(TAG, "Remote Receiver:");
LOG_PIN(" Pin: ", this->pin_);
ESP_LOGCONFIG(TAG, " Channel: %d", this->channel_);
ESP_LOGCONFIG(TAG, " Clock divider: %u", this->clock_divider_);
ESP_LOGCONFIG(TAG, " Tolerance: %u%%", this->tolerance_);
ESP_LOGCONFIG(TAG, " Filter out pulses shorter than: %u us", this->filter_us_);
ESP_LOGCONFIG(TAG, " Signal is done after %u us of no changes", this->idle_us_);
if (this->is_failed()) {
ESP_LOGE(TAG, "Configuring RMT driver failed: %s", esp_err_to_name(this->error_code_));
}
}
void RemoteReceiverComponent::loop() {
size_t len = 0;
auto *item = (rmt_item32_t *) xRingbufferReceive(this->ringbuf_, &len, 0);
if (item != nullptr) {
this->decode_rmt_(item, len);
vRingbufferReturnItem(this->ringbuf_, item);
if (this->temp_.empty())
return;
this->call_listeners_dumpers_();
}
}
void RemoteReceiverComponent::decode_rmt_(rmt_item32_t *item, size_t len) {
bool prev_level = false;
uint32_t prev_length = 0;
this->temp_.clear();
int32_t multiplier = this->pin_->is_inverted() ? -1 : 1;
ESP_LOGVV(TAG, "START:");
for (size_t i = 0; i < len; i++) {
if (item[i].level0) {
ESP_LOGVV(TAG, "%u A: ON %uus (%u ticks)", i, this->to_microseconds(item[i].duration0), item[i].duration0);
} else {
ESP_LOGVV(TAG, "%u A: OFF %uus (%u ticks)", i, this->to_microseconds(item[i].duration0), item[i].duration0);
}
if (item[i].level1) {
ESP_LOGVV(TAG, "%u B: ON %uus (%u ticks)", i, this->to_microseconds(item[i].duration1), item[i].duration1);
} else {
ESP_LOGVV(TAG, "%u B: OFF %uus (%u ticks)", i, this->to_microseconds(item[i].duration1), item[i].duration1);
}
}
ESP_LOGVV(TAG, "\n");
this->temp_.reserve(len / 4);
for (size_t i = 0; i < len; i++) {
if (item[i].duration0 == 0u) {
// Do nothing
} else if (bool(item[i].level0) == prev_level) {
prev_length += item[i].duration0;
} else {
if (prev_length > 0) {
if (prev_level) {
this->temp_.push_back(this->to_microseconds(prev_length) * multiplier);
} else {
this->temp_.push_back(-int32_t(this->to_microseconds(prev_length)) * multiplier);
}
}
prev_level = bool(item[i].level0);
prev_length = item[i].duration0;
}
if (this->to_microseconds(prev_length) > this->idle_us_) {
break;
}
if (item[i].duration1 == 0u) {
// Do nothing
} else if (bool(item[i].level1) == prev_level) {
prev_length += item[i].duration1;
} else {
if (prev_length > 0) {
if (prev_level) {
this->temp_.push_back(this->to_microseconds(prev_length) * multiplier);
} else {
this->temp_.push_back(-int32_t(this->to_microseconds(prev_length)) * multiplier);
}
}
prev_level = bool(item[i].level1);
prev_length = item[i].duration1;
}
if (this->to_microseconds(prev_length) > this->idle_us_) {
break;
}
}
if (prev_length > 0) {
if (prev_level) {
this->temp_.push_back(this->to_microseconds(prev_length) * multiplier);
} else {
this->temp_.push_back(-int32_t(this->to_microseconds(prev_length)) * multiplier);
}
}
}
} // namespace remote_receiver
} // namespace esphome
#endif