#!/usr/bin/python
'''
Raspberry PI LED strip controller.
This uses pi-blaster software PWM to control the GPIO pins.
https://github.com/sarfata/pi-blaster
'''
''' Import stuff '''
import sys
import os
import time
''' Global variables '''
# GPIO pins for the LED strip channels
gpio_r = 23;
gpio_g = 24;
gpio_b = 25;
# Brightness 0-100%
brightness = 100.0;
# Delay 0-1000ms
delay = 10;
speed = 10;
# Predefined colors
color = "ff00ff";
colors1 = "ff0000", "ffff00", "00ff00", "00ffff", "0000ff", "ff00ff";
colors2 = "ff1010", "ffff10", "10ff10", "10ffff", "1010ff", "ff10ff";
''' Color format conversion '''
# Converts hex to rgb array
def hex_to_rgb(value):
value = value.lstrip("#");
lv = len(value);
return tuple(int(value[i:i+lv/3], 16) for i in range(0, lv, lv/3));
# Converts rgb array to hex
def rgb_to_hex(rgb):
rgb = eval(rgb)
r = rgb[0]
g = rgb[1]
b = rgb[2]
return '#%02X%02X%02X' % (r,g,b)
# Sets the led strip color
def setcolor(rgb):
pwm_r = "echo %d=%f > /dev/pi-blaster" % (gpio_r, (rgb[0] / 255.0) * (brightness / 100.0));
os.system(pwm_r);
pwm_g = "echo %d=%f > /dev/pi-blaster" % (gpio_g, (rgb[1] / 255.0) * (brightness / 100.0));
os.system(pwm_g);
pwm_b = "echo %d=%f > /dev/pi-blaster" % (gpio_b, (rgb[2] / 255.0) * (brightness / 100.0));
os.system(pwm_b);
time.sleep(delay / 1000.0);
# Helper to set the color with a hex value
def setcolor_hex(hexcolor):
setcolor(hex_to_rgb(hexcolor));
''' Color manipulation '''
# Shifts the color from one value to the next
def shiftcolor(rgb1, rgb2):
rdiff = (rgb1[0] - rgb2[0]) * 1.0;
gdiff = (rgb1[1] - rgb2[1]) * 1.0;
bdiff = (rgb1[2] - rgb2[2]) * 1.0;
newcolor = rgb1;
speedmult = 1000 / speed;
for i in range(0, speedmult, 1):
newcolor = int(rgb1[0] - ((rdiff / speedmult) * i)), int(rgb1[1] - ((gdiff / speedmult) * i)), int(rgb1[2] - ((bdiff / speedmult) * i));
setcolor(newcolor);
# Shifts the color in hex
def shiftcolor_hex(hex1, hex2):
shiftcolor(hex_to_rgb(hex1), hex_to_rgb(hex2));
''' Modes '''
# Flashes the color on and off
def flash(hexcolor):
setcolor_hex(hexcolor);
time.sleep(10 / speed);
setcolor_hex("0000");
time.sleep(10 / speed);
# Flashes between the colors in the colors array
def strobe(colors):
for i in range(len(colors)):
setcolor_hex(colors[i]);
time.sleep(10 / speed);
# Sigle color breathe mode
def breathe(hexcolor):
shiftcolor_hex(hexcolor, "0000");
shiftcolor_hex("0000", hexcolor);
# Multi color breathe mode
def multi_breathe(colors):
for i in range(len(colors)):
shiftcolor_hex("0000", colors[i]);
shiftcolor_hex(colors[i], "0000");
# Cycles smoothly between the colors in the colors array
def cycle(colors):
for i in range(len(colors)):
if(i < (len(colors) - 1)):
shiftcolor_hex(colors[i], colors[i + 1]);
else:
shiftcolor_hex(colors[i], colors[0]);
''' Main program '''
setcolor_hex("000000");
print("Small demo of the Raspberry PI");
print("RGB LED strip controller I made");
print("");
time.sleep(2);
print("Single color flash");
flash(color);
flash(color);
print("Strobe");
strobe(colors2);
print("Single color breathe");
breathe(color);
breathe(color);
print("Multi color breathe");
multi_breathe(colors2);
print("Rainbow color cycle");
cycle(colors2);
while True:
cycle(colors2);