commit 80ca8f798a1860acb5d9f6378b58d62c33b98651 Author: Ronald Schaten Date: Sat Oct 16 15:17:36 2010 +0200 initial commit diff --git a/fnordlicht.py b/fnordlicht.py new file mode 100644 index 0000000..8aa11c3 --- /dev/null +++ b/fnordlicht.py @@ -0,0 +1,237 @@ +import sys, struct, time, serial + +'connects to a fnordlicht' +""" http://github.com/fd0/fnordlicht/blob/master/doc/PROTOCOL """ + +class Fnordlicht: + + tty = '' + verbose = False + + def __init__(self, tty, verbose = False, addr = 255): + self.tty = tty + self.addr = addr + if verbose: + self.verbose = True + print 'using tty "%s", address "%s"' % (self.tty, self.addr) + + def __send(self, data): + if self.verbose: + print 'sending package:',data + # pad data package with 0-characters + for i in range(15 - len(data)): + data.append(0) + try: + # open serial port at '19200,8N1', 1s timeout + ser = serial.Serial(self.tty, 19200, timeout=1) + for i in range(15): + ser.write(chr(data[i])) + ser.flush() + ser.close() + except: + print 'unable to send string on %s: %s' % (self.tty, sys.exc_info()[0]) + + def sync(self): + if self.verbose: + print 'sync()' + data = [] + for i in range(14): + data.append(0x1b) + data.append(self.addr) + self.__send(data) + + def fade_rgb(self, r, g, b, step, delay): + if self.verbose: + print 'fade_rgb(%d, %d, %d, %d, %d)' % (r, g, b, step, delay) + data = [self.addr, 0x01] + data.append(step) + data.append(delay) + data.append(r) + data.append(g) + data.append(b) + self.__send(data) + + def fade_hsv(self, h, s, v, step, delay): + if self.verbose: + print 'fade_hsv(%d, %d, %d, %d, %d)' % (h, s, v, step, delay) + data = [self.addr, 0x02] + data.append(step) + data.append(delay) + data.append(ord(struct.pack('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 + + 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) diff --git a/lircrc b/lircrc new file mode 100644 index 0000000..e65e90b --- /dev/null +++ b/lircrc @@ -0,0 +1,175 @@ +begin + prog = irexec + button = fnordlicht.allon + config = echo on > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.alloff + config = echo off > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.dim + config = echo light-- > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.bright + config = echo light++ > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.dimmer + config = echo fast-- > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.dimmest + config = echo fast++ > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.smooth + config = echo saturation-- > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.strobe + config = echo saturation++ > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.light10 + config = echo colorwheel1 > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.light11 + config = echo colorwheel2 > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.light12 + config = echo fire > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.light13 + config = echo thunderstorm > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.light14 + config = echo fade-red-green > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.light15 + config = echo fade-green-blue > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.light16 + config = echo fade-blue-red > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.flash + config = echo random > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.1 + config = echo 1 > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.2 + config = echo 2 > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.3 + config = echo 3 > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.4 + config = echo 4 > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.5 + config = echo 5 > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.6 + config = echo 6 > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.7 + config = echo 7 > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.8 + config = echo 8 > /var/run/illuminatus + repeat = 0 +end + +begin + prog = irexec + button = fnordlicht.9 + config = echo 9 > /var/run/illuminatus + repeat = 0 +end +