-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathirremote_nonblocking.py
35 lines (27 loc) · 1.18 KB
/
irremote_nonblocking.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Circuit Playground Express Demo Code
# Adjust the pulseio 'board.PIN' if using something else
import time
import board
import pulseio
import adafruit_irremote
pulsein = pulseio.PulseIn(board.REMOTEIN, maxlen=120, idle_state=True)
decoder = adafruit_irremote.NonblockingGenericDecode(pulsein)
t0 = next_heartbeat = time.monotonic()
while True:
for message in decoder.read():
print(f"t={time.monotonic() - t0:.3} New Message")
print("Heard", len(message.pulses), "Pulses:", message.pulses)
if isinstance(message, adafruit_irremote.IRMessage):
print("Decoded:", message.code)
elif isinstance(message, adafruit_irremote.NECRepeatIRMessage):
print("NEC repeat!")
elif isinstance(message, adafruit_irremote.UnparseableIRMessage):
print("Failed to decode", message.reason)
print("----------------------------")
# This heartbeat confirms that we are not blocked somewhere above.
t = time.monotonic()
if t > next_heartbeat:
print(f"t={time.monotonic() - t0:.3} Heartbeat")
next_heartbeat = t + 0.1