rotary-kb/rotary-kb.py

53 lines
916 B
Python
Raw Permalink Normal View History

2022-05-16 16:44:44 +02:00
#!/bin/env python3
import serial
import time
2022-05-16 17:29:09 +02:00
import os
2022-05-16 16:44:44 +02:00
from pynput import mouse, keyboard
PORT = "/dev/ttyACM1"
2022-05-16 17:29:09 +02:00
keys = list(" abcdefghijklmnopqrstuvwxyz") + [keyboard.Key.enter, keyboard.Key.backspace]
keys_display = "_abcdefghijklmnopqrstuvwxyz⏎<"
2022-05-16 16:44:44 +02:00
kb = keyboard.Controller()
arduino = serial.Serial(port=PORT, baudrate=9600, timeout=.1)
active_btn = -1
pos = 0
2022-05-16 17:29:09 +02:00
if os.name == "nt":
def clear():
os.system("cls")
else:
def clear():
os.system("clear")
2022-05-16 16:44:44 +02:00
def print_state():
global pos
2022-05-16 17:29:09 +02:00
clear()
2022-05-16 16:44:44 +02:00
print(keys_display)
print(" " * pos + "^")
2022-05-16 17:01:20 +02:00
print(pos)
2022-05-16 16:44:44 +02:00
2022-05-16 17:29:09 +02:00
print_state()
2022-05-16 16:44:44 +02:00
while True:
event = arduino.read().decode("utf-8")
if event == "r":
2022-05-16 17:01:20 +02:00
pos = (pos + 1) % len(keys)
elif event == "l":
2022-05-16 17:01:20 +02:00
pos = (pos - 1 + len(keys)) % len(keys)
elif event == "d":
2022-05-16 17:01:20 +02:00
kb.press(keys[pos])
active_btn = pos
elif event == "u":
2022-05-16 17:01:20 +02:00
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