-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLEDThread.py
199 lines (166 loc) · 7.02 KB
/
LEDThread.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
import threading
import sys
# Simple demo of of the WS2801/SPI-like addressable RGB LED lights.
import time
import RPi.GPIO as GPIO
# Import the WS2801 module.
import Adafruit_WS2801
import Adafruit_GPIO.SPI as SPI
class LEDThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
# Configure the count of pixels:
self.PIXEL_COUNT = 28
self.PIXEL_SIZE = 3
# Alternatively specify a hardware SPI connection on /dev/spidev0.0:
self.SPI_PORT = 0
self.SPI_DEVICE = 0
self.pixels = Adafruit_WS2801.WS2801Pixels(self.PIXEL_COUNT, spi=SPI.SpiDev(self.SPI_PORT, self.SPI_DEVICE), gpio=GPIO)
# Default values for starting color
self.sRed = "ff"
self.sGreen = "ff"
self.sBlue = "ff"
self.iBrightness = 50
self.mode = 0
self.relais = 1
self.data = bytearray()
self.gamma = bytearray(256)
# Define Relais
self.RELAIS_1_GPIO = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.RELAIS_1_GPIO, GPIO.OUT)
GPIO.output(self.RELAIS_1_GPIO, GPIO.HIGH)
def wheel(self, pos):
if pos < 85:
return Adafruit_WS2801.RGB_to_color(pos * 3, 255 - pos * 3, 0)
elif pos < 170:
pos -= 85
return Adafruit_WS2801.RGB_to_color(255 - pos * 3, 0, pos * 3)
else:
pos -= 170
return Adafruit_WS2801.RGB_to_color(0, pos * 3, 255 - pos * 3)
def wheel_rgb(self, pos):
if pos < 85:
return Adafruit_WS2801.RGB_to_color(255, 0, 0)
elif pos < 170:
return Adafruit_WS2801.RGB_to_color(0, 255, 0)
else:
return Adafruit_WS2801.RGB_to_color(0, 0, 255)
def rainbow_colors(self, wait=0.05):
for j in range(256):
for i in range(self.pixels.count()):
if self.mode != 2: return
self.pixels.set_pixel(i, self.wheel(((256 // self.pixels.count() + j)) % 256) )
self.pixels.show()
if wait > 0:
time.sleep(wait)
def rainbow_cycle(self, wait=0.005):
for j in range(256):
for i in range(self.pixels.count()):
if self.mode != 3: return
self.pixels.set_pixel(i, self.wheel(((i * 256 // self.pixels.count()) + j) % 256) )
self.pixels.show()
if wait > 0:
time.sleep(wait)
def rainbow_cycle_successive(self, wait=0.05):
self.pixels.clear()
for i in range(self.pixels.count()):
if self.mode != 4: return
self.pixels.set_pixel(i, self.wheel(((i * 256 // self.pixels.count())) % 256) )
self.pixels.show()
if wait > 0:
time.sleep(wait)
for k in range(self.pixels.count()):
if self.mode != 4: return
self.pixels.set_pixel(k, Adafruit_WS2801.RGB_to_color( 0, 0, 0 ))
self.pixels.show()
if wait > 0:
time.sleep(wait)
def rgb_cycle_moving(self, wait=0.05):
self.pixels.clear()
for j in range(self.pixels.count()):
for i in range(self.pixels.count()):
if self.mode != 5: return
self.pixels.set_pixel((j+i)%self.PIXEL_COUNT, self.wheel_rgb( (i * 256 // self.pixels.count() % 256) ))
self.pixels.show()
if wait > 0:
time.sleep(wait)
def appear_from_back(self, color=(255, 0, 0)):
for i in range(self.pixels.count()):
for j in reversed(range(i, self.pixels.count())):
if self.mode != 6: return
self.pixels.clear()
# first set all pixels at the begin
for k in range(i):
self.pixels.set_pixel(k, Adafruit_WS2801.RGB_to_color( color[0], color[1], color[2] ))
# set then the pixel at position j
self.pixels.set_pixel(j, Adafruit_WS2801.RGB_to_color( color[0], color[1], color[2] ))
self.pixels.show()
time.sleep(0.005)
def singleColor(self):
self.mode = 0
self.pixels.clear()
fDimFactor = float(self.iBrightness) / 100
sNewRed = int(round(int(self.sRed,16)*fDimFactor))
sNewGreen = int(round(int(self.sGreen,16)*fDimFactor))
sNewBlue = int(round(int(self.sBlue,16)*fDimFactor))
for k in range(self.pixels.count()):
self.pixels.set_pixel(k, Adafruit_WS2801.RGB_to_color( sNewRed, sNewGreen, sNewBlue ))
self.pixels.show()
def show_pixelcontroller_stream(self):
self.mode = 0
self.pixels.clear()
pixels_in_buffer = len(self.data) / self.PIXEL_SIZE
for pixel_index in range(pixels_in_buffer):
pixel = bytearray(self.data[(pixel_index * self.PIXEL_SIZE):((pixel_index * self.PIXEL_SIZE) + self.PIXEL_SIZE)])
self.pixels.set_pixel(pixel_index, Adafruit_WS2801.RGB_to_color( pixel[0], pixel[1], pixel[2] ))
self.pixels.show()
def show_artnet_stream(self):
self.mode = 0
self.pixels.clear()
color_values = self.data[18:]
#print color_values.encode("hex")
pixels_in_buffer = len(color_values) / self.PIXEL_SIZE
if pixels_in_buffer > self.PIXEL_COUNT:
pixels_in_buffer = self.PIXEL_COUNT
for pixel_index in range(pixels_in_buffer):
pixel = bytearray(color_values[(pixel_index * self.PIXEL_SIZE):((pixel_index * self.PIXEL_SIZE) + self.PIXEL_SIZE)])
self.pixels.set_pixel(pixel_index, Adafruit_WS2801.RGB_to_color( pixel[0], pixel[1], pixel[2] ))
self.pixels.show()
def setData(self,data):
self.data = data
def setColor(self, r, g, b):
self.sRed = r
self.sGreen = g
self.sBlue = b
def getColor(self):
return "" + self.sRed + self.sGreen + self.sBlue
def setBrightness(self, b):
self.iBrightness = b
def getBrightness(self):
return self.iBrightness
def setMode(self, i):
self.mode = i
def toggleRelais(self):
if self.relais == 1:
GPIO.output(self.RELAIS_1_GPIO, GPIO.HIGH) # aus
time.sleep(3) #sleep 3s
else:
GPIO.output(self.RELAIS_1_GPIO, GPIO.LOW) # an
time.sleep(3) #sleep 3s
if self.mode == 0:
self.singleColor()
self.relais = not self.relais
def run(self):
while getattr(self, "do_run", True):
if self.relais == 1:
if self.mode == 1: self.singleColor()
if self.mode == 2: self.rainbow_colors()
if self.mode == 3: self.rainbow_cycle()
if self.mode == 4: self.rainbow_cycle_successive()
if self.mode == 5: self.rgb_cycle_moving()
if self.mode == 6: self.appear_from_back()
if self.mode == 7: self.show_pixelcontroller_stream()
if self.mode == 8: self.show_artnet_stream()
time.sleep(0.01)
print >>sys.stderr, "Shutdown thread..."