@@ -231,29 +231,32 @@ void ICACHE_RAM_ATTR ESPiLight::interruptHandler() {
231231
232232 if (pulseTrain.length == 0 ) {
233233 const unsigned long now = micros ();
234- const unsigned int duration = now - _lastChange;
235-
236- /* We first do some filtering (same as pilight BPF) */
237- if (duration > minpulselen) {
238- if (duration < maxpulselen) {
239- /* All codes are buffered */
240- codes[_nrpulses] = (uint16_t )duration;
241- _nrpulses = (uint8_t )((_nrpulses + 1 ) % MAXPULSESTREAMLENGTH);
242- /* Let's match footers */
243- if (duration > mingaplen) {
244- // Debug('g');
245- /* Only match minimal length pulse streams */
246- if (_nrpulses >= minrawlen && _nrpulses <= maxrawlen) {
247- // Debug(_nrpulses);
248- // Debug('l');
249- pulseTrain.length = _nrpulses;
250- _actualPulseTrain = (_actualPulseTrain + 1 ) % RECEIVER_BUFFER_SIZE;
234+ if (now > _lastChange) { // prevent overflow after 70 minutes
235+ const unsigned long duration = now - _lastChange;
236+
237+ /* We first do some filtering (same as pilight BPF) */
238+ if (duration > minpulselen) {
239+ if (duration < maxpulselen) {
240+ /* All codes are buffered */
241+ codes[_nrpulses] = (uint16_t )duration;
242+ _nrpulses = (uint8_t )((_nrpulses + 1 ) % MAXPULSESTREAMLENGTH);
243+ /* Let's match footers */
244+ if (duration > mingaplen) {
245+ // Debug('g');
246+ /* Only match minimal length pulse streams */
247+ if (_nrpulses >= minrawlen && _nrpulses <= maxrawlen) {
248+ // Debug(_nrpulses);
249+ // Debug('l');
250+ pulseTrain.length = _nrpulses;
251+ _actualPulseTrain =
252+ (_actualPulseTrain + 1 ) % RECEIVER_BUFFER_SIZE;
253+ }
254+ _nrpulses = 0 ;
251255 }
252- _nrpulses = 0 ;
253256 }
254257 }
255- _lastChange = now;
256258 }
259+ _lastChange = now;
257260 } else {
258261 Debug (" _!_" );
259262 }
@@ -399,7 +402,8 @@ size_t ESPiLight::parsePulseTrain(uint16_t *pulses, uint8_t length) {
399402 }
400403
401404 /* Reset # of repeats after a certain delay */
402- if ((protocol->second - protocol->first ) > 500000 ) {
405+ if (protocol->second < protocol->first ||
406+ (protocol->second - protocol->first ) > 500000 ) {
403407 protocol->repeats = 0 ;
404408 }
405409
@@ -417,6 +421,7 @@ size_t ESPiLight::parsePulseTrain(uint16_t *pulses, uint8_t length) {
417421 }
418422 }
419423 pnode = pnode->next ;
424+ yield ();
420425 }
421426 if (_rawCallback != nullptr ) {
422427 (_rawCallback)(pulses, length);
0 commit comments