rotary-kb/rotary-kb.py

41 lines
791 B
Python
Raw Normal View History

2022-05-16 16:44:44 +02:00
#!/bin/env python3
import serial
import time
from pynput import mouse, keyboard
PORT = "/dev/ttyACM1"
keys = list(" abcdefghijklmnopqrstuvwxyz") + [keyboard.Key.enter]
keys_display = " abcdefghijklmnopqrstuvwxyz\\"
kb = keyboard.Controller()
arduino = serial.Serial(port=PORT, baudrate=9600, timeout=.1)
active_btn = -1
pos = 0
def print_state():
global pos
print(keys_display)
print(" " * pos + "^")
2022-05-16 17:01:20 +02:00
print(pos)
2022-05-16 16:44:44 +02:00
while True:
event = arduino.readline().decode("utf-8").replace("\r\n", "")
2022-05-16 17:01:20 +02:00
if event == "cw":
pos = (pos + 1) % len(keys)
elif event == "ccw":
pos = (pos - 1 + len(keys)) % len(keys)
elif event == "down":
kb.press(keys[pos])
active_btn = pos
elif event == "up":
kb.release(keys[active_btn])
active_btn = -1
2022-05-16 16:44:44 +02:00
if event:
2022-05-16 17:01:20 +02:00
print_state()
print(event)
2022-05-16 16:44:44 +02:00