cc-mek-scada/rtu/dev/redstone_rtu.lua

118 lines
3.0 KiB
Lua
Raw Normal View History

local rsio = require("scada-common.rsio")
2023-02-22 04:50:43 +00:00
local rtu = require("rtu.rtu")
local redstone_rtu = {}
2022-02-08 20:42:06 +00:00
local IO_LVL = rsio.IO_LVL
2022-03-23 19:36:14 +00:00
local digital_read = rsio.digital_read
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
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
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
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
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
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)
if level ~= IO_LVL.FLOATING and level ~= IO_LVL.DISCONNECT then
local output = rs.getBundledOutput(side)
2022-02-08 20:42:06 +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
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)
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
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
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
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
2022-05-12 19:36:27 +00:00
return public
2022-02-08 20:42:06 +00:00
end
return redstone_rtu