import board
import time
import digitalio
from digitalio import DigitalInOut, Direction, Pull
from rainbowio import colorwheel
import neopixel
import animation
from animation import Animation
import comet
from comet import Comet
import chase
from chase import Chase
import rainbowchase
from rainbowchase import RainbowChase
import pulseio
import adafruit_irremote
pulsein = pulseio.PulseIn(board.GP0, maxlen=120, idle_state=True)
decoder = adafruit_irremote.GenericDecode()
##COLOR LIST
RED = (255, 0, 0)
YELLOW = (150, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
ORANGE = (255, 165, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# among others, this example works with the Adafruit mini IR remote:
# https://www.adafruit.com/product/389
# size must match what you are decoding! for NEC use 4
received_code = bytearray(4)
# IR Remote Mapping
'''
1: [255, 2, 247, 8]
2: [255, 2, 119, 136]
3: [255, 2, 183, 72]
4: [255, 2, 215, 40]
5: [255, 2, 87, 168]
6: [255, 2, 151, 104]
7: [255, 2, 231, 24]
8: [255, 2, 103, 152]
9: [255, 2, 167, 88]
0: [255, 2, 207, 48]
^ : [255, 2, 95, 160]
v : [255, 2, 79, 176]
> : [255, 2, 175, 80]
< : [255, 2, 239, 16]
Enter: [255, 2, 111, 144]
Setup: [255, 2, 223, 32]
Stop/Mode: [255, 2, 159, 96]
Back: [255, 2, 143, 112]
Vol - : [255, 2, 255, 0]
Vol : [255, 2, 191, 64]
Play/Pause: [255, 2, 127, 128]
'''
red_led = digitalio.DigitalInOut(board.GP25)
red_led.direction = digitalio.Direction.OUTPUT
pixel_pin = board.GP27 # Neopixel(s) data on this pin
num_pixels = 20
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=1, auto_write=False)
comet1 = Comet(pixels, .1, RED, bounce=True)
comet2 = Comet(pixels, .1, ORANGE, bounce=True)
comet3 = Comet(pixels, .1, YELLOW, bounce=True)
chase = Chase(pixels, .1, BLUE)
rainbowchase = RainbowChase(pixels, .1, size=3, spacing=2)
def color_chase(color, wait):
for i in range(num_pixels):
pixels[i] = color
time.sleep(wait)
pixels.show()
time.sleep(0.5)
def rainbow_cycle(wait):
for j in range(255):
for i in range(num_pixels):
rc_index = (i * 256 // num_pixels) + j
pixels[i] = colorwheel(rc_index & 255)
pixels.show()
time.sleep(wait)
def setline(pixel1, pixel2, color):
for i in range(pixel1, pixel2 + 1):
pixels[i] = color
pixels.show()
while True:
red_led.value = False
try:
pulses = decoder.read_pulses(pulsein)
except MemoryError as e:
print("Memory error: ", e)
continue
red_led.value = True
command = None
try:
code = decoder.decode_bits(pulses)
if len(code) > 3:
command = code[2]
print("Decoded:", code)
except adafruit_irremote.IRNECRepeatException: # unusual short code!
print("NEC repeat!")
command = last_command
except adafruit_irremote.IRDecodeException as e: # failed to decode
print("Failed to decode:", e)
except MemoryError as e:
print("Memory error: ", e)
if not command:
continue
last_command = command
print("----------------------------")
red_led.value = False
if command == 243: # IR button 1
setline(0, 4, ORANGE)
setline(5, 19, WHITE)
elif command == 231: # 2
setline(5, 9, YELLOW)
setline(0, 4, WHITE)
setline(10, 19, WHITE)
elif command == 161: # 3
setline(10, 14, GREEN)
setline(0, 9, WHITE)
setline(15, 19, WHITE)
elif command == 247: # 4
setline(15, 19, BLUE)
setline(0, 14, WHITE)
elif command == 227: # 5
pixels.fill(RED)
pixels.show()
elif command == 165: # 6
pixels.fill(WHITE)
pixels.show()
elif command == 189: # 7
comet3.animate()
elif command == 173: # 8
comet2.animate()
elif command == 181: # 9
comet1.animate()
elif command == 233:
pixels.fill(BLACK) # 0/10
elif command == 127:
rainbowchase.animate()