Created
November 30, 2019 08:10
-
-
Save xobs/3ec70ce15cf1d071719d78f7d0adba6f to your computer and use it in GitHub Desktop.
Fomu keyboard test
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
import time | |
import microcontroller | |
import digitalio | |
from adafruit_hid.keyboard import Keyboard | |
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS | |
from adafruit_hid.keycode import Keycode | |
# A simple neat keyboard demo in CircuitPython | |
# The pins we'll use, each will have an internal pullup | |
keypress_pins = [microcontroller.pin.TOUCH1, microcontroller.pin.TOUCH2] | |
# Our array of key objects | |
key_pin_array = [] | |
# The Keycode sent for each button, will be paired with a control key | |
keys_pressed = [Keycode.A, "Hello World!\n"] | |
control_key = Keycode.SHIFT | |
# The keyboard object! | |
keyboard = Keyboard() | |
keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :) | |
# Make all pin objects inputs with pullups | |
for pin in keypress_pins: | |
key_pin = digitalio.DigitalInOut(pin) | |
key_pin.direction = digitalio.Direction.INPUT | |
key_pin.pull = digitalio.Pull.UP | |
key_pin_array.append(key_pin) | |
# led = digitalio.DigitalInOut(board.D13) | |
# led.direction = digitalio.Direction.OUTPUT | |
print("Waiting for key pin...") | |
while True: | |
# Check each pin | |
for key_pin in key_pin_array: | |
if not key_pin.value: # Is it grounded? | |
i = key_pin_array.index(key_pin) | |
print("Pin #%d is grounded." % i) | |
# Turn on the red LED | |
# led.value = True | |
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"! | |
time.sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tested this and it works on the Fomu Hacker. It just starts very slowly for me (+30s).
The required libraries are available here: https://circuitpython.org/libraries
The circuitpython bitstream is also available there.