-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.py
More file actions
executable file
·81 lines (67 loc) · 2.02 KB
/
debug.py
File metadata and controls
executable file
·81 lines (67 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python3
import string
import re
import serial
from dataclasses import dataclass
# Define Key class
@dataclass
class Key:
a: bool = False
b: bool = False
# Initialize keys
keys: list[Key] = [Key() for _ in range(37)]
# Function to convert int to Base64 character
def int_to_base64_char(n: int):
base64_chars: str = (
string.ascii_uppercase + string.ascii_lowercase + string.digits + "+/"
)
if 0 <= n < len(base64_chars):
return base64_chars[n]
else:
raise ValueError("Number must be between 0 and 63")
# Create header
header = "".join([int_to_base64_char(i) for i in range(37)])
# Function to print state
def flush_state():
print("\033[H\033[J", end="") # Clear screen
print(header)
print("".join(["A" if key.a else " " for key in keys]))
print("".join(["B" if key.b else " " for key in keys]))
# Open the serial port
try:
ser = serial.Serial("/dev/ttyACM0", 9600, timeout=1) # Adjust baud rate as needed
except serial.SerialException as e:
print(f"Error opening serial port: {e}")
exit(1)
# Read from serial
while True:
try:
line = ser.readline().decode("utf-8").strip() # Read line and decode
match = re.match(r"(kbd_\w+): (\d+)", line)
if match:
verb, index_str = match.groups()
index = int(index_str)
if index < 0 or index > 36:
print(f"out of range index: {index}")
pass
key = keys[index]
match verb:
case "kbd_start_on":
key.a = True
case "kbd_end_on":
key.b = True
case "kbd_start_off":
key.a = False
case "kbd_end_off":
key.b = False
case _:
pass
flush_state()
except KeyboardInterrupt:
print("Exiting...")
ser.close()
break
except Exception as e:
print(f"Error: {e}")
ser.close()
break