Created
January 11, 2013 22:59
-
-
Save b1nary/4514690 to your computer and use it in GitHub Desktop.
Playing with my raspberry & some adafruit RGB Pixels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Color control for http://www.adafruit.com/blog/2011/07/18/updated-product-12mm-diffused-digital-rgb-led-pixels-strand-of-25/ | |
# based on this gist: https://gist.github.com/3900703 | |
# based on Adafruits examples | |
import RPi.GPIO as GPIO, time, math | |
# Configurable values | |
stripLength = 25 | |
colorList = [ | |
[255,000,000], | |
[255,165,000], | |
[255,255,000], | |
[000,128,000], | |
[000,000,255], | |
[075,000,130], | |
[238,130,238] | |
] | |
curColor = 1 | |
modifier = 1 | |
# rainbow function | |
def rainbowFillStrip(strip): | |
global stripLength, curColor | |
for x in range(stripLength): | |
x3 = x * 3 | |
strip[x3] = colorList[curColor][0] | |
strip[x3+1] = colorList[curColor][1] | |
strip[x3+2] = colorList[curColor][2] | |
curColor = curColor + 1 | |
if curColor >= len(colorList): | |
curColor = 0 | |
# signal | |
def signalFillStrip(strip, color): | |
global stripLength, curColor, modifier | |
for x in range(stripLength): | |
x3 = x * 3 | |
strip[x3] = 0 | |
strip[x3+1] = 0 | |
strip[x3+2] = 0 | |
cur3 = curColor * 3 | |
strip[cur3] = color[0] | |
strip[cur3+1] = color[1] | |
strip[cur3+2] = color[2] | |
curColor = curColor + modifier | |
if curColor >= stripLength-3: | |
modifier = -1 | |
if curColor <= 0: | |
modifier = 1 | |
def justFill(strip, color): | |
global sripLength | |
for x in range(stripLength): | |
x3 = x * 3 | |
strip[x3] = color[0] | |
strip[x3+1] = color[1] | |
strip[x3+2] = color[2] | |
# Open SPI device, load image in RGB format and get dimensions: | |
dev = "/dev/spidev0.0" | |
spidev = file(dev, "wb") | |
# Create list of bytearrays, one for each column of image. | |
# R, G, B byte per pixel, plus extra '0' byte at end for latch. | |
print "Allocating..." | |
strip = bytearray(stripLength*3 + 1) | |
print "Displaying..." | |
i = 0 | |
while True: | |
rainbowFillStrip(strip) | |
spidev.write(strip) | |
spidev.flush() | |
time.sleep(1.05) | |
i = i + 1 | |
if i > 4: | |
break | |
i = 0 | |
while True: | |
rainbowFillStrip(strip) | |
spidev.write(strip) | |
spidev.flush() | |
time.sleep(0.25) | |
i = i + 1 | |
if i > 10: | |
break | |
i = 0 | |
while True: | |
rainbowFillStrip(strip) | |
spidev.write(strip) | |
spidev.flush() | |
time.sleep(0.05) | |
i = i + 1 | |
if i > 40: | |
break | |
i = 0 | |
curColor = 0 | |
while True: | |
signalFillStrip(strip, [40,240,0]) | |
spidev.write(strip) | |
spidev.flush() | |
time.sleep(0.05) | |
i = i + 1 | |
if i > 70: | |
break | |
i = 0 | |
curColor = 0 | |
while True: | |
signalFillStrip(strip, [0,140,240]) | |
spidev.write(strip) | |
spidev.flush() | |
time.sleep(0.02) | |
i = i + 1 | |
if i > 120: | |
break | |
i = 0 | |
curColor = 0 | |
while True: | |
signalFillStrip(strip, [240,40,0]) | |
spidev.write(strip) | |
spidev.flush() | |
time.sleep(0.01) | |
i = i + 1 | |
if i > 120: | |
break | |
i = 0 | |
while True: | |
justFill(strip, [i,i,i]) | |
spidev.write(strip) | |
spidev.flush() | |
time.sleep(0.01) | |
i = i + 1 | |
if i > 255: | |
break | |
i = 0 | |
while True: | |
justFill(strip, [255-i,255-i,255-i]) | |
spidev.write(strip) | |
spidev.flush() | |
time.sleep(0.01) | |
i = i + 1 | |
if i > 255: | |
break | |
i = 0 | |
while True: | |
justFill(strip, [0,i,i/2]) | |
spidev.write(strip) | |
spidev.flush() | |
time.sleep(0.01) | |
i = i + 1 | |
if i > 255: | |
break | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment