-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.lua
More file actions
90 lines (72 loc) · 2.37 KB
/
node.lua
File metadata and controls
90 lines (72 loc) · 2.37 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
util.init_hosted()
-- this is only supported on the Raspi....
--util.noglobals()
node.set_flag("no_clear")
gl.setup(NATIVE_WIDTH, NATIVE_HEIGHT)
local modf = math.modf
local font = CONFIG.font
local fgcol = CONFIG.foreground_color
local bg = resource.create_colored_texture(CONFIG.background_color.rgba())
-- persistent state, survives file reloads
local state = rawget(_G, "._state")
if not state then
state = {}
state.baseTime = 0
state.timeH = nil
state.timeM = nil
state.timeS = nil
state.lastTimeUpdate = 0
rawset(_G, "._state", state)
end
util.data_mapper{
["clock/set"] = function(tm)
updateTime(tm)
end,
}
node.event("config_update", function()
config = assert(CONFIG, "ERROR: no CONFIG found")
font = CONFIG.font
fgcol = CONFIG.foreground_color
bg = resource.create_colored_texture(CONFIG.background_color.rgba())
end)
function getWallClockTime()
local now = sys.now()
return state.baseTime + now, now - state.lastTimeUpdate
end
-- interpolates since last update received
function getWallClockTimeString()
local h, m, s = state.timeH, state.timeM, state.timeS
if not (h and m and s) then
return "--:--:--"
end
local overflow = sys.now() - state.lastTimeUpdate
local si = math.floor(((s + overflow) % 60))
overflow = (s + overflow) / 60
local mi = math.floor((m + overflow) % 60)
overflow = (m + overflow) / 60
local hi = math.floor((h + overflow) % 24)
return ("%02d:%02d:%02d"):format(hi, mi, si)
end
function updateTime(tm)
local now = sys.now() -- this is relative to the start of info-beamer
state.lastTimeUpdate = now
local u, h, m, s = tm:match("([%d%.]+),(%d+),(%d+),([%d%.]+)")
state.baseTime = tonumber(u) - now
state.timeH = tonumber(h)
state.timeM = tonumber(m)
state.timeS = tonumber(s)
tools.debugPrint(4, "updated base time: " .. state.baseTime .. ", it's now: " ..
state.getWallClockTimeString() .. " (" .. state.getWallClockTime() .. ")")
end
local function drawtime(size)
timestr = getWallClockTimeString()
w = font:width(timestr, size)
font:write(WIDTH/2 -w/2, HEIGHT/2 - size/2, timestr, size, fgcol:rgba())
end
function node.render()
aspect = aspect or (WIDTH / HEIGHT)
gl.clear(0.0, 0.0, 0.0, 1.0)
gl.ortho()
bg:draw(0, 0, NATIVE_WIDTH, NATIVE_HEIGHT)
drawtime(HEIGHT/5)
end