keybow2040/code/code_eigenbau.py

125 lines
4.1 KiB
Python
Raw Normal View History

2021-08-25 22:18:23 +02:00
import board
import time
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
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
# Set up Keybow
i2c = board.I2C()
keybow = Keybow2040(i2c)
keys = keybow.keys
# 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)
2021-08-25 22:45:55 +02:00
# Denfiniere Layer als Dictionary
2021-08-25 22:18:23 +02:00
layer_0 = {}
layer_1 = {4: Keycode.PERIOD,
5: Keycode.ONE,
6: Keycode.FOUR,
7: Keycode.SEVEN,
8: Keycode.ZERO,
9: Keycode.TWO,
10: Keycode.FIVE,
11: Keycode.EIGHT,
12: Keycode.ENTER,
13: Keycode.THREE,
14: Keycode.SIX,
15: Keycode.NINE}
layer_2 = {4: "Mit freundlichen Gruessen\nMichael Grote "
}
layer_3 = {6: ConsumerControlCode.VOLUME_DECREMENT,
7: ConsumerControlCode.SCAN_PREVIOUS_TRACK,
10: ConsumerControlCode.MUTE,
11: ConsumerControlCode.PLAY_PAUSE,
14: ConsumerControlCode.VOLUME_INCREMENT,
15: ConsumerControlCode.SCAN_NEXT_TRACK}
layers = {0: layer_0,
1: layer_1,
2: layer_2,
3: layer_3}
2021-08-25 22:45:55 +02:00
# welches ist der aktive Layer
2021-08-25 22:18:23 +02:00
current_layer = 1
2021-08-25 22:45:55 +02:00
# weclche Tasten dienen nicht der Layer-Auswahl
2021-08-25 22:18:23 +02:00
nutzbare_tasten = range(4,16) #zählt von 1-16; nicht 0-15
2021-08-25 22:45:55 +02:00
# Debounce-Werte
2021-08-25 22:32:18 +02:00
short_debounce = 0.03
long_debounce = 0.15
debounce = short_debounce
taste_ausgelöst = False
2021-08-25 22:18:23 +02:00
2021-08-25 22:45:55 +02:00
# Farben der Layer als Dictionary
2021-08-25 22:18:23 +02:00
# https://gist.github.com/ninrod/b0f86d77ebadaccf7d9d4431dd8e2983
# https://colorhunt.co/palette/f7fd04f9b208f98404fc5404
2021-08-25 22:45:55 +02:00
colours = {0: (247, 253, 4),
1: (249, 178, 8),
2: (249, 132, 4),
2021-08-25 23:05:38 +02:00
3: (252, 84, 4),
4: (255,155,255)} # weiß
2021-08-25 22:45:55 +02:00
# setze farben auf layer keys und den nutzbaren tasten nach dem start
keybow.keys[0].set_led(*colours[0])
keybow.keys[1].set_led(*colours[1])
keybow.keys[2].set_led(*colours[2])
keybow.keys[3].set_led(*colours[3])
2021-08-25 22:18:23 +02:00
while True:
# Always remember to call keybow.update()!
keybow.update()
2021-08-25 22:45:55 +02:00
# wenn layer-auswahl key gedrückt dann..
2021-08-25 22:18:23 +02:00
if keys[0].pressed:
2021-08-25 23:31:22 +02:00
# setze ALLE nutzbaren tasten auf weiß
for k in nutzbare_tasten:
keys[k].set_led(*colours[4])
2021-08-25 22:45:55 +02:00
# setze current layer
2021-08-25 22:18:23 +02:00
current_layer = 0
elif keys[1].pressed:
2021-08-25 23:31:22 +02:00
for k in nutzbare_tasten:
keys[k].set_led(*colours[4])
2021-08-25 22:18:23 +02:00
current_layer = 1
elif keys[2].pressed:
2021-08-25 23:31:22 +02:00
for k in nutzbare_tasten:
keys[k].set_led(*colours[4])
2021-08-25 22:18:23 +02:00
current_layer = 2
elif keys[3].pressed:
2021-08-25 23:31:22 +02:00
for k in nutzbare_tasten:
keys[k].set_led(*colours[4])
2021-08-25 22:18:23 +02:00
current_layer = 3
2021-08-25 23:31:22 +02:00
# färbe zum layer gehörende tasten ein
for b in layers[current_layer].keys():
keys[b].set_led(*colours[current_layer])
2021-08-25 22:45:55 +02:00
# "frage" alle tasten ab
2021-08-25 22:18:23 +02:00
for taste in layers[current_layer].keys():
2021-08-25 22:45:55 +02:00
# wenn gedrückt
2021-08-25 22:18:23 +02:00
if keys[taste].pressed:
2021-08-25 23:31:22 +02:00
2021-08-25 22:45:55 +02:00
# setze keycode
2021-08-25 22:18:23 +02:00
key_press = layers[current_layer][taste]
2021-08-25 22:45:55 +02:00
# prufe ob taste vor kurzem ausgelöst wurde
2021-08-25 22:32:18 +02:00
if not taste_ausgelöst:
taste_ausgelöst = True
2021-08-25 22:45:55 +02:00
# prüfe layer wegen debounce und "sende"-art
2021-08-25 22:32:18 +02:00
if current_layer == 0:
keyboard.send(key_press)
elif current_layer == 1:
keyboard.send(key_press)
elif current_layer == 2:
debounce = long_debounce
layout.write(key_press)
elif current_layer == 3:
consumer_control.send(key_press)
2021-08-25 22:45:55 +02:00
# hebe "debounce" auf
2021-08-25 22:32:18 +02:00
if taste_ausgelöst and time.monotonic() - keybow.time_of_last_press > debounce:
taste_ausgelöst = False