forked from kusti8/hue-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhue.py
executable file
·316 lines (249 loc) · 10.8 KB
/
hue.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
#!/usr/bin/env python3
import serial
import sys
import argparse
import os
import picker
import previous
if os.geteuid() != 0:
sys.exit("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'.")
parser = argparse.ArgumentParser(description="Change NZXT Hue+ LEDs")
parser.add_argument("-p", "--port", default="/dev/ttyACM0", type=str, help="The port, defaults to /dev/ttyACM0")
parser.add_argument("-c", "--channel", type=int, default=0, help="The channel, defaults to 0 (BOTH)")
parser.add_argument("-g", "--gui", type=int, default=0, help="How many colors of GUI picker")
subparsers = parser.add_subparsers(help="The type of color (fixed, breathing)", dest='command')
parser_fixed = subparsers.add_parser('fixed', help="One single fixed color")
parser_fixed.add_argument("color", type=str, help="Color in hex")
parser_breathing = subparsers.add_parser('breathing', help="Breathing through a set of colors")
parser_breathing.add_argument("speed", type=int, help="Speed from 0(Slowest) to 4(Fastest)")
parser_breathing.add_argument("colors", type=str, nargs='+', help="Color(s) in hex")
parser_fading = subparsers.add_parser('fading', help="Fading through a set of colors")
parser_fading.add_argument("speed", type=int, help="Speed from 0(Slowest) to 4(Fastest)")
parser_fading.add_argument("colors", type=str, nargs='+', help="Color(s) in hex")
parser_marquee = subparsers.add_parser('marquee', help="A strip of color running")
parser_marquee.add_argument("speed", type=int, help="Speed from 0(Slowest) to 4(Fastest)")
parser_marquee.add_argument("-c", "--comet", action="store_true", help="Enable comet mode (leave a trail)")
parser_marquee.add_argument("-b", "--backwards", action="store_true", help="Enable going backwards")
parser_marquee.add_argument("size", type=int, help="The size of the group of runners (0=2, 1=3, 2=5, 3=10)")
parser_marquee.add_argument("colors", type=str, nargs=2, help="Foreground and background colors in hex")
parser_cover_marquee = subparsers.add_parser('cover_marquee', help="A strip of color running (multiple colors)")
parser_cover_marquee.add_argument("speed", type=int, help="Speed from 0(Slowest) to 4(Fastest)")
parser_cover_marquee.add_argument("-b", "--backwards", action="store_true", help="Enable going backwards")
parser_cover_marquee.add_argument("colors", type=str, nargs='+', help="Colors in hex")
parser_pulse = subparsers.add_parser('pulse', help="Pulsing through a set of colors")
parser_pulse.add_argument("speed", type=int, help="Speed from 0(Slowest) to 4(Fastest)")
parser_pulse.add_argument("colors", type=str, nargs='+', help="Color(s) in hex")
parser_spectrum = subparsers.add_parser('spectrum', help="Pulsing through a set of colors")
parser_spectrum.add_argument("speed", type=int, help="Speed from 0(Slowest) to 4(Fastest)")
parser_spectrum.add_argument("-b", "--backwards", action="store_true", help="Enable going backwards")
parser_alternating = subparsers.add_parser('alternating', help="Two alternating colors")
parser_alternating.add_argument("speed", type=int, help="Speed from 0(Slowest) to 4(Fastest)")
parser_alternating.add_argument("-m", "--moving", action="store_true", help="Enable movement")
parser_alternating.add_argument("-b", "--backwards", action="store_true", help="Enable going backwards (requires movement)")
parser_alternating.add_argument("size", type=int, help="The size of the group of runners (0=2, 1=3, 2=5, 3=10)")
parser_alternating.add_argument("colors", type=str, nargs=2, help="First and second colors in hex")
parser_candlelight = subparsers.add_parser('candlelight', help="A flickering color")
parser_candlelight.add_argument("color", type=str, help="Color in hex")
parser_power = subparsers.add_parser('power', help="Control power to the channels")
parser_power.add_argument("state", type=str, help="State (on/off)")
args = parser.parse_args()
ser = serial.Serial(args.port, 256000)
initial = [bytearray([70, 0, 192, 0, 0, 0, 255])]
def write(outputs):
for channel in outputs:
for line in channel:
ser.write(bytearray.fromhex(line))
ser.read()
def fixed(ser, gui, channel, color):
global initial
for array in initial:
ser.write(array)
ser.read()
pass
if gui != 0:
color = picker.pick("Color")
outputs = previous.get_colors(channel, ["4B0"+str(channel)+"C0"+color+"00"])
write(outputs)
def breathing(ser, gui, channel, color, speed):
global initial
for array in initial:
ser.write(array)
ser.read()
pass
if 1 <= gui <= 8:
color = []
for i in range(gui):
color.append(picker.pick("Color "+str(i+1) + " of "+str(gui)))
lines = ["4B0"+str(channel)+"CA"+color[0]+"0"+str(speed)]
last_byte = speed
for other_color in color[1:]:
last_byte = last_byte+20
lines.append("4B0"+str(channel)+"CA"+other_color+str(last_byte))
outputs = previous.get_colors(channel, lines)
write(outputs)
def fading(ser, gui, channel, color, speed):
global initial
for array in initial:
ser.write(array)
ser.read()
pass
if 1 <= gui <= 8:
color = []
for i in range(gui):
color.append(picker.pick("Color "+str(i+1) + " of "+str(gui)))
lines = ["4B0"+str(channel)+"C1"+color[0]+"0"+str(speed)]
last_byte = speed
for other_color in color[1:]:
last_byte = last_byte+20
lines.append("4B0"+str(channel)+"C1"+other_color+str(last_byte))
outputs = previous.get_colors(channel, lines)
write(outputs)
def marquee(ser, gui, channel, color, speed, size, comet, direction):
global initial
for array in initial:
ser.write(array)
ser.read()
pass
if gui != 0:
color = []
gui = 2
for i in range(2):
color.append(picker.pick("Color "+str(i+1) + " of "+str(gui)))
if comet:
option = size * 8 + speed | 128
else:
option = size * 8 + speed | 0
if direction:
first_option = format(option, '02x')
lines = ["4B0"+str(channel)+"C5"+color[0]+first_option]
second_option = format(option+32, '02x')
lines.append("4B0"+str(channel)+"C5"+color[1]+second_option)
else:
first_option = format(option, '02x')
lines = ["4B0"+str(channel)+"C4"+color[0]+first_option]
second_option = format(option+32, '02x')
lines.append("4B0"+str(channel)+"C4"+color[1]+second_option)
outputs = previous.get_colors(channel, lines)
write(outputs)
def cover_marquee(ser, gui, channel, color, speed, direction):
global initial
for array in initial:
ser.write(array)
ser.read()
pass
if 1 <= gui <= 8:
color = []
for i in range(gui):
color.append(picker.pick("Color "+str(i+1) + " of "+str(gui)))
option = speed | 0
if direction:
first_option = format(option, '02x')
lines = ["4B0"+str(channel)+"C7"+color[0]+first_option]
last_byte = option
for other_color in color[1:]:
last_byte = last_byte+32
loop_option = format(last_byte, '02x')
lines.append("4B0"+str(channel)+"C7"+other_color+loop_option)
else:
first_option = format(option, '02x')
lines = ["4B0"+str(channel)+"C6"+color[0]+first_option]
last_byte = option
for other_color in color[1:]:
last_byte = last_byte+32
loop_option = format(last_byte, '02x')
lines.append("4B0"+str(channel)+"C6"+other_color+loop_option)
outputs = previous.get_colors(channel, lines)
write(outputs)
def pulse(ser, gui, channel, color, speed):
global initial
for array in initial:
ser.write(array)
ser.read()
pass
if 1 <= gui <= 8:
color = []
for i in range(gui):
color.append(picker.pick("Color "+str(i+1) + " of "+str(gui)))
lines = ["4B0"+str(channel)+"C9"+color[0]+"0"+str(speed)]
last_byte = speed
for other_color in color[1:]:
last_byte = last_byte+20
lines.append("4B0"+str(channel)+"C9"+other_color+str(last_byte))
outputs = previous.get_colors(channel, lines)
write(outputs)
def spectrum(ser, channel, speed, direction):
global initial
for array in initial:
ser.write(array)
ser.read()
pass
if direction:
lines = ["4B0"+str(channel)+"C30000FF0"+str(speed)]
else:
lines = ["4B0"+str(channel)+"C20000FF0"+str(speed)]
outputs = previous.get_colors(channel, lines)
write(outputs)
def alternating(ser, gui, channel, color, speed, size, moving, direction):
global initial
for array in initial:
ser.write(array)
ser.read()
pass
if gui != 0:
color = []
gui = 2
for i in range(2):
color.append(picker.pick("Color "+str(i+1) + " of "+str(gui)))
if moving:
if direction:
option = size * 8 + speed | 192
else:
option = size * 8 + speed | 128
else:
option = size * 8 + speed
lines = ["4B0"+str(channel)+"C8"+color[0]+format(option, '02x')]
lines.append("4B0"+str(channel)+"C8"+color[1]+format(option+32, '02x'))
outputs = previous.get_colors(channel, lines)
write(outputs)
def candlelight(ser, gui, channel, color):
global initial
for array in initial:
ser.write(array)
ser.read()
pass
if gui != 0:
color = picker.pick("Color")
lines = ["4B0"+str(channel)+"CC"+color+"00"]
outputs = previous.get_colors(channel, lines)
write(outputs)
def power(ser, channel, state):
if state.lower() == 'on':
fixed(ser, 0, channel, "FFFFFF")
elif state.lower() == 'off':
fixed(ser, 0, channel, "000000")
else:
print("INVALID STATE!")
sys.exit(-1)
if args.command == "fixed":
fixed(ser, args.gui, args.channel, args.color)
elif args.command == 'breathing':
breathing(ser, args.gui, args.channel, args.colors, args.speed)
elif args.command == 'fading':
fading(ser, args.gui, args.channel, args.colors, args.speed)
elif args.command == 'marquee':
marquee(ser, args.gui, args.channel, args.colors, args.speed, args.size, args.comet, args.backwards)
elif args.command == 'cover_marquee':
cover_marquee(ser, args.gui, args.channel, args.colors, args.speed, args.backwards)
elif args.command == 'pulse':
pulse(ser, args.gui, args.channel, args.colors, args.speed)
elif args.command == 'spectrum':
spectrum(ser, args.channel, args.speed, args.backwards)
elif args.command == 'alternating':
alternating(ser, args.gui, args.channel, args.colors, args.speed, args.size, args.moving, args.backwards)
elif args.command == 'candlelight':
candlelight(ser, args.gui, args.channel, args.color)
elif args.command == 'power':
power(ser, args.channel, args.state)
else:
print("INVALID COMMAND")
sys.exit(-1)