-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
executable file
·137 lines (102 loc) · 3.24 KB
/
config.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
125
126
127
128
129
130
131
132
133
134
135
136
137
import serial
import sys
# Change this to the serial port of the MACRO9
SERIAL_PORT = '/dev/ttyACM0'
class Key:
def __init__(self, modifiers=0, keys=[]):
if len(keys) != 6:
print("Invalid keys configuration")
sys.exit(1)
self.modifiers = modifiers
self.keys = keys
class Keypad:
def __init__(self, keys):
if len(keys) != 9:
print("Invalid keypad configuration")
sys.exit(1)
config = []
for k in keys:
config.append(k.modifiers)
config += k.keys
self.structs = config
# Change the keys to the HID keycodes you want
KEY1 = Key(modifiers=0, keys=[4, 0, 0, 0, 0, 0])
KEY2 = Key(modifiers=0, keys=[5, 0, 0, 0, 0, 0])
KEY3 = Key(modifiers=0, keys=[6, 0, 0, 0, 0, 0])
KEY4 = Key(modifiers=0, keys=[7, 0, 0, 0, 0, 0])
KEY5 = Key(modifiers=0, keys=[8, 0, 0, 0, 0, 0])
KEY6 = Key(modifiers=0, keys=[9, 0, 0, 0, 0, 0])
KEY7 = Key(modifiers=0, keys=[10, 0, 0, 0, 0, 0])
KEY8 = Key(modifiers=0, keys=[4, 0, 0, 0, 0, 0])
KEY9 = Key(modifiers=0, keys=[12, 0, 0, 0, 0, 0])
KEYPAD = Keypad([KEY1, KEY2, KEY3, KEY4, KEY5, KEY6, KEY7, KEY8, KEY9])
def main():
if len(sys.argv) != 2:
print(f"Usage: python3 {sys.argv[0]} [COMMAND]\n")
print("COMMAND: GET|SET")
print("For the SET command, edit the KEY1-9 in file")
sys.exit(1)
if sys.argv[1] == "GET":
print("Getting the MACRO9 configuration")
get_command()
elif sys.argv[1] == "SET":
print("Setting the MACRO9 configuration")
set_command()
else:
print("Invalid command")
def set_command():
ser = serial.Serial(SERIAL_PORT)
print("Setting")
request = [0x4D, 0x39, 0x53] + KEYPAD.structs + [0x00]
request[66] = calculate_crc(request[0:66])
request = bytes(request)
ser.write(request)
response = ser.read(4)
crc = calculate_crc(response[:3])
if crc != response[3]:
print("Invalid CRC")
return
if response[:2] != b'M9':
print("Invalid packet received")
return
if response[2] == 0x4B:
print("Set successful")
elif response[2] == 0x45:
print("Set failed")
def get_command():
ser = serial.Serial(SERIAL_PORT)
request = bytes([0x4D, 0x39, 0x47, 0x81])
ser.write(request)
response = ser.read(67)
crc = calculate_crc(response[:66])
if crc != response[66]:
print("Invalid CRC")
return
if response[:2] != b'M9' or response[2] != 0x52:
print("Invalid packet received")
return
button_structs = response[3:66]
for i in range(0, 9):
index = i * 7
struct = button_structs[index:(index + 7)]
print(f"Key {i+1}")
print_buttons_config(struct)
def print_buttons_config(struct):
print(f"Modifiers: {hex(struct[0])}")
print("Buttons: ", end='')
for button in struct[1:]:
print(f"{hex(button)} ", end='')
print()
def calculate_crc(data):
crc = 0
for value in data:
crc ^= value
for i in range(8):
if crc & (1 << (8 - 1)):
crc = (crc << 1) ^ 0x07
else:
crc = crc << 1
crc &= (1 << 8) - 1
return crc ^ 0
if __name__ == '__main__':
main()