cleanup and optimizations

This commit is contained in:
Mikayla Fischler 2024-02-18 16:49:39 -05:00
parent 827953c0a1
commit 1a9892b291
3 changed files with 16 additions and 26 deletions

View File

@ -28,7 +28,7 @@ coordinator.config = config
-- load the coordinator configuration<br> -- load the coordinator configuration<br>
-- status of 0 is OK, 1 is bad config, 2 is bad monitor config -- status of 0 is OK, 1 is bad config, 2 is bad monitor config
---@return 0|1|2 status, nil|monitors_struct|string monitors ---@return 0|1|2 status, nil|monitors_struct|string monitors (or error message)
function coordinator.load_config() function coordinator.load_config()
if not settings.load("/coordinator.settings") then return 1 end if not settings.load("/coordinator.settings") then return 1 end
@ -63,8 +63,7 @@ function coordinator.load_config()
cfv.assert_type_table(config.UnitDisplays) cfv.assert_type_table(config.UnitDisplays)
cfv.assert_type_num(config.SpeakerVolume) cfv.assert_type_num(config.SpeakerVolume)
cfv.assert_min(config.SpeakerVolume, 0.0) cfv.assert_range(config.SpeakerVolume, 0, 3)
cfv.assert_max(config.SpeakerVolume, 3.0)
cfv.assert_channel(config.SVR_Channel) cfv.assert_channel(config.SVR_Channel)
cfv.assert_channel(config.CRD_Channel) cfv.assert_channel(config.CRD_Channel)
@ -115,47 +114,39 @@ function coordinator.load_config()
if mon_cfv.valid() then if mon_cfv.valid() then
local w, h, _ local w, h, _
mon_cfv.assert(util.table_contains(names, config.MainDisplay)) if not util.table_contains(names, config.MainDisplay) then
return 2, "Main monitor is not connected."
if not mon_cfv.valid() then return 2, "Main monitor is not connected." end end
monitors.primary = ppm.get_periph(config.MainDisplay) monitors.primary = ppm.get_periph(config.MainDisplay)
monitors.primary_name = config.MainDisplay monitors.primary_name = config.MainDisplay
w, _ = ppm.monitor_block_size(monitors.primary.getSize()) w, _ = ppm.monitor_block_size(monitors.primary.getSize())
mon_cfv.assert(w == 8) if w ~= 8 then return 2, "Main monitor width is incorrect." end
if not mon_cfv.valid() then return 2, "Main monitor width is incorrect." end
if not config.DisableFlowView then if not config.DisableFlowView then
mon_cfv.assert(util.table_contains(names, config.FlowDisplay)) if not util.table_contains(names, config.FlowDisplay) then
return 2, "Flow monitor is not connected."
if not mon_cfv.valid() then return 2, "Flow monitor is not connected." end end
monitors.flow = ppm.get_periph(config.FlowDisplay) monitors.flow = ppm.get_periph(config.FlowDisplay)
monitors.flow_name = config.FlowDisplay monitors.flow_name = config.FlowDisplay
w, _ = ppm.monitor_block_size(monitors.flow.getSize()) w, _ = ppm.monitor_block_size(monitors.flow.getSize())
mon_cfv.assert(w == 8) if w ~= 8 then return 2, "Flow monitor width is incorrect." end
if not mon_cfv.valid() then return 2, "Flow monitor width is incorrect." end
end end
for i = 1, config.UnitCount do for i = 1, config.UnitCount do
local display = config.UnitDisplays[i] local display = config.UnitDisplays[i]
if type(display) ~= "string" or not util.table_contains(names, display) then
mon_cfv.assert_type_str(display) return 2, "Unit " .. i .. " monitor is not connected."
mon_cfv.assert(util.table_contains(names, display)) end
if not mon_cfv.valid() then return 2, "Unit " .. i .. " monitor is not connected." end
monitors.unit_displays[i] = ppm.get_periph(display) monitors.unit_displays[i] = ppm.get_periph(display)
monitors.unit_name_map[i] = display monitors.unit_name_map[i] = display
w, h = ppm.monitor_block_size(monitors.unit_displays[i].getSize()) w, h = ppm.monitor_block_size(monitors.unit_displays[i].getSize())
mon_cfv.assert(w == 4 and h == 4) if w ~= 4 or h ~= 4 then return 2, "Unit " .. i .. " monitor size is incorrect." end
if not mon_cfv.valid() then return 2, "Unit " .. i .. " monitor size is incorrect." end
end end
else return 2, "Monitor configuration invalid." end else return 2, "Monitor configuration invalid." end
end end

View File

@ -41,8 +41,7 @@ function rtu.load_config()
local cfv = util.new_validator() local cfv = util.new_validator()
cfv.assert_type_num(config.SpeakerVolume) cfv.assert_type_num(config.SpeakerVolume)
cfv.assert_min(config.SpeakerVolume, 0.0) cfv.assert_range(config.SpeakerVolume, 0, 3)
cfv.assert_max(config.SpeakerVolume, 3.0)
cfv.assert_channel(config.SVR_Channel) cfv.assert_channel(config.SVR_Channel)
cfv.assert_channel(config.RTU_Channel) cfv.assert_channel(config.RTU_Channel)

View File

@ -426,7 +426,7 @@ end
-- get the block size of a monitor given its width and height <b>at a text scale of 0.5</b> -- get the block size of a monitor given its width and height <b>at a text scale of 0.5</b>
---@nodiscard ---@nodiscard
---@param w integer character width ---@param w integer character width
---@param h integer character width ---@param h integer character height
---@return integer block_width, integer block_height ---@return integer block_width, integer block_height
function ppm.monitor_block_size(w, h) function ppm.monitor_block_size(w, h)
local width = math.floor((w - 15) / 21) + 1 local width = math.floor((w - 15) / 21) + 1