Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
3 Button Keyboard in CircuitPython for Trinket M0
# Trinket 3 Button Keyboard
import board
from digitalio import DigitalInOut, Direction, Pull
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
import adafruit_dotstar as dotstar
import time
# One pixel connected internally!
dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
# Built in red LED
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
# Digital input with pullup on D1
button1 = DigitalInOut(board.D1)
button1.direction = Direction.INPUT
button1.pull = Pull.UP
button1_in = False
# Digital input with pullup on D2
button2 = DigitalInOut(board.D2)
button2.direction = Direction.INPUT
button2.pull = Pull.UP
button2_in = False
# Digital input with pullup on D3
button3 = DigitalInOut(board.D3)
button3.direction = Direction.INPUT
button3.pull = Pull.UP
button3_in = False
# Used if when we do HID output,
kbd = Keyboard()
######################### HELPERS ##############################
# Helper to give us a nice color swirl
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):
return (0, 0, 0)
if (pos > 255):
return (0, 0, 0)
if (pos < 85):
return (int(pos * 3), int(255 - (pos*3)), 0)
elif (pos < 170):
pos -= 85
return (int(255 - pos*3), 0, int(pos*3))
else:
pos -= 170
return (0, int(pos*3), int(255 - pos*3))
######################### MAIN LOOP ##############################
i = 0
while True:
# spin internal LED around! autoshow is on
dot[0] = wheel(i & 255)
if not button1.value and not button1_in:
print("Button on D1 pressed!")
#Change to be the Keyboard Combo that suits you
kbd.press(Keycode.SPACEBAR)
kbd.release_all()
button1_in = True
elif button1.value and button1_in:
button1_in = False
if not button2.value and not button2_in:
print("Button on D2 pressed!")
#Change to be the Keyboard Combo that suits you
kbd.press(Keycode.COMMAND)
kbd.press(Keycode.SHIFT)
kbd.press(Keycode.DELETE)
kbd.release_all()
button2_in = True
elif button2.value and button2_in:
button2_in = False
if not button3.value and not button3_in:
print("Button on D3 pressed!")
#Change to be the Keyboard Combo that suits you
kbd.press(Keycode.F5)
kbd.release_all()
button3_in = True
elif button3.value and button3_in:
button3_in = False
i = (i+1) % 256 # run from 0 to 255
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.