Skip to content

Instantly share code, notes, and snippets.

@Atarity
Created October 16, 2012 17:24
Show Gist options
  • Save Atarity/3900703 to your computer and use it in GitHub Desktop.
Save Atarity/3900703 to your computer and use it in GitHub Desktop.
RPi SPI rainbow on Python
#!/usr/bin/python
# Light painting / POV demo for Raspberry Pi using
# Adafruit Digital Addressable RGB LED flex strip.
# ----> http://adafruit.com/products/306
import RPi.GPIO as GPIO, time, math
def fillStrip(strip, stripLength, gamma, t):
for x in range(stripLength):
r = int(math.sin((t+x)*0.05)*255)
g = int(math.cos((t+x)*0.05)*255)
b = int(math.cos((t+x+32)*0.05)*255)
x3 = x * 3
strip[x3] = gamma[r]
strip[x3+1] = gamma[g]
strip[x3+2] = gamma[b]
# Configurable values
stripLength = 64
# Open SPI device, load image in RGB format and get dimensions:
dev = "/dev/spidev0.0"
spidev = file(dev, "wb")
gamma = bytearray(256)
for i in range(256):
gamma[i] = 0x80 | int(pow(float(i) / 255.0, 2.5) * 127.0 + 0.5)
# 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)
# Then it's a trivial matter of writing each column to the SPI port.
print "Displaying..."
t = 0
while True:
fillStrip(strip, stripLength, gamma, t)
spidev.write(strip)
spidev.flush()
t += 1
time.sleep(0.05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment