Created
December 31, 2018 00:09
-
-
Save oschettler/5903ca745239de5d5e9b84cc289c9b08 to your computer and use it in GitHub Desktop.
Lampe - A Colorful Lamp with Adafruit Circuit Playground Express and an Infrared Remote Control
This file contains 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
# Lampe v0.1 | |
# License MIT | |
# Olav Schettler <olav@schettler.net> | |
# These need drivers https://circuitpython.readthedocs.io/en/latest/docs/drivers.html | |
import pulseio | |
import board | |
import adafruit_irremote | |
from adafruit_circuitplayground.express import cpx | |
# https://www.rapidtables.com/web/color/RGB_Color.html | |
colors = { | |
'ff08df20': (255, 0, 0), # red | |
'ff08ef10': (255, 128, 0), # orange | |
'ff08d728': (255, 255, 0), # yellow | |
'ff085fa0': (0, 255, 0), # gruen | |
'ff086f90': (128, 255, 0), # hellgruen | |
'ff08d728': (255, 255, 0), # yellow | |
'ff089f60': (0, 0, 255), # blau | |
'ff08b748': (255, 0, 127), # violett | |
'ff081fe0': (255, 255, 255), # weiss | |
'ff089768': (255, 102, 102) # pink | |
} | |
last_color = (255, 255, 255) | |
brightness = 1.0 | |
cpx.pixels.brightness = brightness | |
print("Lampe, v0.1") | |
pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True) | |
decoder = adafruit_irremote.GenericDecode() | |
while True: | |
pulses = decoder.read_pulses(pulsein) | |
try: | |
# Attempt to convert received pulses into numbers | |
received_code = decoder.decode_bits(pulses, debug=False) | |
except adafruit_irremote.IRNECRepeatException: | |
# We got an unusual short code, probably a 'repeat' signal | |
# print("NEC repeat!") | |
continue | |
except adafruit_irremote.IRDecodeException as e: | |
# Something got distorted or maybe its not an NEC-type remote? | |
# print("Failed to decode: ", e.args) | |
continue | |
received = ''.join('{:02x}'.format(x) for x in received_code) | |
print("NEC Infrared code received: ", received_code, " = ", received) | |
if received == 'ff08ff00': | |
if brightness < 1.0: | |
brightness += 0.2 | |
if brightness > 1.0: | |
brightness = 1.0 | |
cpx.pixels.brightness = brightness | |
print("Heller! ", brightness) | |
if received == 'ff087f80': | |
if brightness > 0.0: | |
brightness -= 0.2 | |
if brightness < 0.0: | |
brightness = 0.0 | |
cpx.pixels.brightness = brightness | |
print("Dunkler! ", brightness) | |
elif received == 'ff083fc0': | |
print("An!") | |
cpx.pixels.fill(last_color) | |
elif received == 'ff08bf40': | |
print("Aus!") | |
cpx.pixels.fill((0, 0, 0)) | |
elif received in colors: | |
print("Farbe ", colors[received]) | |
brightness = 1.0 | |
cpx.pixels.brightness = brightness | |
last_color = colors[received] | |
cpx.pixels.fill(last_color) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment