Skip to content

Instantly share code, notes, and snippets.

@sandyjmacdonald
Created January 23, 2021 11:23
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sandyjmacdonald/b465377dc11a8c83a8c40d1c9b990a90 to your computer and use it in GitHub Desktop.
Save sandyjmacdonald/b465377dc11a8c83a8c40d1c9b990a90 to your computer and use it in GitHub Desktop.
Emergency cat GIF macro example for Pimoroni RGB Keypad for Raspberry Pi Pico
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
# Keep reading button states, setting pixels
while True:
pressed = read_button_states(0, 16)
if pressed[0]:
pixels[0] = colourwheel(0 * 16) # Map pixel index to 0-255 range
if not held[0]:
# Trigger Spotlight search
kbd.send(Keycode.COMMAND, Keycode.SPACE)
time.sleep(0.2)
# Open Safari
layout.write("safari")
time.sleep(0.1)
kbd.send(Keycode.ENTER)
time.sleep(0.5)
# Focus on search bar
kbd.send(Keycode.COMMAND, Keycode.L)
time.sleep(0.1)
# Go to giphy car GIF URL
layout.write("https://giphy.com/explore/cat")
time.sleep(0.2)
kbd.send(Keycode.ENTER)
# Update held state to prevent retriggering
held[0] = 1
else: # Released state
pixels[0] = (0, 0, 0) # Turn pixel off
held[0] = 0 # Set held state to off
@marzsman
Copy link

marzsman commented Feb 5, 2021

@sandyjmacdonald I'm getting the error SDA or SCL needs a pull up on line 43
Anything I'm missing? I'm using the latest circuitpython and added the adafruit libraries to the lib folder.

@clawfire
Copy link

Got the same error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment