Last active
December 8, 2019 23:19
-
-
Save deckerego/442f76b935de7e097086dcaaa5527f4e to your computer and use it in GitHub Desktop.
Use a remote control to change the lights on a Circuit Playground Express
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pulseio | |
import board | |
import time | |
import neopixel | |
import touchio | |
import adafruit_irremote | |
from digitalio import DigitalInOut, Direction, Pull | |
power = True | |
color = (255, 255, 255) | |
brightness = 0.1 | |
sleep = 1.0 | |
timeout = 60 * 30 | |
ticks = 0.0 | |
# NeoPixels | |
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, auto_write=False) | |
pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True) | |
decoder = adafruit_irremote.GenericDecode() | |
# Toggle Switch | |
switch = DigitalInOut(board.SLIDE_SWITCH) | |
switch.direction = Direction.INPUT | |
switch.pull = Pull.UP | |
# Button A | |
button_a = DigitalInOut(board.BUTTON_A) | |
button_a.direction = Direction.INPUT | |
button_a.pull = Pull.DOWN | |
# Button B | |
button_b = DigitalInOut(board.BUTTON_B) | |
button_b.direction = Direction.INPUT | |
button_b.pull = Pull.DOWN | |
# Touchpads | |
touchpad_1 = touchio.TouchIn(board.A1) | |
touchpad_2 = touchio.TouchIn(board.A2) | |
touchpad_3 = touchio.TouchIn(board.A3) | |
touchpad_4 = touchio.TouchIn(board.A4) | |
touchpad_5 = touchio.TouchIn(board.A5) | |
touchpad_6 = touchio.TouchIn(board.A6) | |
touchpad_7 = touchio.TouchIn(board.A7) | |
# Color Palette by Receive Code | |
palette = { | |
1: (255, 0, 0), # Touchpad 1 | |
2: ( 0, 255, 0), # Touchpad 2 | |
3: ( 0, 0, 255), # Touchpad 3 | |
4: ( 0, 255, 255), # Touchpad 4 | |
5: (255, 0, 255), # Touchpad 5 | |
6: (255, 255, 0), # Touchpad 6 | |
7: (255, 255, 255), # Touchpad 7 | |
123: (255, 0, 0), # 1 | |
187: ( 0, 255, 0), # 2 | |
59: ( 0, 0, 255), # 3 | |
219: ( 0, 255, 255), # 4 | |
91: (255, 0, 255), # 5 | |
155: (255, 255, 0), # 6 | |
27: (128, 255, 64), # 7 | |
235: ( 64, 128, 255), # 8 | |
107: (255, 64, 128), # 9 | |
51: (255, 255, 255) # 0 | |
} | |
def get_touchpad(): | |
if touchpad_1.value: return 1 | |
elif touchpad_2.value: return 2 | |
elif touchpad_3.value: return 3 | |
elif touchpad_4.value: return 4 | |
elif touchpad_5.value: return 5 | |
elif touchpad_6.value: return 6 | |
elif touchpad_7.value: return 7 | |
else : return False | |
def translate(pulses): | |
try: | |
return decoder.decode_bits(pulses) if pulses else False | |
except adafruit_irremote.IRNECRepeatException: | |
return False | |
except adafruit_irremote.IRDecodeException as e: | |
print("Failed to decode: ", e.args) | |
return False | |
def show_pixels(): | |
for index in range(10): | |
pixels[index] = color if power else (0, 0, 0) | |
pixels.brightness = brightness | |
pixels.show() | |
while True: | |
received_code = translate(decoder.read_pulses(pulsein, blocking=False)) | |
touchpad = get_touchpad() | |
if received_code: | |
print("NEC Infrared code received: ", received_code) | |
received_code = received_code[-1] | |
if button_a.value: | |
brightness = brightness + 0.1 if brightness < 1.0 else 1.0 | |
if button_b.value: | |
brightness = brightness - 0.1 if brightness > 0.0 else 1.0 | |
if touchpad: | |
color = palette[touchpad] | |
elif not received_code: | |
if ticks > timeout: | |
ticks = 0.0 | |
power = False | |
elif power and switch.value: | |
ticks += sleep | |
elif received_code == 47: # Power | |
ticks = 0.0 | |
power = not power | |
elif received_code == 103: # Channel + | |
brightness = brightness + 0.05 if brightness < 1.0 else 1.0 | |
elif received_code == 231: # Channel - | |
brightness = brightness - 0.05 if brightness > 0.0 else 0.0 | |
elif received_code == 159: # Fast Forward | |
brightness = 1.0 | |
elif received_code == 31: # Rewind | |
brightness = 0.01 | |
elif received_code in palette: | |
color = palette[received_code] | |
show_pixels() | |
time.sleep(sleep) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment