Skip to content

Commit

Permalink
Prevent micros overflow after 70 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
mwalliczek committed Apr 7, 2021
1 parent d7bce13 commit dd8925d
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/ESPiLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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("_!_");
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -417,6 +419,7 @@ size_t ESPiLight::parsePulseTrain(uint16_t *pulses, uint8_t length) {
}
}
pnode = pnode->next;
yield();
}
if (_rawCallback != nullptr) {
(_rawCallback)(pulses, length);
Expand Down

0 comments on commit dd8925d

Please sign in to comment.