-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorPicker.py
More file actions
80 lines (70 loc) · 2.49 KB
/
ColorPicker.py
File metadata and controls
80 lines (70 loc) · 2.49 KB
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
import math
import random
import lightpack
import ConfigParser
import os
import threading
import time
class ColorPicker:
def __init__(self):
self.loadConfig()
self.ConnectToLightpack()
self.i = 0
random.seed()
print 'init'
def loadConfig(self):
self.scriptDir = os.path.dirname(os.path.realpath(__file__))
self.config = ConfigParser.ConfigParser()
self.config.read(self.scriptDir + '/ColorPicker.ini')
self.animType = self.config.getint('Animate', 'type')
self.animInterval = self.config.getfloat('Animate', 'interval')
colorConfigs = self.config.items('LEDColors')
maxLed = int(max(colorConfigs, key=lambda x: int(x[0]))[0])
self.ledColors=[ [0,0,0] for k in range(0, maxLed)]
for colorConfig in colorConfigs:
ledNum = int(colorConfig[0])
rgbValues = [int(n) for n in colorConfig[1].split(',')]
self.ledColors[ledNum-1] = rgbValues
def ConnectToLightpack(self):
try:
self.host = self.config.get('Lightpack', 'host')
self.port = self.config.getint('Lightpack', 'port')
self.lpack = lightpack.lightpack(self.host, self.port)
self.lpack.connect()
return True
except: return False
def run(self):
if self.lpack.lock() :
self.lpack.turnOn()
self.lpack.setFrame(self.ledColors)
if self.animType == 1:
self.lastFrame = list(self.ledColors)
while True:
self.twinkle()
time.sleep(self.animInterval)
else:
input("Press ENTER to exit")
def stop(self):
self.timeranim.stop()
self.lpack.unlock()
def twinkle(self):
try:
self.i = self.i+1
newFrame = self.lastFrame
leds = len(self.lastFrame)
att = 0.95
for k in range (0, leds):
if random.randrange(100) < 10 :
r,g,b = self.ledColors[k]
else :
r = int(newFrame[k][0] * att)
g = int(newFrame[k][1] * att)
b = int(newFrame[k][2] * att)
newFrame[k] = [r,g,b]
self.lpack.setFrame(newFrame)
self.lastFrame = newFrame
self.i += 1
except Exception, e:
print(str(e))
colors = ColorPicker()
colors.run()