forked from SebiTimeWaster/G213Colors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
G213Colors.py
124 lines (107 loc) · 4.67 KB
/
G213Colors.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
'''
* The MIT License (MIT)
*
* G213Colors v0.1 Copyright (c) 2016 SebiTimeWaster
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
'''
import sys
import usb.core
import usb.util
import binascii
from time import sleep
standardColor = 'ffb4aa' # Standard color, i found this color to produce a white color on my G213
idVendor = 0x046d # The id of the Logitech company
idProduct = 0xc336 # The id of the G213
bmRequestType = 0x21 # --.
bmRequest = 0x09 # \ The controll transfer
wValue = 0x0211 # / configuration for the G213
wIndex = 0x0001 # --'
colorCommand = "11ff0c3a{}01{}0200000000000000000000" # binary commands in hex format
breatheCommand = "11ff0c3a0002{}{}006400000000000000"
cycleCommand = "11ff0c3a0003ffffff0000{}64000000000000"
device = "" # device resource
isDetached = False # If kernel driver needs to be reattached
numArguments = len(sys.argv) # number of arguments given
if numArguments > 1:
option = str(sys.argv[1]) # option to use
else:
option = ""
def connectG():
global device, isDetached
# find G product
device = usb.core.find(idVendor = idVendor, idProduct = idProduct)
# if not found exit
if device is None:
raise ValueError("USB device not found!")
# if a kernel driver is attached to the interface detach it, otherwise no data can be send
if device.is_kernel_driver_active(wIndex):
device.detach_kernel_driver(wIndex)
isDetached = True
def disconnectG():
# free device resource to be able to reattach kernel driver
usb.util.dispose_resources(device)
# reattach kernel driver, otherwise special key will not work
if isDetached:
device.attach_kernel_driver(wIndex)
def sendData(data):
# decode data to binary and send it
device.ctrl_transfer(bmRequestType, bmRequest, wValue, wIndex, binascii.unhexlify(data))
def sendColorCommand(colorHex, field = 0):
sendData(colorCommand.format(str(format(field, '02x')), colorHex))
def sendBreatheCommand(colorHex, speed):
sendData(breatheCommand.format(colorHex, str(format(speed, '04x'))))
def sendCycleCommand(speed):
sendData(cycleCommand.format(str(format(speed, '04x'))))
def printInfo():
print("G213Colors - Changes the key colors on a Logitech G213 Prodigy Gaming Keyboard")
print("\nOptions:")
print("-c Set the standard color (white)")
print("-c <color> Set a custom color")
print("-c <color1> ... <color5> Set custom colors for the 5 segments")
print("-b <color> <time> Sets a color breathing animation")
print("-x <time> Sets a color cycling animation")
print("\nPlease note:")
print("* Color is a hex encoded color in the format RRGGBB")
print(" i.e. ff0000 is red, 00ff00 is green and so on,")
print(" abbreviated formats are not allowed")
print("* Time is in milliseconds, range: 32 - 65535")
if "-" not in option:
# no option found, exit
printInfo()
sys.exit(1)
connectG()
if "c" in option:
if numArguments == 2:
sendColorCommand(standardColor)
elif numArguments == 3:
sendColorCommand(str(sys.argv[2]))
elif numArguments == 7:
for index in range(1, 6):
sendColorCommand(str(sys.argv[index + 1]), index)
sleep(0.01)
else:
printInfo()
elif "b" in option and numArguments == 4:
sendBreatheCommand(str(sys.argv[2]), int(sys.argv[3]))
elif "x" in option and numArguments == 3:
sendCycleCommand(int(sys.argv[2]))
else:
printInfo()
disconnectG()