-
Notifications
You must be signed in to change notification settings - Fork 0
/
piLED_demo.py
295 lines (259 loc) · 10.8 KB
/
piLED_demo.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
# NeoPixel library strandtest example
# Author: Tony DiCola ([email protected])
#
# Direct port of the Arduino NeoPixel library strandtest example. Showcases
# various animations on a strip of NeoPixels.
import time
from random import randint
from neopixel import *
# LED strip configuration:
LED_1_COUNT = 12 # Number of LED pixels.
LED_1_PIN = 18 # GPIO pin connected to the pixels (must support PWM! GPIO 13 and 18 on RPi 3).
LED_1_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_1_DMA = 5 # DMA channel to use for generating signal (Between 1 and 14)
LED_1_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_1_INVERT = False # True to invert the signal (when using NPN transistor level shift)
LED_1_CHANNEL = 0 # 0 or 1
LED_1_STRIP = ws.WS2811_STRIP_GRB
LED_2_COUNT = 12 # Number of LED pixels.
LED_2_PIN = 13 # GPIO pin connected to the pixels (must support PWM! GPIO 13 or 18 on RPi 3).
LED_2_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_2_DMA = 10 # DMA channel to use for generating signal (Between 1 and 14)
LED_2_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_2_INVERT = False # True to invert the signal (when using NPN transistor level shift)
LED_2_CHANNEL = 1 # 0 or 1
LED_2_STRIP = ws.WS2811_STRIP_GRB
global strip1
global strip2
def multiColorWipe(color1, color2, wait_ms=5):
global strip1
global strip2
"""Wipe color across multiple LED strips a pixel at a time."""
for i in range(strip1.numPixels()):
if i % 2:
# even number
strip1.setPixelColor(i, color1)
strip2.setPixelColor(i / 2, color2)
strip1.show()
time.sleep(wait_ms/1000.0)
strip2.show()
time.sleep(wait_ms/1000.0)
else:
# odd number
strip1.setPixelColor(i, color1)
strip1.show()
time.sleep(wait_ms/1000.0)
time.sleep(1)
def blackout(strip):
for i in range(max(strip1.numPixels(), strip1.numPixels())):
strip.setPixelColor(i, Color(0,0,0))
strip.show()
def greenbreathwheel(pos):
"""Generate rainbow colors across 0-255 positions."""
if pos < 85:
return Color(255 - pos * 3, 0, 0)
elif pos < 170:
pos -= 85
return Color(pos * 3, 255 - pos * 3, 0)
else:
pos -= 170
return Color(0, pos * 3, 0)
def greenabreath(strip1, wait_ms=10):
"""Rainbow movie theater light style chaser animation."""
for j in range(0,120):
for q in range(1):
for i in range(0, strip1.numPixels(), 1):
strip1.setPixelColor(i+q, greenbreathwheel((i+j) % 255))
strip1.show()
time.sleep(wait_ms/500.0)
for i in range(0, strip1.numPixels(), 1):
strip1.setPixelColor(i+q, 0)
def greenabreath2(strip1, wait_ms=10):
"""Rainbow movie theater light style chaser animation."""
for j in range(135, 255):
for q in range(1):
for i in range(0, strip1.numPixels(), 1):
strip1.setPixelColor(i+q, greenbreathwheel((i-j) % 255))
strip1.show()
time.sleep(wait_ms/500.0)
for i in range(0, strip1.numPixels(), 1):
strip1.setPixelColor(i+q, 0)
def greenbreath(strip2, wait_ms=10):
"""Rainbow movie theater light style chaser animation."""
for j in range(0,120):
for q in range(1):
for i in range(0, strip2.numPixels(), 1):
strip2.setPixelColor(i+q, greenbreathwheel((i+j) % 255))
strip2.show()
time.sleep(wait_ms/500.0)
for i in range(0, strip2.numPixels(), 1):
strip1.setPixelColor(i+q, 0)
def greenbreath2(strip2, wait_ms=10):
"""Rainbow movie theater light style chaser animation."""
for j in range(135, 255):
for q in range(1):
for i in range(0, strip2.numPixels(), 1):
strip2.setPixelColor(i+q, greenbreathwheel((i-j) % 255))
strip2.show()
time.sleep(wait_ms/500.0)
for i in range(0, strip2.numPixels(), 1):
strip2.setPixelColor(i+q, 0)
def nightrider(strip1, color, wait_ms=35,iterations=1):
""" Knight raider kitt scanner """
for i in range(strip1.numPixels()):
strip1.setPixelColor(i,color)
strip1.setPixelColor(i-1,dimColor(color))
strip1.setPixelColor(i-2,dimColor2(color))
strip1.setPixelColor(i-3,dimColor3(color))
strip1.show()
time.sleep(wait_ms/2000.0)
strip1.setPixelColor(i,0)
strip1.setPixelColor(i-1,0)
# reverse the direction
for i in xrange(strip1.numPixels()-1,-1,-1):
strip1.setPixelColor(i,color)
strip1.setPixelColor(i+1,dimColor(color))
strip1.setPixelColor(i+2,dimColor2(color))
strip1.setPixelColor(i+3,dimColor3(color))
strip1.show()
time.sleep(wait_ms/2000.0)
strip1.setPixelColor(i,0)
strip1.setPixelColor(i+1,0)
def nightrider2(strip2, color, wait_ms=35,iterations=1):
""" Knight raider kitt scanner """
for i in range(strip2.numPixels()):
strip2.setPixelColor(i,color)
strip2.setPixelColor(i-1,dimColor(color))
strip2.setPixelColor(i-2,dimColor2(color))
strip2.setPixelColor(i-3,dimColor3(color))
strip2.show()
time.sleep(wait_ms/2000.0)
strip2.setPixelColor(i,0)
strip2.setPixelColor(i-1,0)
# reverse the direction
for i in xrange(strip1.numPixels()-1,-1,-1):
strip2.setPixelColor(i,color)
strip2.setPixelColor(i+1,dimColor(color))
strip2.setPixelColor(i+2,dimColor2(color))
strip2.setPixelColor(i+3,dimColor3(color))
strip2.show()
time.sleep(wait_ms/2000.0)
strip2.setPixelColor(i,0)
strip2.setPixelColor(i+1,0)
def nightrider3(strip1, color, wait_ms=35,iterations=1):
""" Knight raider kitt scanner """
# reverse the direction
for i in xrange(strip1.numPixels()-1,-1,-1):
strip1.setPixelColor(i,color)
strip1.setPixelColor(i+1,dimColor(color))
strip1.setPixelColor(i+2,dimColor2(color))
strip1.setPixelColor(i+3,dimColor3(color))
strip1.show()
time.sleep(wait_ms/1000.0)
strip1.setPixelColor(i,0)
strip1.setPixelColor(i+1,0)
def nightrider4(strip2, color, wait_ms=35,iterations=1):
""" Knight raider kitt scanner """
for i in xrange(strip1.numPixels()-1,-1,-1):
strip2.setPixelColor(i,color)
strip2.setPixelColor(i+1,dimColor(color))
strip2.setPixelColor(i+2,dimColor2(color))
strip2.setPixelColor(i+3,dimColor3(color))
strip2.show()
time.sleep(wait_ms/1000.0)
strip2.setPixelColor(i,0)
strip2.setPixelColor(i+1,0)
def nightrider5(strip1, color, wait_ms=35,iterations=1):
""" Knight raider kitt scanner """
for i in range(strip1.numPixels()):
strip1.setPixelColor(i,color)
strip1.setPixelColor(i-1,dimColor(color))
strip1.setPixelColor(i-2,dimColor2(color))
strip1.setPixelColor(i-3,dimColor3(color))
strip1.show()
time.sleep(wait_ms/1000.0)
strip1.setPixelColor(i,0)
strip1.setPixelColor(i-1,0)
def nightrider6(strip2, color, wait_ms=35,iterations=1):
""" Knight raider kitt scanner """
for i in range(strip2.numPixels()):
strip2.setPixelColor(i,color)
strip2.setPixelColor(i-1,dimColor(color))
strip2.setPixelColor(i-2,dimColor2(color))
strip2.setPixelColor(i-3,dimColor3(color))
strip2.show()
time.sleep(wait_ms/1000.0)
strip2.setPixelColor(i,0)
strip2.setPixelColor(i-1,0)
def night4(strip1, strip2):
for t in range (0, 1,1):
nightrider5 (strip1,Color(randint(0,255),randint(0,255),0),65)
for j in range (0, 1,1):
nightrider4 (strip2,Color(0,255,0),65)
for i in range (0, 1,1):
nightrider6 (strip2,Color(randint(0,255),randint(0,255),0),65)
for q in range (0, 1,1):
nightrider3 (strip1,Color(0,255,0),65)
def dimColor (color):
""" Color is an 32-bit int that merges the values into one """
return (((color&0xff0000)/3)&0xff0000) + (((color&0x00ff00)/3)&0x00ff00) + (((color&0x0000ff)/3)&0x0000ff)
def dimColor2 (color):
""" Color is an 32-bit int that merges the values into one """
return (((color&0xff0000)/100)&0xff0000) + (((color&0x00ff00)/100)&0x00ff00) + (((color&0x0000ff)/100)&0x0000ff)
def dimColor3 (color):
""" Color is an 32-bit int that merges the values into one """
return (((color&0xff0000)/randint(1,255))&0) + (((color&0x00ff00)/randint(1,255))&0) + (((color&0x0000ff)/randint(1,255))&0
def night(strip1):
for t in range (0, 1,1):
nightrider (strip1,Color(randint(0,255),randint(0,255),0),65)
for t in range (0, 1,1):
nightrider (strip1,Color(0,255,0),randint(65,125))
def night2(strip2):
for t in range (0, 1,1):
nightrider2 (strip2,Color(randint(0,255),randint(0,255),0),65)
for t in range (0, 1,1):
nightrider2 (strip2,Color(0,255,0),randint(65,125))
def night3(strip1, strip2):
for t in range (0, 1,1):
nightrider (strip1,Color(randint(0,255),randint(0,255),0),65)
for j in range (0, 1,1):
nightrider2 (strip2,Color(0,255,0),65)
# Main program logic follows:
if __name__ == '__main__':
# Create NeoPixel objects with appropriate configuration for each strip.
strip1 = Adafruit_NeoPixel(LED_1_COUNT, LED_1_PIN, LED_1_FREQ_HZ, LED_1_DMA, LED_1_INVERT, LED_1_BRIGHTNESS, LED_1_CHANNEL, LED_1_STRIP)
strip2 = Adafruit_NeoPixel(LED_2_COUNT, LED_2_PIN, LED_2_FREQ_HZ, LED_2_DMA, LED_2_INVERT, LED_2_BRIGHTNESS, LED_2_CHANNEL, LED_2_STRIP)
# Intialize the library (must be called once before other functions).
strip1.begin()
strip2.begin()
print ('Press Ctrl-C to quit.')
# Black out any LEDs that may be still on for the last run
blackout(strip1)
blackout(strip2)
while True:
night4(strip1, strip2)
blackout(strip1)
blackout(strip2)
greenabreath(strip1)
greenbreath(strip2)
greenabreath2(strip1)
greenbreath2(strip2)
multiColorWipe(Color(0, 0, 0), Color(0, 0, 0)) # bo wipe
greenabreath(strip1)
greenabreath2(strip1)
greenbreath(strip2)
greenbreath2(strip2)
multiColorWipe(Color(0, 0, 0), Color(0, 0, 0)) # bo wipe
greenabreath(strip1)
greenbreath(strip2)
greenabreath2(strip1)
greenbreath2(strip2)
multiColorWipe(Color(0, 0, 0), Color(0, 0, 0)) # bo wipe
blackout(strip1)
blackout(strip2)
night3(strip1, strip2)
blackout(strip1)
blackout(strip2)
night4(strip1, strip2)
blackout(strip1)
blackout(strip2)