Commit 80ca8f79 authored by Ronald Schaten's avatar Ronald Schaten
Browse files

initial commit

parents
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('<h', h)[0]))
data.append(ord(struct.pack('<h', h)[1]))
data.append(s)
data.append(v)
self.__send(data)
def save_rgb(self, slot, r, g, b, step, delay, pause):
# TODO untested
if self.verbose:
print 'save_rgb(%d, %d, %d, %d, %d, %d, %d)' % (slot, r, g, b, step, delay, pause)
data = [self.addr, 0x03]
data.append(slot)
data.append(step)
data.append(delay)
data.append(ord(struct.pack('<H', pause)[0]))
data.append(ord(struct.pack('<H', pause)[1]))
data.append(r)
data.append(g)
data.append(b)
self.__send(data)
def save_hsv(self, slot, h, s, v, step, delay, pause):
# TODO untested
if self.verbose:
print 'save_hsv(%d, %d, %d, %d, %d, %d, %d)' % (slot, h, s, v, step, delay, pause)
data = [self.addr, 0x04]
data.append(slot)
data.append(step)
data.append(delay)
data.append(ord(struct.pack('<H', pause)[0]))
data.append(ord(struct.pack('<H', pause)[1]))
data.append(ord(struct.pack('<h', h)[0]))
data.append(ord(struct.pack('<h', h)[1]))
data.append(s)
data.append(v)
self.__send(data)
def save_current(self, slot, step, delay, pause):
# TODO untested
if self.verbose:
print 'save_current(%d, %d, %d, %d)' % (slot, step, delay, pause)
data = [self.addr, 0x05]
data.append(slot)
data.append(step)
data.append(delay)
data.append(ord(struct.pack('<H', pause)[0]))
data.append(ord(struct.pack('<H', pause)[1]))
self.__send(data)
def config_offsets(self, step, delay, h, s, v):
# TODO untested
if self.verbose:
print 'config_offsets(%d, %d, %d, %d, %d)' % (step, delay, h, s, v)
data = [self.addr, 0x06]
data.append(step)
data.append(delay)
data.append(ord(struct.pack('<h', h)[0]))
data.append(ord(struct.pack('<h', h)[1]))
data.append(s)
data.append(v)
self.__send(data)
def __start_program(self, program, params):
if self.verbose:
print 'start_program(%d, %s)' % (program, params)
data = [self.addr, 0x07]
data.append(program)
data.extend(params)
self.__send(data)
def program_colorwheel(self, fade_step, fade_delay, fade_sleep, hue_start, hue_step, add_addr, saturation, value):
if self.verbose:
print 'program_colorwheel(%d, %d, %d, %d, %d, %d, %d, %d)' % (fade_step, fade_delay, fade_sleep, hue_start, hue_step, add_addr, saturation, value)
data = [fade_step, fade_delay, fade_sleep]
data.append(ord(struct.pack('<H', hue_start)[0]))
data.append(ord(struct.pack('<H', hue_start)[1]))
data.append(ord(struct.pack('<h', hue_step)[0]))
data.append(ord(struct.pack('<h', hue_step)[1]))
data.append(add_addr)
data.append(saturation)
data.append(value)
self.__start_program(0, data)
def program_random(self, seed, flags, fade_step, fade_delay, fade_sleep, saturation, value, min_distance):
if self.verbose:
print 'program_random(%d, %d, %d, %d, %d, %d, %d, %d)' % (seed, flags, fade_step, fade_delay, fade_sleep, saturation, value, min_distance)
data = []
data.append(ord(struct.pack('<H', seed)[0]))
data.append(ord(struct.pack('<H', seed)[1]))
data.append(flags)
data.append(fade_step)
data.append(fade_delay)
data.append(ord(struct.pack('<H', fade_sleep)[0]))
data.append(ord(struct.pack('<H', fade_sleep)[1]))
data.append(saturation)
data.append(value)
data.append(min_distance)
self.__start_program(1, data)
def stop(self, fading = 1):
if self.verbose:
print 'stop(%d)' % (fading)
data = [self.addr, 0x08]
data.append(fading)
self.__send(data)
def modify_current(self, step, delay, r, g, b, h, s, v):
if self.verbose:
print 'modify_current(%d, %d, %d, %d, %d, %d, %d, %d)' % (step, delay, r, g, b, h, s, v)
data = [self.addr, 0x09]
data.append(step)
data.append(delay)
data.append(ord(struct.pack('<b', r)))
data.append(ord(struct.pack('<b', g)))
data.append(ord(struct.pack('<b', b)))
data.append(ord(struct.pack('<h', h)[0]))
data.append(ord(struct.pack('<h', h)[1]))
data.append(ord(struct.pack('<b', s)))
data.append(ord(struct.pack('<b', v)))
self.__send(data)
def pull_int(self, delay):
# TODO untested
if self.verbose:
print 'pull_int(%d)' % (delay)
data = [self.addr, 0x0a]
data.append(delay)
self.__send(data)
def config_startup_nothing(self):
# TODO untested
if self.verbose:
print 'config_startup_nothing()'
data = [self.addr, 0x0b]
data.append(0)
self.__send(data)
def config_startup_program(self, program, params):
# TODO untested
if self.verbose:
print 'config_startup_program(%d, %s)' % (program, params)
data = [self.addr, 0x0b]
data.append(1)
data.append(program)
data.extend(params)
self.__send(data)
def powerdown(self):
# TODO untested
if self.verbose:
print 'powerdown()'
data = [self.addr, 0x0c]
self.__send(data)
# secondary functions
def fade_updown_rgb(self, r, g, b, step, delay, sleep_time):
self.fade_rgb(r, g, b, step, delay)
time.sleep(sleep_time)
self.fade_rgb(0, 0, 0, step, delay)
time.sleep(sleep_time)
def fade_updown_hsv(self, h, s, v, step, delay, sleep_time):
self.fade_hsv(h, s, v, step, delay)
time.sleep(sleep_time)
self.fade_hsv(h, s, 0, step, delay)
time.sleep(sleep_time)
#fnordlicht = Fnordlicht('/dev/ttyUSB0', verbose = True)
#fnordlicht.sync()
#fnordlicht.fade_rgb(255, 0, 0, 100, 1)
#fnordlicht.fade_hsv(300, 255, 255, 100, 1)
#fnordlicht.start_program(0, [1, 1, 1, 0, 0, 60, 0, 255, 255, 255]) # colorwheel
#fnordlicht.stop(fading = 1)
#fnordlicht.modify_current(1, 1, 0, 0, 0, 100, 100, 100)
#fnordlicht.fade_updown_rgb(255, 0, 0, 10, 1, 1)
#fnordlicht.program_random(23, 3, 1, 5, 1, 255, 255, 90)
#!/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
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)
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
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment