197 lines
5.4 KiB
Python
Executable File
197 lines
5.4 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
import os, sys, signal, random, time, re
|
|
from fnordlicht import Fnordlicht
|
|
|
|
# TODO: fifo loeschen wenn er schon existiert
|
|
fifoname = '/var/run/illuminatus'
|
|
interface = '/dev/ttyUSB0'
|
|
verbose = False
|
|
|
|
|
|
def showOn(fnordlicht):
|
|
print '>on<'
|
|
showColorwheel1(fnordlicht)
|
|
|
|
def showOff(fnordlicht):
|
|
print '>off<'
|
|
fnordlicht.stop()
|
|
fnordlicht.fade_hsv(0, 0, 0, 1, 1)
|
|
|
|
def showColorwheel1(fnordlicht):
|
|
print '>colorwheel1<'
|
|
fnordlicht.stop()
|
|
fnordlicht.program_colorwheel(1, 1, 1, 0, 60, 255, 255, 255)
|
|
|
|
def showColorwheel2(fnordlicht):
|
|
print '>colorwheel2<'
|
|
fnordlicht.stop()
|
|
fnordlicht.program_colorwheel(1, 1, 1, 0, -60, 255, 255, 255)
|
|
|
|
def showThunderstorm(fnordlicht):
|
|
|
|
def flash(fnordlicht):
|
|
power = random.randint(200, 255)
|
|
if verbose:
|
|
print 'flash power %03d, ' % power,
|
|
fnordlicht.fade_hsv(0, 0, power, 255, 1)
|
|
time.sleep(0.1)
|
|
fnordlicht.fade_hsv(240, 100, 40, 15, 1)
|
|
if (random.random() < 0.2):
|
|
time.sleep(random.uniform(0.05, 0.2))
|
|
flash(fnordlicht)
|
|
|
|
print '>thunderstorm<'
|
|
fnordlicht.stop()
|
|
while True:
|
|
flash(fnordlicht)
|
|
sleep = random.randint(1, 20)
|
|
if verbose:
|
|
print 'sleeping for %02d seconds' % sleep
|
|
time.sleep(sleep)
|
|
|
|
|
|
def showFire(fnordlicht):
|
|
|
|
def flicker(fnordlicht):
|
|
color = random.randint(30, 35)
|
|
top = random.randint(200, 255)
|
|
speed = random.randint(1, 5)
|
|
if verbose:
|
|
print 'flicker %03d %03d@%02d' % (color, top, speed)
|
|
fnordlicht.fade_hsv(color, 255, top, speed, 1)
|
|
|
|
print '>fire<'
|
|
fnordlicht.stop()
|
|
while True:
|
|
flicker(fnordlicht)
|
|
time.sleep(random.uniform(0, 0.3))
|
|
|
|
def modifyDecLight(fnordlicht):
|
|
print '>light--<'
|
|
fnordlicht.modify_current(0, 0, 0, 0, 0, 0, 0, -10)
|
|
|
|
def modifyIncLight(fnordlicht):
|
|
print '>light++<'
|
|
fnordlicht.modify_current(0, 0, 0, 0, 0, 0, 0, 10)
|
|
|
|
def modifyDecSpeed(fnordlicht):
|
|
print '>speed--<'
|
|
fnordlicht.modify_current(-1, 0, 0, 0, 0, 0, 0, 0)
|
|
|
|
def modifyIncSpeed(fnordlicht):
|
|
print '>speed++<'
|
|
fnordlicht.modify_current(1, 0, 0, 0, 0, 0, 0, 0)
|
|
|
|
def modifyDecSaturation(fnordlicht):
|
|
print '>saturation--<'
|
|
fnordlicht.modify_current(0, 0, 0, 0, 0, 0, -10, 0)
|
|
|
|
def modifyIncSaturation(fnordlicht):
|
|
print '>saturation++<'
|
|
fnordlicht.modify_current(0, 0, 0, 0, 0, 0, 10, 0)
|
|
|
|
def showFadeFromTo(fnordlicht, hueFrom, hueTo):
|
|
delay = 5
|
|
fnordlicht.stop()
|
|
while True:
|
|
fnordlicht.fade_hsv(hueFrom, 255, 255, 1, delay)
|
|
time.sleep(delay * 2)
|
|
fnordlicht.fade_hsv(hueTo, 255, 255, 1, delay)
|
|
time.sleep(delay * 2)
|
|
|
|
def showFadeRedGreen(fnordlicht):
|
|
print '>fade-red-green<'
|
|
showFadeFromTo(fnordlicht, 0, 120)
|
|
|
|
def showFadeGreenBlue(fnordlicht):
|
|
print '>fade-green-blue<'
|
|
showFadeFromTo(fnordlicht, 120, 240)
|
|
|
|
def showFadeBlueRed(fnordlicht):
|
|
print '>fade-blue-red<'
|
|
showFadeFromTo(fnordlicht, 240, 360)
|
|
|
|
def showRandom(fnordlicht):
|
|
print '>random<'
|
|
fnordlicht.program_random(23, 3, 1, 5, 1, 255, 255, 90)
|
|
|
|
def showColor(fnordlicht, color):
|
|
color = int(color)
|
|
print '>%d<' % color
|
|
colors = [0, 45, 90, 135, 180, 225, 270, 315, 360]
|
|
fnordlicht.stop()
|
|
if (color == 9):
|
|
fnordlicht.fade_hsv(0, 0, 255, 5, 1)
|
|
else:
|
|
fnordlicht.fade_hsv(colors[color-1], 255, 255, 5, 1)
|
|
|
|
|
|
def child(param):
|
|
print 'child: PID %s' % os.getpid()
|
|
fnordlicht = Fnordlicht(interface)
|
|
fnordlicht.sync()
|
|
if (param == 'on'):
|
|
showOn(fnordlicht)
|
|
elif (param == 'off'):
|
|
showOff(fnordlicht)
|
|
elif (param == 'light--'):
|
|
modifyDecLight(fnordlicht)
|
|
elif (param == 'light++'):
|
|
modifyIncLight(fnordlicht)
|
|
elif (param == 'speed--'):
|
|
modifyDecSpeed(fnordlicht)
|
|
elif (param == 'speed++'):
|
|
modifyIncSpeed(fnordlicht)
|
|
elif (param == 'saturation--'):
|
|
modifyDecSaturation(fnordlicht)
|
|
elif (param == 'saturation++'):
|
|
modifyIncSaturation(fnordlicht)
|
|
elif (param == 'colorwheel1'):
|
|
showColorwheel1(fnordlicht)
|
|
elif (param == 'colorwheel2'):
|
|
showColorwheel2(fnordlicht)
|
|
elif (param == 'thunderstorm'):
|
|
showThunderstorm(fnordlicht)
|
|
elif (param == 'fire'):
|
|
showFire(fnordlicht)
|
|
elif (param == 'fade-red-green'):
|
|
showFadeRedGreen(fnordlicht)
|
|
elif (param == 'fade-green-blue'):
|
|
showFadeGreenBlue(fnordlicht)
|
|
elif (param == 'fade-blue-red'):
|
|
showFadeBlueRed(fnordlicht)
|
|
elif (param == 'random'):
|
|
showRandom(fnordlicht)
|
|
elif (re.match('\d', param)):
|
|
showColor(fnordlicht, param)
|
|
else:
|
|
print 'unknown parameter: "%s"' % param
|
|
sys.exit()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if not os.path.exists(fifoname):
|
|
try:
|
|
os.mkfifo(fifoname)
|
|
except OSError, e:
|
|
print 'Failed to create FIFO: "%s"' % e
|
|
sys.exit()
|
|
|
|
child_pid = 0
|
|
print 'parent: PID %s' % os.getpid()
|
|
while True:
|
|
pipein = open(fifoname, 'r')
|
|
line = pipein.readline()[:-1]
|
|
print 'parent got "%s"' % (line)
|
|
pipein.close()
|
|
if child_pid > 0:
|
|
print 'killing %d' % child_pid
|
|
try:
|
|
os.kill(child_pid, signal.SIGTERM)
|
|
except OSError, e:
|
|
print '... has already been dead'
|
|
child_pid = os.fork()
|
|
if child_pid == 0:
|
|
child(line)
|