-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgbcontrol.py
418 lines (366 loc) · 14.5 KB
/
rgbcontrol.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# Program to control RGB on J-Tech style vertical mouse
# Author: AD-Wright
# Date: 2019-11-16+
# GPL v3.0
# dependencies: pyusb 1.0 (sudo apt-get install python3-usb)
# To enable non-sudo running of this file, add a file to the /etc/udev/rules.d folder
# with this name: 55-jtech.rules
# and these contents:
# SUBSYSTEM=="usb", ATTRS{idVendor}=="1017", ATTRS{idProduct}=="900a", MODE="666"
# and then execute:
# sudo chmod 644 /etc/udev/rules.d/55-jtech.rules
# sudo chown root. /etc/udev/rules.d/55-jtech.rules
# sudo service udev restart
# Then unplug and replug mouse
import usb.core
import usb.util
import sys
from tkinter import *
# Config variables
VENDOR = 0x1017
PRODUCT = 0x900a
red = 0x00
green = 0x00
blue = 0x00
speed = 0x00
direction = 0x00
# function to open USB connection to mouse
def usbconnect():
global dev, interface
dev = usb.core.find(idVendor=VENDOR, idProduct=PRODUCT)
interface = 2
if dev is None:
raise ValueError("Device not found. Please plug in / replug mouse.")
if dev.is_kernel_driver_active(interface):
try:
dev.detach_kernel_driver(interface)
except usb.core.USBError as e:
raise ValueError("Failed to disconnect: %s" % str(e))
usb.util.claim_interface(dev, interface)
dev.set_interface_altsetting(interface=interface, alternate_setting=0)
# function to close the USB connection to the mouse
def usbclose():
usb.util.release_interface(dev,interface)
dev.attach_kernel_driver(interface)
# function to send command to the mouse, returns number of bytes sent
def senddata(data):
ret = dev.ctrl_transfer(
bmRequestType=0x21,
bRequest=0x09,
wValue=0x0300,
wIndex=0x0002,
data_or_wLength=data
)
# function to read data from the mouse
def readdata(data):
senddata(data)
return dev.ctrl_transfer(
bmRequestType=0xa1,
bRequest=0x1,
wValue=0x0307,
wIndex=0x0001,
data_or_wLength=data
)
# function for printing the hex properly
def hex2str(h):
string = "".join("%02x " % b for b in h)
return string
# function to open control channel
def opencontrol():
senddata([0x03, 0x01, 0x0f, 0xd8, 0x40, 0x00, 0x00, 0xd4])
senddata([0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc])
senddata([0x11, 0x05, 0x08, 0x00, 0x00, 0x00, 0x00, 0xe1])
data1 = [0x01, 0x02, 0x03, 0x04, 0x05, 0x80, 0x80, 0x80, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
dev.write(4,data1)
senddata([0x0b, 0x02, 0x08, 0x00, 0x00, 0x00, 0x00, 0xea])
# function to close control
def closecontrol():
senddata([0x12, 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0xac])
data5 = [0x01, 0x00, 0xf0, 0x00, 0x01, 0x00, 0xf1, 0x00, \
0x01, 0x00, 0xf2, 0x00, 0x01, 0x00, 0xf3, 0x00, \
0x01, 0x00, 0xf4, 0x00, 0x07, 0x00, 0x03, 0x00, \
0x0a, 0xf0, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00]
dev.write(4,data5)
data6 = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x04, 0x00, 0x02, 0x00, 0x04, 0x00, 0x01, 0x00]
dev.write(4,data6)
# function to set solid color mode
def solid(r,g,b):
opencontrol()
senddata([0x0c, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x72])
data2 = [0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
dev.write(4,data2)
data3 = [0x0f, 0x04, 0x0a, 0x0a, 0x19, 0x19, 0x05, 0x01, \
0x05, 0x00, 0x64, 0x64, 0x01, 0xc0, 0xf0, 0x03, \
0x01, 0x01, 0x64, 0x00, 0x02, 0x03, 0x04, 0x05, \
0x06, 0x07, 0x07, 0x07, 0x02, 0x03, 0x04, 0x05]
dev.write(4,data3)
senddata([0x0d, 0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0xec])
senddata([0x10, 0x01, 0x18, 0x00, 0x00, 0x00, 0x00, 0xd6])
# set color
if r > 51:
r = 51
if g > 51:
g = 51
if b > 51:
b = 51
# 33 = 51dec = 256 actual. (not full 0-255). Probably power-related.
color1 = [r, g, b] # "warm-up" color (first couple seconds. Not implepented as a feature)
color2 = [r, g, b] # This is actual color Not valid: 7 1a 1f 22 24 26 2b 31
color3 = [0x33, 0x00, 0x00] # 7 26 31 34 36 38 43 49
color4 = [0x33, 0x00, 0x33] # Total number of available colors: 43 ^ 3 = 79507
color5 = [0x00, 0x33, 0x33] # Sufficient number: 64
color6 = [0x33, 0x33, 0x00] # so 4 levels per color: off, 1/3, 2/3, 3/3.
color7 = [0x33, 0x33, 0x33] # 0, 0x11, 0x21, 0x33: 0, 17, 33, 51
end = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
data4 = color1 + color2 + color3 + color4 + color5 + color6 + color7 + end
dev.write(4,data4)
closecontrol()
print(hex2str(color1))
# function for "breathing" LEDs
def breathe(r,g,b):
opencontrol()
senddata([0x0c, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x72])
data2 = [0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
dev.write(4,data2)
data3 = [0x0f, 0x04, 0x0a, 0x0a, 0x19, 0x19, 0x05, 0x02, \
0x0f, 0x00, 0x64, 0x64, 0x01, 0xc0, 0xf0, 0x03, \
0x01, 0x01, 0x64, 0x00, 0x02, 0x03, 0x04, 0x05, \
0x06, 0x07, 0x07, 0x07, 0x02, 0x03, 0x04, 0x05]
dev.write(4,data3)
senddata([0x0d, 0x02, 0x05, 0x00, 0x00, 0x00, 0x00, 0xeb])
senddata([0x10, 0x01, 0x18, 0x00, 0x00, 0x00, 0x00, 0xd6])
# set color
if r > 51:
r = 51
if g > 51:
g = 51
if b > 51:
b = 51
color1 = [r, g, b] # "warm-up" color (first couple seconds. Not implemented as a feature)
color2 = [r, g, b] # This is actual color Not valid: 7 1a 1f 22 24 26 2b 31
color3 = [r, g, b] # 7 26 31 34 36 38 43 49
color4 = [r, g, b] # Total number of available colors: 43 ^ 3 = 79507
color5 = [0x00, 0x00, 0x11] # Sufficient number: 64
color6 = [r, g, b] # so 4 levels per color: off, 1/3, 2/3, 3/3.
color7 = [r, g, b] # 0, 0x11, 0x21, 0x33: 0, 17, 33, 51
end = [r, g, b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
data4 = color1 + color2 + color3 + color4 + color5 + color6 + color7 + end
dev.write(4,data4)
closecontrol()
print(hex2str(color1))
# function for "breathing" LEDs
def neon(r,g,b):
opencontrol()
senddata([0x0c, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x72])
data2 = [0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
dev.write(4,data2)
data3 = [0x0f, 0x04, 0x0a, 0x0a, 0x19, 0x19, 0x05, 0x02, \
0x01, 0x02, 0x64, 0x64, 0x01, 0xc0, 0xf0, 0x03, \
0x01, 0x01, 0x64, 0x00, 0x02, 0x03, 0x04, 0x05, \
0x06, 0x07, 0x07, 0x07, 0x02, 0x03, 0x04, 0x05]
dev.write(4,data3)
senddata([0x0d, 0x04, 0x05, 0x05, 0x00, 0x00, 0x00, 0xe4])
senddata([0x10, 0x01, 0x18, 0x00, 0x00, 0x00, 0x00, 0xd6])
# set color
if r > 51:
r = 51
if g > 51:
g = 51
if b > 51:
b = 51
color1 = [r, g, b]
color2 = [r, g, b]
color3 = [0x33, 0x00, 0x00]
color4 = [0x33, 0x00, 0x33]
color5 = [0x00, 0x33, 0x33]
color6 = [0x33, 0x33, 0x00]
color7 = [0x33, 0x33, 0x33]
end = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
data4 = color1 + color2 + color3 + color4 + color5 + color6 + color7 + end
dev.write(4,data4)
closecontrol()
print(hex2str(color1))
# function for "breathing" LEDs
def floating(r,g,b,speed,direction):
opencontrol()
senddata([0x0c, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x72])
data2 = [0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
dev.write(4,data2)
#5=f,0c=m,14=s
#1 = down, 0 = up
data3 = [0x0f, 0x04, 0x0a, 0x0a, 0x19, 0x19, 0x05, 0x03, \
0x05, 0x01, 0x64, 0x64, 0x01, 0xc0, 0xf0, 0x03, \
0x01, 0x01, 0x64, 0x00, 0x02, 0x03, 0x04, 0x05, \
0x06, 0x07, 0x07, 0x07, 0x02, 0x03, 0x04, 0x05]
dev.write(4,data3)
#5=f,0c=m,14=s
#1 = down, 0 = up
senddata([0x0d, 0x03, 0x05, 0x01, 0x00, 0x00, 0x00, 0xe4])
senddata([0x10, 0x01, 0x18, 0x00, 0x00, 0x00, 0x00, 0xd6])
# set color
if r > 51:
r = 51
if g > 51:
g = 51
if b > 51:
b = 51
color1 = [r, g, b]
color2 = [0x00, 0x00, 0x33]
color3 = [0x33, 0x00, 0x00]
color4 = [0x33, 0x00, 0x33]
color5 = [0x00, 0x33, 0x33]
color6 = [0x33, 0x33, 0x00]
color7 = [0x33, 0x33, 0x33]
end = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
data4 = color1 + color2 + color3 + color4 + color5 + color6 + color7 + end
dev.write(4,data4)
closecontrol()
print(hex2str(color1))
# function to set factory default
def setdefault():
print("### SETTING FACTORY DEFAULT ###")
print("Opening control channel...")
senddata([0x03, 0x01, 0x0f, 0xd8, 0x40, 0x00, 0x00, 0xd4])
senddata([0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc])
senddata([0x11, 0x05, 0x08, 0x00, 0x00, 0x00, 0x00, 0xe1])
data1 = [0x01, 0x02, 0x03, 0x04, 0x05, 0x80, 0x80, 0x80, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
dev.write(4,data1)
senddata([0x0b, 0x02, 0x08, 0x00, 0x00, 0x00, 0x00, 0xea])
senddata([0x0c, 0x02, 0x08, 0x00, 0x00, 0x00, 0x00, 0x71])
data2 = [0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
dev.write(4,data2)
data3 = [0x0f, 0x04, 0x0a, 0x0a, 0x19, 0x19, 0x05, 0x03, \
0x05, 0x00, 0x64, 0x64, 0x01, 0xc0, 0xf0, 0x03, \
0x01, 0x01, 0x64, 0x00, 0x02, 0x03, 0x04, 0x05, \
0x06, 0x07, 0x07, 0x07, 0x02, 0x03, 0x04, 0x05]
dev.write(4,data3)
senddata([0x0d, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00, 0xea])
senddata([0x10, 0x01, 0x18, 0x00, 0x00, 0x00, 0x00, 0xd6])
print("Sending colors...")
color1 = [0x00, 0x33, 0x00]
color2 = [0x00, 0x00, 0x33]
color3 = [0x33, 0x00, 0x00]
color4 = [0x33, 0x00, 0x33]
color5 = [0x00, 0x33, 0x33]
color6 = [0x33, 0x33, 0x00]
color7 = [0x33, 0x33, 0x33]
end = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
data4 = color1 + color2 + color3 + color4 + color5 + color6 + color7 + end
dev.write(4,data4)
print("Closing channel...")
senddata([0x12, 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0xac])
data5 = [0x01, 0x00, 0xf0, 0x00, 0x01, 0x00, 0xf1, 0x00, \
0x01, 0x00, 0xf2, 0x00, 0x01, 0x00, 0xf3, 0x00, \
0x01, 0x00, 0xf4, 0x00, 0x07, 0x00, 0x03, 0x00, \
0x0a, 0xf0, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00]
dev.write(4,data5)
data6 = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x04, 0x00, 0x02, 0x00, 0x04, 0x00, 0x01, 0x00]
dev.write(4,data6)
print("### RESET COMPLETE ###")
# function for rgb grid
def rgb_grid(i,j):
# 4-bit rgb: 0, 17, 33, 51 are the available options
colors = [0x00, 0x11, 0x21, 0x32]
if i < 4 and j < 4:
red = colors[0]
elif i > 4 and j < 4:
red = colors[1]
elif i < 4 and j > 4:
red = colors[2]
else:
red = colors[3]
green = colors[i%4]
blue = colors[j%4]
return (red, green, blue)
## MAIN PROGRAM ##
root = Tk()
previewcolor = StringVar()
mode = StringVar()
def click(event):
global red, green, blue
xc = int(event.x / 40)
yc = int(event.y / 40)
if xc < 8:
(red, green, blue) = rgb_grid(xc,yc)
selection.configure(bg = "#%02x%02x%02x" % (red*5, green*5, blue*5))
if mode.get() == "0":
solid(0, 0, 0)
elif mode.get() == "1":
solid(red, green, blue)
elif mode.get() == "2":
breathe(red, green, blue)
elif mode.get() == "3":
floating(red, green, blue)
elif mode.get() == "4":
neon(red, green, blue)
root.update()
def lightoff(): # radio button of "Off" selected
global red, green, blue
if mode.get() == "0":
solid(0, 0, 0)
elif mode.get() == "1":
solid(red, green, blue)
elif mode.get() == "2":
breathe(red, green, blue)
elif mode.get() == "3":
floating(red, green, blue)
elif mode.get() == "4":
neon(red, green, blue)
root.update()
# make the gui
canvas = Canvas(root, width=320, height=320)
canvas.bind("<Button-1>", click) # bind mouse click
# create grid of "4-bit" colors
for i in range(0, 8):
for j in range(0, 8):
x0 = i * 40 + 2
y0 = j * 40 + 2
(red, green, blue) = rgb_grid(i,j)
color = "#%02x%02x%02x" % (red*5, green*5, blue*5)
previewcolor.set(color)
canvas.create_rectangle(x0, y0, x0 + 36, y0 + 36, fill = color, width = 0)
canvas.grid(row = 0, column = 0, rowspan = 11)
# create "preview" pane
selection = Label(root, bg=previewcolor.get(), padx = 50, pady = 50)
selection.grid(row = 0, column = 1)
# create mode selection radio buttons
MODES = [("Off", "0"),("Solid","1"),("Breathe", "2"),("Floating", "3"),("Neon","4")]
mode.set("1")
for text, temp in MODES:
button = Radiobutton(root, text=text, variable=mode, value=temp, indicatoron=0, command=lightoff)
button.grid(row = int(temp) + 2, column = 1, sticky = W+E+N+S)
# create exit button
Button(root, text="Save & Exit", command=root.destroy).grid(row = 10, column = 1)
# Try to connect to the mouse
usbconnect()
# main program loop
root.mainloop()
# Close connection
usbclose()