-
Notifications
You must be signed in to change notification settings - Fork 5
/
ledmorpher.py
333 lines (310 loc) · 14.5 KB
/
ledmorpher.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/python
# Raspberry Pi and LED Strip lighting -- morphs between any two colours.
# It uses the LED Strip Python library for Adafruit\'s LPD8806 LED strips.
# http://www.adafruit.com/products/306
# You will need this "ledstrip" Python library to run this code:
# https://github.com/labatrockwell/raspberrypi-experiments/tree/ledstrip_v0.0.2
# Based on the library example code provided on this page:
# http://julioterra.com/journal/2013/02/raspberry-pi-and-led-strips-new-python-library/
from ledStrip import ledstrip
import time
import random
import argparse
import re
# Define app description and optional parameters
parser = argparse.ArgumentParser(description='A "LED Strip Light Show" made with Raspberry Pi.')
# Define the leds strip length optional parameter
parser.add_argument('-l', '--leds', '--pixels',
nargs=1, type=int, default=32,
help='length of led strip in leds, or pixels')
# read all command line parameters
args = parser.parse_args()
# Function for taking user's input on colour choices.
def choose_a_colour(userPalette, userDefinedColour, userRed, userGreen, userBlue):
userDefinedColour = raw_input('Colour? (clear, yellow, orange, red, purple, blue, lightblue, green, lightgreen, white, pink, random/r): ')
if (userDefinedColour == 'red'):
userRed = userPalette[0][0]
userGreen = userPalette[0][1]
userBlue = userPalette[0][2]
elif (userDefinedColour == 'green'):
userRed = userPalette[1][0]
userGreen = userPalette[1][1]
userBlue = userPalette[1][2]
elif (userDefinedColour == 'blue'):
userRed = userPalette[2][0]
userGreen = userPalette[2][1]
userBlue = userPalette[2][2]
elif (userDefinedColour == 'yellow'):
userRed = userPalette[3][0]
userGreen = userPalette[3][1]
userBlue = userPalette[3][2]
elif (userDefinedColour == 'purple'):
userRed = userPalette[4][0]
userGreen = userPalette[4][1]
userBlue = userPalette[4][2]
elif (userDefinedColour == 'orange'):
userRed = userPalette[5][0]
userGreen = userPalette[5][1]
userBlue = userPalette[5][2]
elif (userDefinedColour == 'white'):
userRed = userPalette[6][0]
userGreen = userPalette[6][1]
userBlue = userPalette[6][2]
elif (userDefinedColour == 'pink'):
userRed = userPalette[7][0]
userGreen = userPalette[7][1]
userBlue = userPalette[7][2]
elif (userDefinedColour == 'lightblue'):
userRed = userPalette[8][0]
userGreen = userPalette[8][1]
userBlue = userPalette[8][2]
elif (userDefinedColour == 'lightgreen'):
userRed = userPalette[9][0]
userGreen = userPalette[9][1]
userBlue = userPalette[9][2]
elif (userDefinedColour == 'clear'):
userRed = 0
userGreen = 0
userBlue = 0
elif (userDefinedColour=='random' or userDefinedColour=='r'):
userRed = random.randint(0,127)
userGreen = random.randint(0,127)
userBlue = random.randint(0,127)
print 'Random colour -- Red:',userRed,' Green:',userGreen,' Blue:',userBlue
else:
print 'Colour not found'
#print "Returning values:"
#print "userDefinedColour: ", userDefinedColour
#print " userRed:", userRed, " userGreen:", userGreen, " userBlue:", userBlue
morphList = []
morphList.append(userDefinedColour)
morphList.append(userRed)
morphList.append(userGreen)
morphList.append(userBlue)
return morphList # userDefinedColour, userRed, userGreen, userBlue
def colour_Calculator(morphStartColour, morphEndColour):
print "Calculating colour range: "
if morphStartColour == 0: morphStartColour = 1 #Calculations require nonzero
if morphEndColour == 0: morphEndColour = 1 #Calculations require nonzero
if morphStartColour > morphEndColour:
colourRange = morphStartColour - morphEndColour
colourPolarity = 0
if morphStartColour < morphEndColour:
colourRange = morphEndColour - morphStartColour
colourPolarity = 1
if morphStartColour == morphEndColour:
colourRange = 0
colourPolarity = 0
colourStepValue = colourRange / 8
print "Colour range value is: ", colourRange
print "Colour step value is: ", colourStepValue
print "Colour polarity is: ", colourPolarity
colourList = []
colourList.append(colourRange)
colourList.append(colourPolarity)
colourList.append(colourStepValue)
return colourList # colourRange, colourPolarity, colourStepValue
def morph_sequence(leds, morphStartColour, morphEndColour, morphTransitionColour, redPolarity, redStepValue, greenPolarity, greenStepValue, bluePolarity, blueStepValue):
# Morphing
morphTransitionColour = morphStartColour
# Showing initial colour
for each in range(32):
leds.setPixelColorRGB(pixel=each, red=morphStartColour[1], green=morphStartColour[2], blue=morphStartColour[3])
leds.show()
print "%d, %d, %d" % (morphStartColour[1], morphStartColour[2], morphStartColour[3])
time.sleep(0.1)
# Iterating through morph values 2-8 (of 10)
for i in range (1,8):
if redPolarity == 1:
morphTransitionColour[1] = (morphStartColour[1] + redStepValue)
if redPolarity == 0:
morphTransitionColour[1] = (morphStartColour[1] - redStepValue)
if greenPolarity == 1:
morphTransitionColour[2] = (morphStartColour[2] + greenStepValue)
if greenPolarity == 0:
morphTransitionColour[2] = (morphStartColour[2] - greenStepValue)
if bluePolarity == 1:
morphTransitionColour[3] = (morphStartColour[3] + blueStepValue)
if bluePolarity == 0:
morphTransitionColour[3] = (morphStartColour[3] - blueStepValue)
print "%d, %d, %d" % (morphTransitionColour[1], morphTransitionColour[2], morphTransitionColour[3])
for each in range(32):
leds.setPixelColorRGB(pixel=each, red=morphTransitionColour[1], green=morphTransitionColour[2], blue=morphTransitionColour[3])
leds.show()
time.sleep(0.1)
# Setting end colour
for each in range(32):
leds.setPixelColorRGB(pixel=each, red=morphEndColour[1], green=morphEndColour[2], blue=morphEndColour[3])
leds.show()
print "%d, %d, %d" % (morphEndColour[1], morphEndColour[2], morphEndColour[3])
time.sleep(0.1)
return morphEndColour
def morph_backwards(leds, morphStartColour, morphEndColour, morphTransitionColour, redPolarity, redStepValue, greenPolarity, greenStepValue, bluePolarity, blueStepValue):
# Morphing
morphTransitionColour = morphEndColour
# Showing initial colour
for each in range(32):
leds.setPixelColorRGB(pixel=each, red=morphEndColour[1], green=morphEndColour[2], blue=morphEndColour[3])
leds.show()
print "%d, %d, %d" % (morphEndColour[1], morphEndColour[2], morphEndColour[3])
time.sleep(0.1)
# Iterating through morph values 2-8 (of 10)
for i in range (1,8):
if redPolarity == 1:
morphTransitionColour[1] = (morphEndColour[1] + redStepValue)
if redPolarity == 0:
morphTransitionColour[1] = (morphEndColour[1] - redStepValue)
if greenPolarity == 1:
morphTransitionColour[2] = (morphEndColour[2] + greenStepValue)
if greenPolarity == 0:
morphTransitionColour[2] = (morphEndColour[2] - greenStepValue)
if bluePolarity == 1:
morphTransitionColour[3] = (morphEndColour[3] + blueStepValue)
if bluePolarity == 0:
morphTransitionColour[3] = (morphEndColour[3] - blueStepValue)
print "%d, %d, %d" % (morphTransitionColour[1], morphTransitionColour[2], morphTransitionColour[3])
for each in range(32):
leds.setPixelColorRGB(pixel=each, red=morphTransitionColour[1], green=morphTransitionColour[2], blue=morphTransitionColour[3])
leds.show()
time.sleep(0.1)
# Setting end colour
for each in range(32):
leds.setPixelColorRGB(pixel=each, red=morphStartColour[1], green=morphStartColour[2], blue=morphStartColour[3])
leds.show()
print "%d, %d, %d" % (morphStartColour[1], morphStartColour[2], morphStartColour[3])
time.sleep(0.1)
return morphStartColour
# function that initializes all the things
def main():
# initialize spi and leds objects
spidev = file("/dev/spidev0.0", "wb") # ref to spi connection to the led bar
leds = ledstrip.LEDStrip(pixels=args.leds, spi=spidev)
pixel_edge = 0 # current pixel whose state will be flipped
turn_on = False # holds whether pixel will be switched on or off
red_value = 127
green_value = 127
blue_value = 0
# Blank out the strip to begin.
for each in range(32):
leds.setPixelColorRGB(pixel=each, red=0, green=0, blue=0)
leds.show()
# Blink one green light to show boot-up success
for each in range(1):
leds.setPixelColorRGB(pixel=1, red=0, green=127, blue=0)
leds.show()
time.sleep(1)
leds.setPixelColorRGB(pixel=1, red=0, green=0, blue=0)
leds.show()
time.sleep(1)
#Defining a bunch of variables for colour transitions
userDefinedPixel = 'all'
userDefinedPixelInt = 0
userDefinedColour = 'blank'
userDefinedCommand = 'blank'
morphStartColour = []
morphEndColour = []
morphTransitionColour = []
colourList = []
redRange = 0
redStepValue = 0
redPolarity = 0
greenRange = 0
greenStepValue = 0
greenPolarity = 0
blueRange = 0
blueStepValue = 0
bluePolarity = 0
userRed = 0
userGreen = 0
userBlue = 0
userPalette = [[127,0,0],[0,127,0],[0,0,127],[105,50,17],[37,8,92],[83,46,0],[127,127,127],[127,7,29],[10,26,68],[36,74,0]]
#red, green, blue, yellow, purple, orange, white, pink
while (True):
userDefinedCommand = raw_input('Command? (m to morph, b to bounce, x to exit): ')
if (userDefinedCommand=='x'): break
if (userDefinedCommand=='m'):
print "Morphing."
elif (userDefinedCommand=='b'):
print "Bouncing."
else:
print "Couldn't understand your command."
break
if not re.search('\d+', userDefinedCommand):
pass # 'No numbers in command'
# Capturing the user's choice of colours
print "Choose a starting colour."
morphStartColour = choose_a_colour(userPalette, userDefinedColour, userRed, userGreen, userBlue)
#print "Printing the returned values:"
#for i in morphStartColour: print i
print "Choose an ending colour."
morphEndColour = choose_a_colour(userPalette, userDefinedColour, userRed, userGreen, userBlue)
#print "Printing the returned values:"
#for i in morphEndColour: print i
if (userDefinedCommand=='m'):
print "Morphing from", morphStartColour[0], "to", morphEndColour[0]
if (userDefinedCommand=='b'):
print "Bouncing from", morphStartColour[0], "to", morphEndColour[0]
# Blanking old variables from the last colour set
redStepValue = 0
redRange = 0
greenStepValue = 0
greenRange = 0
blueStepValue = 0
blueRange = 0
# Running caclulations for the colour morph values
print "Red spectrum."
colourList = colour_Calculator(morphStartColour[1], morphEndColour[1])
redRange = colourList[0]
redPolarity = colourList[1]
redStepValue = colourList[2]
print "Green spectrum."
colourList = colour_Calculator(morphStartColour[2], morphEndColour[2])
greenRange = colourList[0]
greenPolarity = colourList[1]
greenStepValue = colourList[2]
print "Blue spectrum."
colourList = colour_Calculator(morphStartColour[3], morphEndColour[3])
blueRange = colourList[0]
bluePolarity = colourList[1]
blueStepValue = colourList[2]
# Morphing
if (userDefinedCommand=='m'):
morph_sequence(leds, morphStartColour, morphEndColour, morphTransitionColour, redPolarity, redStepValue, greenPolarity, greenStepValue, bluePolarity, blueStepValue)
# Bouncing
if (userDefinedCommand=='b'):
while (True):
morph_sequence(leds, morphStartColour, morphEndColour, morphTransitionColour, redPolarity, redStepValue, greenPolarity, greenStepValue, bluePolarity, blueStepValue)
morph_backwards(leds, morphStartColour, morphEndColour, morphTransitionColour, redPolarity, redStepValue, greenPolarity, greenStepValue, bluePolarity, blueStepValue)
"""morphInterim = []
morphTransitionColour = []
morphInterim = morphEndColour
morphEndColour = morphStartColour
morphStartColour = morphInterim
# Blanking old variables from the last colour set
redStepValue = 0
redRange = 0
greenStepValue = 0
greenRange = 0
blueStepValue = 0
blueRange = 0
# Running caclulations for the colour morph values
print "Red spectrum."
colourList = colour_Calculator(morphStartColour[1], morphEndColour[1])
redRange = colourList[0]
redPolarity = colourList[1]
redStepValue = colourList[2]
print "Green spectrum."
colourList = colour_Calculator(morphStartColour[2], morphEndColour[2])
greenRange = colourList[0]
greenPolarity = colourList[1]
greenStepValue = colourList[2]
print "Blue spectrum."
colourList = colour_Calculator(morphStartColour[3], morphEndColour[3])
blueRange = colourList[0]
bluePolarity = colourList[1]
blueStepValue = colourList[2]
morphTransitionColour = morphStartColour
morph_sequence(leds, morphStartColour, morphEndColour, morphTransitionColour, redPolarity, redStepValue, greenPolarity, greenStepValue, bluePolarity, blueStepValue)
"""
if __name__ == "__main__":
main()