Code for IR guide.

This commit is contained in:
Kattni Rembor 2018-07-23 20:53:03 -04:00
parent 8ee683c3b8
commit 79d9188257
3 changed files with 89 additions and 0 deletions

View file

@ -0,0 +1,27 @@
import time
from adafruit_circuitplayground.express import cpx
import adafruit_irremote
import pulseio
import board
# Create a 'pulseio' output, to send infrared signals on the IR transmitter @ 38KHz
pwm = pulseio.PWMOut(board.IR_TX, frequency=38000, duty_cycle=2 ** 15)
pulseout = pulseio.PulseOut(pwm)
# Create an encoder that will take numbers and turn them into NEC IR pulses
encoder = adafruit_irremote.GenericTransmit(header=[9500, 4500], one=[550, 550],
zero=[550, 1700], trail=0)
while True:
if cpx.button_a:
print("Button A pressed!")
cpx.red_led = True
encoder.transmit(pulseout, [255, 2, 255, 0])
cpx.red_led = False
# wait so the receiver can get the full message
time.sleep(0.2)
if cpx.button_b:
print("Button B pressed!")
cpx.red_led = True
encoder.transmit(pulseout, [255, 2, 191, 64])
cpx.red_led = False
time.sleep(0.2)

View file

@ -0,0 +1,30 @@
import pulseio
import board
import adafruit_irremote
# Create a 'pulseio' input, to listen to infrared signals on the IR receiver
pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True)
# Create a decoder that will take pulses and turn them into numbers
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
print("NEC Infrared code received: ", received_code)
if received_code == [255, 2, 255, 0]:
print("Received NEC Vol-")
if received_code == [255, 2, 127, 128]:
print("Received NEC Play/Pause")
if received_code == [255, 2, 191, 64]:
print("Received NEC Vol+")

View file

@ -0,0 +1,32 @@
import pulseio
import board
import adafruit_irremote
from adafruit_circuitplayground.express import cpx
# Create a 'pulseio' input, to listen to infrared signals on the IR receiver
pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True)
# Create a decoder that will take pulses and turn them into numbers
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
print("NEC Infrared code received: ", received_code)
if received_code == [255, 2, 255, 0]:
print("Button A signal")
cpx.pixels.fill((130, 0, 100))
if received_code == [255, 2, 191, 64]:
print("Button B Signal")
cpx.pixels.fill((0, 0, 0))
cpx.play_tone(262, 1)