From 1ed62094657a55308631e10055471e9d6fcbd53a Mon Sep 17 00:00:00 2001 From: Lucca Ketterer Date: Sat, 6 Jun 2020 21:50:43 +0200 Subject: [PATCH] add examples --- server/example/demo.py | 101 ++++++++++++++++++++++++++++++++++++++ server/example/rainbow.py | 46 +++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 server/example/demo.py create mode 100644 server/example/rainbow.py diff --git a/server/example/demo.py b/server/example/demo.py new file mode 100644 index 0000000..c4b4d48 --- /dev/null +++ b/server/example/demo.py @@ -0,0 +1,101 @@ +# Simple demo of of the WS2801/SPI-like addressable RGB LED lights. +import time +import RPi.GPIO as GPIO + +# Import the WS2801 module. +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) + +# Define rainbow cycle function to do a cycle of all hues. +def rainbow_cycle_successive(pixels, wait=0.1): + for i in range(pixels.count()): + # tricky math! we use each pixel as a fraction of the full 96-color wheel + # (thats the i / strip.numPixels() part) + # Then add in j which makes the colors go around per pixel + # the % 96 is to make the wheel cycle around + pixels.set_pixel(i, wheel(((i * 256 // pixels.count())) % 256) ) + pixels.show() + if wait > 0: + time.sleep(wait) + +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 rainbow_colors(pixels, wait=0.05): + 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(((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) + +def blink_color(pixels, blink_times=5, wait=0.5, color=(255,0,0)): + for i in range(blink_times): + # blink two times, then wait + pixels.clear() + for j in range(2): + for k in range(pixels.count()): + pixels.set_pixel(k, Adafruit_WS2801.RGB_to_color( color[0], color[1], color[2] )) + pixels.show() + time.sleep(0.08) + pixels.clear() + pixels.show() + time.sleep(0.08) + time.sleep(wait) + +def appear_from_back(pixels, color=(255, 0, 0)): + pos = 0 + for i in range(pixels.count()): + for j in reversed(range(i, pixels.count())): + pixels.clear() + # first set all pixels at the begin + for k in range(i): + pixels.set_pixel(k, Adafruit_WS2801.RGB_to_color( color[0], color[1], color[2] )) + # set then the pixel at position j + pixels.set_pixel(j, Adafruit_WS2801.RGB_to_color( color[0], color[1], color[2] )) + pixels.show() + time.sleep(0.02) + + +if __name__ == "__main__": + while True: + rainbow_colors(pixels, wait=0.3) + diff --git a/server/example/rainbow.py b/server/example/rainbow.py new file mode 100644 index 0000000..9f9bb95 --- /dev/null +++ b/server/example/rainbow.py @@ -0,0 +1,46 @@ +import time +import RPi.GPIO as GPIO + +import Adafruit_WS2801 +import Adafruit_GPIO.SPI as SPI + +PIXEL_COUNT = 32 + +SPI_PORT = 0 +SPI_DEVICE = 0 +pixels = Adafruit_WS2801.WS2801Pixels(PIXEL_COUNT, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE), gpio=GPIO) + +def set_color(r, g, b): + for i in range(pixels.count()): + pixels.set_pixel(i, Adafruit_WS2801.RGB_to_color(r,g,b)) + pixels.show() + +def clear_pixels(): + pixels.clear() + pixels.show() + +def main(): + DELAY = 0.1 + r = 255 + g = 0 + b = 0 + + while True: + while r > 0: + r = r-1 + g = g+1 + set_color(r,g,b) + time.sleep(DELAY) + while g > 0: + g = g-1 + b = b+1 + set_color(r,g,b) + time.sleep(DELAY) + while b > 0: + b = b-1 + r = r+1 + set_color(r,g,b) + time.sleep(DELAY) + +if __name__ == "__main__": + main()