import time, sys, inspect 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 # other settings SPI_PORT = 0 SPI_DEVICE = 0 ################################################################################ # DEFINE YOUR FUNCTIONS HERE ################################################################################ def setRGBColor(pixels, r=255, g=255, b=255): # cast from string to int r, g, b = int(r), int(g), int(b) pixels.set_pixels( Adafruit_WS2801.RGB_to_color( r, g, b ) ) pixels.show() def fadeInOut(pixels, r=255, g=255, b=255, period=10): r, g, b = int(r), int(g), int(b) timestep = 0.04 steps = int(float(period) / timestep) if period < timestep or steps < 2: pixels.set_pixels( Adafruit_WS2801.RGB_to_color( r, g, b ) ) pixels.show() sys.exit(0) while True: for i in [x for x in reversed(range(steps))] + [x for x in range(steps)]: r_t = int(i * (r / float(steps-1))) g_t = int(i * (g / float(steps-1))) b_t = int(i * (b / float(steps-1))) pixels.set_pixels( Adafruit_WS2801.RGB_to_color( r_t, g_t, b_t ) ) pixels.show() time.sleep(timestep) ################################################################################ FUNCTIONS = [obj for name,obj in inspect.getmembers(sys.modules[__name__]) if (inspect.isfunction(obj))] FUNCTION_NAMES = [f.__name__ for f in FUNCTIONS] if __name__ == "__main__": if len(sys.argv) < 2: sys.exit(1) cmdargs = sys.argv[1:] if cmdargs[0] == "getfunctions": print(FUNCTION_NAMES) else: # Init background script pixels = Adafruit_WS2801.WS2801Pixels(PIXEL_COUNT, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE), gpio=GPIO) # Clear all the pixels to turn them off. pixels.clear() func = cmdargs[0] if func in FUNCTION_NAMES: pos = FUNCTION_NAMES.index(func) args = cmdargs[1:] # call function FUNCTIONS[pos](pixels, *args) else: print("ERROR: %s not defined in FUNCTIONS list" % func) sys.exit(1)