code.py
 1  # SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import pulseio
 6  import board
 7  import adafruit_irremote
 8  from adafruit_circuitplayground.express import cpx
 9  
10  # Create a 'pulseio' input, to listen to infrared signals on the IR receiver
11  pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True)
12  # Create a decoder that will take pulses and turn them into numbers
13  decoder = adafruit_irremote.GenericDecode()
14  
15  while True:
16      pulses = decoder.read_pulses(pulsein)
17      try:
18          # Attempt to convert received pulses into numbers
19          received_code = decoder.decode_bits(pulses)
20      except adafruit_irremote.IRNECRepeatException:
21          # We got an unusual short code, probably a 'repeat' signal
22          # print("NEC repeat!")
23          continue
24      except adafruit_irremote.IRDecodeException as e:
25          # Something got distorted or maybe its not an NEC-type remote?
26          # print("Failed to decode: ", e.args)
27          continue
28  
29      print("NEC Infrared code received: ", received_code)
30      if received_code == [255, 2, 255, 0]:
31          print("Button A signal")
32          cpx.pixels.fill((130, 0, 100))
33      if received_code == [255, 2, 191, 64]:
34          print("Button B Signal")
35          cpx.pixels.fill((0, 0, 0))
36          cpx.play_tone(262, 1)