cc-mek-scada/graphics/elements/indicators/trilight.lua

112 lines
3.4 KiB
Lua
Raw Normal View History

2022-09-07 14:39:51 +00:00
-- Tri-State Indicator Light Graphics Element
local util = require("scada-common.util")
2022-09-07 14:39:51 +00:00
local element = require("graphics.element")
local flasher = require("graphics.flasher")
2022-09-07 14:39:51 +00:00
---@class tristate_indicator_light_args
---@field label string indicator label
---@field c1 color color for state 1
---@field c2 color color for state 2
---@field c3 color color for state 3
---@field min_label_width? integer label length if omitted
---@field flash? boolean whether to flash on state 2 or 3 rather than stay on
---@field period? PERIOD flash period
2022-09-07 14:39:51 +00:00
---@field parent graphics_element
---@field id? string element id
---@field x? integer 1 if omitted
2023-07-10 03:42:44 +00:00
---@field y? integer auto incremented if omitted
2022-09-07 14:39:51 +00:00
---@field fg_bg? cpair foreground/background colors
---@field hidden? boolean true to hide on initial draw
2022-09-07 14:39:51 +00:00
-- new tri-state indicator light
2023-02-25 00:50:01 +00:00
---@nodiscard
2022-09-07 14:39:51 +00:00
---@param args tristate_indicator_light_args
---@return graphics_element element, element_id id
local function tristate_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.c1) == "number", "c1 is a required field")
element.assert(type(args.c2) == "number", "c2 is a required field")
element.assert(type(args.c3) == "number", "c3 is a required field")
2022-09-07 14:39:51 +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")
end
2022-09-07 14:39:51 +00:00
args.height = 1
args.width = math.max(args.min_label_width or 1, string.len(args.label)) + 2
-- create new graphics element base object
local e = element.new(args)
e.value = 1
local flash_on = true
local c1 = colors.toBlit(args.c1)
local c2 = colors.toBlit(args.c2)
local c3 = colors.toBlit(args.c3)
2022-09-07 14:39:51 +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)
if flash_on then
if e.value == 2 then
2023-08-31 01:11:57 +00:00
e.w_blit(" \x95", "0" .. c2, c2 .. e.fg_bg.blit_bkg)
elseif e.value == 3 then
2023-08-31 01:11:57 +00:00
e.w_blit(" \x95", "0" .. c3, c3 .. e.fg_bg.blit_bkg)
end
else
2023-08-31 01:11:57 +00:00
e.w_blit(" \x95", "0" .. c1, c1 .. e.fg_bg.blit_bkg)
end
flash_on = not flash_on
end
2022-09-07 14:39:51 +00:00
-- on state change
---@param new_state integer indicator state
function e.on_update(new_state)
local was_off = e.value <= 1
e.value = new_state
2023-08-31 01:11:57 +00:00
e.w_set_cur(1, 1)
if args.flash then
if was_off and (new_state > 1) then
flash_on = true
flasher.start(flash_callback, args.period)
elseif new_state <= 1 then
flash_on = false
flasher.stop(flash_callback)
2023-08-31 01:11:57 +00:00
e.w_blit(" \x95", "0" .. c1, c1 .. e.fg_bg.blit_bkg)
end
elseif new_state == 2 then
2023-08-31 01:11:57 +00:00
e.w_blit(" \x95", "0" .. c2, c2 .. e.fg_bg.blit_bkg)
2022-09-07 14:39:51 +00:00
elseif new_state == 3 then
2023-08-31 01:11:57 +00:00
e.w_blit(" \x95", "0" .. c3, c3 .. e.fg_bg.blit_bkg)
2022-09-07 14:39:51 +00:00
else
2023-08-31 01:11:57 +00:00
e.w_blit(" \x95", "0" .. c1, c1 .. e.fg_bg.blit_bkg)
2022-09-07 14:39:51 +00:00
end
end
2022-09-12 19:58:43 +00:00
-- set indicator state
---@param val integer indicator state
function e.set_value(val) e.on_update(val) end
-- draw light and label
function e.redraw()
e.on_update(1)
e.w_write(args.label)
end
-- initial draw
e.redraw()
2022-09-07 14:39:51 +00:00
return e.complete()
2022-09-07 14:39:51 +00:00
end
return tristate_indicator_light