code.py
  1  # SPDX-FileCopyrightText: 2017 John Park for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  import adafruit_irremote
  6  import board
  7  import digitalio
  8  import neopixel
  9  import pulseio
 10  
 11  pixels = neopixel.NeoPixel(board.NEOPIXEL, 10)
 12  
 13  red_led = digitalio.DigitalInOut(board.D13)
 14  red_led.direction = digitalio.Direction.OUTPUT
 15  
 16  pulsein = pulseio.PulseIn(board.REMOTEIN, maxlen=120, idle_state=True)
 17  decoder = adafruit_irremote.GenericDecode()
 18  
 19  # among others, this example works with the Adafruit mini IR remote:
 20  # https://www.adafruit.com/product/389
 21  # size must match what you are decoding! for NEC use 4
 22  received_code = bytearray(4)
 23  
 24  # IR Remote Mapping
 25  '''
 26   1: [255, 2, 247, 8]
 27   2: [255, 2, 119, 136]
 28   3: [255, 2, 183, 72]
 29   4: [255, 2, 215, 40]
 30   5: [255, 2, 87, 168]
 31   6: [255, 2, 151, 104]
 32   7: [255, 2, 231, 24]
 33   8: [255, 2, 103, 152]
 34   9: [255, 2, 167, 88]
 35   0: [255, 2, 207, 48]
 36  
 37  ^ : [255, 2, 95, 160]
 38  v : [255, 2, 79, 176]
 39  > : [255, 2, 175, 80]
 40  < : [255, 2, 239, 16]
 41  
 42  Enter: [255, 2, 111, 144]
 43  Setup: [255, 2, 223, 32]
 44  Stop/Mode: [255, 2, 159, 96]
 45  Back: [255, 2, 143, 112]
 46  
 47  Vol - : [255, 2, 255, 0]
 48  Vol + : [255, 2, 191, 64]
 49  
 50  Play/Pause: [255, 2, 127, 128]
 51  '''
 52  
 53  RED = (255, 0, 0)
 54  GREEN = (0, 255, 0)
 55  WHITE = (85, 85, 85)
 56  BLUE = (0, 0, 255)
 57  PINK = (128, 0, 128)
 58  YELLOW = (148, 108, 0)
 59  PURPLE = (200, 0, 55)
 60  TEAL = (0, 200, 100)
 61  ORANGE = (100, 45, 0)
 62  BLACK = (0, 0, 0)
 63  
 64  last_command = None
 65  
 66  while True:
 67      red_led.value = False
 68      try:
 69          pulses = decoder.read_pulses(pulsein)
 70      except MemoryError as e:
 71          print("Memory error: ", e)
 72          continue
 73      red_led.value = True
 74      print("Heard", len(pulses), "Pulses:", pulses)
 75      command = None
 76      try:
 77          code = decoder.decode_bits(pulses, debug=False)
 78          if len(code) > 3:
 79              command = code[2]
 80          print("Decoded:", code)
 81      except adafruit_irremote.IRNECRepeatException:  # unusual short code!
 82          print("NEC repeat!")
 83          command = last_command
 84      except adafruit_irremote.IRDecodeException as e:  # failed to decode
 85          print("Failed to decode:", e)
 86      except MemoryError as e:
 87          print("Memory error: ", e)
 88  
 89      if not command:
 90          continue
 91      last_command = command
 92  
 93      print("----------------------------")
 94      red_led.value = False
 95  
 96      if command == 247:  # IR button 1
 97          pixels.fill(RED)
 98      elif command == 119:  # 2
 99          pixels.fill(GREEN)
100      elif command == 183:  # 3
101          pixels.fill(WHITE)
102      elif command == 215:  # 4
103          pixels.fill(BLUE)
104      elif command == 87:  # 5
105          pixels.fill(PINK)
106      elif command == 151:  # 6
107          pixels.fill(YELLOW)
108      elif command == 231:  # 7
109          pixels.fill(PURPLE)
110      elif command == 103:  # 8
111          pixels.fill(TEAL)
112      elif command == 167:  # 9
113          pixels.fill(ORANGE)
114      elif command == 207:
115          pixels.fill(BLACK)  # 0/10+