import board
import digitalio
import time
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
from adafruit_debouncer import Debouncer
import adafruit_displayio_ssd1306
import adafruit_display_shapes.rect as rect
import adafruit_display_shapes.line as line
import busio
import rotaryio
keyboard = Keyboard()
keyboard_layout = KeyboardLayoutUS(keyboard)
consumer_control = ConsumerControl()
switch_pins = [board.GP0, board.GP1, board.GP2, board.GP3, board.GP4, board.GP5, board.GP6, board.GP7, board.GP8]
encoder1_clk = board.GP9
encoder1_dt = board.GP10
encoder1_sw = board.GP11
encoder2_clk = board.GP12
encoder2_dt = board.GP13
encoder2_sw = board.GP14
encoder3_clk = board.GP18
encoder3_dt = board.GP19
encoder3_sw = board.GP20
encoder1_sw_debouncer = Debouncer(digitalio.DigitalInOut(encoder1_sw))
encoder2_sw_debouncer = Debouncer(digitalio.DigitalInOut(encoder2_sw))
encoder3_sw_debouncer = Debouncer(digitalio.DigitalInOut(encoder3_sw))
i2c = busio.I2C(board.GP16, board.GP17)
oled = adafruit_displayio_ssd1306.SSD1306_I2C(128, 32, i2c)
display = displayio.Group(max_size=10)
layer_name = "Default"
text_layer = displayio.Group()
text = adafruit_displayio_ssd1306.Label(adafruit_displayio_ssd1306.paddle_font, text=layer_name, color=0xFFFFFF, x=0, y=0)
text_layer.append(text)
display.append(text_layer)
volume_bar = displayio.Group()
progress_rect = rect.Rect(105, 5, 20, 8, fill=0xFFFFFF)
volume_bar.append(progress_rect)
display.append(volume_bar)
grid_group = displayio.Group()
for x in range(4, 128, 42):
line_obj = line.Line(x1=x, y1=12, x2=x, y2=32, stroke=0xFFFFFF, stroke_width=1)
grid_group.append(line_obj)
for y in range(16, 32, 8):
line_obj = line.Line(x1=0, y1=y, x2=128, y2=y, stroke=0xFFFFFF, stroke_width=1)
grid_group.append(line_obj)
display.append(grid_group)
encoder1 = rotaryio.IncrementalEncoder(encoder1_clk, encoder1_dt)
encoder2 = rotaryio.IncrementalEncoder(encoder2_clk, encoder2_dt)
encoder3 = rotaryio.IncrementalEncoder(encoder3_clk, encoder3_dt)
default_layer_shortcuts = [
[Keycode.CONTROL, Keycode.KEY_A],
[Keycode.CONTROL, Keycode.KEY_B],
[Keycode.CONTROL, Keycode.KEY_C],
[Keycode.CONTROL, Keycode.KEY_D],
[Keycode.CONTROL, Keycode.KEY_E],
[Keycode.CONTROL, Keycode.KEY_F],
[Keycode.CONTROL, Keycode.KEY_G],
[Keycode.CONTROL, Keycode.KEY_H],
[Keycode.CONTROL, Keycode.KEY_I]
]
adobe_layer_shortcuts = [
[Keycode.LEFT_SHIFT, Keycode.KEY_A],
[Keycode.LEFT_SHIFT, Keycode.KEY_B],
[Keycode.LEFT_SHIFT, Keycode.KEY_C],
[Keycode.LEFT_SHIFT, Keycode.KEY_D],
[Keycode.LEFT_SHIFT, Keycode.KEY_E],
[Keycode.LEFT_SHIFT, Keycode.KEY_F],
[Keycode.LEFT_SHIFT, Keycode.KEY_G],
[Keycode.LEFT_SHIFT, Keycode.KEY_H],
[Keycode.LEFT_SHIFT, Keycode.KEY_I]
]
vscode_layer_shortcuts = [
[Keycode.ALT, Keycode.KEY_A],
[Keycode.ALT, Keycode.KEY_B],
[Keycode.ALT, Keycode.KEY_C],
[Keycode.ALT, Keycode.KEY_D],
[Keycode.ALT, Keycode.KEY_E],
[Keycode.ALT, Keycode.KEY_F],
[Keycode.ALT, Keycode.KEY_G],
[Keycode.ALT, Keycode.KEY_H],
[Keycode.ALT, Keycode.KEY_I]
]
current_layer = 0
def update_display():
global current_layer
text.text = "Layer: " + ["Default", "Adobe", "VsCode"][current_layer]
progress_rect.width = int(encoder2.position / 2) + 10.
def update_grid_display():
for i in range(len(grid_group)):
grid_group.pop()
shortcuts = [default_layer_shortcuts, adobe_layer_shortcuts, vscode_layer_shortcuts][current_layer]
for i in range(9):
row = i // 3
col = i % 3
key_label = adafruit_displayio_ssd1306.Label(adafruit_displayio_ssd1306.paddle_font, text=chr(shortcuts[i][1]), color=0xFFFFFF, x=col * 42, y=row * 8 + 16)
grid_group.append(key_label)
def update_encoder3_functionality():
global encoder3_function
if current_layer == 0:
encoder3_function = "brightness"
elif current_layer == 1:
encoder3_function = "zoom"
elif current_layer == 2:
encoder3_function = "scroll"
while True:
encoder1_sw_debouncer.update()
if encoder1_sw_debouncer.fell:
current_layer = (current_layer + 1) % 3
update_display()
encoder2_sw_debouncer.update()
if encoder2_sw_debouncer.fell:
consumer_control.send(ConsumerControlCode.PLAY_PAUSE)
encoder3_sw_debouncer.update()
if encoder3_sw_debouncer.fell:
if encoder3_function == "brightness":
pass
elif encoder3_function == "zoom":
pass
elif encoder3_function == "scroll":
pass
update_display()
for i, pin in enumerate(switch_pins):
switch = digitalio.DigitalInOut(pin)
switch.switch_to_input(pull=digitalio.Pull.DOWN)
if switch.value:
shortcut = [default_layer_shortcuts, adobe_layer_shortcuts, vscode_layer_shortcuts][current_layer][i]
for key in shortcut:
keyboard.press(key)
time.sleep(0.1)
for key in shortcut:
keyboard.release(key)
update_grid_display()
encoder1_position = encoder1.position
encoder2_position = encoder2.position
encoder3_position = encoder3.position
if encoder1_position > 0:
current_layer = (current_layer + 1) % 3
encoder1_position = 0
update_display()
elif encoder1_position < 0:
current_layer = (current_layer - 1) % 3
encoder1_position = 0
update_display()
if encoder2_position > 0:
consumer_control.send(ConsumerControlCode.VOLUME_INCREMENT)
encoder2_position = 0
elif encoder2_position < 0:
consumer_control.send(ConsumerControlCode.VOLUME_DECREMENT)
encoder2_position = 0
if encoder3_position > 0:
if encoder3_function == "brightness":
pass
elif encoder3_function == "zoom":
pass
elif encoder3_function == "scroll":
pass
encoder3_position = 0
time.sleep(0.01)