-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.lua
425 lines (366 loc) · 11.2 KB
/
node.lua
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
-- License: BSD 2 clause (see LICENSE.txt)
gl.setup(NATIVE_WIDTH, NATIVE_HEIGHT)
util.no_globals()
-- Start preloading images/videos this many second
-- before they are displayed.
local PREPARE_TIME = 1 -- seconds
local SERIAL = sys.get_env "SERIAL"
-------------------------------------------------------------
local matrix = require "matrix2d"
local font = resource.load_font "silkscreen.ttf"
local white = resource.create_colored_texture(1,1,1,0.5)
local min, max = math.min, math.max
local audio = false
local empty_text = {
main = "",
sub = "",
}
local function round(v)
return math.floor(v+.5)
end
-- patch in method for older info-beamer versions
if not matrix.apply_gl then
matrix.apply_gl = function(m)
return gl.modelMultiply(
m.v11, m.v21, 0, m.v31,
m.v12, m.v22, 0, m.v32,
0, 0, 1, 0,
m.v13, m.v23, 0, m.v33
)
end
end
local function Screen()
local rotation = 0
local screen_offset = 0
local screen_spread = 1
local forced_pos
local is_portrait = false
local transform
local w, h = NATIVE_WIDTH, NATIVE_HEIGHT
local function update_placement(new_rotation, new_offset, new_spread, new_forced_pos)
rotation = new_rotation
screen_offset = new_offset
screen_spread = new_spread
forced_pos = new_forced_pos
is_portrait = rotation == 90 or rotation == 270
gl.setup(w, h)
transform = matrix.ident()
if forced_pos then
local forced_w, forced_h = forced_pos.x2 - forced_pos.x1,
forced_pos.y2 - forced_pos.y1
transform = transform *
matrix.scale(1 / w * forced_w, 1 / h * forced_h) *
matrix.trans(forced_pos.x1, forced_pos.y1)
end
if rotation == 0 then
-- nothing to do
elseif rotation == 90 then
transform = transform *
matrix.trans(w, 0) *
matrix.rotate_deg(rotation)
elseif rotation == 180 then
transform = transform *
matrix.trans(w, h) *
matrix.rotate_deg(rotation)
elseif rotation == 270 then
transform = transform *
matrix.trans(0, h) *
matrix.rotate_deg(rotation)
else
return error(string.format("cannot rotate by %d degree", rotation))
end
if is_portrait then
transform = transform *
matrix.trans(-h * screen_offset, 0)
else
transform = transform *
matrix.trans(-w * screen_offset, 0)
end
end
local function draw_video(vid, x1, y1, x2, y2)
local tx1, ty1 = transform(x1, y1)
local tx2, ty2 = transform(x2, y2)
local x1, y1, x2, y2 = round(math.min(tx1, tx2)),
round(math.min(ty1, ty2)),
round(math.max(tx1, tx2)),
round(math.max(ty1, ty2))
return vid:place(x1, y1, x2, y2, rotation)
end
local function draw_image(img, x1, y1, x2, y2)
return img:draw(x1, y1, x2, y2)
end
local function frame_setup()
return matrix.apply_gl(transform)
end
local function screen_size()
if is_portrait then
return h, w
else
return w, h
end
end
local function place(content_offset, content_spread)
content_offset = content_offset or screen_offset
content_spread = content_spread or screen_spread
local w, h = screen_size()
return w * content_offset, 0, w * (content_offset+content_spread), h
end
local function covers(left, right)
return left < screen_offset + screen_spread and right > screen_offset
end
local function draw_debug()
if forced_pos then
local x1, y1, x2, y2 = place(screen_offset, screen_spread)
draw_image(white, -10000, y1-5, 10000, y1-0)
draw_image(white, -10000, y2+0, 10000, y2+5)
draw_image(white, x1-5, -10000, x1-0, 10000)
draw_image(white, x2+0, -10000, x2+5, 10000)
end
end
update_placement(0, 0, 1)
return {
update_placement = update_placement;
frame_setup = frame_setup;
draw_image = draw_image;
draw_video = draw_video;
draw_debug = draw_debug;
place = place;
covers = covers;
}
end
local screen = Screen()
local function Image(item)
local obj
local function prepare()
if not obj then
obj = resource.load_image{
file = item.file:copy(),
}
end
end
local function tick(now)
local state, w, h = obj:state()
local x1, y1, x2, y2 = screen.place(item.offset, item.spread)
screen.draw_image(obj, x1, y1, x2, y2)
end
local function stop()
if obj then
obj:dispose()
obj = nil
end
end
return {
prepare = prepare,
tick = tick,
stop = stop,
}
end
local function Video(item)
local obj
local function prepare()
if not obj then
obj = resource.load_video{
file = item.file:copy(),
raw = true,
paused = true,
audio = audio,
looped = true,
}
end
end
local function tick(now)
obj:start()
local state, w, h = obj:state()
if state ~= "loaded" and state ~= "finished" then
print "not loaded yet :("
else
local x1, y1, x2, y2 = screen.place(item.offset, item.spread)
obj:layer(1)
screen.draw_video(obj, x1, y1, x2, y2)
end
end
local function stop()
if obj then
obj:layer(-1)
obj:dispose()
obj = nil
end
end
return {
prepare = prepare,
tick = tick,
stop = stop,
}
end
local function Empty()
local function tick()
local x1, y1, x2, y2 = screen.place()
local cx = (x1+x2) / 2
local cy = (y1+y2) / 2
local text = empty_text.main
local size = 48
local w = font:width(text, size)
font:write(cx - w/2, cy - size/2, text, size, 1,1,1,1)
local text = empty_text.sub
local size = 16
local w = font:width(text, size)
font:write(cx - w/2, cy - size/2+48, text, size, .5,.5,.5,1)
end
return {
prepare = function() end,
tick = tick,
stop = function() end,
}
end
local function Playlist()
local total_duration, playlist
local cur = Empty()
local nxt
local switch_time
local function update(new_playlist)
playlist = new_playlist
total_duration = 0
for _, item in ipairs(playlist) do
total_duration = total_duration + item.duration
end
end
local function setup_player(item)
pp(item)
return ({
image = Image,
video = Video,
empty = Empty,
})[item and item.filetype or "empty"](item)
end
local function get_next_item(now)
if #playlist == 0 then
return nil, now + 1
end
local epoch_offset = now % total_duration
local epoch_start = now - epoch_offset
local current_idx
for idx = #playlist, 1, -1 do
local item = playlist[idx]
if item.epoch_offset <= epoch_offset then
current_idx = idx
break
end
end
local next_item = playlist[current_idx % #playlist + 1]
local wrap_around = current_idx == #playlist
return next_item, epoch_start + next_item.epoch_offset + (
wrap_around and total_duration or 0
)
end
local function tick(now)
if not nxt then
print "deciding next item"
local next_item
next_item, switch_time = get_next_item(now)
nxt = setup_player(next_item)
end
if switch_time - now < PREPARE_TIME then
nxt.prepare()
end
if now >= switch_time then
cur.stop()
cur = nxt
nxt = nil
end
cur.tick(now)
end
return {
update = update;
tick = tick;
}
end
local playlist = Playlist()
local function apply_config(config)
print "Applying new configuration"
audio = config.audio
local forced_pos
if (config.x1 ~= 0 or config.y1 ~= 0 or
config.x2 ~= 0 or config.y2 ~= 0)
then
forced_pos = {
x1 = config.x1, y1 = config.y1,
x2 = config.x2, y2 = config.y2,
}
else
forced_pos = nil
end
local found_screen = false
for _, assignment in ipairs(config.assignments) do
local offset = 0
for _, device in ipairs(assignment.devices) do
local spread = device.layout == "single" and 1 or 2
if device.serial == SERIAL then
print("found my placement", offset, spread)
screen.update_placement(config.rotation, offset, spread, forced_pos)
found_screen = true
end
offset = offset + spread
end
end
local new_playlist = {}
local epoch_offset = 0
local has_playlist = #config.playlist > 0
for _, item in ipairs(config.playlist) do
local offset = 0
for _, content in ipairs(item.content) do
local spread = content.spread
if screen.covers(offset, offset + spread) then
new_playlist[#new_playlist+1] = {
file = resource.open_file(content.file.asset_name),
filetype = content.file.type,
duration = item.duration,
epoch_offset = epoch_offset,
offset = offset,
spread = spread
}
break
end
offset = offset + spread
end
epoch_offset = epoch_offset + item.duration
end
playlist.update(new_playlist)
if not found_screen then
empty_text = {
main = "Not assigned to this setup",
sub = "Use 'Device Assignment' to add this device to this setup"
}
elseif not has_playlist then
empty_text = {
main = "No playlist configured",
sub = "Add items to the playlist of this setup"
}
else
empty_text = {
main = "Waiting for sync",
sub = "Waiting for correct system time and item start"
}
end
end
do
local config_switch, config
util.json_watch("config.json", function(new_config)
if new_config.synced_changes == 0 or not config then
config_switch = os.time()
else
config_switch = new_config.__metadata.sync_ts + config.synced_changes
end
config = new_config
end)
node.event("tick", function()
if config_switch and os.time() >= config_switch then
config_switch = nil
apply_config(config)
end
end)
end
function node.render()
screen.frame_setup()
playlist.tick(os.time())
screen.draw_debug()
end