From d18dc8978620b5eaeaa6e8d493b0214c6292b044 Mon Sep 17 00:00:00 2001 From: Kry-Vosa Date: Sun, 29 Oct 2023 09:57:32 +0100 Subject: [PATCH 1/2] Fix for uncleared buffer in NonblockingGenericDecode The code in the generator now correctly clears the _unparsed_pulses buffer. It also removes the long "end of message" pulses from the buffer before parsing, since they should be discarded by decode_bits anyway and it fixes NECRepeatIRMessage. --- adafruit_irremote.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/adafruit_irremote.py b/adafruit_irremote.py index c3316df..3fa9c47 100644 --- a/adafruit_irremote.py +++ b/adafruit_irremote.py @@ -237,23 +237,20 @@ def read(self) -> None: # Consume from PulseIn. while self.pulses: pulse = self.pulses.popleft() - self._unparsed_pulses.append(pulse) - if pulse > self.max_pulse: + if pulse <= self.max_pulse: + self._unparsed_pulses.append(pulse) + else: # End of message! Decode it and yield a BaseIRMessage. + result = None; try: - yield decode_bits(self._unparsed_pulses) + result = decode_bits(self._unparsed_pulses) except FailedToDecode as err: # If you want to debug failed decodes, this would be a good # place to print/log or (re-)raise. - unparseable_message = err.args[0] - yield unparseable_message + result = err.args[0] + self._unparsed_pulses.clear() - # TODO Do we need to consume and throw away more pulses here? - # I'm unclear about the role that "pruning" plays in the - # original implementation in GenericDecode._read_pulses_non_blocking. - # When we reach here, we have consumed everything from PulseIn. - # If there are some pulses in self._unparsed_pulses, they represent - # partial messages. We'll finish them next time read() is called. + yield result class GenericDecode: From a44922fa5ee89fdb77c8990c7ad367b4b2e004dc Mon Sep 17 00:00:00 2001 From: foamyguy Date: Tue, 20 Feb 2024 15:56:42 -0600 Subject: [PATCH 2/2] code format --- adafruit_irremote.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_irremote.py b/adafruit_irremote.py index 3fa9c47..f640607 100644 --- a/adafruit_irremote.py +++ b/adafruit_irremote.py @@ -238,17 +238,17 @@ def read(self) -> None: while self.pulses: pulse = self.pulses.popleft() if pulse <= self.max_pulse: - self._unparsed_pulses.append(pulse) + self._unparsed_pulses.append(pulse) else: # End of message! Decode it and yield a BaseIRMessage. - result = None; + result = None try: result = decode_bits(self._unparsed_pulses) except FailedToDecode as err: # If you want to debug failed decodes, this would be a good # place to print/log or (re-)raise. result = err.args[0] - + self._unparsed_pulses.clear() yield result