Compare commits

...

4 Commits

Author SHA1 Message Date
ABelliqueux 027c075be5 Add ALT keys for rot enc 2025-03-01 19:34:42 +01:00
ABelliqueux 3cadd95e65 Use rot enc bush button for Q and S 2025-02-24 14:40:31 +01:00
ABelliqueux 88fb7d18db Add ALT key for rotary encoder 2025-01-12 10:30:14 +01:00
ABelliqueux 42b97aeea7 Add rotary encoder support 2025-01-11 14:49:15 +01:00
1 changed files with 43 additions and 17 deletions

58
code.py
View File

@ -19,6 +19,7 @@
import time import time
import board import board
import digitalio import digitalio
import rotaryio
import usb_hid import usb_hid
# Regular keys # Regular keys
@ -26,6 +27,13 @@ from adafruit_hid.keyboard import Keyboard
from keyboard_layout_win_fr import KeyboardLayout from keyboard_layout_win_fr import KeyboardLayout
from keycode_win_fr import Keycode from keycode_win_fr import Keycode
# Rotary encoder setup
enc = rotaryio.IncrementalEncoder(board.GP0, board.GP1)
last_pos = 0
# Keys sent on rotary encoder rotation
rot_right = Keycode.A
rot_left = Keycode.Z
# When in ALT mode, buttons use an alternative keycode. # When in ALT mode, buttons use an alternative keycode.
ALT = False ALT = False
@ -35,33 +43,39 @@ keyboard_layout = KeyboardLayout(keyboard)
# Buttons setup # Buttons setup
buttons_gpio = { buttons_gpio = {
# name GPIO Keycode_1 Keycode_2(ALT mode) State DigitalIO (switch) # name GPIO Keycode_1 Keycode_2(ALT mode) State DigitalIO Pull (Switch)
"red" : dict(gpio=board.GP18, keycode_1=Keycode.DELETE, keycode_2=Keycode.E, state=False, DIO=None), "red" : dict(gpio=board.GP18, keycode_1=Keycode.DELETE, keycode_2=Keycode.E, state=False, DIO=None, pullr=digitalio.Pull.DOWN),
"green" : dict(gpio=board.GP19, keycode_1=Keycode.SPACEBAR, keycode_2=Keycode.RETURN, state=False, DIO=None), "green" : dict(gpio=board.GP19, keycode_1=Keycode.SPACEBAR, keycode_2=Keycode.RETURN, state=False, DIO=None, pullr=digitalio.Pull.DOWN),
"blue" : dict(gpio=board.GP16, keycode_1=Keycode.RIGHT_ARROW, keycode_2=Keycode.UP_ARROW, state=False, DIO=None), "blue" : dict(gpio=board.GP16, keycode_1=Keycode.RIGHT_ARROW, keycode_2=Keycode.UP_ARROW, state=False, DIO=None, pullr=digitalio.Pull.DOWN),
"black" : dict(gpio=board.GP20, keycode_1=Keycode.O, keycode_2=Keycode.R, state=False, DIO=None), "black" : dict(gpio=board.GP20, keycode_1=Keycode.O, keycode_2=Keycode.R, state=False, DIO=None, pullr=digitalio.Pull.DOWN),
"yellow": dict(gpio=board.GP17, keycode_1=Keycode.LEFT_ARROW, keycode_2=Keycode.DOWN_ARROW,state=False, DIO=None), "yellow": dict(gpio=board.GP17, keycode_1=Keycode.LEFT_ARROW, keycode_2=Keycode.DOWN_ARROW,state=False, DIO=None, pullr=digitalio.Pull.DOWN),
"switch": dict(gpio=board.GP21, keycode_1=None, keycode_2=None, state=False, DIO=None, switch_setup=dict(gpio=board.GP7, state=False, DIO=None)), "switch": dict(gpio=board.GP21, keycode_1=None, keycode_2=None, state=False, DIO=None, pullr=digitalio.Pull.DOWN, switch_setup=dict(gpio=board.GP7, state=False, DIO=None)),
"fn_1": dict(gpio=board.GP15, keycode_1=Keycode.W, keycode_2=Keycode.X, state=False, DIO=None), "fn_1": dict(gpio=board.GP15, keycode_1=Keycode.W, keycode_2=Keycode.X, state=False, DIO=None, pullr=digitalio.Pull.DOWN),
"fn_2": dict(gpio=board.GP14, keycode_1=Keycode.L, keycode_2=Keycode.F, state=False, DIO=None), "fn_2": dict(gpio=board.GP14, keycode_1=Keycode.L, keycode_2=Keycode.F, state=False, DIO=None, pullr=digitalio.Pull.DOWN),
# This one is reversed (True when not pressed) so we need some logic to interpret a HIGH as False
"rot": dict(gpio=board.GP6, keycode_1=Keycode.Q, keycode_2=Keycode.S, state=False, DIO=None, pullr=digitalio.Pull.UP, reverse=True),
} }
# GPIO setup # GPIO setup
for btn in buttons_gpio: for btn in buttons_gpio:
buttons_gpio[btn]['DIO'] = digitalio.DigitalInOut(buttons_gpio[btn]['gpio']) buttons_gpio[btn]['DIO'] = digitalio.DigitalInOut(buttons_gpio[btn]['gpio'])
buttons_gpio[btn]['DIO'].direction = digitalio.Direction.INPUT buttons_gpio[btn]['DIO'].direction = digitalio.Direction.INPUT
buttons_gpio[btn]['DIO'].pull = digitalio.Pull.DOWN buttons_gpio[btn]['DIO'].pull = buttons_gpio[btn]['pullr']
# Switch setup # Switch setup
if 'switch_setup' in buttons_gpio[btn]: if 'switch_setup' in buttons_gpio[btn]:
buttons_gpio[btn]['switch_setup']['DIO'] = digitalio.DigitalInOut(buttons_gpio[btn]['switch_setup']['gpio']) buttons_gpio[btn]['switch_setup']['DIO'] = digitalio.DigitalInOut(buttons_gpio[btn]['switch_setup']['gpio'])
buttons_gpio[btn]['switch_setup']['DIO'].direction = digitalio.Direction.OUTPUT buttons_gpio[btn]['switch_setup']['DIO'].direction = digitalio.Direction.OUTPUT
btn_scan_delay = 1/200 btn_scan_delay = 1.0/200
while True: while True:
for btn in buttons_gpio: for btn in buttons_gpio:
# If digital input is high and was not pressed previously # Logic for switches that read True when not pressed
if buttons_gpio[btn]['DIO'].value and not buttons_gpio[btn]['state']: if 'reverse' in buttons_gpio[btn]:
btn_value = not buttons_gpio[btn]['DIO'].value
else:
btn_value = buttons_gpio[btn]['DIO'].value
# If digital input is true and was not pressed previously
if btn_value and not buttons_gpio[btn]['state']:
if ALT: if ALT:
if buttons_gpio[btn]['keycode_2'] is not None: if buttons_gpio[btn]['keycode_2'] is not None:
keyboard.press(buttons_gpio[btn]['keycode_2']) keyboard.press(buttons_gpio[btn]['keycode_2'])
@ -74,8 +88,8 @@ while True:
if not buttons_gpio[btn]['switch_setup']['DIO'].value: if not buttons_gpio[btn]['switch_setup']['DIO'].value:
buttons_gpio[btn]['switch_setup']['DIO'].value = True buttons_gpio[btn]['switch_setup']['DIO'].value = True
ALT = True ALT = True
# If digital input is down and was pressed previously # If digital input is false and was pressed previously
if not buttons_gpio[btn]['DIO'].value and buttons_gpio[btn]['state']: if not btn_value and buttons_gpio[btn]['state']:
if ALT: if ALT:
if buttons_gpio[btn]['keycode_2'] is not None: if buttons_gpio[btn]['keycode_2'] is not None:
keyboard.release(buttons_gpio[btn]['keycode_2']) keyboard.release(buttons_gpio[btn]['keycode_2'])
@ -88,4 +102,16 @@ while True:
if buttons_gpio[btn]['switch_setup']['DIO'].value: if buttons_gpio[btn]['switch_setup']['DIO'].value:
buttons_gpio[btn]['switch_setup']['DIO'].value = False buttons_gpio[btn]['switch_setup']['DIO'].value = False
ALT = False ALT = False
pos = enc.position
if pos > last_pos:
if ALT:
keyboard.send(Keycode.LEFT_SHIFT, rot_right)
else:
keyboard.send(rot_right)
elif pos < last_pos:
if ALT:
keyboard.send(Keycode.LEFT_SHIFT, rot_left)
else:
keyboard.send(rot_left)
last_pos = pos
time.sleep(btn_scan_delay) time.sleep(btn_scan_delay)