|
73 | 73 | # pylint: disable=no-self-use |
74 | 74 |
|
75 | 75 | import array |
| 76 | +import time |
76 | 77 |
|
77 | 78 | __version__ = "0.0.0-auto.0" |
78 | 79 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_IRRemote.git" |
@@ -192,30 +193,54 @@ def decode_bits(self, pulses, debug=False): |
192 | 193 | output[i // 8] |= 1 |
193 | 194 | return output |
194 | 195 |
|
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``. |
197 | 199 |
|
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 |
203 | 203 | """ |
204 | | - if not input_pulses and not blocking: |
205 | | - return None |
206 | | - received = [] |
| 204 | + received = None |
| 205 | + recent_count = 0 |
| 206 | + pruning = False |
207 | 207 | while True: |
208 | | - while not input_pulses: |
209 | | - pass |
210 | 208 | while input_pulses: |
211 | 209 | pulse = input_pulses.popleft() |
| 210 | + recent_count += 1 |
212 | 211 | if pulse > max_pulse: |
213 | | - if not received: |
| 212 | + if received is None: |
214 | 213 | 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 |
219 | 244 |
|
220 | 245 | class GenericTransmit: |
221 | 246 | """Generic infrared transmit class that handles encoding.""" |
|
0 commit comments