#434 #454 PPM improvements and undefined function overhaul

This commit is contained in:
Mikayla Fischler
2024-03-23 00:26:58 -04:00
parent 1c0f61b3e0
commit 19869416af
12 changed files with 186 additions and 265 deletions

View File

@@ -92,6 +92,8 @@ function rtu.init_unit(device)
local insert = table.insert
local stub = function () log.warning("tried to call an RTU function stub") end
---@class rtu_device
local public = {}
@@ -113,13 +115,26 @@ function rtu.init_unit(device)
return self.io_count_cache[1], self.io_count_cache[2], self.io_count_cache[3], self.io_count_cache[4]
end
-- pass a function through or generate one to call a function by name from the device
---@param f function|string function or device function name
local function _as_func(f)
if type(f) == "string" then
local name = f
if device then
f = function (...) return device[name](...) end
else f = stub end
end
return f
end
-- discrete inputs: single bit read-only
-- connect discrete input
---@param f function
---@param f function|string function or function name
---@return integer count count of discrete inputs
function protected.connect_di(f)
insert(self.discrete_inputs, { read = f })
insert(self.discrete_inputs, { read = _as_func(f) })
_count_io()
return #self.discrete_inputs
end
@@ -135,11 +150,11 @@ function rtu.init_unit(device)
-- coils: single bit read-write
-- connect coil
---@param f_read function
---@param f_write function
---@param f_read function|string function or function name
---@param f_write function|string function or function name
---@return integer count count of coils
function protected.connect_coil(f_read, f_write)
insert(self.coils, { read = f_read, write = f_write })
insert(self.coils, { read = _as_func(f_read), write = _as_func(f_write) })
_count_io()
return #self.coils
end
@@ -164,10 +179,10 @@ function rtu.init_unit(device)
-- input registers: multi-bit read-only
-- connect input register
---@param f function
---@param f function|string function or function name
---@return integer count count of input registers
function protected.connect_input_reg(f)
insert(self.input_regs, { read = f })
insert(self.input_regs, { read = _as_func(f) })
_count_io()
return #self.input_regs
end
@@ -183,11 +198,11 @@ function rtu.init_unit(device)
-- holding registers: multi-bit read-write
-- connect holding register
---@param f_read function
---@param f_write function
---@param f_read function|string function or function name
---@param f_write function|string function or function name
---@return integer count count of holding registers
function protected.connect_holding_reg(f_read, f_write)
insert(self.holding_regs, { read = f_read, write = f_write })
insert(self.holding_regs, { read = _as_func(f_read), write = _as_func(f_write) })
_count_io()
return #self.holding_regs
end