From 02b06c658172927f822d6653c16267a4606a9294 Mon Sep 17 00:00:00 2001 From: j Date: Mon, 16 Oct 2017 20:31:44 +0200 Subject: [PATCH] trigger lights --- cdoseaplay/config.py | 15 +++++---- cdoseaplay/lights.py | 77 +++++++++++++++++++++++++++++++++++--------- cdoseaplay/pi.py | 20 ++++++++++++ cdoseaplay/play.py | 4 ++- cdoseaplay/sync.py | 6 +--- cdoseaplay/utils.py | 5 +++ setup.py | 2 ++ 7 files changed, 100 insertions(+), 29 deletions(-) create mode 100644 cdoseaplay/pi.py diff --git a/cdoseaplay/config.py b/cdoseaplay/config.py index f09ac61..0ba6e8f 100644 --- a/cdoseaplay/config.py +++ b/cdoseaplay/config.py @@ -1,16 +1,17 @@ -import os import json +import os +import sys base = 'https://cdosea.0x2620.org/static/render/' lang = '1080p' start = '' +lights = False + + conf = os.path.expanduser('~/.config/cdosea.json') if os.path.exists(conf): with open(conf) as fd: config = json.load(fd) - if 'lang' in config: - lang = config['lang'] - if 'base' in config: - base = config['base'] - if 'start' in config: - start = config['start'] + _this = sys.modules[__name__] + for key in config: + setattr(_this, key, config[key]) diff --git a/cdoseaplay/lights.py b/cdoseaplay/lights.py index 96f8e5f..755d565 100755 --- a/cdoseaplay/lights.py +++ b/cdoseaplay/lights.py @@ -5,22 +5,67 @@ import os import time import ox -from pi import random +import lanbox -path = sys.argv[1] +from .pi import random -n = int(os.path.getctime(path) - 1495280000) -duration = ox.avinfo(path)['duration'] -seq = random(n * 1000) -pos = 0 -lights = [] -while pos < duration - 15: - sleep = seq() + 15 - light = seq() + 1 - if pos + sleep > duration: - break - time.sleep(sleep) - cmd = ['/opt/LanBox-JSONRPC/fade.py', str(light)] - subprocess.Popen(cmd) - pos += sleep +# Screen 1-5, Control, 6,7 +''' +> _*10 patterns / cues that correspond to 10 digit:*_ +> 0 = Screen LED @ 100% fade time = 9s (cue list 1) +> 1 = LED Control @ 10% fade time = 8s (cue list 2) +> 2 = LED Control @ 20% fade time = 7s (cue list 3) +> 3 = LED Control @ 30% fade time = 6s (cue list 4) +> 4 = LED Control @ 40% fade time = 5s (cue list 5) +> 5 = LED Control @ 50% fade time = 4s (cue List 6) +> 6 = LED Control @ 60% fade time = 3s (cue list 7) +> 7 = LED Control @ 70% fade time = 2s (cue list 8) +> 8 = LED Control @ 80% fade time = 1s (cue list 9) +> 9 = LED Control @ 90% fade time = 0s (cue list 10) +''' +def channels(screen, control): + screen = int(screen * 255) + control = int(control * 255) + return {'1': screen, '2': screen, '3': screen, '4': screen, '5': screen, '6': control, '7': control} + + +LIGHTS = { + 0: (channels(1, 0.0), 9), + 1: (channels(0, 0.1), 8), + 2: (channels(0, 0.2), 7), + 3: (channels(0, 0.3), 6), + 4: (channels(0, 0.4), 5), + 5: (channels(0, 0.5), 4), + 6: (channels(0, 0.6), 3), + 7: (channels(0, 0.7), 2), + 8: (channels(0, 0.8), 1), + 9: (channels(0, 0.9), 0.1), +} + + +def switch(state): + lights, fade = LIGHTS[state] + lb = lanbox.LanboxMethods() + #lb.getChannels(lights) + lb.fadeTo(lights, fade) + + +if __name__ == '__main__': + path = sys.argv[1] + + n = int(os.path.getctime(path) - 1495280000) + info = ox.avinfo(path) + duration = info.get('duration', 0) + + seq = random(n * 1000) + pos = 0 + lights = [] + while pos < duration - 15: + sleep = seq() + 15 + light = seq() + if pos + sleep > duration: + break + time.sleep(sleep) + switch(light) + pos += sleep diff --git a/cdoseaplay/pi.py b/cdoseaplay/pi.py new file mode 100644 index 0000000..491a633 --- /dev/null +++ b/cdoseaplay/pi.py @@ -0,0 +1,20 @@ +from mpmath import mp +mp.dps = 10000 +PI = str(mp.pi).replace('.', '') + +class random(object): + PI = str(mp.pi).replace('.', '') + + def __init__(self, offset=0): + self.position = offset + self.numbers = list(map(int, self.PI[offset:])) + + def __call__(self): + if not self.numbers: + offset = mp.dps + mp.dps += 1000 + self.PI = str(mp.pi).replace('.', '') + self.numbers = list(map(int, self.PI[offset:])) + self.position += 1 + return self.numbers.pop(0) + diff --git a/cdoseaplay/play.py b/cdoseaplay/play.py index dd61f4c..eecf260 100755 --- a/cdoseaplay/play.py +++ b/cdoseaplay/play.py @@ -4,7 +4,7 @@ from glob import glob import os import sys -from .utils import update_playlist, get_player +from .utils import update_playlist, get_player, trigger_lights from .subtitleserver import SubtitleServer from . import config @@ -57,6 +57,8 @@ def main(): player.pause = True while True: + if config.lights and player.path: + trigger_lights(player.path.decode()) try: player.wait_for_playback() except: diff --git a/cdoseaplay/sync.py b/cdoseaplay/sync.py index 05598a6..aaaf684 100755 --- a/cdoseaplay/sync.py +++ b/cdoseaplay/sync.py @@ -13,7 +13,7 @@ from queue import Queue from socketserver import UDPServer, ThreadingMixIn, BaseRequestHandler from threading import Thread -from .utils import mpv_log, update_playlist +from .utils import mpv_log, update_playlist, trigger_lights import logging logger = logging.getLogger('cdosea') @@ -22,10 +22,6 @@ DEFAULT_PORT = 2680 DEBUG = False -def trigger_lights(path): - cmd = ['python3', '-m', 'cdoseaplay.lights', path] - subprocess.Popen(cmd) - class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass diff --git a/cdoseaplay/utils.py b/cdoseaplay/utils.py index e68b2df..fd08499 100644 --- a/cdoseaplay/utils.py +++ b/cdoseaplay/utils.py @@ -5,6 +5,7 @@ import datetime import time import string import random +import subprocess import ox @@ -78,3 +79,7 @@ def update_playlist(playlist, prefix, position=None, shift=True): f.write('\n'.join(files)) f.write('\n') return position, len(files) + +def trigger_lights(path): + cmd = ['python3', '-m', 'cdoseaplay.lights', path] + subprocess.Popen(cmd) diff --git a/setup.py b/setup.py index 79a3eb9..d7e27ed 100644 --- a/setup.py +++ b/setup.py @@ -18,6 +18,8 @@ setup( 'ox >= 2.1.541,<3', 'requests >= 1.1.0', 'tornado', + 'mpmath', + 'lanbox', ], keywords=[], classifiers=[