Skip to content

Instantly share code, notes, and snippets.

@richinfante richinfante/ir_remote_light.py Secret
Created Jan 19, 2020

Embed
What would you like to do?
import time
import board
from digitalio import DigitalInOut, Direction, Pull
import adafruit_irremote
import adafruit_dotstar
import pulseio
import touchio
# Status LED
led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
led.brightness = 0.5
ON_CODES = [
[ 255, 0, 207, 48 ],
[ 136, 30, 47, 156 ]
]
OFF_CODES = [
[ 255, 0, 239, 16 ],
[ 136, 30, 79, 156 ]
]
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
while True:
# small delay for cleaner results
pulses.clear()
pulses.resume()
time.sleep(.5)
# 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
# Toggle light on
if received_code in ON_CODES:
led1.value = True
print("got on code")
led.fill([0, 255, 0])
time.sleep(0.25)
# Toggle light off
elif received_code in OFF_CODES:
led1.value = False
print("got off code")
led.fill([0, 255, 0])
time.sleep(0.25)
# Invert light status
elif received_code in TOGGLE_CODES:
led1.value = not led1.value
print("got toggle code")
led.fill([0, 255, 0])
time.sleep(0.25)
else:
led.fill([255, 0, 0])
time.sleep(0.5)
# Disable status LED
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
You can’t perform that action at this time.