Skip to content

Instantly share code, notes, and snippets.

@hsgw
Last active July 7, 2022 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsgw/db5753c29c71fa35bb79a933cf316e77 to your computer and use it in GitHub Desktop.
Save hsgw/db5753c29c71fa35bb79a933cf316e77 to your computer and use it in GitHub Desktop.
code.py for ultimate unicode input device
import time
import board
import digitalio
from adafruit_debouncer import Debouncer
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
TOGGLE_PINS = [
board.GP0,
board.GP1,
board.GP2,
board.GP3,
board.GP4,
board.GP5,
board.GP6,
board.GP7,
board.GP8,
board.GP9,
board.GP10,
board.GP11,
board.GP12,
board.GP13,
board.GP14,
board.GP15,
board.GP16,
board.GP17,
]
ENTER_PIN = board.GP18
def initializeDebouncer(pin_name):
pin = digitalio.DigitalInOut(pin_name)
pin.direction = digitalio.Direction.INPUT
pin.pull = digitalio.Pull.UP
return Debouncer(pin)
def send_linux(value):
keyboard.press(Keycode.LEFT_CONTROL, Keycode.LEFT_SHIFT, Keycode.U)
time.sleep(0.05)
keyboard.release_all()
keyboard_layout.write('{:05x} '.format(value))
def send_win(value):
keyboard.press(Keycode.ALT)
time.sleep(0.05)
keyboard.release_all()
keyboard_layout.write('u{:05x} '.format(value))
toggles = list(map(initializeDebouncer, TOGGLE_PINS))
enter = initializeDebouncer(ENTER_PIN)
time.sleep(1)
keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutUS(keyboard)
prev_fell_time = 0.0;
sended = False;
while True:
toggle_value = 0
for sw in toggles:
sw.update()
toggle_value = toggle_value << 1
toggle_value += sw.value == False if 1 else 0
enter.update()
if enter.fell:
prev_fell_time = time.monotonic();
continue
elif enter.rose:
if sended:
sended = False
continue
elif enter.value == False and sended == False and time.monotonic() - prev_fell_time > 0.5:
# long press
toggle_value = 0xE0000 + (toggle_value & 0xFFFF)
sended = True
else:
continue
# debug
# print('0b{:#018b}'.format(toggle_value))
# print('0x{:05x}'.format(toggle_value))
send_win(toggle_value)
# send_linux(toggle_value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment