-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathk9000_fbm_toolkit.py
124 lines (111 loc) · 3.81 KB
/
k9000_fbm_toolkit.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import argparse
import sys
from pyfis.krone import Krone9000FBM
from pyfis.krone.util import calibrate_fbm_interactive
def read_value_table(filename):
"""
Read the value table in KRONE .DAT format
and return it as a dict
"""
with open(filename, 'r', encoding='latin-1') as f:
lines = [line.strip() for line in f.readlines()]
table = {}
for line in lines:
if not line:
continue
try:
parts = line.split(";")
remainder = ";".join(parts[1:])
pos = int(parts[0])
if remainder.startswith("0x"):
value = int(remainder.split(";")[0], 16)
else:
value = ord(remainder[0])
table[pos] = value
except:
continue
return table
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--port", type=str, required=True)
parser.add_argument("-t", "--table", type=str, required=False)
parser.add_argument("-d", "--debug", action='store_true')
args = parser.parse_args()
fbm = Krone9000FBM(args.port, exclusive=True, debug=args.debug)
if args.table:
table = read_value_table(args.table)
else:
table = None
while True:
data = input("What to do? [Code/Status/Letter/Text/Home/WrtTbl/DelTbl/caliBrate/Exit] ").upper()
fbm.port.read(fbm.port.inWaiting()) # Flush buffer
if not data:
continue
action = data[0]
if action not in ("H", "E"):
if len(data) < 2:
print(" No address specified!")
continue
if action == "L":
addr = int(data[1:-1])
else:
addr = int(data[1:])
if action == "C":
code = fbm.read_code(addr)
sys.stdout.write(" FBM Code: ")
if len(code) < 1:
print("No data received!")
continue
code = code[0]
if code == 0x10:
print("Undefined position")
elif code == 0x20:
print("Home position")
elif code < 0x20:
print(f"Unknown status code 0x{code:02X}")
else:
print(chr(code))
elif action == "S":
try:
fbm_status = fbm.get_status(addr) or ["OK"]
print(" FBM Status:", ", ".join(fbm_status))
except:
print(" FBM Status: No data received!")
continue
elif action == "L":
if len(data) < 3:
print(" No letter specified!")
continue
letter = data[-1]
fbm.set_code(addr, ord(letter[0]))
fbm.set_all()
elif action == "T":
text = input(" Enter text: ").upper()
fbm.set_text(text, addr)
fbm.set_all()
elif action == "H":
print(" Rotating to home position")
fbm.set_home()
elif action == "W":
if not table:
print(" No value table loaded!")
continue
print(" Writing value table")
for pos, value in table.items():
print(f" Writing flap {pos}: {chr(value)}")
resp = fbm.set_table(addr, value, pos)[0]
elif action == "D":
if not table:
print(" No value table loaded!")
continue
print(" Deleting value table")
resp = fbm.delete_table(addr)[0]
elif action == "B":
if addr != 0:
print(" Calibration is only possible for address 0!")
continue
calibrate_fbm_interactive(fbm, addr)
elif action == "E":
break
if __name__ == "__main__":
main()