-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.lua
273 lines (231 loc) · 7.08 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
gl.setup(1920, 1080)
util.noglobals()
-- We need to access files in playlist/
node.make_nested()
-- Start preloading images this many second before
-- they are displayed.
local PREPARE_TIME = 1 -- seconds
-- must be enough time to load a video and have it
-- ready in the paused state. Normally 500ms should
-- be enough.
local VIDEO_PRELOAD_TIME = .5 -- seconds
local json = require "json"
local font = resource.load_font "silkscreen.ttf"
local serial = sys.get_env "SERIAL"
local min = math.min
local assigned = false
local function msg(str, ...)
font:write(10, HEIGHT-30, str:format(...), 24, 1,1,1,.5)
end
local function Screen()
local screen_x, screen_y, screen_rot
local content_w, content_h
local function update(screen, unscaled_w, unscaled_h)
local diagonal_pixels = math.sqrt(math.pow(screen.width, 2) + math.pow(screen.height, 2))
local cm_per_pixel = (screen.inches * 2.54) / diagonal_pixels
gl.setup(screen.width, screen.height)
screen_x = screen.x / cm_per_pixel
screen_y = screen.y / cm_per_pixel
screen_rot = screen.rotation
print(('initialized %d" screen (%dx%d)'):format(screen.inches, screen.width, screen.height))
content_w = unscaled_w / cm_per_pixel
content_h = unscaled_h / cm_per_pixel
end
local function draw(obj)
gl.rotate(-screen_rot, 0, 0, 1)
gl.translate(-screen_x, -screen_y)
util.draw_correct(obj, 0, 0, content_w, content_h)
end
return {
update = update;
draw = draw;
}
end
local screen = Screen()
local Image = {
slot_time = function(self)
return self.duration
end;
prepare = function(self)
self.obj = resource.load_image(self.file:copy())
end;
tick = function(self, now)
local state, w, h = self.obj:state()
screen.draw(self.obj)
end;
stop = function(self)
if self.obj then
self.obj:dispose()
self.obj = nil
end
end;
}
local Video = {
slot_time = function(self)
return VIDEO_PRELOAD_TIME + self.duration
end;
prepare = function(self)
end;
tick = function(self, now)
if not self.obj then
self.obj = resource.load_video{
file = self.file:copy();
paused = true;
}
end
if now < self.t_start + VIDEO_PRELOAD_TIME then
return
end
self.obj:start()
local state, w, h = self.obj:state()
if state ~= "loaded" and state ~= "finished" then
print[[
.--------------------------------------------.
WARNING:
lost video frame. video is most likely out
of sync. increase VIDEO_PRELOAD_TIME (on all
devices)
'--------------------------------------------'
]]
else
screen.draw(self.obj)
end
end;
stop = function(self)
if self.obj then
self.obj:dispose()
self.obj = nil
end
end;
}
local function Playlist()
local items = {}
local total_duration = 0
local function calc_start(idx, now)
local item = items[idx]
local epoch_offset = now % total_duration
local epoch_start = now - epoch_offset
item.t_start = epoch_start + item.epoch_offset
if item.t_start - PREPARE_TIME < now then
item.t_start = item.t_start + total_duration
end
item.t_prepare = item.t_start - PREPARE_TIME
item.t_end = item.t_start + item:slot_time()
-- pp(item)
end
local function tick(now)
local num_running = 0
local next_running = 99999999999999
if not assigned then
msg("[%s] screen not configured for this setup", serial)
return
end
if #items == 0 then
msg("[%s] no playlist configured", serial)
return
end
for idx = 1, #items do
local item = items[idx]
if item.t_prepare <= now and item.state == "waiting" then
print(now, "preparing ", item.file)
item:prepare()
item.state = "prepared"
elseif item.t_start <= now and item.state == "prepared" then
print(now, "running ", item.file)
item.state = "running"
elseif item.t_end <= now and item.state == "running" then
print(now, "resetting ", item.file)
item:stop()
calc_start(idx, now)
item.state = "waiting"
end
next_running = min(next_running, item.t_start)
if item.state == "running" then
item:tick(now)
num_running = num_running + 1
end
end
if num_running == 0 then
local wait = next_running - now
msg("[%s] waiting for sync %.1f", serial, wait)
end
end
local function stop_all()
for idx = 1, #items do
local item = items[idx]
item:stop()
end
end
local function set(new_items)
local now = os.time()
total_duration = 0
for idx = 1, #new_items do
local item = new_items[idx]
if item.type == "image" then
setmetatable(item, {__index = Image})
elseif item.type == "video" then
setmetatable(item, {__index = Video})
else
return error("unsupported type" .. item.type)
end
item.epoch_offset = total_duration
item.state = "waiting"
total_duration = total_duration + item:slot_time()
end
stop_all()
items = new_items
for idx = 1, #new_items do
calc_start(idx, now)
end
end
return {
set = set;
tick = tick;
}
end
local playlist = Playlist()
local function prepare_playlist(playlist)
if #playlist >= 2 then
return playlist
elseif #playlist == 1 then
-- only a single item? Copy it
local item = playlist[1]
playlist[#playlist+1] = {
file = item.file,
type = item.type,
duration = item.duration,
}
end
return playlist
end
util.file_watch("config.json", function(raw)
local config = json.decode(raw)
for idx = 1, #config.screens do
local screen_config = config.screens[idx]
if screen_config.serial == serial then
screen.update(screen_config, config.width, config.height)
assigned = true
return
end
end
assigned = false
end)
util.file_watch("playlist/config.json", function(raw)
local config = json.decode(raw)
local items = {}
for idx = 1, #config.playlist do
local item = config.playlist[idx]
items[#items+1] = {
file = resource.open_file('playlist/' .. item.file.asset_name),
type = item.file.type,
duration = item.duration,
}
end
playlist.set(prepare_playlist(items))
node.gc()
end)
function node.render()
gl.clear(0,0,0,1)
playlist.tick(os.time())
-- screen.draw(test)
end