code.py
  1  # SPDX-FileCopyrightText: 2017 John Edgar Park for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  # Read Adafruit Remote Codes with Circuit Playground Express
  6  #
  7  # Simplified code based on https://learn.adafruit.com/remote-
  8  # control-tree-ornament-with-circuit-playground-express?view=all
  9  #
 10  import adafruit_irremote
 11  import board
 12  import neopixel
 13  import pulseio
 14  
 15  pixels = neopixel.NeoPixel(board.NEOPIXEL, 10)
 16  
 17  # Set up the reading of pulses on the IR receiver and the IR library
 18  pulsein = pulseio.PulseIn(board.REMOTEIN, maxlen=120, idle_state=True)
 19  decoder = adafruit_irremote.GenericDecode()
 20  
 21  # Example works with the Adafruit mini IR remote #389
 22  # The size below must match what you are decoding! For NEC use 4
 23  received_code = bytearray(4)
 24  
 25  # IR Remote Mapping
 26  '''
 27   1: [255, 2, 247, 8]
 28   2: [255, 2, 119, 136]
 29   3: [255, 2, 183, 72]
 30   4: [255, 2, 215, 40]
 31   5: [255, 2, 87, 168]
 32   6: [255, 2, 151, 104]
 33   7: [255, 2, 231, 24]
 34   8: [255, 2, 103, 152]
 35   9: [255, 2, 167, 88]
 36   0: [255, 2, 207, 48]
 37  
 38  ^ : [255, 2, 95, 160]
 39  v : [255, 2, 79, 176]
 40  > : [255, 2, 175, 80]
 41  < : [255, 2, 239, 16]
 42  
 43  Enter: [255, 2, 111, 144]
 44  Setup: [255, 2, 223, 32]
 45  Stop/Mode: [255, 2, 159, 96]
 46  Back: [255, 2, 143, 112]
 47  
 48  Vol - : [255, 2, 255, 0]
 49  Vol + : [255, 2, 191, 64]
 50  
 51  Play/Pause: [255, 2, 127, 128]
 52  '''
 53  
 54  last_command = None
 55  
 56  while True:
 57      try:
 58          pulses = decoder.read_pulses(pulsein)
 59      except MemoryError as e:
 60          print("Memory error: ", e)
 61          continue
 62      print("Heard", len(pulses), "Pulses:", pulses)
 63      command = None
 64      try:
 65          code = decoder.decode_bits(pulses, debug=False)
 66          if len(code) > 3:
 67              command = code[2]
 68          print("Decoded:", code)
 69      except adafruit_irremote.IRNECRepeatException:  # unusual short code!
 70          print("NEC repeat!")
 71          command = last_command
 72      except adafruit_irremote.IRDecodeException as e:  # failed to decode
 73          print("Failed to decode:", e)
 74      except MemoryError as e:
 75          print("Memory error: ", e)
 76  
 77      if not command:
 78          continue
 79      last_command = command
 80  
 81      print("----------------------------")
 82  
 83      if command == 247:  # IR button 1
 84          pixels[1] = (0, 30, 0)
 85      elif command == 119:  # 2
 86          pixels[2] = (0, 30, 0)
 87      elif command == 183:  # 3
 88          pixels[3] = (0, 30, 0)
 89      elif command == 215:  # 4
 90          pixels[4] = (0, 30, 0)
 91      elif command == 87:  # 5
 92          pixels[5] = (0, 30, 0)
 93      elif command == 151:  # 6
 94          pixels[6] = (0, 30, 0)
 95      elif command == 231:  # 7
 96          pixels[7] = (0, 30, 0)
 97      elif command == 103:  # 8
 98          pixels[8] = (0, 30, 0)
 99      elif command == 167:  # 9
100          pixels[9] = (0, 30, 0)
101      elif command == 207:
102          pixels[0] = (0, 30, 0)
103      elif command in (111, 143):
104          pixels.fill((0, 0, 0)) # clear on enter or back key