From f9403c8489b7de69f99c7618e0082f53ac12b513 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 3 Jan 2021 16:30:45 +0100 Subject: [PATCH] Optimize sleepdelay Optimize sleepdelay (#10379) --- tasmota/tasmota.ino | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index 84c2252b1..79e648200 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -367,13 +367,19 @@ void BacklogLoop(void) { void SleepDelay(uint32_t mseconds) { if (mseconds) { - for (uint32_t wait = 0; wait < mseconds; wait++) { - // ESP8266 does an optimistic_yield(1000) in Serial.available() - // ESP32 does not so needs delay here -#ifdef ESP32 - delay(1); + uint32_t wait = millis() + mseconds; + while (!TimeReached(wait)) { +#ifdef ESP8266 + if ((wait - millis()) > 10) { // ESP8266 does an optimistic_yield(10000) in Serial.available() #endif - if (Serial.available()) { break; } // We need to service serial buffer ASAP as otherwise we get uart buffer overrun + if (Serial.available()) { return; } // We need to service serial buffer ASAP as otherwise we get uart buffer overrun +#ifdef ESP8266 + } else { +#endif + delay(1); +#ifdef ESP8266 + } +#endif // ESP8266 } } else { delay(0);