-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuorafxctl.py
executable file
·267 lines (216 loc) · 6.6 KB
/
suorafxctl.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
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
#!/usr/bin/env python3
import os
import sys
import json
import argparse
import usb1
def get_xdg_config_home():
"""Returns the path of the folder where to store the configs (generally
``$HOME/.config``).
:rtype: str
"""
if "XDG_CONFIG_HOME" in os.environ and os.environ["XDG_CONFIG_HOME"]:
return os.environ["XDG_CONFIG_HOME"]
return os.path.join(os.path.expanduser("~"), ".config")
class SuoraFX(object):
VENDOR_ID = 0x1E7D
PRODUCT_ID = 0x3246
INTERFACE = 0x03
EFFECTS = {
"full-lit": [0x01, 0x00],
"breathing": [0x02, 0x00],
"color-shift": [0x08, 0x00],
"wave-right": [0x03, 0x01],
"wave-left": [0x03, 0x02],
"wave-up": [0x03, 0x03],
"wave-down": [0x03, 0x04],
"fade-out": [0x04, 0x00],
"fade-in": [0x07, 0x00],
"ripple": [0x06, 0x00],
"rain": [0x0A, 0x00],
"snake": [0x05, 0x00],
"spiral": [0x0B, 0x00],
"game-over": [0x09, 0x00],
"scanner": [0x0C, 0x00],
"radar": [0x0D, 0x00],
}
COLORS = {
"red": 0x01,
"green": 0x02,
"yellow": 0x03,
"blue": 0x04,
"aqua": 0x05,
"purple": 0x06,
"white": 0x07,
}
DEFAULT_CONFIG = {
"effect": "wave-right",
"speed": 3,
"brightness": 50,
"color": "aqua",
}
CONFIG_FILE_NAME = "suorafxctl.json"
def __init__(self):
self._context = usb1.USBContext()
self._handle = None
self._config = dict(self.DEFAULT_CONFIG)
def acquire(self):
"""Get the access to the device."""
if not self._handle:
self._handle = self._context.openByVendorIDAndProductID(
self.VENDOR_ID,
self.PRODUCT_ID,
skip_on_error=True,
)
self._handle.detachKernelDriver(self.INTERFACE)
self._handle.claimInterface(self.INTERFACE)
def release(self):
"""Release the access to the device."""
self._handle.releaseInterface(self.INTERFACE)
self._handle.attachKernelDriver(self.INTERFACE)
def read_config(self):
"""Read configuration from config file."""
config_path = os.path.join(
get_xdg_config_home(),
self.CONFIG_FILE_NAME,
)
if not os.path.isfile(config_path):
return
with open(config_path, "r") as file_:
self._config.update(json.load(file_))
def write_config(self):
"""Save config to a file."""
if not os.path.isdir(get_xdg_config_home()):
os.makedirs(get_xdg_config_home())
config_path = os.path.join(
get_xdg_config_home(),
self.CONFIG_FILE_NAME,
)
with open(config_path, "w") as file_:
json.dump(self._config, file_)
@property
def effect(self):
return self._config["effect"]
@effect.setter
def effect(self, value):
if value not in self.EFFECTS:
raise ValueError("Unsupported effect '%s'" % value)
self._config["effect"] = value
@property
def speed(self):
return self._config["speed"]
@speed.setter
def speed(self, value):
if not 0 <= value <= 10:
raise ValueError("Speed must be an integer between 0 and 10")
self._config["speed"] = int(value)
@property
def brightness(self):
return self._config["brightness"]
@brightness.setter
def brightness(self, value):
if not 0 <= value <= 50:
raise ValueError("Brightness must be an integer between 0 and 50")
self._config["brightness"] = int(value)
@property
def color(self):
return self._config["color"]
@color.setter
def color(self, value):
if value not in self.COLORS:
raise ValueError("Unsupported color '%s'" % value)
self._config["color"] = value
def commit(self):
"Send the settings to the keyboard."
self._device_write(
[
0x08,
0x02,
self.EFFECTS[self.effect][0],
self.speed,
self.brightness,
self.COLORS[self.color],
self.EFFECTS[self.effect][1],
]
)
def _calculate_checksum(self, data):
checksum = 0xFF
for value in data:
checksum -= value
return checksum
def _device_write(self, data):
self._handle.controlWrite(
0x21,
0x09,
0x0300,
0x03,
bytes(data + [self._calculate_checksum(data)]),
)
def build_cli():
cli = argparse.ArgumentParser(
epilog="The first call to this command will reset all unspecified "
"settings to their default value"
)
cli.add_argument(
"-e",
"--effect",
help="Illumination effect (%s)" % ", ".join(SuoraFX.EFFECTS.keys()),
choices=SuoraFX.EFFECTS.keys(),
metavar="EFFECT",
)
cli.add_argument(
"-s",
"--speed",
help="Illumination effect speed, from 0 (fast) to 10 (slow)",
metavar="0-10",
type=int,
choices=range(0, 11),
)
cli.add_argument(
"-b",
"--brightness",
help="keyboard brightness, from 0 (light off) to 50",
metavar="0-50",
type=int,
choices=range(0, 51),
)
cli.add_argument(
"-c",
"--color",
help="Illumination color (%s)" % ", ".join(SuoraFX.COLORS.keys()),
choices=SuoraFX.COLORS.keys(),
metavar="COLOR",
)
cli.add_argument(
"-r",
"--reset",
help="reset all settings to their default",
dest="reset",
action="store_true",
)
return cli
def main(args=sys.argv[1:]):
cli = build_cli()
settings = cli.parse_args(args)
suorafx = SuoraFX()
suorafx.read_config()
suorafx.acquire()
if settings.reset:
suorafx.effect = SuoraFX.DEFAULT_CONFIG["effect"]
suorafx.speed = SuoraFX.DEFAULT_CONFIG["speed"]
suorafx.brightness = SuoraFX.DEFAULT_CONFIG["brightness"]
suorafx.color = SuoraFX.DEFAULT_CONFIG["color"]
else:
if settings.effect is not None:
suorafx.effect = settings.effect
if settings.speed is not None:
suorafx.speed = settings.speed
if settings.brightness is not None:
suorafx.brightness = settings.brightness
if settings.color is not None:
suorafx.color = settings.color
suorafx.commit()
suorafx.write_config()
suorafx.release()
if __name__ == "__main__":
main()