2022-06-08 21:16:53 +00:00
|
|
|
-- Indicator Light Graphics Element
|
|
|
|
|
|
|
|
local element = require("graphics.element")
|
2022-10-12 20:37:11 +00:00
|
|
|
local flasher = require("graphics.flasher")
|
|
|
|
local util = require("scada-common.util")
|
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
|
|
|
|
---@field y? integer 1 if omitted
|
2022-06-11 21:06:32 +00:00
|
|
|
---@field fg_bg? cpair foreground/background colors
|
2022-06-08 21:16:53 +00:00
|
|
|
|
|
|
|
-- new indicator light
|
|
|
|
---@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)
|
2022-06-16 15:19:32 +00:00
|
|
|
assert(type(args.label) == "string", "graphics.elements.indicators.light: label is a required field")
|
|
|
|
assert(type(args.colors) == "table", "graphics.elements.indicators.light: colors is a required field")
|
|
|
|
|
2022-10-12 20:37:11 +00:00
|
|
|
if args.flash then
|
|
|
|
assert(util.is_int(args.period), "graphics.elements.indicators.light: period is a required field if flash is enabled")
|
|
|
|
end
|
|
|
|
|
2022-06-16 15:19:32 +00:00
|
|
|
-- single line
|
|
|
|
args.height = 1
|
2022-06-11 21:06:32 +00:00
|
|
|
|
2022-06-08 21:16:53 +00:00
|
|
|
-- determine width
|
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
|
|
|
-- flasher state
|
|
|
|
local flash_on = true
|
|
|
|
|
2022-06-08 21:16:53 +00:00
|
|
|
-- create new graphics element base object
|
|
|
|
local e = element.new(args)
|
|
|
|
|
2022-10-12 20:37:11 +00:00
|
|
|
-- called by flasher when enabled
|
|
|
|
local function flash_callback()
|
2022-06-08 21:16:53 +00:00
|
|
|
e.window.setCursorPos(1, 1)
|
2022-10-12 20:37:11 +00:00
|
|
|
|
|
|
|
if flash_on then
|
2022-07-16 17:25:07 +00:00
|
|
|
e.window.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
|
2022-07-16 17:25:07 +00:00
|
|
|
e.window.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
|
|
|
|
e.window.setCursorPos(1, 1)
|
|
|
|
e.window.blit(" \x95", "0" .. args.colors.blit_a, args.colors.blit_a .. e.fg_bg.blit_bkg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- disable light or stop flashing
|
|
|
|
local function disable()
|
|
|
|
if args.flash then
|
|
|
|
flash_on = false
|
|
|
|
flasher.stop(flash_callback)
|
|
|
|
end
|
|
|
|
|
|
|
|
e.window.setCursorPos(1, 1)
|
|
|
|
e.window.blit(" \x95", "0" .. args.colors.blit_b, args.colors.blit_b .. e.fg_bg.blit_bkg)
|
|
|
|
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
|
|
|
|
|
2022-07-16 17:25:07 +00:00
|
|
|
-- write label and initial indicator light
|
|
|
|
e.on_update(false)
|
2022-10-12 20:37:11 +00:00
|
|
|
e.window.setCursorPos(3, 1)
|
2022-07-16 17:25:07 +00:00
|
|
|
e.window.write(args.label)
|
|
|
|
|
2022-07-28 15:17:34 +00:00
|
|
|
return e.get()
|
2022-06-08 21:16:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return indicator_light
|