-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtd_operator.py
More file actions
93 lines (64 loc) · 2.22 KB
/
td_operator.py
File metadata and controls
93 lines (64 loc) · 2.22 KB
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
# me - this DAT
# scriptOp - the OP which is cooking
import sys
#https://docs.derivative.ca/Category:Python#Installing_Custom_Python_Packages
mypath = "/Users/jimmyghaderi/.pyenv/versions/3.7.12/lib/python3.7/site-packages"
if mypath not in sys.path:
sys.path.append(mypath)
import xled
import numpy
import io
def deviceToController(device):
return xled.ControlInterface(
device.ip_address, device.hw_address)
def deviceNames(device):
return device.id
devices = []
try:
for device in xled.discover.xdiscover(timeout=2):
devices.append(device)
except xled.exceptions.DiscoverTimeout as e:
pass
devices = sorted(devices, key = lambda d: d.id)
controls = list(map(lambda d: deviceToController(d), devices))
for control in controls:
control.set_mode("rt")
layouts = list(map(lambda c: c.get_led_layout().data, controls))
device_infos = list(map(lambda c: c.get_device_info().data, controls))
def flatten(inlist):
return [a for tup in inlist for a in tup]
def normalizedFloatToInt8(float_val):
return round(float_val * 255)
def renormalize(coord):
return (coord +1)/2
def makeImg(input_op, coords, rgbw=False):
out = []
for coord in coords:
colours = input_op.sample(
u=renormalize(coord['x']), v=coord['y'])
[r, g, b, a] = list(map(lambda c: normalizedFloatToInt8(c), colours))
if rgbw:
out.append([r, g, b, 0])
else:
out.append([r,g,b])
return flatten(out)
device_par = None
previous_device_par = None
def onSetupParameters(scriptOp):
page = scriptOp.appendCustomPage('Twinkly')
[device_par] = page.appendMenu("Device")
device_par.menuNames = list(range(len(devices)))
device_par.menuLabels = list(map(lambda d: deviceNames(d), devices))
return
def onPulse(par):
return
def onCook(scriptOp):
current_id = scriptOp.pars("Device")[0]
control = controls[current_id]
layout = layouts[current_id]
coords = layout['coordinates']
frame = io.BytesIO(bytes(
makeImg(scriptOp.inputs[0], coords, device_infos[current_id]['led_profile'] == 'RGBW')))
#TODO: Handle closing previous socket when changing device
control.set_rt_frame_socket(frame, 3)
return