default examples

This commit is contained in:
ladyada 2018-05-23 17:24:19 -04:00
parent 65d77764f9
commit d0ab3fda41
4 changed files with 178 additions and 0 deletions

View file

@ -0,0 +1,76 @@
# Simple NEC remote decode-and-type keyboard example
# When used with the Adafruit NEC remote will act like a keyboard and
# type out keypresses.
import adafruit_irremote
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
import adafruit_dotstar
import pulseio
import board
import time
led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
# The keyboard object!
time.sleep(1) # Sleep for a bit to avoid a race condition on some systems
keyboard = Keyboard()
keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :)
# our infrared pulse decoder helpers
pulsein = pulseio.PulseIn(board.REMOTEIN, maxlen=120, idle_state=True)
decoder = adafruit_irremote.GenericDecode()
# size must match what you are decoding! for NEC use 4
received_code = bytearray(4)
# Make a list of lists with the IR code expected and our keypress out's
infrared_to_key = (
([255, 2, 191, 64], (Keycode.LEFT_CONTROL, Keycode.UP_ARROW)), # Vol+
([255, 2, 255, 0], (Keycode.LEFT_CONTROL, Keycode.DOWN_ARROW)), # Vol-
([255, 2, 127, 128],(Keycode.SPACE,)),
([255, 2, 111, 144],(Keycode.ENTER,)), # Enter / Save
([255, 2, 143, 112],(Keycode.DELETE,)), # Backwards arrow
([255, 2, 159, 96], (Keycode.ESCAPE,)), # Setup
([255, 2, 79, 176], (Keycode.DOWN_ARROW,)),
([255, 2, 239, 16], (Keycode.LEFT_ARROW,)),
([255, 2, 95, 160], (Keycode.UP_ARROW,)),
([255, 2, 175, 80], (Keycode.RIGHT_ARROW,)),
([255, 2, 207, 48], (Keycode.ZERO,)),
([255, 2, 247, 8], (Keycode.ONE,)),
([255, 2, 119, 136],(Keycode.TWO,)),
([255, 2, 183, 72], (Keycode.THREE,)),
([255, 2, 215, 40], (Keycode.FOUR,)),
([255, 2, 87, 168], (Keycode.FIVE,)),
([255, 2, 151, 104],(Keycode.SIX,)),
([255, 2, 231, 24], (Keycode.SEVEN,)),
([255, 2, 103, 152],(Keycode.EIGHT,)),
([255, 2, 167, 88], (Keycode.NINE,)),
)
print("Ready for NEC remote input!")
while True:
led[0] = (0, 0, 0) # LED off
pulses = decoder.read_pulses(pulsein)
#print("\tHeard", len(pulses), "Pulses:", pulses)
try:
code = decoder.decode_bits(pulses, debug=False)
print("Decoded:", code)
# Reads 4-byte code transmitted by NEC remotes and
# sends a matching key command
for pairs in infrared_to_key:
if pairs[0] == code:
led[0] = (0, 100, 0) # flash green
print("Matched IR code to keypresses: ", pairs[1])
keyboard.press(*pairs[1])
keyboard.release_all()
except adafruit_irremote.IRNECRepeatException: # unusual short code!
print("NEC repeat!")
except adafruit_irremote.IRDecodeException as e: # failed to decode
led[0] = (100, 0, 0) # flash red
print("Failed to decode: ", e.args)
print("----------------------------")

View file

@ -0,0 +1,34 @@
# Simple NEC remote decode-and-print example
# Prints out the 4-byte code transmitted by NEC remotes
import pulseio
import board
import adafruit_dotstar
import adafruit_irremote
led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
pulsein = pulseio.PulseIn(board.REMOTEIN, maxlen=120, idle_state=True)
decoder = adafruit_irremote.GenericDecode()
# size must match what you are decoding! for NEC use 4
received_code = bytearray(4)
print("Ready for NEC remote input!")
while True:
led[0] = (0, 0, 0) # LED off
pulses = decoder.read_pulses(pulsein)
print("\tHeard", len(pulses), "Pulses:", pulses)
try:
code = decoder.decode_bits(pulses, debug=False)
led[0] = (0, 100, 0) # flash green
print("Decoded:", code)
except adafruit_irremote.IRNECRepeatException: # unusual short code!
led[0] = (100, 100, 0) # flash yellow
print("NEC repeat!")
except adafruit_irremote.IRDecodeException as e: # failed to decode
led[0] = (100, 0, 0) # flash red
print("Failed to decode: ", e.args)
print("----------------------------")

View file

@ -0,0 +1,50 @@
Welcome to CircuitPython!
#############################
Visit the pIRkey M0 product page here for more info:
https://adafruit.com/product/3364
#############################
The pIRkey has a very tiny disk drive so we have disabled Mac OS X indexing
which could take up that valuable space.
So *please* do not remove the empty .fseventsd/no_log, .metadata_never_index
or .Trashes files!
#############################
The pre-loaded demo files show off what your pIRkey M0 can do with
CircuitPython:
* The default 'main.py' will read infrared pulses and print them out
to the serial console/REPL
* "NEC print example.py" will decode common 'NEC protocol' remotes and
print out the 4-digit code to the console
* "NEC keyboard example.py" will read NEC protocol remotes and show how
to trigger keyboard commands based on what codes are received. This example
is designed to be used with our simple low cost remote
https://www.adafruit.com/product/389
but is easily adapted to other NEC remotes
For more details on how to use and customize the pIRkey & CircuitPython, visit
https://adafruit.com/product/3364 and check out all the tutorials we have!
#############################
CircuitPython Quick Start:
Changing the code is as easy as editing main.py in your favorite text editor.
Our recommended editor is Mu, which is great for simple projects, and comes
with a built in REPL serial viewer! It is available for Mac, Windows & Linux
https://learn.adafruit.com/welcome-to-circuitpython/installing-mu-editor
After the file is saved, CircuitPython will automatically reload the latest
code. Try this out by renaming 'main.py' to 'my backup.py' and then renaming
'NEC print example.py' to main.py to load the different example sketch.
These sketches work best when you are also connected to the serial port / REPL
Connecting to the serial port will give you access to sensor information,
better error messages and an interactive CircuitPython (known as the REPL).
On Windows we recommend Mu, Tera Term or PuTTY.
On Mac OSX and Linux, use Mu or 'screen' can be used from a terminal.

18
Adafruit_pIRKey/main.py Normal file
View file

@ -0,0 +1,18 @@
# Simple IR remote listener example
# Good for basic testing!
import pulseio
import board
import time
import adafruit_dotstar
import adafruit_irremote
led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
decoder = adafruit_irremote.GenericDecode()
pulsein = pulseio.PulseIn(board.REMOTEIN, maxlen=200, idle_state=True)
while True:
led[0] = (0, 0, 0) # LED off
pulses = decoder.read_pulses(pulsein)
led[0] = (0, 0, 100) # flash blue
print("\tHeard", len(pulses), "Pulses:", pulses)