Skip to content

Instantly share code, notes, and snippets.

@benevpi
Created February 10, 2021 16:50
Show Gist options
  • Save benevpi/797a572ec13fb499b3145ba05856b859 to your computer and use it in GitHub Desktop.
Save benevpi/797a572ec13fb499b3145ba05856b859 to your computer and use it in GitHub Desktop.
pico keyboard
# Write your code here :-)
import time
import board
import busio
import usb_hid
from adafruit_bus_device.i2c_device import I2CDevice
import adafruit_dotstar
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
from digitalio import DigitalInOut, Direction, Pull
# Emergency cat GIF example for Pimoroni RGB Keypad for Raspberry Pi Pico
# Prerequisites
#
# Requires Adafruit CircuitPython: https://learn.adafruit.com/getting-started-with-raspberry-pi-pico-circuitpython
#
# Also requires the following CircuitPython libs: adafruit_hid, adafruit_bus_device, adafruit_dotstar
# (drop them into the lib folder)
#
# Save this code in code.py on your Raspberry Pi Pico CIRCUITPY drive
#
# Note that this example is specific for macOS and uses button 0 to trigger the macro.
# Pull CS pin low to enable level shifter
cs = DigitalInOut(board.GP17)
cs.direction = Direction.OUTPUT
cs.value = 0
# Set up APA102 pixels
num_pixels = 16
pixels = adafruit_dotstar.DotStar(board.GP18, board.GP19, num_pixels, brightness=0.1, auto_write=True)
# Set up I2C for IO expander (addr: 0x20)
i2c = busio.I2C(board.GP5, board.GP4)
device = I2CDevice(i2c, 0x20)
# Set up the keyboard
kbd = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(kbd)
# Function to map 0-255 to position on colour wheel
def colourwheel(pos):
if pos < 0 or pos > 255:
return (0, 0, 0)
if pos < 85:
return (255 - pos * 3, pos * 3, 0)
if pos < 170:
pos -= 85
return (0, 255 - pos * 3, pos * 3)
pos -= 170
return (pos * 3, 0, 255 - pos * 3)
# Read button states from the I2C IO expander on the keypad
def read_button_states(x, y):
pressed = [0] * 16
with device:
# Read from IO expander, 2 bytes (8 bits) correspond to the 16 buttons
device.write(bytes([0x0]))
result = bytearray(2)
device.readinto(result)
b = result[0] | result[1] << 8
# Loop through the buttons
for i in range(x, y):
if not (1 << i) & b:
pressed[i] = 1
else:
pressed[i] = 0
return pressed
# List to store the button states
held = [0] * 16
key_codes = [Keycode.A,Keycode.B,Keycode.C, Keycode.D,
Keycode.E, Keycode.F, Keycode.G, Keycode.H,
Keycode.I,Keycode.J,Keycode.K, Keycode.L,
Keycode.M, Keycode.N, Keycode.O, Keycode.P,]
# Keep reading button states, setting pixels
while True:
pressed = read_button_states(0, 16)
for i in range(16):
if pressed[i]:
pixels[i] = colourwheel(0 * 16) # Map pixel index to 0-255 range
if not held[i]:
#kbd.send(key_codes[i])
layout.write("tweeting from Pico (sort of)! ")
layout.write("Thanks @sandyjmacdonald for the code. ")
layout.write("Stolen from https://gist.github.com/wildestpixel/6b684b8bc886392f7c4c57015fab3d97")
# Update held state to prevent retriggering
held[i] = 1
else: # Released state
pixels[i] = (0, 0, 0) # Turn pixel off
held[i] = 0 # Set held state to off
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment