code.py
1 # SPDX-FileCopyrightText: 2018 Limor Fried for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 # Simple NEC remote decode-and-print example 6 # Prints out the 4-byte code transmitted by NEC remotes 7 8 import pulseio 9 import board 10 import adafruit_dotstar 11 import adafruit_irremote 12 13 led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1) 14 pulsein = pulseio.PulseIn(board.REMOTEIN, maxlen=120, idle_state=True) 15 decoder = adafruit_irremote.GenericDecode() 16 17 # size must match what you are decoding! for NEC use 4 18 received_code = bytearray(4) 19 20 print("Ready for NEC remote input!") 21 22 while True: 23 led[0] = (0, 0, 0) # LED off 24 pulses = decoder.read_pulses(pulsein) 25 print("\tHeard", len(pulses), "Pulses:", pulses) 26 try: 27 code = decoder.decode_bits(pulses, debug=False) 28 led[0] = (0, 100, 0) # flash green 29 print("Decoded:", code) 30 except adafruit_irremote.IRNECRepeatException: # unusual short code! 31 led[0] = (100, 100, 0) # flash yellow 32 print("NEC repeat!") 33 except adafruit_irremote.IRDecodeException as e: # failed to decode 34 led[0] = (100, 0, 0) # flash red 35 print("Failed to decode: ", e.args) 36 37 print("----------------------------")