mirror of
https://github.com/esphome/esphome.git
synced 2024-12-23 16:47:57 +01:00
[remote_transmitter] accurate pulse timing for ESP8266 (#2476)
This commit is contained in:
parent
4395d6156d
commit
abf3708cc2
@ -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,42 +33,49 @@ 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);
|
|
||||||
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);
|
this->pin_->digital_write(true);
|
||||||
|
|
||||||
delayMicroseconds(std::min(on_time, usec - elapsed));
|
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->pin_->digital_write(false);
|
||||||
if (elapsed + on_time >= usec)
|
|
||||||
return;
|
|
||||||
|
|
||||||
delayMicroseconds(std::min(usec - elapsed - on_time, off_time));
|
this->target_time_ += off_time;
|
||||||
|
if (this->target_time_ >= target)
|
||||||
current_time = micros();
|
break;
|
||||||
|
this->await_target_time_();
|
||||||
|
this->pin_->digital_write(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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++) {
|
||||||
{
|
|
||||||
InterruptLock lock;
|
|
||||||
for (int32_t item : this->temp_.get_data()) {
|
for (int32_t item : this->temp_.get_data()) {
|
||||||
if (item > 0) {
|
if (item > 0) {
|
||||||
const auto length = uint32_t(item);
|
const auto length = uint32_t(item);
|
||||||
@ -79,10 +86,11 @@ void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t sen
|
|||||||
}
|
}
|
||||||
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 New Issue
Block a user