cdosea-play/cdoseaplay/lights.py

145 lines
3.5 KiB
Python
Executable File

#!/usr/bin/python3
import os
import string
import subprocess
import sys
import time
import ox
import sacn
from .pi import random
from . import config
# Screen 1-5, Control, 6,7
'''
>
_*10 patterns / cues that correspond to 10 digit:*_
0 = Cue list 1 = 1x flash (on, off)
1 = Cue list 2 = 2x flashes (on, off, on, off)
2 = Cue list 3 = 3x flashes (on, off, on, off, on, off)
...and so on...
8 = Cue list 9 = 9x flashes
9 = Cue list 10 = 10x flashes
The basic timing for 1x flash is:
- Flash on over 0s
- Hold on for 0.2s
- Flash off over 0s
- Hold off for 0.2s
'''
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),
}
CURRENT_STATE = [0] * 16
# avoid overload and turn light on with a fade
def fade_to(sender, start, end, duration, channels=16, universe=1):
distance = end - start
steps = int(duration * 10)
if not steps:
steps = 2
step = duration/steps
delta = distance/steps
current = start
value = current
switch = 0
while steps:
switch += 1
value += delta
n = int(round(value))
if n != current:
current = n
old = sender[1].dmx_data[:channels]
target = [n] * channels
if switch % 2:
a = target
b = old
else:
a = old
b = target
sender[universe].dmx_data = [a[x] if x % 2 else b[x] for x in range(channels)]
time.sleep(step)
steps -= 1
def flash(flashes):
step = 0.2
flash_duration = 0.02
base = 0
brightness = config.lights_brightness
universe = 1
channels = 16
sender = sacn.sACNsender()
sender.start()
sender.activate_output(universe)
sender[universe].multicast = True
sender[universe].dmx_data = [0] * channels
while flashes:
fade_to(sender, base, brightness, flash_duration, channels, universe)
time.sleep(step)
sender[1].dmx_data = [0] * channels
time.sleep(step)
flashes -= 1
sender[universe].dmx_data = [0] * channels
time.sleep(step)
sender.stop()
def switch(state, seq):
import lanbox
lb = lanbox.Lanbox()
lb.layerGo(state)
if __name__ == '__main__':
path = sys.argv[1]
no_sleep = len(sys.argv) > 2 and sys.argv[2] == "no-sleep"
n = int(os.path.getctime(path) - 1495280000) % 1000
info = ox.avinfo(path)
duration = info.get('duration', 0)
seq = random(n * 100)
pos = 0
lights = []
letter_offset = 0
letter = path.split('/')[-1][0].lower()
if config.letter_offset:
letter_offset = 11 + string.ascii_lowercase.index(letter) * 10
while pos < duration - 15:
sleep = seq() + 15
light = seq()
if pos + sleep > duration:
break
if not no_sleep:
time.sleep(sleep)
print("letter", letter, "offset", letter_offset, "light", light, "cue", letter_offset + light)
if config.lanbox:
switch(light + letter_offset, seq)
else:
flash(light + 1)
pos += sleep