summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Bruhin <me@the-compiler.org>2012-09-01 22:03:24 +0200
committerFlorian Bruhin <me@the-compiler.org>2012-09-01 22:03:24 +0200
commitb61dc2ef5249e3113204692cd9e6d15da17e4709 (patch)
treef50b00e92ab999ad2148da7b73ba898624d2b6c1
downloadfarbrauch-b61dc2ef5249e3113204692cd9e6d15da17e4709.tar.gz
farbrauch-b61dc2ef5249e3113204692cd9e6d15da17e4709.zip
initial commit (haha I suck)
-rw-r--r--farbrauch.py193
-rw-r--r--farbrauch_test.py16
2 files changed, 209 insertions, 0 deletions
diff --git a/farbrauch.py b/farbrauch.py
new file mode 100644
index 0000000..cdc56af
--- /dev/null
+++ b/farbrauch.py
@@ -0,0 +1,193 @@
+#!/usr/bin/python3
+# vim: set fileencoding=utf-8 ts=4 sts=4 sw=4 tw=80 expandtab :
+
+# Copyright (C) 2012 Florian Bruhin <me@the-compiler.org>
+
+# farbrauch is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# farbrauch is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with farbrauch. If not, see <http://www.gnu.org/licenses/>.
+
+import subprocess
+import serial
+import struct
+import random
+import sys
+
+videofile = "/home/florian/Desktop/farbrauch_neu.avi"
+ser = serial.Serial(sys.argv[1])
+
+null = open('/dev/null', 'w')
+
+data = {
+ 140: (0,0,0,0,0,0),
+# 184: (255,255,255,255,255,255),
+# 463: (100,200,200,220,190,210),
+# 660: (None,None,None,None,None,None),
+ 734: (0,0,255,0,0,255),
+ 799: (255,0,0,255,0,0),
+ 960: (242,29,24,242,29,24),
+ 2508: (195,196,196,163,89,34),
+ 2675: (249,38,51,251,254,248),
+ 2800: (0,0,0,0,0,0),
+ 2840: (253,208,175,251,254,248),
+ 3062: (250,119,252,54,240,253), # Farben
+ 3128: (252,120,48,252,253,108), # Rot
+ 3141: (42,252,187,255,255,255), # Grün
+ 3170: (250,119,252,54,240,253), # Farben
+ 3181: (252,120,48,252,253,108), # Rot
+ 3200: (42,252,187,255,255,255), # Grün
+ 3219: (250,119,252,54,240,253), # Farben
+ 3305: (252,120,48,252,253,108), # Rot
+ 3327: (42,252,187,255,255,255), # Grün
+ 3386: (252,246,67,253,96,57), # Gelb
+ 3406: (221,142,13,121,43,0),
+ 3910: (0,0,0,0,0,0)
+
+ }
+fades = [ (2675,2800), (2800,2840), (100,140), (3406,3910)]
+
+def rcolor(s=0, e=254):
+ print("randint: {} {}".format(s,e))
+ return random.randint(s,e)
+
+def clamp(val, min=0, max=254):
+ if val > max:
+ return max
+ elif val < min:
+ return min
+ else:
+ return val
+
+def psychoblink():
+ for i in range(1019,1512,3):
+ r1 = random.choice([0,255])
+ g1 = random.choice([0,255])
+ b1 = random.choice([0,255])
+ r2 = random.choice([0,255])
+ g2 = random.choice([0,255])
+ b2 = random.choice([0,255])
+ data[i] = (r1,g1,b1,r2,g2,b2)
+
+def addfire(start, end):
+ for i in range(start, end, 10):
+ r1 = rcolor(0, 255)
+ r2 = rcolor(0, 255)
+ g1 = rcolor(10, clamp(r1 - 50, 10))
+ g2 = rcolor(10, clamp(r2 - 50, 10))
+ b1 = 0
+ b2 = 0
+ data[i] = (r1, g1, b1, r2, g2, b2)
+ if i < (end - 1):
+ fades.append((i, i+10))
+
+def whiteflicker():
+ for i in range(184,734, 5):
+ val = rcolor()
+ val2 = rcolor()
+ offset = rcolor(-10,10)
+ offset2 = rcolor(-10,10)
+ v = clamp(val+offset)
+ v2 = clamp(val2+offset2)
+ data[i] = (v, v, v, v2, v2, v2)
+ if i < 733:
+ fades.append((i, i+5))
+
+def filldata():
+ prevdata = [None, None, None, None, None, None]
+ for line in sorted(data):
+ outdata = []
+ dataline = data[line]
+ for (i, elem) in enumerate(dataline):
+ if elem is None:
+ if prevdata[i] is None:
+ raise ValueError("Prevdata and data None")
+ outdata.append(prevdata[i])
+ else:
+ prevdata[i] = elem
+ outdata.append(elem)
+ data[line] = tuple(outdata)
+
+def addfade(startframe, endframe):
+ if startframe not in data or endframe not in data:
+ raise ValueError("Start or Endframe not in data"
+ "({}/{})".format(startframe, endframe))
+ dt = endframe - startframe
+ deltas = []
+ for (i, elem) in enumerate(data[startframe]):
+ deltas.append(data[endframe][i] - elem)
+ steps = [ (delta / dt) for delta in deltas ]
+ for i in range(startframe + 1, endframe):
+ newdata = []
+ for e in range(0, 6):
+ newdata.append(round(data[startframe][e] + (i - startframe) *
+ steps[e]))
+ data[i] = newdata
+
+def adddisco():
+ colors = [(243,15,33),(35,245,169),(10,162,250),(255,128,250),(248,22,44),(250,80,27)]
+ for i in range(1844,2508,10):
+ led1 = random.choice(colors)
+ led2 = random.choice(colors)
+ data[i] = led1 + led2
+ print("disco: {}".format(data[i]))
+
+def out_dummy(data):
+ print(repr(data))
+
+def out_serial(data):
+ if data is not None:
+ try:
+ ser.write(struct.pack('B', 255))
+ except (OSError, serial.serialutil.SerialException):
+ try:
+ ser.write(struct.pack('B', 255))
+ except (OSError, serial.serialutil.SerialException):
+ pass
+
+ for byte in data:
+ if byte == 255: byte = 254
+ try:
+ print("Writing {}".format(byte))
+ ser.write(struct.pack('B', byte))
+ except (OSError, serial.serialutil.SerialException):
+ pass
+
+mplayer = subprocess.Popen(['/usr/bin/mplayer', '-msglevel', 'decvideo=7',
+ videofile], stdout=subprocess.PIPE,
+ stderr=null)
+
+def precalc():
+ filldata()
+ addfire(0,101)
+ whiteflicker()
+ psychoblink()
+ addfire(1512,1843)
+ adddisco()
+ for fade in fades:
+ addfade(*fade)
+
+precalc()
+while True:
+ driver = out_serial
+ line = mplayer.stdout.readline().decode('UTF-8')
+ if 'STATUSLINE' in line:
+ frame = line.split(' ct: ')[1].split()[1].split('/')[0].strip()
+ try:
+ frame = int(frame)
+ except ValueError:
+ frame = None
+ print(frame)
+ if frame and frame in data:
+ driver(data[frame])
+
+close(null)
+
diff --git a/farbrauch_test.py b/farbrauch_test.py
new file mode 100644
index 0000000..5005320
--- /dev/null
+++ b/farbrauch_test.py
@@ -0,0 +1,16 @@
+import sys,serial,struct
+ser = serial.Serial(sys.argv[1])
+
+def write(num):
+ print("Writing {}".format(num))
+ ser.write(struct.pack('B', num))
+
+while True:
+ vals = input("Values?").strip().split(',')
+ if len(vals) != 6:
+ raise ValueError("Need 6 values!")
+ write(255)
+ for val in vals:
+ val = int(val)
+ if val == 255: val = 254
+ write(val)