[remote_transmitter] accurate pulse timing for ESP8266 (#2476)

This commit is contained in:
Carlos Garcia Saura 2021-11-10 23:28:45 +01:00 committed by GitHub
parent 4395d6156d
commit abf3708cc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 35 deletions

View File

@ -32,6 +32,9 @@ class RemoteTransmitterComponent : public remote_base::RemoteTransmitterBase,
void mark_(uint32_t on_time, uint32_t off_time, uint32_t usec);
void space_(uint32_t usec);
void await_target_time_();
uint32_t target_time_;
#endif
#ifdef USE_ESP32

View File

@ -33,56 +33,64 @@ void RemoteTransmitterComponent::calculate_on_off_time_(uint32_t carrier_frequen
*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) {
if (this->carrier_duty_percent_ == 100 || (on_time == 0 && off_time == 0)) {
this->pin_->digital_write(true);
delayMicroseconds(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);
delayMicroseconds(std::min(on_time, usec - elapsed));
this->pin_->digital_write(false);
if (elapsed + on_time >= usec)
return;
delayMicroseconds(std::min(usec - elapsed - on_time, off_time));
current_time = micros();
this->await_target_time_();
this->pin_->digital_write(true);
const uint32_t target = this->target_time_ + usec;
if (this->carrier_duty_percent_ < 100 && (on_time > 0 || off_time > 0)) {
while (true) { // Modulate with carrier frequency
this->target_time_ += on_time;
if (this->target_time_ >= target)
break;
this->await_target_time_();
this->pin_->digital_write(false);
this->target_time_ += off_time;
if (this->target_time_ >= target)
break;
this->await_target_time_();
this->pin_->digital_write(true);
}
}
this->target_time_ = target;
}
void RemoteTransmitterComponent::space_(uint32_t usec) {
this->await_target_time_();
this->pin_->digital_write(false);
delayMicroseconds(usec);
this->target_time_ += usec;
}
void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t send_wait) {
ESP_LOGD(TAG, "Sending remote code...");
uint32_t 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++) {
{
InterruptLock lock;
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();
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();
}
this->await_target_time_(); // wait for duration of last pulse
this->pin_->digital_write(false);
if (i + 1 < send_times)
delayMicroseconds(send_wait);
this->target_time_ += send_wait;
}
}