-
Notifications
You must be signed in to change notification settings - Fork 2
/
LED.py
169 lines (131 loc) · 4.34 KB
/
LED.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
#!/usr/bin/python
import datetime
import math
#import thread
import TLC5940
from multiprocessing import Process, Queue, Manager
import time
def millis():
dt = datetime.datetime.now()
ms = (dt.second) * 1000 + dt.microsecond / 1000.0
return ms
######################################
# led functions
######################################
def breathe(period, maxbrightness, minbrightness=0, offset=0, *other, **kwargs):
rate=2*math.pi/period
brightness=maxbrightness-minbrightness
return int((brightness*(math.sin(float(millis()*rate)-offset)+1)/2)+minbrightness)
def blink(period,ontime,offset,brightness, *other, **kwargs):
blinkprogress = (int(millis())-offset) % period
if blinkprogress >= ontime:
return 0
else:
return brightness
def hueblend(period,offset,brightness,colour, *other, **kwargs):
# colour acts as angle offset
if colour == 1:
coloffset = 0
elif colour == 2:
coloffset = 120
elif colour == 3:
coloffset = 240
progress = (int(millis())+offset) % period
deg = (float(progress) / float(period) * float(360) + float(coloffset))%360
#print deg
# create blends
if deg < 60:
val=brightness
elif ((deg >= 60) and (deg < 120)):
val=brightness - (((deg - 60) * brightness)/60) #drops
elif ((deg >= 120) and (deg < 240)):
val=0
elif ((deg >= 240) and (deg < 300)):
val=((deg - 240) * brightness)/60 # rises
elif (deg >= 300):
val=brightness
return int(val)
def off(*other, **kwargs):
return 0
def constant(value, *other, **kwargs):
return value
###################################
# led mode mixers
###################################
def pulse(mode0,mode0settings,mode1,mode1settings,pulsesettings,ledid, instance, *other):
endtime = pulsesettings[0]
now=millis()
if now >= endtime:
instance.clearmixer(ledid,0)
return mode0(*mode0settings, instance=instance)
else:
return mode1(*mode1settings, instance=instance)
def modeblend(mode0,mode0settings,mode1,mode1settings,blendsettings,ledid, instance, *other):
starttime = blendsettings[0]
endtime = blendsettings[1]
now=millis()
if now <= starttime:
return mode0(*mode0settings, instance=instance)
else:
if now >= endtime:
instance.clearmixer(ledid,1)
return mode1(*mode1settings, instance=instance)
else:
progress=(now-starttime)/(endtime-starttime)
outgoing=(1-progress)*mode0(*mode0settings, instance=instance)
incoming=progress*mode1(*mode1settings, instance=instance)
return int(outgoing+incoming)
class LEDController:
###################################
# mixer mode helpers
###################################
def setmixer(self,ledid,newmode,newmodesettings,mixer,mixersettings):
oldmode=self.mode[ledid]
oldsettings=self.settings[ledid]
self.mode[ledid]=mixer
self.settings[ledid]=(oldmode,oldsettings,newmode,(newmodesettings),(mixersettings),ledid)
def setmodemixer(self,data,mixer,mixersettings):
for i in range(len(self.settings)):
self.setmixer(i,data[0][i],data[1][i],mixer,mixersettings)
self.setconfig(self.mode, self.settings)
def clearmixer(self,ledid,new):
if new==1:
self.mode[ledid]=self.settings[ledid][2]
self.settings[ledid]=self.settings[ledid][3]
else:
self.mode[ledid]=self.settings[ledid][0]
self.settings[ledid]=self.settings[ledid][1]
self.setconfig(self.mode, self.settings)
###################################
# led processing commands
###################################
def __init__(self, count):
self.count = count
self.brightness = [0]*16*count
self.mode = [off]*16*count
self.settings = [(0,)]*16*count
self.queue = Queue()
def setconfig(self, mode, settings):
self.settings = settings
self.mode = mode
#print self.mode
#print self.settings
self.queue.put([mode, settings])
def startledcontroler(self):
p = Process(target=self.ledcontroler,args=(self.queue,))
p.daemon = True
p.start()
def setmode(self, data):
print "use of depricated setmode"
self.setconfig(data[0], data[1])
def ledcontroler(self, queue):
TLC5940.resetTLC()
while(1):
while not queue.empty():
(self.mode, self.settings) = self.queue.get()
for i in range(len(self.settings)):
self.brightness[i]=self.mode[i](*(self.settings[i]), instance=self)
TLC5940.setTLCvalue(TLC5940.buildvalue(self.brightness,TLC5940.regPWM),TLC5940.regPWM)
if __name__ == "__main__":
# run a simple test of breathe animation
print "test where out of date and have been removed"