Skip to content

Instantly share code, notes, and snippets.

@thnikk
Created April 10, 2019 17:14
Show Gist options
  • Save thnikk/5f9cbb6f344f9df37a7519b7e540bdae to your computer and use it in GitHub Desktop.
Save thnikk/5f9cbb6f344f9df37a7519b7e540bdae to your computer and use it in GitHub Desktop.
Simplified CircuitPython code for new hot-swap keypad
# Import all necessary libraries
import board
import touchio
from digitalio import DigitalInOut, Direction, Pull
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
import adafruit_dotstar as dotstar
#import time
import neopixel
# Change these values for what you want each key to print when pressed
keys_pressed = ["Button 1 ", "Button 2 ", "Button 3 "]
# RGB LED code is necessary for disabling the LED since it would otherwise keep its last color
dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=1)
dot[0] = 0
button1 = DigitalInOut(board.D0) # Pin assignment for button 1
button1.direction = Direction.INPUT
button1.pull = Pull.UP
button2 = DigitalInOut(board.D2) # Pin assignment for button 2
button2.direction = Direction.INPUT
button2.pull = Pull.UP
screw = board.D4 # Pin assignmnet for side screw
touch = touchio.TouchIn(screw)
# Set up keyboard
kbd = Keyboard()
layout = KeyboardLayoutUS(kbd) # Set US layout
# Keeps keys from being constantly pressed/released
button1Check=1
button2Check=1
button3Check=0 # Inverted to avoid press at startup
######################### MAIN LOOP ##############################
while True:
if not button1.value: # If the button isn't pressed...
if button1Check: # Check value so it only does this once per press
layout.write(keys_pressed[0]) # Print string
button1Check = 0 # Update check value
if button1.value: # If button is pressed...
button1Check = 1 # Update check value
if not button2.value:
if button2Check:
layout.write(keys_pressed[1])
button2Check = 0
if button2.value:
button2Check = 1
if not touch.value:
if button3Check:
layout.write(keys_pressed[2])
button3Check = 0
if touch.value:
button3Check = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment