2022-10-12 20:37:11 +00:00
|
|
|
--
|
|
|
|
-- Indicator Light Flasher
|
|
|
|
--
|
|
|
|
|
2023-06-03 21:40:57 +00:00
|
|
|
local tcd = require("scada-common.tcd")
|
2022-10-12 20:37:11 +00:00
|
|
|
|
|
|
|
local flasher = {}
|
|
|
|
|
|
|
|
-- note: no additional call needs to be made in a main loop as this class automatically uses the TCD to operate
|
|
|
|
|
|
|
|
---@alias PERIOD integer
|
|
|
|
local PERIOD = {
|
|
|
|
BLINK_250_MS = 1,
|
|
|
|
BLINK_500_MS = 2,
|
|
|
|
BLINK_1000_MS = 3
|
|
|
|
}
|
|
|
|
|
|
|
|
flasher.PERIOD = PERIOD
|
|
|
|
|
|
|
|
local active = false
|
|
|
|
local registry = { {}, {}, {} } -- one registry table per period
|
|
|
|
local callback_counter = 0
|
|
|
|
|
2023-02-25 00:50:01 +00:00
|
|
|
-- blink registered indicators<br>
|
2022-10-20 16:23:00 +00:00
|
|
|
-- this assumes it is called every 250ms, it does no checking of time on its own
|
|
|
|
local function callback_250ms()
|
|
|
|
if active then
|
2022-11-02 16:02:52 +00:00
|
|
|
for _, f in ipairs(registry[PERIOD.BLINK_250_MS]) do f() end
|
2022-10-20 16:23:00 +00:00
|
|
|
|
|
|
|
if callback_counter % 2 == 0 then
|
2022-11-02 16:02:52 +00:00
|
|
|
for _, f in ipairs(registry[PERIOD.BLINK_500_MS]) do f() end
|
2022-10-20 16:23:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if callback_counter % 4 == 0 then
|
2022-11-02 16:02:52 +00:00
|
|
|
for _, f in ipairs(registry[PERIOD.BLINK_1000_MS]) do f() end
|
2022-10-20 16:23:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
callback_counter = callback_counter + 1
|
|
|
|
|
2022-10-21 19:15:56 +00:00
|
|
|
tcd.dispatch_unique(0.25, callback_250ms)
|
2022-10-20 16:23:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-23 05:41:02 +00:00
|
|
|
-- start/resume the flasher periodic
|
|
|
|
function flasher.run()
|
2023-07-10 03:24:41 +00:00
|
|
|
if not active then
|
|
|
|
active = true
|
|
|
|
callback_250ms()
|
|
|
|
end
|
2022-10-12 20:37:11 +00:00
|
|
|
end
|
|
|
|
|
2022-10-20 16:23:00 +00:00
|
|
|
-- clear all blinking indicators and stop the flasher periodic
|
2022-10-12 20:37:11 +00:00
|
|
|
function flasher.clear()
|
|
|
|
active = false
|
2022-10-23 05:41:02 +00:00
|
|
|
callback_counter = 0
|
2022-10-12 20:37:11 +00:00
|
|
|
registry = { {}, {}, {} }
|
|
|
|
end
|
|
|
|
|
2023-02-25 00:50:01 +00:00
|
|
|
-- register a function to be called on the selected blink period<br>
|
2022-10-12 20:37:11 +00:00
|
|
|
-- times are not strictly enforced, but all with a given period will be set at the same time
|
|
|
|
---@param f function function to call each period
|
|
|
|
---@param period PERIOD time period option (1, 2, or 3)
|
|
|
|
function flasher.start(f, period)
|
|
|
|
if type(registry[period]) == "table" then
|
|
|
|
table.insert(registry[period], f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- stop a function from being called at the blink period
|
|
|
|
---@param f function function callback registered
|
|
|
|
function flasher.stop(f)
|
|
|
|
for i = 1, #registry do
|
2022-11-02 16:02:52 +00:00
|
|
|
for key, val in ipairs(registry[i]) do
|
|
|
|
if val == f then
|
|
|
|
table.remove(registry[i], key)
|
|
|
|
return
|
2022-10-12 20:37:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return flasher
|