Created
May 13, 2022 22:35
-
-
Save odwdinc/8d88b9829e6e7e57cce6c1e900b5d485 to your computer and use it in GitHub Desktop.
circuitpython Keyboard Code
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 keypad | |
import neopixel | |
import usb_hid | |
from adafruit_hid.keyboard import Keyboard | |
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS | |
from adafruit_hid.keycode import Keycode | |
import math | |
import random | |
keys_pressed = [ | |
Keycode.GRAVE_ACCENT, Keycode.ONE, Keycode.TWO, Keycode.THREE, Keycode.FOUR, Keycode.FIVE, Keycode.SIX, Keycode.SEVEN, Keycode.EIGHT, Keycode.NINE, Keycode.ZERO, Keycode.BACKSPACE, Keycode.MINUS, Keycode.EQUALS, u'😀', Keycode.KEYPAD_FORWARD_SLASH, Keycode.KEYPAD_ASTERISK, Keycode.KEYPAD_MINUS, | |
Keycode.TAB, Keycode.Q, Keycode.W, Keycode.E, Keycode.R, Keycode.T, Keycode.Y, Keycode.U, Keycode.I, Keycode.O, Keycode.P, Keycode.LEFT_BRACKET, Keycode.RIGHT_BRACKET, Keycode.BACKSLASH, Keycode.KEYPAD_SEVEN, Keycode.KEYPAD_EIGHT, Keycode.KEYPAD_NINE, Keycode.KEYPAD_PLUS, | |
Keycode.ESCAPE, Keycode.A, Keycode.S, Keycode.D, Keycode.F, Keycode.G, Keycode.H, Keycode.J, Keycode.K, Keycode.L, Keycode.SEMICOLON, Keycode.QUOTE, Keycode.SHIFT, Keycode.RETURN, Keycode.KEYPAD_FOUR, Keycode.KEYPAD_FIVE, Keycode.KEYPAD_SIX, Keycode.KEYPAD_NUMLOCK, | |
Keycode.LEFT_SHIFT, Keycode.Z, Keycode.X, Keycode.C, Keycode.V, Keycode.B, Keycode.N, Keycode.M, Keycode.COMMA, Keycode.PERIOD, Keycode.FORWARD_SLASH, Keycode.UP_ARROW, '', Keycode.PAGE_UP, Keycode.KEYPAD_ONE, Keycode.KEYPAD_TWO, Keycode.KEYPAD_THREE, Keycode.KEYPAD_PERIOD, | |
Keycode.CONTROL, Keycode.ALT, Keycode.GUI, Keycode.DELETE, Keycode.HOME, Keycode.SPACE, Keycode.SPACE, Keycode.END, 'FN', Keycode.APPLICATION, Keycode.LEFT_ARROW, Keycode.DOWN_ARROW, Keycode.RIGHT_ARROW, Keycode.PAGE_DOWN, '', Keycode.KEYPAD_ZERO, Keycode.BACKSPACE, Keycode.KEYPAD_ENTER, | |
] | |
ORDER = neopixel.GRB | |
COLUMNS = 6 * 3 | |
ROWS = 5 | |
pixels = [ | |
neopixel.NeoPixel(board.GP28, 30, brightness=0.3), | |
neopixel.NeoPixel(board.GP0, 30, brightness=0.3), | |
neopixel.NeoPixel(board.GP15, 30, brightness=0.3) | |
] | |
keys = keypad.KeyMatrix( | |
row_pins=(board.GP27, board.GP26, board.GP22, board.GP21, board.GP20), | |
column_pins=( | |
board.GP19, board.GP18, board.GP17, board.GP16, board.GP14, board.GP13, | |
board.GP12, board.GP11, board.GP10, board.GP9, board.GP8, board.GP7, | |
board.GP6, board.GP5, board.GP4, board.GP3, board.GP2, board.GP1), | |
columns_to_anodes=False, | |
) | |
def wheel(pos): | |
# Input a value 0 to 255 to get a color value. | |
# The colours are a transition r - g - b - back to r. | |
if pos < 0 or pos > 255: | |
r = g = b = 0 | |
elif pos < 85: | |
r = int(pos * 3) | |
g = int(255 - pos * 3) | |
b = 0 | |
elif pos < 170: | |
pos -= 85 | |
r = int(255 - pos * 3) | |
g = 0 | |
b = int(pos * 3) | |
else: | |
pos -= 170 | |
r = 0 | |
g = int(pos * 3) | |
b = int(255 - pos * 3) | |
return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0) | |
# maps key_number to neopixel | |
def key_to_pixel_map(key_number): | |
row = key_number // COLUMNS | |
panal = math.floor((key_number % COLUMNS) / 6) | |
column = (key_number % COLUMNS) % 6 | |
if panal is 1 and row is 4: | |
column -= 1 | |
if row % 2 == 1: | |
column = 6 - column - 1 | |
return row * 6 + column, panal | |
pixels[0].fill((0, 0, 0)) # Begin with pixels off. | |
pixels[1].fill((0, 0, 0)) # Begin with pixels off. | |
pixels[2].fill((0, 0, 0)) # Begin with pixels off. | |
lastrun = [] | |
keyboard = Keyboard(usb_hid.devices) | |
keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :) | |
while True: | |
key_event = keys.events.get() | |
if key_event: | |
# print(key_event) | |
KeyNum = key_event.key_number | |
led, panal = key_to_pixel_map(KeyNum) | |
key = keys_pressed[KeyNum] # Get the corresponding Keycode or string | |
if isinstance(key, str): # If it's a string... | |
if key_event.pressed: | |
try: | |
keyboard_layout.write(key) # ...Print the string | |
except ValueError: | |
sets = [hex(ord(c)) for c in key] # ...Print the Emoticone / Emoji | |
for k in sets: | |
keyboard.press(Keycode.RIGHT_ALT) | |
keyboard.release(Keycode.RIGHT_ALT) | |
keyboard_layout.write('U') | |
keyboard_layout.write(k[2:]) | |
keyboard_layout.write('\n') | |
else: # If it's not a string... | |
if key_event.pressed: | |
keyboard.press(key) # "Press"... | |
pixels[panal][led] = wheel(random.randint(0, 255)) | |
else: | |
pixels[panal].fill((0, 0, 0)) | |
keyboard.release(key) # ..."Release"! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment