mirror of
https://github.com/esphome/esphome.git
synced 2024-11-14 02:58:11 +01:00
[remote_transmitter] accurate pulse timing for ESP8266 (#2476)
This commit is contained in:
parent
4b7fe202ec
commit
21c896d8f8
2 changed files with 46 additions and 35 deletions
|
@ -32,6 +32,9 @@ class RemoteTransmitterComponent : public remote_base::RemoteTransmitterBase,
|
||||||
void mark_(uint32_t on_time, uint32_t off_time, uint32_t usec);
|
void mark_(uint32_t on_time, uint32_t off_time, uint32_t usec);
|
||||||
|
|
||||||
void space_(uint32_t usec);
|
void space_(uint32_t usec);
|
||||||
|
|
||||||
|
void await_target_time_();
|
||||||
|
uint32_t target_time_;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
|
|
|
@ -33,56 +33,64 @@ void RemoteTransmitterComponent::calculate_on_off_time_(uint32_t carrier_frequen
|
||||||
*off_time_period = period - *on_time_period;
|
*off_time_period = period - *on_time_period;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RemoteTransmitterComponent::await_target_time_() {
|
||||||
|
const uint32_t current_time = micros();
|
||||||
|
if (this->target_time_ == 0)
|
||||||
|
this->target_time_ = current_time;
|
||||||
|
else if (this->target_time_ > current_time)
|
||||||
|
delayMicroseconds(this->target_time_ - current_time);
|
||||||
|
}
|
||||||
|
|
||||||
void RemoteTransmitterComponent::mark_(uint32_t on_time, uint32_t off_time, uint32_t usec) {
|
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->await_target_time_();
|
||||||
this->pin_->digital_write(true);
|
this->pin_->digital_write(true);
|
||||||
delayMicroseconds(usec);
|
|
||||||
this->pin_->digital_write(false);
|
const uint32_t target = this->target_time_ + usec;
|
||||||
return;
|
if (this->carrier_duty_percent_ < 100 && (on_time > 0 || off_time > 0)) {
|
||||||
}
|
while (true) { // Modulate with carrier frequency
|
||||||
|
this->target_time_ += on_time;
|
||||||
const uint32_t start_time = micros();
|
if (this->target_time_ >= target)
|
||||||
uint32_t current_time = start_time;
|
break;
|
||||||
|
this->await_target_time_();
|
||||||
while (current_time - start_time < usec) {
|
this->pin_->digital_write(false);
|
||||||
const uint32_t elapsed = current_time - start_time;
|
|
||||||
this->pin_->digital_write(true);
|
this->target_time_ += off_time;
|
||||||
|
if (this->target_time_ >= target)
|
||||||
delayMicroseconds(std::min(on_time, usec - elapsed));
|
break;
|
||||||
this->pin_->digital_write(false);
|
this->await_target_time_();
|
||||||
if (elapsed + on_time >= usec)
|
this->pin_->digital_write(true);
|
||||||
return;
|
}
|
||||||
|
|
||||||
delayMicroseconds(std::min(usec - elapsed - on_time, off_time));
|
|
||||||
|
|
||||||
current_time = micros();
|
|
||||||
}
|
}
|
||||||
|
this->target_time_ = target;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RemoteTransmitterComponent::space_(uint32_t usec) {
|
void RemoteTransmitterComponent::space_(uint32_t usec) {
|
||||||
|
this->await_target_time_();
|
||||||
this->pin_->digital_write(false);
|
this->pin_->digital_write(false);
|
||||||
delayMicroseconds(usec);
|
this->target_time_ += usec;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t send_wait) {
|
void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t send_wait) {
|
||||||
ESP_LOGD(TAG, "Sending remote code...");
|
ESP_LOGD(TAG, "Sending remote code...");
|
||||||
uint32_t on_time, off_time;
|
uint32_t on_time, off_time;
|
||||||
this->calculate_on_off_time_(this->temp_.get_carrier_frequency(), &on_time, &off_time);
|
this->calculate_on_off_time_(this->temp_.get_carrier_frequency(), &on_time, &off_time);
|
||||||
|
this->target_time_ = 0;
|
||||||
for (uint32_t i = 0; i < send_times; i++) {
|
for (uint32_t i = 0; i < send_times; i++) {
|
||||||
{
|
for (int32_t item : this->temp_.get_data()) {
|
||||||
InterruptLock lock;
|
if (item > 0) {
|
||||||
for (int32_t item : this->temp_.get_data()) {
|
const auto length = uint32_t(item);
|
||||||
if (item > 0) {
|
this->mark_(on_time, off_time, length);
|
||||||
const auto length = uint32_t(item);
|
} else {
|
||||||
this->mark_(on_time, off_time, length);
|
const auto length = uint32_t(-item);
|
||||||
} else {
|
this->space_(length);
|
||||||
const auto length = uint32_t(-item);
|
|
||||||
this->space_(length);
|
|
||||||
}
|
|
||||||
App.feed_wdt();
|
|
||||||
}
|
}
|
||||||
|
App.feed_wdt();
|
||||||
}
|
}
|
||||||
|
this->await_target_time_(); // wait for duration of last pulse
|
||||||
|
this->pin_->digital_write(false);
|
||||||
|
|
||||||
if (i + 1 < send_times)
|
if (i + 1 < send_times)
|
||||||
delayMicroseconds(send_wait);
|
this->target_time_ += send_wait;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue