Repository navigation
Expand file tree
/
Copy pathprotocol.py
More file actions
274 lines (224 loc) · 7.6 KB
/
Copy pathprotocol.py
File metadata and controls
274 lines (224 loc) · 7.6 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import os
import hid
FWK_VID = 0x32AC
QMK_INTERFACE = 0x01
RAW_HID_BUFFER_SIZE = 32
RAW_USAGE_PAGE = 0xFF60
CONSOLE_USAGE_PAGE = 0xFF31
# Generic Desktop
G_DESK_USAGE_PAGE = 0x01
CONSUMER_USAGE_PAGE = 0x0C
GET_PROTOCOL_VERSION = 0x01 # always 0x01
GET_KEYBOARD_VALUE = 0x02
SET_KEYBOARD_VALUE = 0x03
# DynamicKeymapGetKeycode = 0x04
# DynamicKeymapSetKeycode = 0x05
# DynamicKeymapReset = 0x06
CUSTOM_SET_VALUE = 0x07
CUSTOM_GET_VALUE = 0x08
CUSTOM_SAVE = 0x09
EEPROM_RESET = 0x0A
BOOTLOADER_JUMP = 0x0B
CHANNEL_CUSTOM = 0
CHANNEL_BACKLIGHT = 1
CHANNEL_RGB_LIGHT = 2
CHANNEL_RGB_MATRIX = 3
CHANNEL_AUDIO = 4
BACKLIGHT_VALUE_BRIGHTNESS = 1
BACKLIGHT_VALUE_EFFECT = 2
RGB_MATRIX_VALUE_BRIGHTNESS = 1
RGB_MATRIX_VALUE_EFFECT = 2
RGB_MATRIX_VALUE_EFFECT_SPEED = 3
RGB_MATRIX_VALUE_COLOR = 4
RED_HUE = 0
YELLOW_HUE = 43
GREEN_HUE = 85
CYAN_HUE = 125
BLUE_HUE = 170
PURPLE_HUE = 213
RGB_EFFECTS = [
"Off",
"SOLID_COLOR",
"ALPHAS_MODS",
"GRADIENT_UP_DOWN",
"GRADIENT_LEFT_RIGHT",
"BREATHING",
"BAND_SAT",
"BAND_VAL",
"BAND_PINWHEEL_SAT",
"BAND_PINWHEEL_VAL",
"BAND_SPIRAL_SAT",
"BAND_SPIRAL_VAL",
"CYCLE_ALL",
"CYCLE_LEFT_RIGHT",
"CYCLE_UP_DOWN",
"CYCLE_OUT_IN",
"CYCLE_OUT_IN_DUAL",
"RAINBOW_MOVING_CHEVRON",
"CYCLE_PINWHEEL",
"CYCLE_SPIRAL",
"DUAL_BEACON",
"RAINBOW_BEACON",
"RAINBOW_PINWHEELS",
"RAINDROPS",
"JELLYBEAN_RAINDROPS",
"HUE_BREATHING",
"HUE_PENDULUM",
"HUE_WAVE",
"PIXEL_FRACTAL",
"PIXEL_FLOW",
"PIXEL_RAIN",
"TYPING_HEATMAP",
"DIGITAL_RAIN",
"SOLID_REACTIVE_SIMPLE",
"SOLID_REACTIVE",
"SOLID_REACTIVE_WIDE",
"SOLID_REACTIVE_MULTIWIDE",
"SOLID_REACTIVE_CROSS",
"SOLID_REACTIVE_MULTICROSS",
"SOLID_REACTIVE_NEXUS",
"SOLID_REACTIVE_MULTINEXUS",
"SPLASH",
"MULTISPLASH",
"SOLID_SPLASH",
"SOLID_MULTISPLASH",
]
def format_fw_ver(fw_ver):
return f"{fw_ver >> 8}.{(fw_ver >> 4) & 0xF}.{fw_ver & 0xF}"
def find_devs(show, verbose):
if verbose:
show = True
devices = []
for device_dict in hid.enumerate():
vid = device_dict["vendor_id"]
pid = device_dict["product_id"]
product = device_dict["product_string"]
manufacturer = device_dict["manufacturer_string"]
sn = device_dict['serial_number']
interface = device_dict['interface_number']
path = device_dict['path']
if vid != FWK_VID:
if verbose:
print("Vendor ID not matching")
continue
usage_page = device_dict.get('usage_page', 0)
if usage_page != RAW_USAGE_PAGE and (usage_page != 0 or interface != QMK_INTERFACE):
if verbose:
print("Interface not matching")
continue
# For some reason on Linux it'll always show usage_page==0
if os.name == 'nt' and usage_page != RAW_USAGE_PAGE:
if verbose:
print("Usage Page not matching")
continue
# Lots of false positives, so at least skip Framework false positives
if vid == FWK_VID and pid not in [0x12, 0x13, 0x14, 0x18, 0x19, 0x30]:
if verbose:
print("False positive, device is not allowed")
continue
fw_ver = device_dict["release_number"]
if show:
print(f"Manufacturer: {manufacturer}")
print(f"Product: {product}")
print("FW Version: {}".format(format_fw_ver(fw_ver)))
print(f"Serial No: {sn}")
if verbose:
print(f"Path: {path}")
print(f"VID/PID: {vid:02X}:{pid:02X}")
print(f"Interface: {interface}")
# TODO: print Usage Page
print("")
devices.append(device_dict)
return devices
def send_message(dev, message_id, msg, out_len):
# hidapi requires a report ID byte in addition to the 32-byte report.
data = [0x00, message_id] + [0xFE] * (RAW_HID_BUFFER_SIZE - 1)
if msg:
if len(msg) > RAW_HID_BUFFER_SIZE - 1:
raise ValueError("Message exceeds the raw HID report size")
data[2:2 + len(msg)] = msg
if not 0 <= out_len <= RAW_HID_BUFFER_SIZE - 3:
raise ValueError("Invalid response length")
h = hid.device()
try:
h.open_path(dev['path'])
if h.write(data) != len(data):
raise OSError("Incomplete HID write")
# VIA echoes setters too. Wait for the acknowledgement before closing
# the handle, so the USB transfer completes before the next command.
if out_len == 0 and message_id in (BOOTLOADER_JUMP, EEPROM_RESET):
return None
output = h.read(RAW_HID_BUFFER_SIZE, 1000)
if not output:
raise TimeoutError("Timed out waiting for HID response")
if output[0] == 0xFF:
return None # VIA command unsupported by this device.
prefix = list(msg or [])[:2]
if len(output) < max(out_len + 3, 1 + len(prefix)):
raise OSError("Incomplete HID response")
if output[0] != message_id or output[1:1 + len(prefix)] != prefix:
raise OSError("Unexpected HID response")
return output if out_len else None
finally:
h.close()
def set_keyboard_value(dev, value, number):
msg = [value, number]
send_message(dev, SET_KEYBOARD_VALUE, msg, 0)
def set_rgb_u8(dev, value, value_data):
msg = [CHANNEL_RGB_MATRIX, value, value_data]
send_message(dev, CUSTOM_SET_VALUE, msg, 0)
# Returns brightness level: x/255
def get_rgb_u8(dev, value):
msg = [CHANNEL_RGB_MATRIX, value]
output = send_message(dev, CUSTOM_GET_VALUE, msg, 1)
return None if output is None else output[3]
# Returns (hue, saturation)
def get_rgb_color(dev):
msg = [CHANNEL_RGB_MATRIX, RGB_MATRIX_VALUE_COLOR]
output = send_message(dev, CUSTOM_GET_VALUE, msg, 2)
return None if output is None else (output[3], output[4])
# Returns brightness level: x/255
def get_backlight(dev, value):
msg = [CHANNEL_BACKLIGHT, value]
output = send_message(dev, CUSTOM_GET_VALUE, msg, 1)
return None if output is None else output[3]
def set_backlight(dev, value, value_data):
msg = [CHANNEL_BACKLIGHT, value, value_data]
send_message(dev, CUSTOM_SET_VALUE, msg, 0)
def save(dev):
save_rgb(dev)
save_backlight(dev)
def save_rgb(dev):
msg = [CHANNEL_RGB_MATRIX]
send_message(dev, CUSTOM_SAVE, msg, 0)
def save_backlight(dev):
msg = [CHANNEL_BACKLIGHT]
send_message(dev, CUSTOM_SAVE, msg, 0)
def eeprom_reset(dev):
send_message(dev, EEPROM_RESET, None, 0)
def bootloader_jump(dev):
send_message(dev, BOOTLOADER_JUMP, None, 0)
def bios_mode(dev, enable):
param = 0x01 if enable else 0x00
send_message(dev, BOOTLOADER_JUMP, [0x05, param], 0)
def factory_mode(dev, enable):
param = 0x01 if enable else 0x00
send_message(dev, BOOTLOADER_JUMP, [0x06, param], 0)
def set_rgb_brightness(dev, brightness):
set_rgb_u8(dev, RGB_MATRIX_VALUE_BRIGHTNESS, brightness)
def set_brightness(dev, brightness):
set_backlight(dev, BACKLIGHT_VALUE_BRIGHTNESS, brightness)
def set_white_effect(dev, breathing_on):
set_backlight(dev, BACKLIGHT_VALUE_EFFECT, breathing_on)
# Set both
def set_white_rgb_brightness(dev, brightness):
set_brightness(dev, brightness)
set_rgb_brightness(dev, brightness)
def set_rgb_color(dev, hue, saturation):
if hue is None:
color = get_rgb_color(dev)
if color is None:
return
hue = color[0]
msg = [CHANNEL_RGB_MATRIX, RGB_MATRIX_VALUE_COLOR, hue, saturation]
send_message(dev, CUSTOM_SET_VALUE, msg, 0)