Skip to content

Commit c87d4fa

Browse files
authored
Merge pull request adafruit#18 from fmorton/master
improve read_pulses/blocking reliability
2 parents ec11164 + 78d1610 commit c87d4fa

File tree

1 file changed

+42
-17
lines changed

1 file changed

+42
-17
lines changed

adafruit_irremote.py

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
# pylint: disable=no-self-use
7474

7575
import array
76+
import time
7677

7778
__version__ = "0.0.0-auto.0"
7879
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_IRRemote.git"
@@ -192,30 +193,54 @@ def decode_bits(self, pulses, debug=False):
192193
output[i // 8] |= 1
193194
return output
194195

195-
def read_pulses(self, input_pulses, max_pulse=10000, blocking=True):
196-
"""Read out a burst of pulses until a pulse is longer than ``max_pulse``.
196+
def _read_pulses_non_blocking(self, input_pulses, max_pulse=10000, pulse_window=0.10):
197+
"""Read out a burst of pulses without blocking until pulses stop for a specified
198+
period (pulse_window), pruning pulses after a pulse longer than ``max_pulse``.
197199
198-
:param ~pulseio.PulseIn input_pulses: Object to read pulses from
199-
:param int max_pulse: Pulse duration to end a burst
200-
:param bool blocking: If True, will block until pulses found.
201-
If False, will return None if no pulses.
202-
Defaults to True for backwards compatibility
200+
:param ~pulseio.PulseIn input_pulses: Object to read pulses from
201+
:param int max_pulse: Pulse duration to end a burst
202+
:param float pulse_window: pulses are collected for this period of time
203203
"""
204-
if not input_pulses and not blocking:
205-
return None
206-
received = []
204+
received = None
205+
recent_count = 0
206+
pruning = False
207207
while True:
208-
while not input_pulses:
209-
pass
210208
while input_pulses:
211209
pulse = input_pulses.popleft()
210+
recent_count += 1
212211
if pulse > max_pulse:
213-
if not received:
212+
if received is None:
214213
continue
215-
else:
216-
return received
217-
received.append(pulse)
218-
return received
214+
pruning = True
215+
if not pruning:
216+
if received is None:
217+
received = []
218+
received.append(pulse)
219+
220+
if recent_count == 0:
221+
return received
222+
recent_count = 0
223+
time.sleep(pulse_window)
224+
225+
def read_pulses(self, input_pulses, *, max_pulse=10000, blocking=True,
226+
pulse_window=0.10, blocking_delay=0.10):
227+
"""Read out a burst of pulses until pulses stop for a specified
228+
period (pulse_window), pruning pulses after a pulse longer than ``max_pulse``.
229+
230+
:param ~pulseio.PulseIn input_pulses: Object to read pulses from
231+
:param int max_pulse: Pulse duration to end a burst
232+
:param bool blocking: If True, will block until pulses found.
233+
If False, will return None if no pulses.
234+
Defaults to True for backwards compatibility
235+
:param float pulse_window: pulses are collected for this period of time
236+
:param float blocking_delay: delay between pulse checks when blocking
237+
"""
238+
while True:
239+
pulses = self._read_pulses_non_blocking(input_pulses, max_pulse, pulse_window)
240+
if blocking and pulses is None:
241+
time.sleep(blocking_delay)
242+
continue
243+
return pulses
219244

220245
class GenericTransmit:
221246
"""Generic infrared transmit class that handles encoding."""

0 commit comments

Comments
 (0)