|
| 1 | +# Simple demo of of the WS2801/SPI-like addressable RGB LED lights. |
| 2 | +# Will animate a rainbow color cycle on all pixels. |
| 3 | +# Author: Tony DiCola |
| 4 | +# License: Public Domain |
| 5 | +from __future__ import division |
| 6 | +import time |
| 7 | + |
| 8 | +# Import the WS2801 module. |
| 9 | +import Adafruit_WS2801 |
| 10 | +import Adafruit_GPIO.SPI as SPI |
| 11 | + |
| 12 | +# Configure the count of pixels: |
| 13 | +PIXEL_COUNT = 10 |
| 14 | + |
| 15 | +# The WS2801 library makes use of the BCM pin numbering scheme. See the README.md for details. |
| 16 | + |
| 17 | +# Specify a software SPI connection for Raspberry Pi on the following pins: |
| 18 | +PIXEL_CLOCK = 18 |
| 19 | +PIXEL_DOUT = 23 |
| 20 | +pixels = Adafruit_WS2801.WS2801Pixels(PIXEL_COUNT, clk=PIXEL_CLOCK, do=PIXEL_DOUT) |
| 21 | + |
| 22 | +# Alternatively specify a hardware SPI connection on /dev/spidev0.0: |
| 23 | +#SPI_PORT = 0 |
| 24 | +#SPI_DEVICE = 0 |
| 25 | +#pixels = Adafruit_WS2801.WS2801Pixels(PIXEL_COUNT, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE)) |
| 26 | + |
| 27 | +# Clear all the pixels to turn them off. |
| 28 | +pixels.clear() |
| 29 | +pixels.show() # Make sure to call show() after changing any pixels! |
| 30 | + |
| 31 | +# Define the wheel function to interpolate between different hues. |
| 32 | +def wheel(pos): |
| 33 | + if pos < 85: |
| 34 | + return Adafruit_WS2801.RGB_to_color(pos * 3, 255 - pos * 3, 0) |
| 35 | + elif pos < 170: |
| 36 | + pos -= 85 |
| 37 | + return Adafruit_WS2801.RGB_to_color(255 - pos * 3, 0, pos * 3) |
| 38 | + else: |
| 39 | + pos -= 170 |
| 40 | + return Adafruit_WS2801.RGB_to_color(0, pos * 3, 255 - pos * 3) |
| 41 | + |
| 42 | +# Define rainbow cycle function to do a cycle of all hues. |
| 43 | +def rainbow_cycle(pixels, wait=0): |
| 44 | + for j in range(256): # one cycle of all 256 colors in the wheel |
| 45 | + for i in range(pixels.count()): |
| 46 | + # tricky math! we use each pixel as a fraction of the full 96-color wheel |
| 47 | + # (thats the i / strip.numPixels() part) |
| 48 | + # Then add in j which makes the colors go around per pixel |
| 49 | + # the % 96 is to make the wheel cycle around |
| 50 | + pixels.set_pixel(i, wheel(((i * 256 // pixels.count()) + j) % 256) ) |
| 51 | + pixels.show() |
| 52 | + if wait > 0: |
| 53 | + time.sleep(wait) |
| 54 | + |
| 55 | +print('Rainbow cycling, press Ctrl-C to quit...') |
| 56 | +while True: |
| 57 | + rainbow_cycle(pixels) |
0 commit comments