-
Notifications
You must be signed in to change notification settings - Fork 0
/
css2dmx.py
92 lines (72 loc) · 2.07 KB
/
css2dmx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from datetime import datetime
from functools import lru_cache
import serial
from time import sleep
import array
import ola.ClientWrapper
from lib.core import apply_style_on_dom, compute_dmx
from lib.hardware import load_devices
from lib.tree import parse_tree_file
from lib.css import parse_css_file
from lib.utils import trange
@lru_cache(maxsize=1)
def get_ola_client():
try:
return ola.ClientWrapper.OlaClient()
except Exception as e:
print(e)
return None
@lru_cache(maxsize=1)
def get_serial():
try:
return serial.Serial("/dev/ttyACM0", 115200, timeout=.1)
except serial.serialutil.SerialException as e:
print(e)
return None
@lru_cache()
def create_bytes(state):
state = dict(state)
bs = []
for i in range(1, 512):
bs.append(state.get(i, 0))
bs = bytes(bs)
return bs
def send_ola(state):
client = get_ola_client()
if client:
bs = create_bytes(state)
client.SendDmx(universe=1, data=array.array('B', bs))
def send_serial(state):
ser = get_serial()
if ser:
bs = b"\x00" + create_bytes(state)
for i in range(8):
chunk = bs[i * 16:(i + 1) * 16]
ser.write(chunk)
sleep(1e-4)
def send_dmx(state):
state = tuple(state)
send_ola(state)
send_serial(state)
def run(devices, tree, css, verbose=False):
apply_style_on_dom(tree, css)
tree.print()
keyframes = css.keyframes
now = datetime.now()
for t in trange(interval=0.02):
state = compute_dmx(tree, devices, keyframes, t.timestamp() - now.timestamp())
if verbose:
print(state)
send_dmx(state)
if __name__ == '__main__':
import sys
import os
dir_path = sys.argv[1]
dir_name = os.path.basename(dir_path)
tree_file = os.path.join(dir_path, "tree.xml")
css_file = os.path.join(dir_path, "style.css")
verbose = len(sys.argv) == 3 and sys.argv[2] == "-v"
devices = load_devices()
tree = parse_tree_file(tree_file)
css = parse_css_file(css_file)
run(devices, tree, css, verbose)