cc-mek-scada/scada-common/util.lua

49 lines
975 B
Lua
Raw Normal View History

2022-04-05 21:28:19 +00:00
-- we are overwriting 'print' so save it first
local _print = print
-- print
function print(message)
term.write(message)
end
-- print line
function println(message)
_print(message)
end
2022-01-13 15:11:42 +00:00
-- timestamped print
function print_ts(message)
term.write(os.date("[%H:%M:%S] ") .. message)
end
2022-04-05 21:28:19 +00:00
-- timestamped print line
function println_ts(message)
_print(os.date("[%H:%M:%S] ") .. message)
end
2022-01-13 15:11:42 +00:00
-- ComputerCraft OS Timer based Watchdog
-- triggers a timer event if not fed within 'timeout' seconds
function new_watchdog(timeout)
local self = {
_timeout = timeout,
2022-04-05 21:28:19 +00:00
_wd_timer = os.startTimer(timeout)
2022-01-13 15:11:42 +00:00
}
local get_timer = function ()
return self._wd_timer
end
local feed = function ()
if self._wd_timer ~= nil then
os.cancelTimer(self._wd_timer)
end
self._wd_timer = os.startTimer(self._timeout)
end
return {
get_timer = get_timer,
feed = feed
}
end