-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.py
executable file
·124 lines (88 loc) · 2.9 KB
/
render.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
"""
Initial implementation in python
"""
import socket
import time
import inspect
import argparse
import binascii
from shaders import procs, state
from treppe import protocol
from prtcl import space
from prtcl import programs as prtcl_programs
CHANNELS_ACTIVE = 16
SYNTH = state.SynthState(CHANNELS_ACTIVE)
SPACE = space.Space(fps=60)
def open_socket(host="localhost", port=2334):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect((host, int(port)))
return sock
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--shader")
parser.add_argument("-H", "--host", default="localhost")
parser.add_argument("-p", "--port", default=3123)
parser.add_argument("-f", "--fps", default=60)
parser.add_argument("-c", "--crap", default=False, action="store_true")
parser.add_argument("-l", "--leds", default=16, type=int)
return parser.parse_args()
def get_shaders():
return {name: proc
for name, proc in inspect.getmembers(procs, inspect.isfunction)
if not name.startswith("_")}
def list_shaders(shaders_available):
print("Shaders available:")
for name, _ in shaders_available.items():
print(" {}".format(name))
def render_loop(conn, crap, leds, shader, fps):
"""
Rendering loop for a shader
"""
t0 = time.time()
fps = float(fps)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
prtcl_samples = set()
while True:
t = time.time() - t0
# Make shader state
frame = []
for i in range(0, leds):
s = state.ShaderState(t=t,
u=0,
v=i,
h_res=1,
v_res=leds,
synth=SYNTH)
# Simulate particles
prtcl_programs.random_push(SPACE)
# Use particles as synth input
p_on = set(SPACE.sample1d()) - prtcl_samples
p_off = prtcl_samples - p_on
prtcl_samples = p_on
for p in p_on:
SYNTH.on(p)
for p in p_off:
SYNTH.off(p)
# Draw strip
frame.append(shader(s))
if crap:
sock.sendto(protocol.encode_frame_crap8(frame), conn)
else:
sock.sendto(protocol.cmd_frame_rgbw16(frame), conn)
time.sleep(1.0/fps)
def main(args):
shaders_available = get_shaders()
shader = shaders_available.get(args.shader)
if not shader:
list_shaders(shaders_available)
return
print("Sending data to {}:{}".format(args.host, args.port))
render_loop((args.host, int(args.port)),
args.crap,
args.leds,
shader,
args.fps)
if __name__ == "__main__":
args = parse_args()
main(args)