2022-05-04 17:37:01 +00:00
|
|
|
--
|
|
|
|
-- Redstone I/O
|
|
|
|
--
|
|
|
|
|
|
|
|
local rsio = {}
|
|
|
|
|
2022-05-06 15:11:53 +00:00
|
|
|
----------------------
|
|
|
|
-- RS I/O CONSTANTS --
|
|
|
|
----------------------
|
|
|
|
|
2022-05-10 21:06:27 +00:00
|
|
|
---@alias IO_LVL integer
|
2022-05-04 17:37:01 +00:00
|
|
|
local IO_LVL = {
|
2022-03-15 15:58:08 +00:00
|
|
|
LOW = 0,
|
2022-05-15 00:27:06 +00:00
|
|
|
HIGH = 1,
|
2022-05-16 15:52:03 +00:00
|
|
|
DISCONNECT = -1 -- use for RTU session to indicate this RTU is not connected to this channel
|
2022-03-15 15:58:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 21:06:27 +00:00
|
|
|
---@alias IO_DIR integer
|
2022-05-04 17:37:01 +00:00
|
|
|
local IO_DIR = {
|
2022-03-23 19:36:14 +00:00
|
|
|
IN = 0,
|
|
|
|
OUT = 1
|
|
|
|
}
|
|
|
|
|
2022-05-10 21:06:27 +00:00
|
|
|
---@alias IO_MODE integer
|
2022-05-04 17:37:01 +00:00
|
|
|
local IO_MODE = {
|
2022-05-16 14:38:47 +00:00
|
|
|
DIGITAL_IN = 0,
|
|
|
|
DIGITAL_OUT = 1,
|
|
|
|
ANALOG_IN = 2,
|
|
|
|
ANALOG_OUT = 3
|
2022-03-23 19:36:14 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 21:06:27 +00:00
|
|
|
---@alias RS_IO integer
|
2022-05-04 17:37:01 +00:00
|
|
|
local RS_IO = {
|
2022-02-08 20:42:06 +00:00
|
|
|
-- digital inputs --
|
|
|
|
|
|
|
|
-- facility
|
2022-03-15 15:58:08 +00:00
|
|
|
F_SCRAM = 1, -- active low, facility-wide scram
|
2022-02-08 20:42:06 +00:00
|
|
|
|
|
|
|
-- reactor
|
2022-05-16 14:38:47 +00:00
|
|
|
R_SCRAM = 2, -- active low, reactor scram
|
|
|
|
R_ENABLE = 3, -- active high, reactor enable
|
2022-02-08 20:42:06 +00:00
|
|
|
|
|
|
|
-- digital outputs --
|
|
|
|
|
2022-05-16 14:38:47 +00:00
|
|
|
-- facility
|
|
|
|
F_ALARM = 4, -- active high, facility safety alarm
|
|
|
|
|
2022-02-08 20:42:06 +00:00
|
|
|
-- waste
|
2022-03-15 15:58:08 +00:00
|
|
|
WASTE_PO = 5, -- active low, polonium routing
|
|
|
|
WASTE_PU = 6, -- active low, plutonium routing
|
|
|
|
WASTE_AM = 7, -- active low, antimatter routing
|
2022-02-08 20:42:06 +00:00
|
|
|
|
|
|
|
-- reactor
|
2022-05-16 14:38:47 +00:00
|
|
|
R_ALARM = 8, -- active high, reactor safety alarm
|
|
|
|
R_SCRAMMED = 9, -- active high, if the reactor is scrammed
|
|
|
|
R_AUTO_SCRAM = 10, -- active high, if the reactor was automatically scrammed
|
|
|
|
R_ACTIVE = 11, -- active high, if the reactor is active
|
|
|
|
R_AUTO_CTRL = 12, -- active high, if the reactor burn rate is automatic
|
|
|
|
R_DMG_CRIT = 13, -- active high, if the reactor damage is critical
|
|
|
|
R_HIGH_TEMP = 14, -- active high, if the reactor is at a high temperature
|
|
|
|
R_NO_COOLANT = 15, -- active high, if the reactor has no coolant
|
|
|
|
R_EXCESS_HC = 16, -- active high, if the reactor has excess heated coolant
|
|
|
|
R_EXCESS_WS = 17, -- active high, if the reactor has excess waste
|
|
|
|
R_INSUFF_FUEL = 18, -- active high, if the reactor has insufficent fuel
|
|
|
|
R_PLC_FAULT = 19, -- active high, if the reactor PLC reports a device access fault
|
|
|
|
R_PLC_TIMEOUT = 20 -- active high, if the reactor PLC has not been heard from
|
2022-02-08 20:42:06 +00:00
|
|
|
}
|
2022-03-15 15:58:08 +00:00
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
rsio.IO_LVL = IO_LVL
|
|
|
|
rsio.IO_DIR = IO_DIR
|
|
|
|
rsio.IO_MODE = IO_MODE
|
|
|
|
rsio.IO = RS_IO
|
|
|
|
|
2022-05-06 15:11:53 +00:00
|
|
|
-----------------------
|
|
|
|
-- UTILITY FUNCTIONS --
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
-- channel to string
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param channel RS_IO
|
2022-05-04 17:37:01 +00:00
|
|
|
rsio.to_string = function (channel)
|
2022-03-23 19:36:14 +00:00
|
|
|
local names = {
|
|
|
|
"F_SCRAM",
|
|
|
|
"R_SCRAM",
|
|
|
|
"R_ENABLE",
|
2022-05-16 14:38:47 +00:00
|
|
|
"F_ALARM",
|
2022-03-23 19:36:14 +00:00
|
|
|
"WASTE_PO",
|
|
|
|
"WASTE_PU",
|
|
|
|
"WASTE_AM",
|
2022-05-16 14:38:47 +00:00
|
|
|
"R_ALARM",
|
2022-03-23 19:36:14 +00:00
|
|
|
"R_SCRAMMED",
|
|
|
|
"R_AUTO_SCRAM",
|
|
|
|
"R_ACTIVE",
|
|
|
|
"R_AUTO_CTRL",
|
|
|
|
"R_DMG_CRIT",
|
|
|
|
"R_HIGH_TEMP",
|
|
|
|
"R_NO_COOLANT",
|
|
|
|
"R_EXCESS_HC",
|
|
|
|
"R_EXCESS_WS",
|
|
|
|
"R_INSUFF_FUEL",
|
2022-05-16 14:38:47 +00:00
|
|
|
"R_PLC_FAULT",
|
|
|
|
"R_PLC_TIMEOUT"
|
2022-03-23 19:36:14 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 21:36:54 +00:00
|
|
|
if type(channel) == "number" and channel > 0 and channel <= #names then
|
2022-03-23 19:36:14 +00:00
|
|
|
return names[channel]
|
|
|
|
else
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-06 15:11:53 +00:00
|
|
|
local _B_AND = bit.band
|
2022-03-23 19:36:14 +00:00
|
|
|
|
2022-03-15 15:58:08 +00:00
|
|
|
local _TRINARY = function (cond, t, f) if cond then return t else return f end end
|
|
|
|
|
|
|
|
local _DI_ACTIVE_HIGH = function (level) return level == IO_LVL.HIGH end
|
|
|
|
local _DI_ACTIVE_LOW = function (level) return level == IO_LVL.LOW end
|
|
|
|
local _DO_ACTIVE_HIGH = function (on) return _TRINARY(on, IO_LVL.HIGH, IO_LVL.LOW) end
|
|
|
|
local _DO_ACTIVE_LOW = function (on) return _TRINARY(on, IO_LVL.LOW, IO_LVL.HIGH) end
|
|
|
|
|
2022-03-23 19:36:14 +00:00
|
|
|
-- I/O mappings to I/O function and I/O mode
|
2022-03-15 15:58:08 +00:00
|
|
|
local RS_DIO_MAP = {
|
|
|
|
-- F_SCRAM
|
2022-03-23 19:36:14 +00:00
|
|
|
{ _f = _DI_ACTIVE_LOW, mode = IO_DIR.IN },
|
2022-03-15 15:58:08 +00:00
|
|
|
-- R_SCRAM
|
2022-03-23 19:36:14 +00:00
|
|
|
{ _f = _DI_ACTIVE_LOW, mode = IO_DIR.IN },
|
2022-03-15 15:58:08 +00:00
|
|
|
-- R_ENABLE
|
2022-03-23 19:36:14 +00:00
|
|
|
{ _f = _DI_ACTIVE_HIGH, mode = IO_DIR.IN },
|
2022-05-16 14:38:47 +00:00
|
|
|
-- F_ALARM
|
|
|
|
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
2022-03-15 15:58:08 +00:00
|
|
|
-- WASTE_PO
|
2022-03-23 19:36:14 +00:00
|
|
|
{ _f = _DO_ACTIVE_LOW, mode = IO_DIR.OUT },
|
2022-03-15 15:58:08 +00:00
|
|
|
-- WASTE_PU
|
2022-03-23 19:36:14 +00:00
|
|
|
{ _f = _DO_ACTIVE_LOW, mode = IO_DIR.OUT },
|
2022-03-15 15:58:08 +00:00
|
|
|
-- WASTE_AM
|
2022-03-23 19:36:14 +00:00
|
|
|
{ _f = _DO_ACTIVE_LOW, mode = IO_DIR.OUT },
|
2022-05-16 14:38:47 +00:00
|
|
|
-- R_ALARM
|
|
|
|
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
2022-03-15 15:58:08 +00:00
|
|
|
-- R_SCRAMMED
|
2022-03-23 19:36:14 +00:00
|
|
|
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
2022-03-15 15:58:08 +00:00
|
|
|
-- R_AUTO_SCRAM
|
2022-03-23 19:36:14 +00:00
|
|
|
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
2022-03-15 15:58:08 +00:00
|
|
|
-- R_ACTIVE
|
2022-03-23 19:36:14 +00:00
|
|
|
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
2022-03-15 15:58:08 +00:00
|
|
|
-- R_AUTO_CTRL
|
2022-03-23 19:36:14 +00:00
|
|
|
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
2022-03-15 15:58:08 +00:00
|
|
|
-- R_DMG_CRIT
|
2022-03-23 19:36:14 +00:00
|
|
|
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
2022-03-15 15:58:08 +00:00
|
|
|
-- R_HIGH_TEMP
|
2022-03-23 19:36:14 +00:00
|
|
|
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
2022-03-15 15:58:08 +00:00
|
|
|
-- R_NO_COOLANT
|
2022-03-23 19:36:14 +00:00
|
|
|
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
2022-03-15 15:58:08 +00:00
|
|
|
-- R_EXCESS_HC
|
2022-03-23 19:36:14 +00:00
|
|
|
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
2022-03-15 15:58:08 +00:00
|
|
|
-- R_EXCESS_WS
|
2022-03-23 19:36:14 +00:00
|
|
|
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
2022-03-15 15:58:08 +00:00
|
|
|
-- R_INSUFF_FUEL
|
2022-03-23 19:36:14 +00:00
|
|
|
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
2022-05-16 14:38:47 +00:00
|
|
|
-- R_PLC_FAULT
|
|
|
|
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
2022-03-15 15:58:08 +00:00
|
|
|
-- R_PLC_TIMEOUT
|
2022-03-23 19:36:14 +00:00
|
|
|
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT }
|
2022-03-15 15:58:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 15:11:53 +00:00
|
|
|
-- get the mode of a channel
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param channel RS_IO
|
|
|
|
---@return IO_MODE
|
2022-05-04 17:37:01 +00:00
|
|
|
rsio.get_io_mode = function (channel)
|
2022-03-23 19:36:14 +00:00
|
|
|
local modes = {
|
|
|
|
IO_MODE.DIGITAL_IN, -- F_SCRAM
|
|
|
|
IO_MODE.DIGITAL_IN, -- R_SCRAM
|
|
|
|
IO_MODE.DIGITAL_IN, -- R_ENABLE
|
2022-05-16 14:38:47 +00:00
|
|
|
IO_MODE.DIGITAL_OUT, -- F_ALARM
|
2022-03-23 19:36:14 +00:00
|
|
|
IO_MODE.DIGITAL_OUT, -- WASTE_PO
|
|
|
|
IO_MODE.DIGITAL_OUT, -- WASTE_PU
|
|
|
|
IO_MODE.DIGITAL_OUT, -- WASTE_AM
|
2022-05-16 14:38:47 +00:00
|
|
|
IO_MODE.DIGITAL_OUT, -- R_ALARM
|
2022-03-23 19:36:14 +00:00
|
|
|
IO_MODE.DIGITAL_OUT, -- R_SCRAMMED
|
|
|
|
IO_MODE.DIGITAL_OUT, -- R_AUTO_SCRAM
|
|
|
|
IO_MODE.DIGITAL_OUT, -- R_ACTIVE
|
|
|
|
IO_MODE.DIGITAL_OUT, -- R_AUTO_CTRL
|
|
|
|
IO_MODE.DIGITAL_OUT, -- R_DMG_CRIT
|
|
|
|
IO_MODE.DIGITAL_OUT, -- R_HIGH_TEMP
|
|
|
|
IO_MODE.DIGITAL_OUT, -- R_NO_COOLANT
|
|
|
|
IO_MODE.DIGITAL_OUT, -- R_EXCESS_HC
|
|
|
|
IO_MODE.DIGITAL_OUT, -- R_EXCESS_WS
|
|
|
|
IO_MODE.DIGITAL_OUT, -- R_INSUFF_FUEL
|
2022-05-16 14:38:47 +00:00
|
|
|
IO_MODE.DIGITAL_OUT, -- R_PLC_FAULT
|
|
|
|
IO_MODE.DIGITAL_OUT -- R_PLC_TIMEOUT
|
2022-03-23 19:36:14 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 21:36:54 +00:00
|
|
|
if type(channel) == "number" and channel > 0 and channel <= #modes then
|
2022-03-23 19:36:14 +00:00
|
|
|
return modes[channel]
|
|
|
|
else
|
|
|
|
return IO_MODE.ANALOG_IN
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-06 15:11:53 +00:00
|
|
|
--------------------
|
|
|
|
-- GENERIC CHECKS --
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
local RS_SIDES = rs.getSides()
|
|
|
|
|
|
|
|
-- check if a channel is valid
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param channel RS_IO
|
|
|
|
---@return boolean valid
|
2022-05-06 15:11:53 +00:00
|
|
|
rsio.is_valid_channel = function (channel)
|
2022-05-23 21:36:54 +00:00
|
|
|
return (type(channel) == "number") and (channel > 0) and (channel <= RS_IO.R_PLC_TIMEOUT)
|
2022-05-06 15:11:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- check if a side is valid
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param side string
|
|
|
|
---@return boolean valid
|
2022-05-06 15:11:53 +00:00
|
|
|
rsio.is_valid_side = function (side)
|
|
|
|
if side ~= nil then
|
|
|
|
for i = 0, #RS_SIDES do
|
|
|
|
if RS_SIDES[i] == side then return true end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- check if a color is a valid single color
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param color integer
|
|
|
|
---@return boolean valid
|
2022-05-06 15:11:53 +00:00
|
|
|
rsio.is_color = function (color)
|
2022-05-23 21:36:54 +00:00
|
|
|
return (type(color) == "number") and (color > 0) and (_B_AND(color, (color - 1)) == 0);
|
2022-05-06 15:11:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------
|
|
|
|
-- DIGITAL I/O --
|
|
|
|
-----------------
|
|
|
|
|
2022-03-15 15:58:08 +00:00
|
|
|
-- get digital IO level reading
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param rs_value boolean
|
|
|
|
---@return IO_LVL
|
2022-05-04 17:37:01 +00:00
|
|
|
rsio.digital_read = function (rs_value)
|
2022-03-15 15:58:08 +00:00
|
|
|
if rs_value then
|
|
|
|
return IO_LVL.HIGH
|
|
|
|
else
|
|
|
|
return IO_LVL.LOW
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-23 19:36:14 +00:00
|
|
|
-- returns the level corresponding to active
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param channel RS_IO
|
|
|
|
---@param active boolean
|
|
|
|
---@return IO_LVL
|
2022-05-04 17:37:01 +00:00
|
|
|
rsio.digital_write = function (channel, active)
|
2022-05-23 21:36:54 +00:00
|
|
|
if type(channel) ~= "number" or channel < RS_IO.F_ALARM or channel > RS_IO.R_PLC_TIMEOUT then
|
2022-03-23 19:36:14 +00:00
|
|
|
return IO_LVL.LOW
|
2022-03-15 15:58:08 +00:00
|
|
|
else
|
2022-05-10 15:35:52 +00:00
|
|
|
return RS_DIO_MAP[channel]._f(active)
|
2022-03-15 15:58:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-23 19:36:14 +00:00
|
|
|
-- returns true if the level corresponds to active
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param channel RS_IO
|
|
|
|
---@param level IO_LVL
|
|
|
|
---@return boolean
|
2022-05-04 17:37:01 +00:00
|
|
|
rsio.digital_is_active = function (channel, level)
|
2022-05-23 21:36:54 +00:00
|
|
|
if type(channel) ~= "number" or channel > RS_IO.R_ENABLE then
|
2022-03-23 19:36:14 +00:00
|
|
|
return false
|
2022-03-15 15:58:08 +00:00
|
|
|
else
|
2022-03-23 19:36:14 +00:00
|
|
|
return RS_DIO_MAP[channel]._f(level)
|
2022-03-15 15:58:08 +00:00
|
|
|
end
|
|
|
|
end
|
2022-05-04 17:37:01 +00:00
|
|
|
|
2022-05-16 14:38:47 +00:00
|
|
|
----------------
|
|
|
|
-- ANALOG I/O --
|
|
|
|
----------------
|
|
|
|
|
|
|
|
-- read an analog value scaled from min to max
|
|
|
|
---@param rs_value number redstone reading (0 to 15)
|
|
|
|
---@param min number minimum of range
|
|
|
|
---@param max number maximum of range
|
|
|
|
---@return number value scaled reading (min to max)
|
|
|
|
rsio.analog_read = function (rs_value, min, max)
|
|
|
|
local value = rs_value / 15
|
|
|
|
return (value * (max - min)) + min
|
|
|
|
end
|
|
|
|
|
|
|
|
-- write an analog value from the provided scale range
|
|
|
|
---@param value number value to write (from min to max range)
|
|
|
|
---@param min number minimum of range
|
|
|
|
---@param max number maximum of range
|
|
|
|
---@return number rs_value scaled redstone reading (0 to 15)
|
|
|
|
rsio.analog_write = function (value, min, max)
|
|
|
|
local scaled_value = (value - min) / (max - min)
|
|
|
|
return scaled_value * 15
|
|
|
|
end
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
return rsio
|