2021-08-25 09:45:46 +02:00
|
|
|
|
import board
|
2021-08-25 09:48:08 +02:00
|
|
|
|
import time
|
2021-08-25 09:45:46 +02:00
|
|
|
|
from keybow2040 import Keybow2040
|
|
|
|
|
|
2021-08-25 09:48:08 +02:00
|
|
|
|
import usb_hid
|
|
|
|
|
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
|
|
|
|
|
|
2021-08-25 09:45:46 +02:00
|
|
|
|
# Set up Keybow
|
|
|
|
|
i2c = board.I2C()
|
|
|
|
|
keybow = Keybow2040(i2c)
|
|
|
|
|
keys = keybow.keys
|
|
|
|
|
|
2021-08-25 09:48:08 +02:00
|
|
|
|
# Set up the keyboard and layout
|
|
|
|
|
keyboard = Keyboard(usb_hid.devices)
|
|
|
|
|
layout = KeyboardLayoutUS(keyboard)
|
|
|
|
|
|
|
|
|
|
# Set up consumer control (used to send media key presses)
|
|
|
|
|
consumer_control = ConsumerControl(usb_hid.devices)
|
|
|
|
|
|
|
|
|
|
# Our layers. The key of item in the layer dictionary is the key number on
|
|
|
|
|
# Keybow to map to, and the value is the key press to send.
|
|
|
|
|
|
|
|
|
|
# Note that keys 0-3 are reserved as the modifier and layer selector keys
|
|
|
|
|
# respectively.
|
|
|
|
|
|
2021-08-25 10:22:31 +02:00
|
|
|
|
layer_0 = {4: Keycode.COMMA,
|
|
|
|
|
5: Keycode.ONE,
|
|
|
|
|
9: Keycode.FOUR,
|
|
|
|
|
7: Keycode.SEVEN}
|
|
|
|
|
|
2021-08-25 10:10:23 +02:00
|
|
|
|
layer_1 = {4: Keycode.COMMA,
|
2021-08-25 09:48:08 +02:00
|
|
|
|
5: Keycode.ONE,
|
|
|
|
|
6: Keycode.FOUR,
|
|
|
|
|
7: Keycode.SEVEN,
|
2021-08-25 10:10:23 +02:00
|
|
|
|
8: Keycode.ZERO,
|
2021-08-25 09:48:08 +02:00
|
|
|
|
9: Keycode.TWO,
|
|
|
|
|
10: Keycode.FIVE,
|
|
|
|
|
11: Keycode.EIGHT,
|
|
|
|
|
12: Keycode.ENTER,
|
|
|
|
|
13: Keycode.THREE,
|
|
|
|
|
14: Keycode.SIX,
|
|
|
|
|
15: Keycode.NINE}
|
|
|
|
|
|
2021-08-25 10:10:49 +02:00
|
|
|
|
layer_2 = {4: Keycode.COMMA,
|
|
|
|
|
5: Keycode.ONE,
|
|
|
|
|
6: Keycode.FOUR,
|
|
|
|
|
7: Keycode.SEVEN}
|
2021-08-25 09:48:08 +02:00
|
|
|
|
|
2021-08-25 10:28:30 +02:00
|
|
|
|
layer_3 = {5: ConsumerControlCode.VOLUME_DECREMENT,
|
2021-08-25 09:48:08 +02:00
|
|
|
|
7: ConsumerControlCode.SCAN_PREVIOUS_TRACK,
|
2021-08-25 10:28:30 +02:00
|
|
|
|
9: ConsumerControlCode.MUTE,
|
2021-08-25 09:48:08 +02:00
|
|
|
|
11: ConsumerControlCode.PLAY_PAUSE,
|
2021-08-25 10:28:30 +02:00
|
|
|
|
13: ConsumerControlCode.VOLUME_INCREMENT,
|
2021-08-25 09:48:08 +02:00
|
|
|
|
15: ConsumerControlCode.SCAN_NEXT_TRACK}
|
|
|
|
|
|
2021-08-25 10:22:31 +02:00
|
|
|
|
layers = {0: layer_0,
|
|
|
|
|
1: layer_1,
|
2021-08-25 10:10:49 +02:00
|
|
|
|
2: layer_2,
|
2021-08-25 09:48:08 +02:00
|
|
|
|
3: layer_3}
|
|
|
|
|
|
2021-08-25 10:22:31 +02:00
|
|
|
|
selectors = {0: keys[0],
|
|
|
|
|
1: keys[1],
|
2021-08-25 10:10:49 +02:00
|
|
|
|
2: keys[2],
|
2021-08-25 09:48:08 +02:00
|
|
|
|
3: keys[3]}
|
|
|
|
|
|
2021-08-25 10:11:01 +02:00
|
|
|
|
# Start on layer 3
|
|
|
|
|
current_layer = 3
|
2021-08-25 09:48:08 +02:00
|
|
|
|
|
|
|
|
|
# The colours for each layer
|
2021-08-25 10:35:52 +02:00
|
|
|
|
# https://gist.github.com/ninrod/b0f86d77ebadaccf7d9d4431dd8e2983
|
|
|
|
|
# https://colorhunt.co/palette/f7fd04f9b208f98404fc5404
|
|
|
|
|
colours = {0: (247, 253, 4),
|
|
|
|
|
1: (249, 178, 8),
|
|
|
|
|
2: (249, 132, 4),
|
|
|
|
|
3: (252, 84, 4)}
|
2021-08-25 09:48:08 +02:00
|
|
|
|
|
|
|
|
|
layer_keys = range(4, 16)
|
|
|
|
|
|
|
|
|
|
# Set the LEDs for each key in the current layer
|
|
|
|
|
for k in layers[current_layer].keys():
|
|
|
|
|
keys[k].set_led(*colours[current_layer])
|
|
|
|
|
|
|
|
|
|
# To prevent the strings (as opposed to single key presses) that are sent from
|
|
|
|
|
# refiring on a single key press, the debounce time for the strings has to be
|
|
|
|
|
# longer.
|
|
|
|
|
short_debounce = 0.03
|
|
|
|
|
long_debounce = 0.15
|
2021-08-25 10:43:56 +02:00
|
|
|
|
debounce = short_debounce
|
2021-08-25 09:48:08 +02:00
|
|
|
|
fired = False
|
2021-08-25 09:45:46 +02:00
|
|
|
|
|
|
|
|
|
while True:
|
2021-08-25 09:48:08 +02:00
|
|
|
|
# Always remember to call keybow.update()!
|
2021-08-25 09:45:46 +02:00
|
|
|
|
keybow.update()
|
|
|
|
|
|
2021-08-25 09:48:08 +02:00
|
|
|
|
|
2021-08-25 10:11:42 +02:00
|
|
|
|
# If the modifier key is held, light up the layer selector keys
|
|
|
|
|
for layer in layers.keys():
|
|
|
|
|
keys[layer].set_led(*colours[layer])
|
2021-08-25 09:48:08 +02:00
|
|
|
|
|
2021-08-25 10:11:42 +02:00
|
|
|
|
# Change layer if layer key is pressed
|
|
|
|
|
if current_layer != layer:
|
|
|
|
|
if selectors[layer].pressed:
|
|
|
|
|
current_layer = layer
|
2021-08-25 09:48:08 +02:00
|
|
|
|
|
2021-08-25 10:11:42 +02:00
|
|
|
|
# Set the key LEDs first to off, then to their layer colour
|
|
|
|
|
for k in layer_keys:
|
|
|
|
|
keys[k].set_led(0, 0, 0)
|
2021-08-25 09:48:08 +02:00
|
|
|
|
|
2021-08-25 10:11:42 +02:00
|
|
|
|
for k in layers[layer].keys():
|
|
|
|
|
keys[k].set_led(*colours[layer])
|
2021-08-25 09:48:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Loop through all of the keys in the layer and if they're pressed, get the
|
|
|
|
|
# key code from the layer's key map
|
|
|
|
|
for k in layers[current_layer].keys():
|
|
|
|
|
if keys[k].pressed:
|
|
|
|
|
key_press = layers[current_layer][k]
|
|
|
|
|
|
|
|
|
|
# If the key hasn't just fired (prevents refiring)
|
|
|
|
|
if not fired:
|
|
|
|
|
fired = True
|
|
|
|
|
|
|
|
|
|
# Send the right sort of key press and set debounce for each
|
|
|
|
|
# layer accordingly (layer 2 needs a long debounce)
|
|
|
|
|
if current_layer == 1:
|
|
|
|
|
debounce = short_debounce
|
|
|
|
|
keyboard.send(key_press)
|
|
|
|
|
elif current_layer == 3:
|
|
|
|
|
debounce = short_debounce
|
|
|
|
|
consumer_control.send(key_press)
|
|
|
|
|
|
|
|
|
|
# If enough time has passed, reset the fired variable
|
|
|
|
|
if fired and time.monotonic() - keybow.time_of_last_press > debounce:
|
|
|
|
|
fired = False
|