added util timer functions, tweaks to flasher and some debug prints for #110

This commit is contained in:
Mikayla Fischler 2022-10-23 01:41:02 -04:00
parent d202a49011
commit 307bf6e2c8
4 changed files with 69 additions and 26 deletions

View File

@ -109,7 +109,7 @@ function renderer.start_ui()
end end
-- start flasher callback task -- start flasher callback task
flasher.init() flasher.run()
-- report ui as ready -- report ui as ready
engine.ui_ready = true engine.ui_ready = true

View File

@ -42,17 +42,16 @@ local function callback_250ms()
end end
end end
-- start the flasher periodic -- start/resume the flasher periodic
function flasher.init() function flasher.run()
active = true active = true
callback_counter = 0
registry = { {}, {}, {} }
callback_250ms() callback_250ms()
end end
-- clear all blinking indicators and stop the flasher periodic -- clear all blinking indicators and stop the flasher periodic
function flasher.clear() function flasher.clear()
active = false active = false
callback_counter = 0
registry = { {}, {}, {} } registry = { {}, {}, {} }
end end

View File

@ -13,8 +13,14 @@ local registry = {}
---@param time number seconds ---@param time number seconds
---@param f function callback function ---@param f function callback function
function tcallbackdsp.dispatch(time, f) function tcallbackdsp.dispatch(time, f)
---@diagnostic disable-next-line: undefined-field local timer = util.start_timer(time)
registry[os.startTimer(time)] = { callback = f } registry[timer] = {
callback = f,
duration = time,
expiry = time + util.time_s()
}
log.debug("queued callback for " .. util.strval(f) .. " timer #" .. timer)
end end
-- request a function to be called after the specified time, aborting any registered instances of that function reference -- request a function to be called after the specified time, aborting any registered instances of that function reference
@ -25,25 +31,44 @@ function tcallbackdsp.dispatch_unique(time, f)
for timer, entry in pairs(registry) do for timer, entry in pairs(registry) do
if entry.callback == f then if entry.callback == f then
-- found an instance of this function reference, abort it -- found an instance of this function reference, abort it
log.debug(util.c("TCD: aborting duplicate timer callback (timer: ", timer, ", function: ", f, ")")) log.debug(util.c("TCD: aborting duplicate timer callback (timer: ", timer, ", ", f, ")"))
-- cancel event and remove from registry (even if it fires it won't call) -- cancel event and remove from registry (even if it fires it won't call)
---@diagnostic disable-next-line: undefined-field util.cancel_timer(timer)
os.cancelTimer(timer)
registry[timer] = nil registry[timer] = nil
end end
end end
---@diagnostic disable-next-line: undefined-field local timer = util.start_timer(time)
registry[os.startTimer(time)] = { callback = f } registry[timer] = {
callback = f,
duration = time,
expiry = time + util.time_s()
}
log.debug("queued callback for " .. util.strval(f) .. " timer #" .. timer)
end end
-- lookup a timer event and execute the callback if found -- lookup a timer event and execute the callback if found
---@param event integer timer event timer ID ---@param event integer timer event timer ID
function tcallbackdsp.handle(event) function tcallbackdsp.handle(event)
if registry[event] ~= nil then if registry[event] ~= nil then
registry[event].callback() local callback = registry[event].callback
-- clear first so that dispatch_unique call from inside callback won't throw a debug message
registry[event] = nil registry[event] = nil
callback()
end
end
-- identify any overdo callbacks
--
-- prints to log debug output
function tcallbackdsp.diagnostics()
for timer, entry in pairs(registry) do
if entry.expiry >= util.time_s() then
local overtime = util.time_s() - entry.expiry
log.debug(util.c("TCD: unserviced timer ", timer, " for callback ", entry.callback, " is at least ", overtime, "s late"))
end
end end
end end

View File

@ -202,6 +202,33 @@ function util.pull_event(target_event)
return os.pullEventRaw(target_event) return os.pullEventRaw(target_event)
end end
-- OS queue event raw wrapper with types
---@param event os_event
---@param param1 any
---@param param2 any
---@param param3 any
---@param param4 any
---@param param5 any
function util.push_event(event, param1, param2, param3, param4, param5)
---@diagnostic disable-next-line: undefined-field
return os.queueEvent(event, param1, param2, param3, param4, param5)
end
-- start an OS timer
---@param t number timer duration in seconds
---@return integer timer ID
function util.start_timer(t)
---@diagnostic disable-next-line: undefined-field
return os.startTimer(t)
end
-- cancel an OS timer
---@param timer integer timer ID
function util.cancel_timer(timer)
---@diagnostic disable-next-line: undefined-field
os.cancelTimer(timer)
end
-- PARALLELIZATION -- -- PARALLELIZATION --
-- protected sleep call so we still are in charge of catching termination -- protected sleep call so we still are in charge of catching termination
@ -312,14 +339,9 @@ end
--- ---
--- triggers a timer event if not fed within 'timeout' seconds --- triggers a timer event if not fed within 'timeout' seconds
function util.new_watchdog(timeout) function util.new_watchdog(timeout)
---@diagnostic disable-next-line: undefined-field
local start_timer = os.startTimer
---@diagnostic disable-next-line: undefined-field
local cancel_timer = os.cancelTimer
local self = { local self = {
timeout = timeout, timeout = timeout,
wd_timer = start_timer(timeout) wd_timer = util.start_timer(timeout)
} }
---@class watchdog ---@class watchdog
@ -333,15 +355,15 @@ function util.new_watchdog(timeout)
-- satiate the beast -- satiate the beast
function public.feed() function public.feed()
if self.wd_timer ~= nil then if self.wd_timer ~= nil then
cancel_timer(self.wd_timer) util.cancel_timer(self.wd_timer)
end end
self.wd_timer = start_timer(self.timeout) self.wd_timer = util.start_timer(self.timeout)
end end
-- cancel the watchdog -- cancel the watchdog
function public.cancel() function public.cancel()
if self.wd_timer ~= nil then if self.wd_timer ~= nil then
cancel_timer(self.wd_timer) util.cancel_timer(self.wd_timer)
end end
end end
@ -355,9 +377,6 @@ end
--- ---
--- fires a timer event at the specified period, does not start at construct time --- fires a timer event at the specified period, does not start at construct time
function util.new_clock(period) function util.new_clock(period)
---@diagnostic disable-next-line: undefined-field
local start_timer = os.startTimer
local self = { local self = {
period = period, period = period,
timer = nil timer = nil
@ -373,7 +392,7 @@ function util.new_clock(period)
-- start the clock -- start the clock
function public.start() function public.start()
self.timer = start_timer(self.period) self.timer = util.start_timer(self.period)
end end
return public return public