-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
535 lines (429 loc) · 14.1 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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
from tkinter import *
from tkinter.colorchooser import askcolor
import socket
import threading
import struct
from threading import Thread
import time
import random
import math
import argparse
import cmd
from collections import deque
# local imports
import colorUtils
def cos(x, offset=0, period=1, minn=0, maxx=1):
"""A cosine curve scaled to fit in a 0-1 range and 0-1 domain by default.
offset: how much to slide the curve across the domain (should be 0-1)
period: the length of one wave
minn, maxx: the output range
"""
value = math.cos((x/period - offset) * math.pi * 2) / 2 + 0.5
return value*(maxx-minn) + minn
def fade(pixels, start_time):
""" Use cosine function to fade pixel list (in or out) """
t = time.time() - start_time
newPixels = []
for idx in range(len(pixels)):
r = cos(.1, offset=t/8, period=1) * pixels[idx][0]
g = cos(.1, offset=t/8, period=1) * pixels[idx][1]
b = cos(.1, offset=t/8, period=1) * pixels[idx][2]
pixel = (int(r), int(g), int(b))
newPixels.append(pixel)
return newPixels
def shift(pixels, num):
tempPixels = deque(pixels)
tempPixels.rotate(num)
return list(tempPixels)
def defaultFrameCreate(numPixels, pixelColor):
pixels = []
for idx in range(numPixels):
pixels.append(pixelColor)
return pixels
def shiftFrameCreate(numPixels, startPixel):
pixels = []
for idx in range(numPixels):
val = idx/numPixels
if val < .5:
val = 0.5
r = round(cos(val, offset=0, period=1) * startPixel[0])
g = round(cos(val, offset=0, period=1) * startPixel[1])
b = round(cos(val, offset=0, period=1) * startPixel[2])
pixel = (r, g, b)
pixels.append(pixel)
return pixels
def changeBrightness(pixels, modifier):
newPixels = []
for pixel in pixels:
newPixels.append((int(pixel[0]*modifier), int(pixel[1]*modifier), int(pixel[2]*modifier)))
return newPixels
class gui:
def __init__(self, lightcycle):
self.lightcycle = lightcycle
self.master = Tk()
self.master.title("LightCycle")
self.master.minsize(width=400, height=200)
try:
self.master.iconbitmap('lightcycle_16x16.ico')
except:
pass
self.menubar = Menu(self.master)
self.filemenu = Menu(self.menubar, tearoff=0)
#self.filemenu.add_command(label="Open Ctl-O")#, command=test)
#self.filemenu.add_command(label="Connect Ctl-N", command=self.startAction)
self.menubar.add_cascade(label="File", menu=self.filemenu)
self.brightnessSlider = Scale(self.master, from_=0, to=100, length=300, tickinterval=5, orient=HORIZONTAL, command=self.brightnessAction)
self.brightnessSlider.set(50)
self.brightnessSlider.pack()
self.speedSlider = Scale(self.master, from_=1, to=10, length=200, tickinterval=1, orient=HORIZONTAL, command=self.speedAction)
self.speedSlider.set(30)
self.speedSlider.pack()
var = StringVar(self.master)
var.set('none')
self.optionMenu = OptionMenu(self.master, var, *self.lightcycle.modes, command=self.modeAction)
self.optionMenu.pack()
self.colorbutton = Button(self.master, text='Select Color', command=self.colorAction)
self.colorbutton.pack()
self.ipTextBox = Text(self.master, height=1, width=15)
self.ipTextBox.insert(END, self.lightcycle.ip)
self.ipTextBox.pack()
self.portTextBox = Text(self.master, height=1, width=15)
self.portTextBox.insert(END, self.lightcycle.port)
self.portTextBox.pack()
self.startButton = Button(self.master, text='Start', command=self.startAction)
self.startButton.pack()
self.stopButton = Button(self.master, text='Stop', command=self.stopAction, state='disabled')
self.stopButton.pack()
self.master.config(menu=self.menubar)
def startAction(self):
self.lightcycle.start()
if self.lightcycle.sock is not None:
self.stopButton['state'] = 'normal'
def stopAction(self):
self.lightcycle.stop()
self.stopButton['state'] = 'disabled'
def modeAction(self, value):
self.lightcycle.switchMode(value)
def brightnessAction(self, event):
self.lightcycle.brightness = self.brightnessSlider.get()
def colorAction(self):
color, colorString = askcolor(parent=self.master)
self.lightcycle.color = color
def speedAction(self, event):
self.lightcycle.speed = self.speedSlider.get()
def cleanup(self):
self.lightcycle.stop()
class LightCycleCommandline(cmd.Cmd):
def __init__(self, lightcycle):
super().__init__()
self.lightcycle = lightcycle
self.prompt = '> '
self.intro = 'LightCycle: LED controller'
def do_help(self, line):
usage = [
' help - print this help message',
' start - connect and start default animation logic',
' stop - disconnect and stop animation logic',
' mode - change current animation mode. ex: mode rainbowShift',
' exit - stop everything and close program',
' list - list available animation modes',
' on - turn all LEDs on',
' off - turn all LEDs off',
' brightness - change brightness level (0 - 100, default is 50)',
' speed - change speed of animations (1 is fastest, default is 5)',
' direction - change direction of linear animations (left or right)',
]
for line in usage:
print(line)
def emptyline(self):
""" Do nothing when empty line is input """
pass
def do_start(self, line):
self.lightcycle.start()
def do_stop(self, line):
self.lightcycle.stop()
def do_exit(self, line):
self.lightcycle.stop()
return True
def do_list(self, line):
for mode in self.lightcycle.modes:
print(mode)
def do_mode(self, line):
self.lightcycle.switchMode(line.strip())
def do_speed(self, line):
self.lightcycle.speed = int(line.strip())
def do_color(self, line):
''' Change base color of animations '''
self.lightcycle.color = colorUtils.selectColor(line.strip())
def do_direction(self, line):
self.lightcycle.switchDirection(line.strip())
def do_brightness(self, line):
self.lightcycle.brightness = int(line.strip())
def do_on(self, line):
self.lightcycle.on()
def do_off(self, line):
self.lightcycle.off()
def complete_mode(self, text, line, start_index, end_index):
# TODO
pass
class LightCycle:
def __init__(self):
self.interface = LightCycleCommandline(self)
self.ip = '127.0.0.1'
self.port = 7890
self.sock = None
self.lightCycleThread = None
self.stopRun = False
self.stopMode = False
self.numPixels = 0
self.numChannels = 0
self.mode = 'none'
self.modes = ['none', 'test', 'flicker', 'flickerOn', 'rainbow', 'shift', 'rainbowShift', 'fill', 'fade']
self.refreshRate = 60
self.speed = 5
self.color = (255,255,255)
self.direction = 'right'
self.brightness = 50
def interactive_prompt(self):
""" Start the interactive command prompt """
self.interactive = True
do_quit = False
while do_quit is not True:
try:
self.interface.cmdloop()
do_quit = True
except KeyboardInterrupt:
sys.stdout.write('\n')
def start(self):
self.stopRun = False
self.stopMode = False
self.connect()
self.lightCycleThread = threading.Thread(target=self.run)
self.lightCycleThread.start()
def stop(self):
self.stopMode = True
self.stopRun = True
if self.lightCycleThread is not None:
self.lightCycleThread.join()
self.lightCycleThread = None
self.off()
self.disconnect()
def disconnect(self):
if self.sock is not None:
self.sock.close()
def connect(self):
# Create a TCP/IP socket
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
try:
print("connecting to", self.ip, self.port)
server_address = (self.ip, self.port)
self.sock.connect(server_address)
except:
print("failed to connect")
self.sock = None
def opcSend(self, pixels):
""" Turn list of pixels into OPC format and send to server """
pixels = changeBrightness(pixels, self.brightness/100)
comm = 0
for c in range(self.numChannels):
chan = c
length = len(pixels)*3
message = struct.pack('B', chan)
message += struct.pack('B', comm)
message += struct.pack('!H', length)
for pix in pixels:
message += struct.pack('B', pix[0])
message += struct.pack('B', pix[1])
message += struct.pack('B', pix[2])
#testing gamma correction
#r1, g1, b1 = (pix[0]/255.0, pix[1]/255.0, pix[2]/255.0)
#r, g, b = colorUtils.gamma((r1, g1, b1), 2.5)
#message += struct.pack('B', r*255)
#message += struct.pack('B', g*255)
#message += struct.pack('B', b*255)
try:
self.sock.sendall(message)
except:
print("Error in sending")
break
def switchMode(self, mode):
if mode in self.modes:
print('new mode selected')
self.mode = mode
self.stopMode = True
else:
print('bad mode:', mode)
def switchDirection(self, direction):
if direction == 'right' or direction == 'left':
self.direction = direction
else:
print('bad direction given. need "right" or "left"')
def run(self):
modeThread = None
while self.stopRun is False:
if self.mode != 'none':
self.stopMode = False
if self.mode == 'test':
modeThread = threading.Thread(target=self.testMode)
elif self.mode == 'flicker':
modeThread = threading.Thread(target=self.flickerMode)
elif self.mode == 'flickerOn':
modeThread = threading.Thread(target=self.flickerOnMode)
elif self.mode == 'rainbow':
modeThread = threading.Thread(target=self.rainbowMode)
elif self.mode == 'shift':
modeThread = threading.Thread(target=self.shiftMode)
elif self.mode == 'rainbowShift':
modeThread = threading.Thread(target=self.rainbowshiftMode)
elif self.mode == 'fill':
modeThread = threading.Thread(target=self.fillMode)
elif self.mode == 'fade':
modeThread = threading.Thread(target=self.fadeMode)
else:
print('not a supported mode', self.mode)
else:
pass
if modeThread is not None:
self.mode = 'none'
self.stopMode = False
modeThread.start()
modeThread.join()
modeThread = None
self.stopMode = True
time.sleep(1)
def off(self):
pixels = defaultFrameCreate(self.numPixels, (0,0,0))
self.opcSend(pixels)
def on(self):
pixels = defaultFrameCreate(self.numPixels, self.color)
self.opcSend(pixels)
def update(self):
pass
###################
# mode functions
###################
def testMode(self):
while self.stopMode is False:
pixels = []
for x in range(self.numPixels):
pixels.append((random.randint(0,255),random.randint(0,255),random.randint(0,255)))
self.opcSend(pixels)
time.sleep((1/self.refreshRate) * self.speed)
def flickerMode(self):
on_pixels = defaultFrameCreate(self.numPixels, self.color)
off_pixels = defaultFrameCreate(self.numPixels, (0,0,0))
self.opcSend(off_pixels)
total = 1
flicker_time = 0.1
while self.stopMode is False:
self.opcSend(on_pixels)
time.sleep(flicker_time)
self.opcSend(off_pixels)
time.sleep(2 * random.uniform(0,0.5))
self.opcSend(on_pixels)
def flickerOnMode(self):
on_pixels = defaultFrameCreate(self.numPixels, self.color)
off_pixels = defaultFrameCreate(self.numPixels, (0,0,0))
self.opcSend(off_pixels)
flicker_count = 3
total = 1
flicker_time = 0.1
while flicker_count > 0:
self.opcSend(on_pixels)
time.sleep(flicker_time)
self.opcSend(off_pixels)
time.sleep(flicker_count * random.uniform(0,0.5))
flicker_count = flicker_count -1
self.opcSend(on_pixels)
def rainbowMode(self):
angle = 0
while self.stopMode is False:
# get color from rainbow at current "angle"
pixel = colorUtils.getRainbow2(angle)
# create array of pixels of same color
pixels = defaultFrameCreate(self.numPixels, pixel)
# send pixels and sleep
self.opcSend(pixels)
time.sleep((1/self.refreshRate) * self.speed)
# go to next angle in raindbow sequence
angle = angle + 1
if angle > 359:
angle = 0
def shiftMode(self):
# make shift frame
shiftnum = 0
pixels = shiftFrameCreate(self.numPixels, self.color)
# if left shift then reverse shift frame
if self.direction == 'left':
pixels = pixels[::-1]
while self.stopMode is False:
if self.direction == 'right':
pixels = shift(pixels, 1)
else:
pixels = shift(pixels, -1)
shiftnum = shiftnum +1
if shiftnum > self.numPixels:
shiftnum = 0
# send pixels and sleep
self.opcSend(pixels)
time.sleep((1/self.refreshRate) * self.speed)
def rainbowshiftMode(self):
shiftnum = 0
angle = 0
while self.stopMode is False:
# pick color from rainbow sequence
pixel = colorUtils.getRainbow(angle)
angle = angle + 1
if angle > 359:
angle = 0
# create shift frame
pixels = shiftFrameCreate(self.numPixels, pixel)
if self.direction == 'left':
tempNum = shiftnum - (shiftnum*2)
else:
tempNum = shiftnum
pixels = shift(pixels, tempNum)
shiftnum = shiftnum + 1
if shiftnum > self.numPixels:
shiftnum = 0
# send pixels and sleep
self.opcSend(pixels)
time.sleep((1/self.refreshRate) * self.speed)
def fillMode(self):
pixels = defaultFrameCreate(self.numPixels, (0,0,0))
pixels = []
for x in range(self.numPixels):
pixels.append((0,0,0))
for idx in range(self.numPixels):
pixels[idx] = self.color
# send pixels and sleep
self.opcSend(pixels)
time.sleep((1/self.refreshRate) * self.speed)
def fadeMode(self):
startPixels = defaultFrameCreate(self.numPixels, self.color)
start_time = time.time()
while self.stopMode is False:
pixels = fade(startPixels, start_time)
# send pixels and sleep
self.opcSend(pixels)
time.sleep((1/self.refreshRate) * self.speed)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-g", "--gui", action='store_true', help="enable the GUI")
parser.add_argument("-i", "--ip", default="127.0.0.1", help="ip address of OPC server")
parser.add_argument("-p", "--port", type=int, default=7890, help="port number for OPC server")
args = parser.parse_args()
lightcycle = LightCycle()
lightcycle.ip = args.ip.strip()
lightcycle.port = int(args.port)
lightcycle.numPixels = 64 # pixels per channel..change this if your setup is different
lightcycle.numChannels = 8 # fadecandy supports up to 8 channels
if args.gui is True:
program = gui(lightcycle)
mainloop()
program.cleanup()
else:
lightcycle.interactive_prompt()
if __name__ == "__main__":
main()