Created
March 19, 2017 13:43
-
-
Save raspberrytipsnl/96bdc53ee9e1e84efd5b682a9cb79f44 to your computer and use it in GitHub Desktop.
WS2801 ledstrip demo
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
# https://raspberrytips.nl/ws2801-ledstrip/ | |
# Simple demo script for a WS2801/SPI-like addressable RGB LED lightstrip. | |
import time | |
import RPi.GPIO as GPIO | |
import Adafruit_WS2801 | |
import Adafruit_GPIO.SPI as SPI | |
# Configure the count of pixels: | |
PIXEL_COUNT = 32 | |
# Alternatively specify a hardware SPI connection on /dev/spidev0.0: | |
SPI_PORT = 0 | |
SPI_DEVICE = 0 | |
pixels = Adafruit_WS2801.WS2801Pixels(PIXEL_COUNT, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE), gpio=GPIO) | |
# Define the wheel function to interpolate between different hues. | |
def wheel(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 rainbow_cycle(pixels, wait=0.005): | |
for j in range(256): # one cycle of all 256 colors in the wheel | |
for i in range(pixels.count()): | |
pixels.set_pixel(i, wheel(((i * 256 // pixels.count()) + j) % 256) ) | |
pixels.show() | |
if wait > 0: | |
time.sleep(wait) | |
def brightness_decrease(pixels, wait=0.01, step=1): | |
for j in range(int(256 // step)): | |
for i in range(pixels.count()): | |
r, g, b = pixels.get_pixel_rgb(i) | |
r = int(max(0, r - step)) | |
g = int(max(0, g - step)) | |
b = int(max(0, b - step)) | |
pixels.set_pixel(i, Adafruit_WS2801.RGB_to_color( r, g, b )) | |
pixels.show() | |
if wait > 0: | |
time.sleep(wait) | |
if __name__ == "__main__": | |
# Clear all the pixels to turn them off. | |
pixels.clear() | |
pixels.show() # Make sure to call show() after changing any pixels! | |
rainbow_cycle(pixels, wait=0.02) | |
brightness_decrease(pixels) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment