2021-03-12 17:56:59 +01:00
|
|
|
# SPDX-FileCopyrightText: 2021 Sandy Macdonald
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
# A simple example of how to set up a keymap and HID keyboard on Keybow 2040.
|
|
|
|
|
|
|
|
# You'll need to connect Keybow 2040 to a computer, as you would with a regular
|
|
|
|
# USB keyboard.
|
|
|
|
|
|
|
|
# Drop the keybow2040.py file into your `lib` folder on your `CIRCUITPY` drive.
|
|
|
|
|
|
|
|
# NOTE! Requires the adafruit_hid CircuitPython library also!
|
|
|
|
|
2021-03-12 13:38:06 +01:00
|
|
|
import board
|
|
|
|
from keybow2040 import Keybow2040
|
|
|
|
|
|
|
|
import usb_hid
|
|
|
|
from adafruit_hid.keyboard import Keyboard
|
|
|
|
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
|
|
|
|
from adafruit_hid.keycode import Keycode
|
|
|
|
|
2021-03-12 17:56:59 +01:00
|
|
|
# Set up Keybow
|
2021-03-12 13:38:06 +01:00
|
|
|
i2c = board.I2C()
|
|
|
|
keybow = Keybow2040(i2c)
|
|
|
|
keys = keybow.keys
|
|
|
|
|
2021-03-12 17:56:59 +01:00
|
|
|
# Set up the keyboard and layout
|
2021-03-12 13:38:06 +01:00
|
|
|
keyboard = Keyboard(usb_hid.devices)
|
|
|
|
layout = KeyboardLayoutUS(keyboard)
|
|
|
|
|
2021-03-12 17:56:59 +01:00
|
|
|
# A map of keycodes that will be mapped sequentially to each of the keys, 0-15
|
2021-03-12 13:38:06 +01:00
|
|
|
keymap = [Keycode.ZERO,
|
|
|
|
Keycode.ONE,
|
|
|
|
Keycode.TWO,
|
|
|
|
Keycode.THREE,
|
|
|
|
Keycode.FOUR,
|
|
|
|
Keycode.FIVE,
|
|
|
|
Keycode.SIX,
|
|
|
|
Keycode.SEVEN,
|
|
|
|
Keycode.EIGHT,
|
|
|
|
Keycode.NINE,
|
|
|
|
Keycode.A,
|
|
|
|
Keycode.B,
|
|
|
|
Keycode.C,
|
|
|
|
Keycode.D,
|
|
|
|
Keycode.E,
|
|
|
|
Keycode.F]
|
|
|
|
|
2021-03-12 17:56:59 +01:00
|
|
|
# The colour to set the keys when pressed, yellow.
|
|
|
|
rgb = (255, 255, 0)
|
|
|
|
|
|
|
|
# Attach handler functions to all of the keys
|
2021-03-12 13:38:06 +01:00
|
|
|
for key in keys:
|
2021-03-12 17:56:59 +01:00
|
|
|
# A press handler that sends the keycode and turns on the LED
|
2021-03-12 13:38:06 +01:00
|
|
|
@keybow.on_press(key)
|
|
|
|
def press_handler(key):
|
|
|
|
keycode = keymap[key.number]
|
|
|
|
keyboard.send(keycode)
|
2021-03-12 17:56:59 +01:00
|
|
|
key.set_led(*rgb)
|
|
|
|
|
|
|
|
# A release handler that turns off the LED
|
|
|
|
@keybow.on_release(key)
|
|
|
|
def release_handler(key):
|
|
|
|
key.led_off()
|
2021-03-12 13:38:06 +01:00
|
|
|
|
|
|
|
while True:
|
2021-03-12 17:56:59 +01:00
|
|
|
# Always remember to call keybow.update()!
|
2021-03-12 13:38:06 +01:00
|
|
|
keybow.update()
|