-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleds.py
177 lines (161 loc) · 4.96 KB
/
leds.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
try:
from rpi_ws281x import *
emulator = False
except ImportError:
emulator = True
import threading
import math
from time import sleep
# These are the defaults
#LED_COUNT = 5
#LED_PIN = 18
#LED_CHANNEL = 0
#LED_FREQ_HZ = 800000
#LED_DMA = 10
#LED_BRIGHTNESS = 255
#LED_INVERT = False
# RGB/HSV stuff from http://code.activestate.com/recipes/576919-python-rgb-and-hsv-conversion/
def hsv2rgb(h, s, v):
h = float(h)
s = float(s)
v = float(v)
h60 = h / 60.0
h60f = math.floor(h60)
hi = int(h60f) % 6
f = h60 - h60f
p = v * (1 - s)
q = v * (1 - f * s)
t = v * (1 - (1 - f) * s)
r, g, b = 0, 0, 0
if hi == 0: r, g, b = v, t, p
elif hi == 1: r, g, b = q, v, p
elif hi == 2: r, g, b = p, v, t
elif hi == 3: r, g, b = p, q, v
elif hi == 4: r, g, b = t, p, v
elif hi == 5: r, g, b = v, p, q
r, g, b = int(r * 255), int(g * 255), int(b * 255)
return r, g, b
def rgb2hsv(r, g, b):
r, g, b = r/255.0, g/255.0, b/255.0
mx = max(r, g, b)
mn = min(r, g, b)
df = mx-mn
if mx == mn:
h = 0
elif mx == r:
h = (60 * ((g-b)/df) + 360) % 360
elif mx == g:
h = (60 * ((b-r)/df) + 120) % 360
elif mx == b:
h = (60 * ((r-g)/df) + 240) % 360
if mx == 0:
s = 0
else:
s = df/mx
v = mx
return h, s, v
class Leds (threading.Thread):
def __init__(self, pin, count):
threading.Thread.__init__(self)
if not emulator:
self.strip = Adafruit_NeoPixel(count, pin)
self.stop_thread = False
else:
self.stop_thread = True
self.num_steps = 16
self.state = "off"
self.color = (0, 183, 255)
self.cond = threading.Condition()
self.__states = {
"off" : self.stateOff,
"waiting" : self.stateWait,
"playing" : self.statePlay,
"error" : self.stateErr,
}
if not emulator:
self.strip.begin()
def run(self):
while (not self.stop_thread):
self.cond.acquire()
self.cond.wait()
self.__states[self.state](self.color)
self.cond.release()
print("Exiting LED thread")
def kill(self):
self.running = False
self.stop_thread = True
self.cond.acquire()
self.cond.notifyAll()
self.cond.release()
def stop(self):
self.running = False
def manageState(self):
self.running = False
self.cond.acquire()
self.running = True
self.cond.notifyAll()
self.cond.release()
def startWait(self, color):
self.color = color
self.state = "waiting"
self.manageState()
def startPlay(self, color):
self.color = color
self.state = "playing"
self.manageState()
def startOff(self):
self.state = "off"
self.manageState()
self.running = False
def startErr(self):
self.state = "error"
self.manageState()
def stateOff(self, color):
for i in range(self.strip.numPixels()):
self.strip.setPixelColorRGB(i, 0, 0, 0)
self.strip.show()
def stateWait(self, color):
while self.running:
for j in range(128):
for i in range(self.strip.numPixels()):
tail = j / 128
red = int(color[0] * tail)
green = int(color[1] * tail)
blue = int(color[2] * tail)
self.strip.setPixelColorRGB(i, red, green, blue)
self.strip.show()
sleep(0.005)
if not self.running: return
for j in range(128, 0, -1):
for i in range(self.strip.numPixels()):
tail = j / 128
red = int(color[0] * tail)
green = int(color[1] * tail)
blue = int(color[2] * tail)
self.strip.setPixelColorRGB(i, red, green, blue)
self.strip.show()
sleep(0.007)
if not self.running: return
def statePlay(self, color):
step = self.strip.numPixels()
while self.running:
for i in range(self.strip.numPixels()):
if (i == (step - 1)):
self.strip.setPixelColorRGB(i, color[0], color[1], color[2])
else:
self.strip.setPixelColorRGB(i, 0, 0, 0)
if (step == 0):
step = self.strip.numPixels()
else:
step = step - 1
self.strip.show()
sleep(0.5)
def stateErr(self, color):
for j in range(8):
for i in range(self.strip.numPixels()):
if not (j % 2):
self.strip.setPixelColorRGB(i, 255, 0, 0)
else:
self.strip.setPixelColorRGB(i, 0, 0, 0)
self.strip.show()
sleep(0.3)