Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@richinfante
Last active March 23, 2020 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richinfante/46cbacfca540ccb47cc829d0e5c912ae to your computer and use it in GitHub Desktop.
Save richinfante/46cbacfca540ccb47cc829d0e5c912ae to your computer and use it in GitHub Desktop.
import time
import board
from digitalio import DigitalInOut, Direction, Pull
import adafruit_irremote
import adafruit_dotstar
import pulseio
import touchio
import random
# NOTE: to connect, use `screen -L /dev/tty...`
# Status LED
led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
STATUS_BRIGHTNESS = 0.5
led.brightness = STATUS_BRIGHTNESS
ON_CODES = [
[ 255, 0, 207, 48 ],
[ 136, 30, 47, 156 ],
[ 255, 2, 111, 144 ], # "Enter"
[ 255, 2, 183, 72 ] # Btn #3
]
OFF_CODES = [
[ 255, 0, 239, 16 ],
[ 136, 30, 79, 156 ],
[ 255, 2, 159, 96 ] # Btn "stop"
]
TOGGLE_CODES = [
[ 255, 2, 127, 128 ],
[ 136, 30, 133, 156 ]
]
# Output LED
led1 = DigitalInOut(board.D3)
led1.direction = Direction.OUTPUT
# IR IN
IR_PIN = board.D2 # Pin connected to IR receiver.
# Create pulse input and IR decoder.
pulses = pulseio.PulseIn(IR_PIN, maxlen=200, idle_state=True)
decoder = adafruit_irremote.GenericDecode()
# Default to ON
led1.value = True
dotstar_mode = False
flicker = False
while True:
# small delay for cleaner results
pulses.clear()
pulses.resume()
if flicker:
time.sleep(.2)
else:
time.sleep(0.5)
if dotstar_mode:
if flicker:
led.brightness = 0.5 + float(random.randrange(0, 10, 1)) / 20.0
else:
led.brightness = 1
# Wait for a pulse to be detected.
detected = decoder.read_pulses(pulses, blocking=False)
# If pulses are detected, decode + toggle if needed
if detected:
try:
# Attempt to convert received pulses into numbers
received_code = decoder.decode_bits(detected)
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
print("code: %s" % received_code)
# Mode #2 - Green
if received_code == [255, 2, 247, 8]:
led1.value = False
led.fill([0, 255, 0])
dotstar_mode = True
led.brightness = 1
# Mode #2 - Red
elif received_code == [255, 2, 119, 136]:
led1.value = False
led.fill([255, 0, 0])
dotstar_mode = True
led.brightness = 1
# Mode #3 - Blue
elif received_code == [255, 2, 215, 40]:
led1.value = False
led.fill([80, 100, 255])
dotstar_mode = True
led.brightness = 1
# Mode #4 - Yellow
elif received_code == [255, 2, 87, 168]:
led1.value = False
led.fill([255, 200, 0])
dotstar_mode = True
led.brightness = 1
# Mode #5 - White
elif received_code == [255, 2, 151, 104]:
led1.value = False
led.fill([255, 255, 255])
dotstar_mode = True
led.brightness = 1
# Toggle light on
elif received_code in ON_CODES:
led1.value = True
print("got on code")
led.brightness = STATUS_BRIGHTNESS
led.fill([0, 255, 0])
time.sleep(0.25)
led.fill([0, 0, 0])
dotstar_mode = False
# Toggle light off
elif received_code in OFF_CODES:
led1.value = False
print("got off code")
led.brightness = STATUS_BRIGHTNESS
dotstar_mode = False
led.fill([0, 255, 0])
time.sleep(0.25)
led.fill([0, 0, 0])
# Invert light status
elif received_code in TOGGLE_CODES:
led1.value = not led1.value
print("got toggle code")
led.brightness = STATUS_BRIGHTNESS
led.fill([0, 255, 0])
time.sleep(0.25)
led.fill([0, 0, 0])
dotstar_mode = False
# Flicker on/off
elif received_code == [255, 2, 143, 112]:
flicker = not flicker
print("toggle flicker to %s" % flicker)
time.sleep(0.5)
# Unknown code - red color
elif not dotstar_mode:
print("unknown code")
led.brightness = STATUS_BRIGHTNESS
led.fill([255, 0, 0])
time.sleep(0.5)
# If not in dotstar mode, clear
if not dotstar_mode:
led.brightness = STATUS_BRIGHTNESS
led.fill([0, 0, 0])
else:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment