Last active
January 9, 2023 05:17
-
-
Save ckxng/14c0e1e12782546227a4eca332e953a8 to your computer and use it in GitHub Desktop.
QtPy Keyboard
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
import board | |
import neopixel | |
import touchio | |
from time import sleep | |
import usb_hid | |
from adafruit_hid.keyboard import Keyboard | |
from adafruit_hid.keycode import Keycode | |
# pin that the NeoPixel is connected to | |
PIXEL_PIN: Pin = board.NEOPIXEL | |
# the NeoPixel object | |
pixel: NeoPixel = neopixel.NeoPixel(PIXEL_PIN, 1) | |
# list of touch pads understood by touch(i) | |
touch_pads: list = [ | |
touchio.TouchIn(board.A0), | |
touchio.TouchIn(board.TX), | |
touchio.TouchIn(board.RX), | |
] | |
# the keyboard object | |
kbd: Kepboard = Keyboard(usb_hid.devices) | |
def light(r: int, g: int, b: int) -> None: | |
""" | |
set the first neopixel to (r,g,b) | |
makes the light display a color. | |
colors are built by mixing red, green, and blue light | |
each light can be specified in a range of 0 - 255 | |
for example: | |
light(255, 255, 255) - is a bright white light | |
light(255, 0, 0) - is a bright red light | |
light(0, 50, 0) - is a dark green light | |
light(0, 0, 0) - is off | |
""" | |
pixel[0] = (r, g, b) | |
def touch(pin: int = None) -> bool: | |
""" | |
return True if pin (or any pin if pin=None) is being touched | |
when called without arguments as touch(), returns true | |
if any touch pad is being touched. | |
when called with an argument as touch(0), touch(1), or touch(2), | |
returns true only if pad 0, 1, or 2 is being touched. | |
return(boolean) | |
""" | |
if pin is None: | |
for pad in touch_pads: | |
if pad.value: | |
return True | |
return False | |
# coerce to a boolean | |
return touch_pads[pin].value and True | |
# main loop | |
while True: | |
# when the first pad is being touched | |
if touch(0): | |
# set a purple light and send a keyboard scancode for a capital A | |
light(100, 0, 100) | |
kbd.send(Keycode.SHIFT, Keycode.A) | |
# spin while the button is held, then set the light to off | |
while touch(0): | |
pass | |
light(0, 0, 0) | |
# while the second pad is being touched | |
elif touch(1): | |
# set a red light and send a keyboard scancode for a lowecase X | |
light(100, 0, 0) | |
kbd.send(0, Keycode.X) | |
# spin while the button is held, then set the light to off | |
while touch(1): | |
pass | |
light(0, 0, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment