mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-13 08:03:32 +02:00
6682c43dfa
## 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).
96 lines
2.8 KiB
C++
96 lines
2.8 KiB
C++
#include "remote_transmitter.h"
|
|
#include "esphome/core/log.h"
|
|
#include "esphome/core/application.h"
|
|
|
|
#ifdef ARDUINO_ARCH_ESP8266
|
|
|
|
namespace esphome {
|
|
namespace remote_transmitter {
|
|
|
|
static const char *TAG = "remote_transmitter";
|
|
|
|
void RemoteTransmitterComponent::setup() {
|
|
this->pin_->setup();
|
|
this->pin_->digital_write(false);
|
|
}
|
|
|
|
void RemoteTransmitterComponent::dump_config() {
|
|
ESP_LOGCONFIG(TAG, "Remote Transmitter...");
|
|
ESP_LOGCONFIG(TAG, " Carrier Duty: %u%%", this->carrier_duty_percent_);
|
|
LOG_PIN(" Pin: ", this->pin_);
|
|
}
|
|
|
|
void RemoteTransmitterComponent::calculate_on_off_time_(uint32_t carrier_frequency, uint32_t *on_time_period,
|
|
uint32_t *off_time_period) {
|
|
if (carrier_frequency == 0) {
|
|
*on_time_period = 0;
|
|
*off_time_period = 0;
|
|
return;
|
|
}
|
|
uint32_t period = (1000000UL + carrier_frequency / 2) / carrier_frequency; // round(1000000/freq)
|
|
period = std::max(uint32_t(1), period);
|
|
*on_time_period = (period * this->carrier_duty_percent_) / 100;
|
|
*off_time_period = period - *on_time_period;
|
|
}
|
|
|
|
void RemoteTransmitterComponent::mark_(uint32_t on_time, uint32_t off_time, uint32_t usec) {
|
|
if (this->carrier_duty_percent_ == 100 || (on_time == 0 && off_time == 0)) {
|
|
this->pin_->digital_write(true);
|
|
delay_microseconds_accurate(usec);
|
|
this->pin_->digital_write(false);
|
|
return;
|
|
}
|
|
|
|
const uint32_t start_time = micros();
|
|
uint32_t current_time = start_time;
|
|
|
|
while (current_time - start_time < usec) {
|
|
const uint32_t elapsed = current_time - start_time;
|
|
this->pin_->digital_write(true);
|
|
|
|
delay_microseconds_accurate(std::min(on_time, usec - elapsed));
|
|
this->pin_->digital_write(false);
|
|
if (elapsed + on_time >= usec)
|
|
return;
|
|
|
|
delay_microseconds_accurate(std::min(usec - elapsed - on_time, off_time));
|
|
|
|
current_time = micros();
|
|
}
|
|
}
|
|
void RemoteTransmitterComponent::space_(uint32_t usec) {
|
|
this->pin_->digital_write(false);
|
|
delay_microseconds_accurate(usec);
|
|
}
|
|
void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t send_wait) {
|
|
for (uint32_t i = 0; i < send_times; i++) {
|
|
uint32_t on_time, off_time;
|
|
this->calculate_on_off_time_(this->temp_.get_carrier_frequency(), &on_time, &off_time);
|
|
ESP_LOGD(TAG, "Sending remote code...");
|
|
|
|
ESP.wdtFeed();
|
|
disable_interrupts();
|
|
for (int32_t item : this->temp_.get_data()) {
|
|
if (item > 0) {
|
|
const auto length = uint32_t(item);
|
|
this->mark_(on_time, off_time, length);
|
|
} else {
|
|
const auto length = uint32_t(-item);
|
|
this->space_(length);
|
|
}
|
|
App.feed_wdt();
|
|
}
|
|
enable_interrupts();
|
|
|
|
if (i + 1 < send_times) {
|
|
delay(send_wait / 1000UL);
|
|
delayMicroseconds(send_wait % 1000UL);
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace remote_transmitter
|
|
} // namespace esphome
|
|
|
|
#endif
|