2023-02-23 04:09:47 +00:00
|
|
|
--
|
|
|
|
-- Graphics Rendering Control
|
|
|
|
--
|
|
|
|
|
2023-04-21 18:53:28 +00:00
|
|
|
local log = require("scada-common.log")
|
2022-09-03 14:50:14 +00:00
|
|
|
|
2023-07-12 00:32:10 +00:00
|
|
|
local iocontrol = require("coordinator.iocontrol")
|
|
|
|
|
2023-04-21 18:53:28 +00:00
|
|
|
local style = require("coordinator.ui.style")
|
2023-07-10 03:31:56 +00:00
|
|
|
local pgi = require("coordinator.ui.pgi")
|
2022-09-10 14:42:56 +00:00
|
|
|
|
2023-08-10 03:26:06 +00:00
|
|
|
local flow_view = require("coordinator.ui.layout.flow_view")
|
2023-07-10 03:31:56 +00:00
|
|
|
local panel_view = require("coordinator.ui.layout.front_panel")
|
2023-04-21 18:53:28 +00:00
|
|
|
local main_view = require("coordinator.ui.layout.main_view")
|
|
|
|
local unit_view = require("coordinator.ui.layout.unit_view")
|
2022-06-06 19:42:39 +00:00
|
|
|
|
2023-09-30 16:19:04 +00:00
|
|
|
local core = require("graphics.core")
|
2023-04-21 18:53:28 +00:00
|
|
|
local flasher = require("graphics.flasher")
|
|
|
|
|
|
|
|
local DisplayBox = require("graphics.elements.displaybox")
|
2023-02-03 04:07:09 +00:00
|
|
|
|
2023-09-30 16:19:04 +00:00
|
|
|
---@class coord_renderer
|
2022-05-29 18:34:09 +00:00
|
|
|
local renderer = {}
|
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
-- render engine
|
2022-05-29 18:34:09 +00:00
|
|
|
local engine = {
|
2023-04-21 18:53:28 +00:00
|
|
|
monitors = nil, ---@type monitors_struct|nil
|
|
|
|
dmesg_window = nil, ---@type table|nil
|
|
|
|
ui_ready = false,
|
2023-07-10 03:31:56 +00:00
|
|
|
fp_ready = false,
|
2023-04-21 18:53:28 +00:00
|
|
|
ui = {
|
2023-07-10 03:31:56 +00:00
|
|
|
front_panel = nil, ---@type graphics_element|nil
|
2023-04-21 18:53:28 +00:00
|
|
|
main_display = nil, ---@type graphics_element|nil
|
2023-08-10 03:26:06 +00:00
|
|
|
flow_display = nil, ---@type graphics_element|nil
|
2023-04-21 18:53:28 +00:00
|
|
|
unit_displays = {}
|
2023-08-21 03:23:23 +00:00
|
|
|
},
|
|
|
|
disable_flow_view = false
|
2022-06-06 19:42:39 +00:00
|
|
|
}
|
|
|
|
|
2023-02-28 04:59:46 +00:00
|
|
|
-- init a display to the "default", but set text scale to 0.5
|
2022-07-05 16:46:31 +00:00
|
|
|
---@param monitor table monitor
|
2023-02-28 04:59:46 +00:00
|
|
|
local function _init_display(monitor)
|
2022-06-06 19:42:39 +00:00
|
|
|
monitor.setTextScale(0.5)
|
|
|
|
monitor.setTextColor(colors.white)
|
|
|
|
monitor.setBackgroundColor(colors.black)
|
|
|
|
monitor.clear()
|
|
|
|
monitor.setCursorPos(1, 1)
|
2022-07-05 16:46:31 +00:00
|
|
|
|
2023-02-28 04:59:46 +00:00
|
|
|
-- set overridden colors
|
|
|
|
for i = 1, #style.colors do
|
|
|
|
monitor.setPaletteColor(style.colors[i].c, style.colors[i].hex)
|
2022-07-05 16:46:31 +00:00
|
|
|
end
|
2022-06-06 19:42:39 +00:00
|
|
|
end
|
|
|
|
|
2023-08-21 03:23:23 +00:00
|
|
|
-- disable the flow view
|
|
|
|
---@param disable boolean
|
|
|
|
function renderer.legacy_disable_flow_view(disable)
|
|
|
|
engine.disable_flow_view = disable
|
|
|
|
end
|
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
-- link to the monitor peripherals
|
2022-05-29 18:34:09 +00:00
|
|
|
---@param monitors monitors_struct
|
2023-07-12 00:32:10 +00:00
|
|
|
function renderer.set_displays(monitors)
|
|
|
|
engine.monitors = monitors
|
2022-05-29 18:34:09 +00:00
|
|
|
|
2023-07-12 00:32:10 +00:00
|
|
|
-- report to front panel as connected
|
2023-08-21 03:23:23 +00:00
|
|
|
iocontrol.fp_monitor_state("main", engine.monitors.primary ~= nil)
|
|
|
|
iocontrol.fp_monitor_state("flow", engine.monitors.flow ~= nil)
|
2023-07-12 00:32:10 +00:00
|
|
|
for i = 1, #engine.monitors.unit_displays do iocontrol.fp_monitor_state(i, true) end
|
2022-09-13 20:07:21 +00:00
|
|
|
end
|
|
|
|
|
2023-02-28 04:59:46 +00:00
|
|
|
-- init all displays in use by the renderer
|
|
|
|
function renderer.init_displays()
|
2023-08-10 03:26:06 +00:00
|
|
|
-- init primary and flow monitors
|
2023-02-28 04:59:46 +00:00
|
|
|
_init_display(engine.monitors.primary)
|
2023-08-21 03:23:23 +00:00
|
|
|
if not engine.disable_flow_view then _init_display(engine.monitors.flow) end
|
2022-05-29 18:34:09 +00:00
|
|
|
|
2023-02-28 04:59:46 +00:00
|
|
|
-- init unit displays
|
2023-04-22 03:43:28 +00:00
|
|
|
for _, monitor in ipairs(engine.monitors.unit_displays) do
|
2023-02-28 04:59:46 +00:00
|
|
|
_init_display(monitor)
|
2022-05-29 18:34:09 +00:00
|
|
|
end
|
2023-07-10 03:31:56 +00:00
|
|
|
|
|
|
|
-- init terminal
|
|
|
|
term.setTextColor(colors.white)
|
|
|
|
term.setBackgroundColor(colors.black)
|
|
|
|
term.clear()
|
|
|
|
term.setCursorPos(1, 1)
|
|
|
|
|
|
|
|
-- set overridden colors
|
|
|
|
for i = 1, #style.fp.colors do
|
|
|
|
term.setPaletteColor(style.fp.colors[i].c, style.fp.colors[i].hex)
|
|
|
|
end
|
2022-05-29 18:34:09 +00:00
|
|
|
end
|
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
-- initialize the dmesg output window
|
2022-05-29 18:34:09 +00:00
|
|
|
function renderer.init_dmesg()
|
|
|
|
local disp_x, disp_y = engine.monitors.primary.getSize()
|
|
|
|
engine.dmesg_window = window.create(engine.monitors.primary, 1, 1, disp_x, disp_y)
|
|
|
|
log.direct_dmesg(engine.dmesg_window)
|
|
|
|
end
|
|
|
|
|
2023-09-30 17:31:41 +00:00
|
|
|
-- try to start the front panel
|
|
|
|
---@return boolean success, any error_msg
|
|
|
|
function renderer.try_start_fp()
|
|
|
|
local status, msg = true, nil
|
|
|
|
|
2023-07-10 03:31:56 +00:00
|
|
|
if not engine.fp_ready then
|
|
|
|
-- show front panel view on terminal
|
2023-09-30 17:31:41 +00:00
|
|
|
status, msg = pcall(function ()
|
|
|
|
engine.ui.front_panel = DisplayBox{window=term.native(),fg_bg=style.fp.root}
|
|
|
|
panel_view(engine.ui.front_panel, #engine.monitors.unit_displays)
|
|
|
|
end)
|
|
|
|
|
|
|
|
if status then
|
|
|
|
-- start flasher callback task and report ready
|
|
|
|
flasher.run()
|
|
|
|
engine.fp_ready = true
|
|
|
|
else
|
|
|
|
-- report fail and close front panel
|
|
|
|
msg = core.extract_assert_msg(msg)
|
|
|
|
renderer.close_fp()
|
|
|
|
end
|
2023-07-10 03:31:56 +00:00
|
|
|
end
|
2023-09-30 17:31:41 +00:00
|
|
|
|
|
|
|
return status, msg
|
2023-07-10 03:31:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- close out the front panel
|
|
|
|
function renderer.close_fp()
|
|
|
|
if engine.fp_ready then
|
|
|
|
if not engine.ui_ready then
|
|
|
|
-- stop blinking indicators
|
|
|
|
flasher.clear()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- disable PGI
|
|
|
|
pgi.unlink()
|
|
|
|
|
|
|
|
-- hide to stop animation callbacks and clear root UI elements
|
|
|
|
engine.ui.front_panel.hide()
|
|
|
|
engine.ui.front_panel = nil
|
|
|
|
engine.fp_ready = false
|
|
|
|
|
|
|
|
-- restore colors
|
|
|
|
for i = 1, #style.colors do
|
|
|
|
local r, g, b = term.nativePaletteColor(style.colors[i].c)
|
|
|
|
term.setPaletteColor(style.colors[i].c, r, g, b)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- reset terminal
|
|
|
|
term.setTextColor(colors.white)
|
|
|
|
term.setBackgroundColor(colors.black)
|
|
|
|
term.clear()
|
|
|
|
term.setCursorPos(1, 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-30 17:31:41 +00:00
|
|
|
-- try to start the main GUI
|
2023-09-30 16:19:04 +00:00
|
|
|
---@return boolean success, any error_msg
|
|
|
|
function renderer.try_start_ui()
|
|
|
|
local status, msg = true, nil
|
|
|
|
|
2023-07-12 00:32:10 +00:00
|
|
|
if not engine.ui_ready then
|
|
|
|
-- hide dmesg
|
|
|
|
engine.dmesg_window.setVisible(false)
|
|
|
|
|
2023-09-30 16:19:04 +00:00
|
|
|
status, msg = pcall(function ()
|
|
|
|
-- show main view on main monitor
|
|
|
|
if engine.monitors.primary ~= nil then
|
|
|
|
engine.ui.main_display = DisplayBox{window=engine.monitors.primary,fg_bg=style.root}
|
|
|
|
main_view(engine.ui.main_display)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- show flow view on flow monitor
|
|
|
|
if engine.monitors.flow ~= nil then
|
|
|
|
engine.ui.flow_display = DisplayBox{window=engine.monitors.flow,fg_bg=style.root}
|
|
|
|
flow_view(engine.ui.flow_display)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- show unit views on unit displays
|
|
|
|
for idx, display in pairs(engine.monitors.unit_displays) do
|
|
|
|
engine.ui.unit_displays[idx] = DisplayBox{window=display,fg_bg=style.root}
|
|
|
|
unit_view(engine.ui.unit_displays[idx], idx)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
if status then
|
|
|
|
-- start flasher callback task and report ready
|
|
|
|
flasher.run()
|
|
|
|
engine.ui_ready = true
|
|
|
|
else
|
|
|
|
-- report fail and close ui
|
|
|
|
msg = core.extract_assert_msg(msg)
|
|
|
|
renderer.close_ui()
|
2023-07-12 00:32:10 +00:00
|
|
|
end
|
|
|
|
end
|
2023-09-30 16:19:04 +00:00
|
|
|
|
|
|
|
return status, msg
|
2023-07-12 00:32:10 +00:00
|
|
|
end
|
|
|
|
|
2022-06-06 19:42:39 +00:00
|
|
|
-- close out the UI
|
2022-09-11 02:08:29 +00:00
|
|
|
function renderer.close_ui()
|
2023-07-10 03:31:56 +00:00
|
|
|
if not engine.fp_ready then
|
|
|
|
-- stop blinking indicators
|
|
|
|
flasher.clear()
|
|
|
|
end
|
2022-09-13 20:07:21 +00:00
|
|
|
|
2023-05-14 23:13:12 +00:00
|
|
|
-- delete element trees
|
|
|
|
if engine.ui.main_display ~= nil then engine.ui.main_display.delete() end
|
2023-08-10 03:26:06 +00:00
|
|
|
if engine.ui.flow_display ~= nil then engine.ui.flow_display.delete() end
|
2023-07-12 00:32:10 +00:00
|
|
|
for _, display in pairs(engine.ui.unit_displays) do display.delete() end
|
2023-04-21 18:53:28 +00:00
|
|
|
|
|
|
|
-- report ui as not ready
|
|
|
|
engine.ui_ready = false
|
2022-09-11 02:08:29 +00:00
|
|
|
|
2022-10-12 20:37:11 +00:00
|
|
|
-- clear root UI elements
|
2023-04-21 18:53:28 +00:00
|
|
|
engine.ui.main_display = nil
|
2023-08-10 03:26:06 +00:00
|
|
|
engine.ui.flow_display = nil
|
2023-04-21 18:53:28 +00:00
|
|
|
engine.ui.unit_displays = {}
|
|
|
|
|
|
|
|
-- clear unit monitors
|
2023-04-22 03:43:28 +00:00
|
|
|
for _, monitor in ipairs(engine.monitors.unit_displays) do monitor.clear() end
|
2022-06-11 21:58:29 +00:00
|
|
|
|
2022-10-12 20:37:11 +00:00
|
|
|
-- re-draw dmesg
|
|
|
|
engine.dmesg_window.setVisible(true)
|
|
|
|
engine.dmesg_window.redraw()
|
2022-06-06 19:42:39 +00:00
|
|
|
end
|
|
|
|
|
2023-07-10 03:31:56 +00:00
|
|
|
-- is the front panel ready?
|
|
|
|
---@nodiscard
|
|
|
|
---@return boolean ready
|
|
|
|
function renderer.fp_ready() return engine.fp_ready end
|
|
|
|
|
2022-09-03 14:50:14 +00:00
|
|
|
-- is the UI ready?
|
2023-02-23 04:09:47 +00:00
|
|
|
---@nodiscard
|
2022-09-03 14:50:14 +00:00
|
|
|
---@return boolean ready
|
|
|
|
function renderer.ui_ready() return engine.ui_ready end
|
|
|
|
|
2023-07-12 00:32:10 +00:00
|
|
|
-- handle a monitor peripheral being disconnected
|
|
|
|
---@param device table monitor
|
|
|
|
---@return boolean is_used if the monitor is one of the configured monitors
|
|
|
|
function renderer.handle_disconnect(device)
|
|
|
|
local is_used = false
|
|
|
|
|
|
|
|
if engine.monitors ~= nil then
|
|
|
|
if engine.monitors.primary == device then
|
|
|
|
if engine.ui.main_display ~= nil then
|
|
|
|
-- delete element tree and clear root UI elements
|
|
|
|
engine.ui.main_display.delete()
|
|
|
|
end
|
|
|
|
|
|
|
|
is_used = true
|
|
|
|
engine.monitors.primary = nil
|
|
|
|
engine.ui.main_display = nil
|
|
|
|
|
2023-08-21 03:28:48 +00:00
|
|
|
iocontrol.fp_monitor_state("main", false)
|
|
|
|
elseif engine.monitors.flow == device then
|
|
|
|
if engine.ui.flow_display ~= nil then
|
|
|
|
-- delete element tree and clear root UI elements
|
|
|
|
engine.ui.flow_display.delete()
|
|
|
|
end
|
|
|
|
|
|
|
|
is_used = true
|
|
|
|
engine.monitors.flow = nil
|
|
|
|
engine.ui.flow_display = nil
|
|
|
|
|
|
|
|
iocontrol.fp_monitor_state("flow", false)
|
2023-07-12 00:32:10 +00:00
|
|
|
else
|
|
|
|
for idx, monitor in pairs(engine.monitors.unit_displays) do
|
|
|
|
if monitor == device then
|
|
|
|
if engine.ui.unit_displays[idx] ~= nil then
|
|
|
|
engine.ui.unit_displays[idx].delete()
|
|
|
|
end
|
|
|
|
|
|
|
|
is_used = true
|
|
|
|
engine.monitors.unit_displays[idx] = nil
|
|
|
|
engine.ui.unit_displays[idx] = nil
|
|
|
|
|
|
|
|
iocontrol.fp_monitor_state(idx, false)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return is_used
|
|
|
|
end
|
|
|
|
|
|
|
|
-- handle a monitor peripheral being reconnected
|
|
|
|
---@param name string monitor name
|
|
|
|
---@param device table monitor
|
|
|
|
---@return boolean is_used if the monitor is one of the configured monitors
|
|
|
|
function renderer.handle_reconnect(name, device)
|
|
|
|
local is_used = false
|
|
|
|
|
|
|
|
if engine.monitors ~= nil then
|
|
|
|
if engine.monitors.primary_name == name then
|
|
|
|
is_used = true
|
|
|
|
_init_display(device)
|
|
|
|
engine.monitors.primary = device
|
|
|
|
|
|
|
|
local disp_x, disp_y = engine.monitors.primary.getSize()
|
|
|
|
engine.dmesg_window.reposition(1, 1, disp_x, disp_y, engine.monitors.primary)
|
|
|
|
|
|
|
|
if engine.ui_ready and (engine.ui.main_display == nil) then
|
|
|
|
engine.dmesg_window.setVisible(false)
|
|
|
|
|
|
|
|
engine.ui.main_display = DisplayBox{window=device,fg_bg=style.root}
|
|
|
|
main_view(engine.ui.main_display)
|
|
|
|
else
|
|
|
|
engine.dmesg_window.setVisible(true)
|
|
|
|
engine.dmesg_window.redraw()
|
|
|
|
end
|
|
|
|
|
2023-08-21 03:28:48 +00:00
|
|
|
iocontrol.fp_monitor_state("main", true)
|
|
|
|
elseif engine.monitors.flow_name == name then
|
|
|
|
is_used = true
|
|
|
|
_init_display(device)
|
|
|
|
engine.monitors.flow = device
|
|
|
|
|
|
|
|
if engine.ui_ready and (engine.ui.flow_display == nil) then
|
|
|
|
engine.ui.flow_display = DisplayBox{window=device,fg_bg=style.root}
|
|
|
|
flow_view(engine.ui.flow_display)
|
|
|
|
end
|
|
|
|
|
|
|
|
iocontrol.fp_monitor_state("flow", true)
|
2023-07-12 00:32:10 +00:00
|
|
|
else
|
|
|
|
for idx, monitor in ipairs(engine.monitors.unit_name_map) do
|
|
|
|
if monitor == name then
|
|
|
|
is_used = true
|
|
|
|
_init_display(device)
|
|
|
|
engine.monitors.unit_displays[idx] = device
|
|
|
|
|
|
|
|
if engine.ui_ready and (engine.ui.unit_displays[idx] == nil) then
|
|
|
|
engine.ui.unit_displays[idx] = DisplayBox{window=device,fg_bg=style.root}
|
|
|
|
unit_view(engine.ui.unit_displays[idx], idx)
|
|
|
|
end
|
|
|
|
|
|
|
|
iocontrol.fp_monitor_state(idx, true)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return is_used
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-07-20 17:28:58 +00:00
|
|
|
-- handle a touch event
|
2023-05-11 00:01:06 +00:00
|
|
|
---@param event mouse_interaction|nil
|
2023-04-09 01:33:54 +00:00
|
|
|
function renderer.handle_mouse(event)
|
2023-07-10 03:31:56 +00:00
|
|
|
if event ~= nil then
|
|
|
|
if engine.fp_ready and event.monitor == "terminal" then
|
|
|
|
engine.ui.front_panel.handle_mouse(event)
|
|
|
|
elseif engine.ui_ready then
|
|
|
|
if event.monitor == engine.monitors.primary_name then
|
|
|
|
engine.ui.main_display.handle_mouse(event)
|
2023-08-10 03:26:06 +00:00
|
|
|
elseif event.monitor == engine.monitors.flow_name then
|
|
|
|
engine.ui.flow_display.handle_mouse(event)
|
2023-07-10 03:31:56 +00:00
|
|
|
else
|
|
|
|
for id, monitor in ipairs(engine.monitors.unit_name_map) do
|
|
|
|
if event.monitor == monitor then
|
|
|
|
local layout = engine.ui.unit_displays[id] ---@type graphics_element
|
|
|
|
layout.handle_mouse(event)
|
|
|
|
break
|
|
|
|
end
|
2023-04-21 18:53:28 +00:00
|
|
|
end
|
2022-07-20 17:28:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-29 18:34:09 +00:00
|
|
|
return renderer
|