Last active
September 24, 2022 20:22
-
-
Save Corteil/18fc0794e03bf9416fa5eda144e3ac54 to your computer and use it in GitHub Desktop.
Circuitpython code for the Keybow using a Raspberry Pico instead of a Raspberry Pi Zero
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
# based on example code from Adafruit and dglaude plus some of my own! | |
# | |
# https://github.com/dglaude/circuitpython_phat_on_pico/blob/main/keybow_mini.py | |
# | |
import time | |
import board | |
import digitalio | |
import adafruit_dotstar | |
# keyboard inputs | |
import usb_hid | |
from adafruit_hid.keyboard import Keyboard | |
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS | |
from adafruit_hid.keycode import Keycode | |
# define dotstar leds | |
pixels = adafruit_dotstar.DotStar(board.GP2, board.GP3, 12) | |
# define keys IO pins. | |
pins = [board.GP7, board.GP8, board.GP27, board.GP9, board.GP26, board.GP10, board.GP11, board.GP18, board.GP12, | |
board.GP16, board.GP17, board.GP14] | |
# map the LEDs position to the keys position | |
leds_mapping = [3, 7, 11, 2, 6, 10, 1, 5, 9, 0, 4, 8] | |
# setup keys | |
keys = [] | |
for pin in pins: | |
key_pin = digitalio.DigitalInOut(pin) | |
key_pin.direction = digitalio.Direction.INPUT | |
key_pin.pull = digitalio.Pull.UP | |
keys.append(key_pin) | |
# define keycodes | |
keys_pressed = ["Robot", Keycode.B, Keycode.C, Keycode.D, Keycode.E, Keycode.F, Keycode.G, Keycode.H, Keycode.I, Keycode.J, Keycode.K, Keycode.L] | |
control_key = Keycode.SHIFT | |
# The keyboard object! | |
time.sleep(1) # Sleep for a bit to avoid a race condition on some systems | |
keyboard = Keyboard(usb_hid.devices) | |
keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the UK :) | |
for pixel in range(0,12): | |
print(f"pixel no:{pixel} ") | |
time.sleep(0.1) | |
pixels[pixel] = (0, 125, 125) | |
# define board LED | |
led = digitalio.DigitalInOut(board.LED) | |
led.direction = digitalio.Direction.OUTPUT | |
# main loop | |
print("Waiting for key pin...") | |
while True: | |
# Check each pin | |
for key_pin in keys: | |
if not key_pin.value: # Is it grounded? | |
i = keys.index(key_pin) | |
print("Pin #%d is grounded." % i) | |
# Turn on the Pico LED and key LED | |
led.value = True | |
pixels[leds_mapping[i]] = (255, 125, 0) | |
while not key_pin.value: | |
pass # Wait for it to be ungrounded! | |
# "Type" the Keycode or string | |
key = keys_pressed[i] # Get the corresponding Keycode or string | |
if isinstance(key, str): # If it's a string... | |
keyboard_layout.write(key) # ...Print the string | |
else: # If it's not a string... | |
keyboard.press(control_key, key) # "Press"... | |
keyboard.release_all() # ..."Release"! | |
# Turn off the Pico LED | |
led.value = False | |
pixels[leds_mapping[i]] = (0, 125, 125) | |
time.sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rename as code.py to run on startup, you will also need to save the adafruit_hid and the dotstart libraries in the lib folder on the Pico