2023-04-08 20:49:54 +00:00
|
|
|
-- Indicator RGB LED Graphics Element
|
|
|
|
|
|
|
|
local element = require("graphics.element")
|
|
|
|
|
|
|
|
---@class indicator_led_rgb_args
|
|
|
|
---@field label string indicator label
|
|
|
|
---@field colors table colors to use
|
|
|
|
---@field min_label_width? integer label length if omitted
|
|
|
|
---@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
|
2023-04-08 20:49:54 +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
|
2023-04-08 20:49:54 +00:00
|
|
|
|
2023-04-09 02:00:51 +00:00
|
|
|
-- new RGB LED indicator light
|
2023-04-08 20:49:54 +00:00
|
|
|
---@nodiscard
|
|
|
|
---@param args indicator_led_rgb_args
|
|
|
|
---@return graphics_element element, element_id id
|
|
|
|
local function indicator_led_rgb(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")
|
2023-04-08 20:49:54 +00:00
|
|
|
|
|
|
|
args.height = 1
|
2023-04-21 00:40:28 +00:00
|
|
|
args.width = math.max(args.min_label_width or 0, string.len(args.label)) + 2
|
2023-04-08 20:49:54 +00:00
|
|
|
|
|
|
|
-- create new graphics element base object
|
|
|
|
local e = element.new(args)
|
|
|
|
|
|
|
|
e.value = 1
|
|
|
|
|
|
|
|
-- on state change
|
|
|
|
---@param new_state integer indicator state
|
|
|
|
function e.on_update(new_state)
|
|
|
|
e.value = new_state
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_set_cur(1, 1)
|
2023-04-08 20:49:54 +00:00
|
|
|
if type(args.colors[new_state]) == "number" then
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_blit("\x8c", colors.toBlit(args.colors[new_state]), e.fg_bg.blit_bkg)
|
2023-04-08 20:49:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- set indicator state
|
|
|
|
---@param val integer indicator state
|
|
|
|
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(e.value)
|
|
|
|
if string.len(args.label) > 0 then
|
|
|
|
e.w_set_cur(3, 1)
|
|
|
|
e.w_write(args.label)
|
|
|
|
end
|
2023-04-21 00:40:28 +00:00
|
|
|
end
|
2023-04-08 20:49:54 +00:00
|
|
|
|
2023-09-29 23:34:10 +00:00
|
|
|
-- initial draw
|
|
|
|
e.redraw()
|
|
|
|
|
2023-05-30 23:51:10 +00:00
|
|
|
return e.complete()
|
2023-04-08 20:49:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return indicator_led_rgb
|