-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
207 lines (168 loc) · 7.62 KB
/
main.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
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
"""This file contain class designed to communicate with mouse."""
import usb.core
import usb.util
class M601:
"""Represents a M601-RGB mouse."""
def __init__(self):
"""Establish connection with a mouse."""
self.VID = 0x258a
self.PID = 0x1007
self.dev = usb.core.find(idVendor=self.VID, idProduct=self.PID)
if self.dev is None:
raise ValueError('Device not found')
# detach if busy:
if self.dev.is_kernel_driver_active(1):
try:
self.dev.detach_kernel_driver(1)
except usb.core.USBError as e:
raise SystemError("Could not detatch kernel driver from interface({0}): {1}".format(1, str(e)))
def read_settings(self):
"""Read current settings from mouse."""
self.set_report(0x0305, [5, 1, 0, 0, 0, 0])
self.get_report(0x0305, 6)
self.set_report(0x0305, [5, 2, 0, 0, 0, 0])
self.get_report(0x0305, 6)
self.set_report(0x0305, [5, 0x11, 0, 0, 0, 0])
self.settings_1 = self.get_report(0x0304, 520)
self.set_report(0x0305, [5, 0x12, 0, 0, 0, 0])
self.buttons_1 = self.get_report(0x0304, 520)
self.set_report(0x0305, [5, 0x21, 0, 0, 0, 0])
self.settings_2 = self.get_report(0x0304, 520)
self.set_report(0x0305, [5, 0x22, 0, 0, 0, 0])
self.buttons_2 = self.get_report(0x0304, 520)
def write_settings(self, byte):
"""Write settings to the mouse.
byte = 0x11 - Mode 1
byte = 0x21 - Mode 2
"""
self.set_report(0x0305, [5, byte, 0, 0, 0, 0])
self.get_report(0x0304, 520)
self.check_len(self.settings_package, 520)
self.check_len(self.button_package, 520)
self.set_report(0x0304, self.button_package)
self.set_report(0x0304, self.settings_package)
def parse_settings(self, settings):
"""Parse specific values from settings package."""
settings = list(settings)
self.raw_header = settings[0:10]
self.raw_polling_rate = settings[10]
self.raw_active_dpi_presets = settings[11]
self.raw_disabled_dpi_presets = settings[12]
self.raw_dpi_values = settings[13:18]
self.raw_dpi_colors = settings[29:44]
self.raw_current_lighting_effect = settings[53]
self.raw_colorful_streaming_speed = settings[54]
self.raw_colorful_streaming_direction = settings[55]
self.raw_steady_brightness = settings[56]
self.raw_steady_color = settings[57:60]
self.raw_breathing_speed = settings[60]
self.raw_breathing_number_of_colors = settings[61]
self.raw_breathing_colors = settings[62:83]
self.raw_tail_speed = settings[83]
self.raw_neon_speed = settings[84]
self.raw_colorful_steady_colors = settings[86:101]
self.raw_flicker_colors = settings[111:117]
self.raw_streaming_speed = settings[117]
self.raw_wave_speed = settings[118]
def parse_buttons(self, buttons):
"""Parse specific values from buttons package."""
self.buttons_header = buttons[0:8]
self.button_1 = buttons[8:12]
self.button_2 = buttons[12:16]
self.button_3 = buttons[16:20]
self.button_4 = buttons[24:28]
self.button_5 = buttons[20:24]
self.button_6 = buttons[32:36]
self.button_7 = buttons[40:44]
def make_package(self):
"""Make settings and buttons package."""
self.settings_package = [*self.raw_header,
self.raw_polling_rate,
self.raw_active_dpi_presets,
self.raw_disabled_dpi_presets,
*self.raw_dpi_values,
*[0] * 11, *self.raw_dpi_colors,
*[0] * 9, self.raw_current_lighting_effect,
self.raw_colorful_streaming_speed,
self.raw_colorful_streaming_direction,
self.raw_steady_brightness,
*self.raw_steady_color,
self.raw_breathing_speed,
self.raw_breathing_number_of_colors,
*self.raw_breathing_colors,
self.raw_tail_speed,
self.raw_neon_speed, 0x30,
*self.raw_colorful_steady_colors,
*[0] * 9, 2, *self.raw_flicker_colors,
self.raw_streaming_speed,
self.raw_wave_speed, *[0] * 401]
disabled_button = [0x50, 0x01, 0, 0]
self.button_package = [*self.buttons_header,
*self.button_1,
*self.button_2,
*self.button_3,
*self.button_5,
*self.button_4,
0x50, 0x06, 0, 0,
*self.button_6,
*disabled_button,
*self.button_7,
*disabled_button,
*disabled_button,
*disabled_button,
*disabled_button,
*disabled_button,
*disabled_button,
*disabled_button,
*disabled_button,
*disabled_button,
*disabled_button,
*disabled_button,
*[0] * 432]
def set_report(self, wvalue, payload):
"""Send HID report."""
self.dev.ctrl_transfer(
0x21, # bmRequestType
0x09, # bRequest
wvalue,
1, # wIndex: 1
payload # the HID payload as a byte array
)
def get_report(self, wvalue, wlength):
"""Get HID report."""
return self.dev.ctrl_transfer(
0xa1, # bmRequestType
0x01, # bRequest
wvalue,
1, # wIndex: 1
wlength # the length of expected package to read
)
def check_len(self, package, length):
"""Check length of package."""
if (len(package) != length):
raise ValueError("Length of package is incorrect.")
def change_mode(self, mode):
"""Change mouse mode."""
if mode not in [1, 2]:
raise ValueError('Wrong mode')
self.set_report(0x0305, [5, 2, mode, 0, 0, 0])
def hard_reset(self):
"""Reset mouse values by sending 4 hard coded packages."""
from values import hard_reset_package_1, hard_reset_package_2, hard_reset_package_3, hard_reset_package_4
self.set_report(0x0305, [5, 0x11, 0, 0, 0, 0])
self.get_report(0x0304, 520)
self.set_report(0x0304, hard_reset_package_2)
self.set_report(0x0304, hard_reset_package_1)
self.set_report(0x0305, [5, 0x21, 0, 0, 0, 0])
self.get_report(0x0304, 520)
self.set_report(0x0304, hard_reset_package_4)
self.set_report(0x0304, hard_reset_package_3)
self.change_mode(1)
def write_macros(self, macros_package):
"""Write macros to the mouse"""
self.check_len(macros_package, 520)
self.set_report(0x0305, [5, 0x11, 0, 0, 0, 0])
self.get_report(0x0304, 520)
self.set_report(0x0304, macros_package)
if __name__ == '__main__':
pass