2022-06-08 21:16:53 +00:00
|
|
|
-- Indicator Light Graphics Element
|
|
|
|
|
2022-11-25 03:49:35 +00:00
|
|
|
local util = require("scada-common.util")
|
|
|
|
|
2022-06-08 21:16:53 +00:00
|
|
|
local element = require("graphics.element")
|
2022-10-12 20:37:11 +00:00
|
|
|
local flasher = require("graphics.flasher")
|
2022-06-08 21:16:53 +00:00
|
|
|
|
|
|
|
---@class indicator_light_args
|
2022-06-11 16:21:14 +00:00
|
|
|
---@field label string indicator label
|
2022-06-08 21:16:53 +00:00
|
|
|
---@field colors cpair on/off colors (a/b respectively)
|
2022-06-11 16:21:14 +00:00
|
|
|
---@field min_label_width? integer label length if omitted
|
2022-10-12 20:37:11 +00:00
|
|
|
---@field flash? boolean whether to flash on true rather than stay on
|
|
|
|
---@field period? PERIOD flash period
|
2022-06-08 21:16:53 +00:00
|
|
|
---@field parent graphics_element
|
2022-07-28 14:09:34 +00:00
|
|
|
---@field id? string element id
|
2022-06-08 21:16:53 +00:00
|
|
|
---@field x? integer 1 if omitted
|
2023-07-10 03:42:44 +00:00
|
|
|
---@field y? integer auto incremented if omitted
|
2022-06-11 21:06:32 +00:00
|
|
|
---@field fg_bg? cpair foreground/background colors
|
2023-05-25 21:40:16 +00:00
|
|
|
---@field hidden? boolean true to hide on initial draw
|
2022-06-08 21:16:53 +00:00
|
|
|
|
|
|
|
-- new indicator light
|
2023-02-25 00:50:01 +00:00
|
|
|
---@nodiscard
|
2022-06-08 21:16:53 +00:00
|
|
|
---@param args indicator_light_args
|
2022-07-28 14:09:34 +00:00
|
|
|
---@return graphics_element element, element_id id
|
2022-06-08 21:16:53 +00:00
|
|
|
local function indicator_light(args)
|
2023-09-30 15:46:47 +00:00
|
|
|
element.assert(type(args.label) == "string", "label is a required field")
|
|
|
|
element.assert(type(args.colors) == "table", "colors is a required field")
|
2022-06-16 15:19:32 +00:00
|
|
|
|
2022-10-12 20:37:11 +00:00
|
|
|
if args.flash then
|
2023-09-30 15:46:47 +00:00
|
|
|
element.assert(util.is_int(args.period), "period is a required field if flash is enabled")
|
2022-10-12 20:37:11 +00:00
|
|
|
end
|
|
|
|
|
2022-06-16 15:19:32 +00:00
|
|
|
args.height = 1
|
2022-07-16 17:25:07 +00:00
|
|
|
args.width = math.max(args.min_label_width or 1, string.len(args.label)) + 2
|
2022-06-08 21:16:53 +00:00
|
|
|
|
2022-10-12 20:37:11 +00:00
|
|
|
local flash_on = true
|
|
|
|
|
2022-06-08 21:16:53 +00:00
|
|
|
-- create new graphics element base object
|
|
|
|
local e = element.new(args)
|
|
|
|
|
2023-09-29 23:34:10 +00:00
|
|
|
e.value = false
|
|
|
|
|
2022-10-12 20:37:11 +00:00
|
|
|
-- called by flasher when enabled
|
|
|
|
local function flash_callback()
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_set_cur(1, 1)
|
2022-10-23 16:21:17 +00:00
|
|
|
|
2022-10-12 20:37:11 +00:00
|
|
|
if flash_on then
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_blit(" \x95", "0" .. args.colors.blit_a, args.colors.blit_a .. e.fg_bg.blit_bkg)
|
2022-06-08 21:16:53 +00:00
|
|
|
else
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_blit(" \x95", "0" .. args.colors.blit_b, args.colors.blit_b .. e.fg_bg.blit_bkg)
|
2022-06-08 21:16:53 +00:00
|
|
|
end
|
2022-10-12 20:37:11 +00:00
|
|
|
|
|
|
|
flash_on = not flash_on
|
|
|
|
end
|
|
|
|
|
|
|
|
-- enable light or start flashing
|
|
|
|
local function enable()
|
|
|
|
if args.flash then
|
|
|
|
flash_on = true
|
|
|
|
flasher.start(flash_callback, args.period)
|
|
|
|
else
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_set_cur(1, 1)
|
|
|
|
e.w_blit(" \x95", "0" .. args.colors.blit_a, args.colors.blit_a .. e.fg_bg.blit_bkg)
|
2022-10-12 20:37:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- disable light or stop flashing
|
|
|
|
local function disable()
|
|
|
|
if args.flash then
|
|
|
|
flash_on = false
|
|
|
|
flasher.stop(flash_callback)
|
|
|
|
end
|
|
|
|
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_set_cur(1, 1)
|
|
|
|
e.w_blit(" \x95", "0" .. args.colors.blit_b, args.colors.blit_b .. e.fg_bg.blit_bkg)
|
2022-10-12 20:37:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- on state change
|
|
|
|
---@param new_state boolean indicator state
|
|
|
|
function e.on_update(new_state)
|
|
|
|
e.value = new_state
|
|
|
|
if new_state then enable() else disable() end
|
2022-06-08 21:16:53 +00:00
|
|
|
end
|
|
|
|
|
2022-09-12 19:58:43 +00:00
|
|
|
-- set indicator state
|
|
|
|
---@param val boolean indicator state
|
2022-09-12 16:59:28 +00:00
|
|
|
function e.set_value(val) e.on_update(val) end
|
|
|
|
|
2023-09-29 23:34:10 +00:00
|
|
|
-- draw label and indicator light
|
|
|
|
function e.redraw()
|
|
|
|
e.on_update(false)
|
|
|
|
e.w_set_cur(3, 1)
|
|
|
|
e.w_write(args.label)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- initial draw
|
|
|
|
e.redraw()
|
2022-07-16 17:25:07 +00:00
|
|
|
|
2023-05-30 23:51:10 +00:00
|
|
|
return e.complete()
|
2022-06-08 21:16:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return indicator_light
|