2022-05-04 17:37:01 +00:00
|
|
|
local rsio = require("scada-common.rsio")
|
|
|
|
|
2023-02-22 04:50:43 +00:00
|
|
|
local rtu = require("rtu.rtu")
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
local redstone_rtu = {}
|
2022-02-08 20:42:06 +00:00
|
|
|
|
2022-12-01 04:31:14 +00:00
|
|
|
local IO_LVL = rsio.IO_LVL
|
|
|
|
|
2022-03-23 19:36:14 +00:00
|
|
|
local digital_read = rsio.digital_read
|
2022-05-27 22:10:06 +00:00
|
|
|
local digital_write = rsio.digital_write
|
2022-03-23 19:36:14 +00:00
|
|
|
|
2022-05-12 19:36:27 +00:00
|
|
|
-- create new redstone device
|
2023-02-22 04:50:43 +00:00
|
|
|
---@nodiscard
|
2023-04-06 16:52:25 +00:00
|
|
|
---@return rtu_rs_device interface, boolean faulted
|
2022-05-31 18:54:55 +00:00
|
|
|
function redstone_rtu.new()
|
|
|
|
local unit = rtu.init_unit()
|
2022-02-08 20:42:06 +00:00
|
|
|
|
2022-05-12 19:36:27 +00:00
|
|
|
-- get RTU interface
|
2022-05-31 18:54:55 +00:00
|
|
|
local interface = unit.interface()
|
2022-05-12 19:36:27 +00:00
|
|
|
|
2023-02-22 04:50:43 +00:00
|
|
|
-- extends rtu_device; fields added manually to please Lua diagnostics
|
2022-05-12 19:36:27 +00:00
|
|
|
---@class rtu_rs_device
|
|
|
|
local public = {
|
|
|
|
io_count = interface.io_count,
|
|
|
|
read_coil = interface.read_coil,
|
|
|
|
read_di = interface.read_di,
|
|
|
|
read_holding_reg = interface.read_holding_reg,
|
|
|
|
read_input_reg = interface.read_input_reg,
|
|
|
|
write_coil = interface.write_coil,
|
|
|
|
write_holding_reg = interface.write_holding_reg
|
|
|
|
}
|
2022-02-08 20:42:06 +00:00
|
|
|
|
2022-05-12 19:36:27 +00:00
|
|
|
-- link digital input
|
|
|
|
---@param side string
|
|
|
|
---@param color integer
|
2022-05-31 18:54:55 +00:00
|
|
|
function public.link_di(side, color)
|
2022-02-08 20:42:06 +00:00
|
|
|
local f_read = nil
|
|
|
|
|
|
|
|
if color then
|
|
|
|
f_read = function ()
|
2022-03-23 19:36:14 +00:00
|
|
|
return digital_read(rs.testBundledInput(side, color))
|
2022-02-08 20:42:06 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
f_read = function ()
|
2022-03-23 19:36:14 +00:00
|
|
|
return digital_read(rs.getInput(side))
|
2022-02-08 20:42:06 +00:00
|
|
|
end
|
|
|
|
end
|
2022-05-10 16:01:56 +00:00
|
|
|
|
2022-05-31 18:54:55 +00:00
|
|
|
unit.connect_di(f_read)
|
2022-02-08 20:42:06 +00:00
|
|
|
end
|
|
|
|
|
2022-05-12 19:36:27 +00:00
|
|
|
-- link digital output
|
|
|
|
---@param side string
|
|
|
|
---@param color integer
|
2022-12-01 04:31:14 +00:00
|
|
|
function public.link_do(side, color)
|
2022-02-08 20:42:06 +00:00
|
|
|
local f_read = nil
|
|
|
|
local f_write = nil
|
|
|
|
|
|
|
|
if color then
|
|
|
|
f_read = function ()
|
2022-03-23 19:36:14 +00:00
|
|
|
return digital_read(colors.test(rs.getBundledOutput(side), color))
|
2022-02-08 20:42:06 +00:00
|
|
|
end
|
|
|
|
|
2022-03-23 19:36:14 +00:00
|
|
|
f_write = function (level)
|
2022-12-01 04:31:14 +00:00
|
|
|
if level ~= IO_LVL.FLOATING and level ~= IO_LVL.DISCONNECT then
|
|
|
|
local output = rs.getBundledOutput(side)
|
2022-02-08 20:42:06 +00:00
|
|
|
|
2022-12-01 04:31:14 +00:00
|
|
|
if digital_write(level) then
|
|
|
|
output = colors.combine(output, color)
|
|
|
|
else
|
|
|
|
output = colors.subtract(output, color)
|
|
|
|
end
|
2022-02-08 20:42:06 +00:00
|
|
|
|
2022-12-01 04:31:14 +00:00
|
|
|
rs.setBundledOutput(side, output)
|
|
|
|
end
|
2022-02-08 20:42:06 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
f_read = function ()
|
2022-03-23 19:36:14 +00:00
|
|
|
return digital_read(rs.getOutput(side))
|
2022-02-08 20:42:06 +00:00
|
|
|
end
|
|
|
|
|
2022-03-23 19:36:14 +00:00
|
|
|
f_write = function (level)
|
2022-12-01 04:31:14 +00:00
|
|
|
if level ~= IO_LVL.FLOATING and level ~= IO_LVL.DISCONNECT then
|
|
|
|
rs.setOutput(side, digital_write(level))
|
|
|
|
end
|
2022-02-08 20:42:06 +00:00
|
|
|
end
|
|
|
|
end
|
2022-05-10 16:01:56 +00:00
|
|
|
|
2022-05-31 18:54:55 +00:00
|
|
|
unit.connect_coil(f_read, f_write)
|
2022-02-08 20:42:06 +00:00
|
|
|
end
|
|
|
|
|
2022-05-12 19:36:27 +00:00
|
|
|
-- link analog input
|
|
|
|
---@param side string
|
2022-05-31 18:54:55 +00:00
|
|
|
function public.link_ai(side)
|
|
|
|
unit.connect_input_reg(
|
2022-02-08 20:42:06 +00:00
|
|
|
function ()
|
|
|
|
return rs.getAnalogInput(side)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-05-12 19:36:27 +00:00
|
|
|
-- link analog output
|
|
|
|
---@param side string
|
2022-05-31 18:54:55 +00:00
|
|
|
function public.link_ao(side)
|
|
|
|
unit.connect_holding_reg(
|
2022-02-08 20:42:06 +00:00
|
|
|
function ()
|
|
|
|
return rs.getAnalogOutput(side)
|
|
|
|
end,
|
|
|
|
function (value)
|
|
|
|
rs.setAnalogOutput(side, value)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-04-06 16:52:25 +00:00
|
|
|
return public, false
|
2022-02-08 20:42:06 +00:00
|
|
|
end
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
return redstone_rtu
|