circuitplayground_ir_receive.py
1 """THIS EXAMPLE REQUIRES A SEPARATE LIBRARY BE LOADED ONTO YOUR CIRCUITPY DRIVE. 2 This example requires the adafruit_irremote.mpy library. 3 4 This example uses the IR receiver found near the center of the board. Works with another CPX 5 running the cpx_ir_transmit.py example. The NeoPixels will light up when the buttons on the 6 TRANSMITTING CPX are pressed!""" 7 import pulseio 8 import board 9 import adafruit_irremote 10 from adafruit_circuitplayground.express import cpx 11 12 # Create a 'pulseio' input, to listen to infrared signals on the IR receiver 13 pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True) 14 # Create a decoder that will take pulses and turn them into numbers 15 decoder = adafruit_irremote.GenericDecode() 16 17 while True: 18 cpx.red_led = True 19 pulses = decoder.read_pulses(pulsein) 20 try: 21 # Attempt to convert received pulses into numbers 22 received_code = decoder.decode_bits(pulses, debug=False) 23 except adafruit_irremote.IRNECRepeatException: 24 # We got an unusual short code, probably a 'repeat' signal 25 continue 26 except adafruit_irremote.IRDecodeException: 27 # Something got distorted 28 continue 29 30 print("Infrared code received: ", received_code) 31 if received_code == [66, 84, 78, 65]: 32 print("Button A signal") 33 cpx.pixels.fill((100, 0, 155)) 34 if received_code == [66, 84, 78, 64]: 35 print("Button B Signal") 36 cpx.pixels.fill((210, 45, 0))