From 255ac7ddde0a4cb8e3bd380a60b166ae412eccee Mon Sep 17 00:00:00 2001 From: Petar Kozul Date: Thu, 2 May 2019 19:33:45 +1000 Subject: [PATCH] Use safeDelayMicroseconds() for guard delay to avoid wdt resets --- lib/rc-switch/RCSwitch.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/rc-switch/RCSwitch.cpp b/lib/rc-switch/RCSwitch.cpp index c8e7424..31439cd 100644 --- a/lib/rc-switch/RCSwitch.cpp +++ b/lib/rc-switch/RCSwitch.cpp @@ -503,6 +503,26 @@ void RCSwitch::sendTriState(const char* sCodeWord) { this->send(code, length); } +/** + * @param duration no. of microseconds to delay + */ +static inline void safeDelayMicroseconds(unsigned long duration) { +#if defined(ESP8266) || defined(ESP32) + if (duration > 10000) { + // if delay > 10 milliseconds, use yield() to avoid wdt reset + unsigned long start = micros(); + while ((micros() - start) < duration) { + yield(); + } + } + else { + delayMicroseconds(duration); + } +#else + delayMicroseconds(duration); +#endif +} + /** * @param sCodeWord a binary code word consisting of the letter 0, 1 */ @@ -569,7 +589,7 @@ void RCSwitch::send(unsigned long long code, unsigned int length) { // выдерживаем Защитное время (Guard Time) if (protocol.Guard > 0) { digitalWrite(this->nTransmitterPin, LOW); - delayMicroseconds(this->protocol.pulseLength * protocol.Guard); + safeDelayMicroseconds(this->protocol.pulseLength * protocol.Guard); } }