From dd8925ddaf1542c765929dc6202f6c5bfc610834 Mon Sep 17 00:00:00 2001 From: Matthias Walliczek <35632862+mwalliczek@users.noreply.github.com> Date: Wed, 7 Apr 2021 09:04:08 +0200 Subject: [PATCH 1/2] Prevent micros overflow after 70 minutes --- src/ESPiLight.cpp | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/ESPiLight.cpp b/src/ESPiLight.cpp index e936b29..c41caa8 100644 --- a/src/ESPiLight.cpp +++ b/src/ESPiLight.cpp @@ -231,29 +231,31 @@ void ICACHE_RAM_ATTR ESPiLight::interruptHandler() { if (pulseTrain.length == 0) { const unsigned long now = micros(); - const unsigned int duration = now - _lastChange; - - /* We first do some filtering (same as pilight BPF) */ - if (duration > minpulselen) { - if (duration < maxpulselen) { - /* All codes are buffered */ - codes[_nrpulses] = (uint16_t)duration; - _nrpulses = (uint8_t)((_nrpulses + 1) % MAXPULSESTREAMLENGTH); - /* Let's match footers */ - if (duration > mingaplen) { - // Debug('g'); - /* Only match minimal length pulse streams */ - if (_nrpulses >= minrawlen && _nrpulses <= maxrawlen) { - // Debug(_nrpulses); - // Debug('l'); - pulseTrain.length = _nrpulses; - _actualPulseTrain = (_actualPulseTrain + 1) % RECEIVER_BUFFER_SIZE; + if (now > _lastChange) { // prevent overflow after 70 minutes + const unsigned long duration = now - _lastChange; + + /* We first do some filtering (same as pilight BPF) */ + if (duration > minpulselen) { + if (duration < maxpulselen) { + /* All codes are buffered */ + codes[_nrpulses] = (uint16_t)duration; + _nrpulses = (uint8_t)((_nrpulses + 1) % MAXPULSESTREAMLENGTH); + /* Let's match footers */ + if (duration > mingaplen) { + // Debug('g'); + /* Only match minimal length pulse streams */ + if (_nrpulses >= minrawlen && _nrpulses <= maxrawlen) { + // Debug(_nrpulses); + // Debug('l'); + pulseTrain.length = _nrpulses; + _actualPulseTrain = (_actualPulseTrain + 1) % RECEIVER_BUFFER_SIZE; + } + _nrpulses = 0; } - _nrpulses = 0; } } - _lastChange = now; } + _lastChange = now; } else { Debug("_!_"); } @@ -399,7 +401,7 @@ size_t ESPiLight::parsePulseTrain(uint16_t *pulses, uint8_t length) { } /* Reset # of repeats after a certain delay */ - if ((protocol->second - protocol->first) > 500000) { + if (protocol->second < protocol->first || (protocol->second - protocol->first) > 500000) { protocol->repeats = 0; } @@ -417,6 +419,7 @@ size_t ESPiLight::parsePulseTrain(uint16_t *pulses, uint8_t length) { } } pnode = pnode->next; + yield(); } if (_rawCallback != nullptr) { (_rawCallback)(pulses, length); From 59110c0e8d2e9493af4ebffd304150f1b51b8024 Mon Sep 17 00:00:00 2001 From: mwalliczek Date: Wed, 7 Apr 2021 09:23:11 +0200 Subject: [PATCH 2/2] adapt formatting --- src/ESPiLight.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ESPiLight.cpp b/src/ESPiLight.cpp index c41caa8..27b7dad 100644 --- a/src/ESPiLight.cpp +++ b/src/ESPiLight.cpp @@ -231,7 +231,7 @@ void ICACHE_RAM_ATTR ESPiLight::interruptHandler() { if (pulseTrain.length == 0) { const unsigned long now = micros(); - if (now > _lastChange) { // prevent overflow after 70 minutes + if (now > _lastChange) { // prevent overflow after 70 minutes const unsigned long duration = now - _lastChange; /* We first do some filtering (same as pilight BPF) */ @@ -248,7 +248,8 @@ void ICACHE_RAM_ATTR ESPiLight::interruptHandler() { // Debug(_nrpulses); // Debug('l'); pulseTrain.length = _nrpulses; - _actualPulseTrain = (_actualPulseTrain + 1) % RECEIVER_BUFFER_SIZE; + _actualPulseTrain = + (_actualPulseTrain + 1) % RECEIVER_BUFFER_SIZE; } _nrpulses = 0; } @@ -401,7 +402,8 @@ size_t ESPiLight::parsePulseTrain(uint16_t *pulses, uint8_t length) { } /* Reset # of repeats after a certain delay */ - if (protocol->second < protocol->first || (protocol->second - protocol->first) > 500000) { + if (protocol->second < protocol->first || + (protocol->second - protocol->first) > 500000) { protocol->repeats = 0; }