2022-05-04 17:37:01 +00:00
|
|
|
--
|
|
|
|
-- Redstone I/O
|
|
|
|
--
|
|
|
|
|
2022-06-05 20:51:38 +00:00
|
|
|
local util = require("scada-common.util")
|
|
|
|
|
2022-12-01 04:31:14 +00:00
|
|
|
---@class rsio
|
2022-05-04 17:37:01 +00:00
|
|
|
local rsio = {}
|
|
|
|
|
2023-10-04 21:26:07 +00:00
|
|
|
--#region RS I/O Constants
|
2022-05-06 15:11:53 +00:00
|
|
|
|
2023-02-19 05:14:27 +00:00
|
|
|
---@enum IO_LVL I/O logic level
|
2022-05-04 17:37:01 +00:00
|
|
|
local IO_LVL = {
|
2022-12-01 04:31:14 +00:00
|
|
|
DISCONNECT = -1, -- use for RTU session to indicate this RTU is not connected to this port
|
2022-03-15 15:58:08 +00:00
|
|
|
LOW = 0,
|
2022-05-15 00:27:06 +00:00
|
|
|
HIGH = 1,
|
2022-12-01 04:31:14 +00:00
|
|
|
FLOATING = 2 -- use for RTU session to indicate this RTU is connected but not yet read
|
2022-03-15 15:58:08 +00:00
|
|
|
}
|
|
|
|
|
2023-02-19 05:14:27 +00:00
|
|
|
---@enum IO_DIR I/O direction
|
2022-05-04 17:37:01 +00:00
|
|
|
local IO_DIR = {
|
2022-03-23 19:36:14 +00:00
|
|
|
IN = 0,
|
|
|
|
OUT = 1
|
|
|
|
}
|
|
|
|
|
2023-02-19 05:14:27 +00:00
|
|
|
---@enum IO_MODE I/O mode (digital/analog input/output)
|
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
|
|
|
}
|
|
|
|
|
2023-02-19 05:14:27 +00:00
|
|
|
---@enum IO_PORT redstone I/O logic port
|
2022-12-01 04:31:14 +00:00
|
|
|
local IO_PORT = {
|
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
|
2023-02-19 05:14:27 +00:00
|
|
|
F_ACK = 2, -- active high, facility alarm acknowledge
|
2022-02-08 20:42:06 +00:00
|
|
|
|
|
|
|
-- reactor
|
2023-02-19 05:14:27 +00:00
|
|
|
R_SCRAM = 3, -- active low, reactor scram
|
|
|
|
R_RESET = 4, -- active high, reactor RPS reset
|
|
|
|
R_ENABLE = 5, -- active high, reactor enable
|
|
|
|
|
|
|
|
-- unit
|
|
|
|
U_ACK = 6, -- active high, unit alarm acknowledge
|
2022-02-08 20:42:06 +00:00
|
|
|
|
|
|
|
-- digital outputs --
|
|
|
|
|
2022-05-16 14:38:47 +00:00
|
|
|
-- facility
|
2023-03-04 06:37:15 +00:00
|
|
|
F_ALARM = 7, -- active high, facility-wide alarm (any high priority unit alarm)
|
2023-10-04 21:26:07 +00:00
|
|
|
F_ALARM_ANY = 8, -- active high, any alarm regardless of priority
|
2024-04-28 17:08:16 +00:00
|
|
|
F_MATRIX_LOW = 27, -- active high, induction matrix charge low
|
2024-04-20 20:32:18 +00:00
|
|
|
F_MATRIX_HIGH = 28, -- active high, induction matrix charge high
|
2022-05-16 14:38:47 +00:00
|
|
|
|
2022-02-08 20:42:06 +00:00
|
|
|
-- waste
|
2023-10-04 21:26:07 +00:00
|
|
|
WASTE_PU = 9, -- active low, waste -> plutonium -> pellets route
|
|
|
|
WASTE_PO = 10, -- active low, waste -> polonium route
|
|
|
|
WASTE_POPL = 11, -- active low, polonium -> pellets route
|
|
|
|
WASTE_AM = 12, -- active low, polonium -> anti-matter route
|
2022-02-08 20:42:06 +00:00
|
|
|
|
|
|
|
-- reactor
|
2023-10-04 21:26:07 +00:00
|
|
|
R_ACTIVE = 13, -- active high, reactor is active
|
|
|
|
R_AUTO_CTRL = 14, -- active high, reactor burn rate is automatic
|
|
|
|
R_SCRAMMED = 15, -- active high, reactor is scrammed
|
|
|
|
R_AUTO_SCRAM = 16, -- active high, reactor was automatically scrammed
|
|
|
|
R_HIGH_DMG = 17, -- active high, reactor damage is high
|
|
|
|
R_HIGH_TEMP = 18, -- active high, reactor is at a high temperature
|
|
|
|
R_LOW_COOLANT = 19, -- active high, reactor has very low coolant
|
|
|
|
R_EXCESS_HC = 20, -- active high, reactor has excess heated coolant
|
|
|
|
R_EXCESS_WS = 21, -- active high, reactor has excess waste
|
|
|
|
R_INSUFF_FUEL = 22, -- active high, reactor has insufficent fuel
|
|
|
|
R_PLC_FAULT = 23, -- active high, reactor PLC reports a device access fault
|
|
|
|
R_PLC_TIMEOUT = 24, -- active high, reactor PLC has not been heard from
|
2023-02-13 23:20:48 +00:00
|
|
|
|
|
|
|
-- unit outputs
|
2023-10-04 21:26:07 +00:00
|
|
|
U_ALARM = 25, -- active high, unit alarm
|
2024-04-20 20:32:18 +00:00
|
|
|
U_EMER_COOL = 26, -- active low, emergency coolant control
|
|
|
|
|
|
|
|
-- analog outputs --
|
|
|
|
|
|
|
|
-- facility
|
|
|
|
F_MATRIX_CHG = 29 -- analog charge level of the induction matrix
|
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
|
2022-12-01 04:31:14 +00:00
|
|
|
rsio.IO = IO_PORT
|
2024-04-20 20:32:18 +00:00
|
|
|
|
|
|
|
rsio.NUM_PORTS = 29
|
|
|
|
rsio.NUM_DIG_PORTS = 28
|
|
|
|
rsio.NUM_ANA_PORTS = 1
|
2023-10-20 03:22:04 +00:00
|
|
|
|
|
|
|
-- self checks
|
|
|
|
|
2024-04-20 20:32:18 +00:00
|
|
|
assert(rsio.NUM_PORTS == (rsio.NUM_DIG_PORTS + rsio.NUM_ANA_PORTS), "port counts inconsistent")
|
|
|
|
|
2023-10-20 03:22:04 +00:00
|
|
|
local dup_chk = {}
|
|
|
|
for _, v in pairs(IO_PORT) do
|
|
|
|
assert(dup_chk[v] ~= true, "duplicate in port list")
|
|
|
|
dup_chk[v] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
assert(#dup_chk == rsio.NUM_PORTS, "port list malformed")
|
2022-05-04 17:37:01 +00:00
|
|
|
|
2023-10-04 21:26:07 +00:00
|
|
|
--#endregion
|
|
|
|
|
2024-04-20 20:32:18 +00:00
|
|
|
--#region Utility Functions and Attribute Tables
|
|
|
|
|
|
|
|
local IO = IO_PORT
|
2023-10-20 03:22:04 +00:00
|
|
|
|
2024-04-20 20:32:18 +00:00
|
|
|
-- list of all port names
|
|
|
|
local PORT_NAMES = {}
|
|
|
|
for k, v in pairs(IO) do PORT_NAMES[v] = k end
|
|
|
|
|
|
|
|
-- list of all port I/O modes
|
2023-11-30 03:25:34 +00:00
|
|
|
local MODES = {
|
2024-04-20 20:32:18 +00:00
|
|
|
[IO.F_SCRAM] = IO_MODE.DIGITAL_IN,
|
|
|
|
[IO.F_ACK] = IO_MODE.DIGITAL_IN,
|
|
|
|
[IO.R_SCRAM] = IO_MODE.DIGITAL_IN,
|
|
|
|
[IO.R_RESET] = IO_MODE.DIGITAL_IN,
|
|
|
|
[IO.R_ENABLE] = IO_MODE.DIGITAL_IN,
|
|
|
|
[IO.U_ACK] = IO_MODE.DIGITAL_IN,
|
|
|
|
[IO.F_ALARM] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.F_ALARM_ANY] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.F_MATRIX_LOW] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.F_MATRIX_HIGH] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.WASTE_PU] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.WASTE_PO] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.WASTE_POPL] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.WASTE_AM] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.R_ACTIVE] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.R_AUTO_CTRL] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.R_SCRAMMED] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.R_AUTO_SCRAM] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.R_HIGH_DMG] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.R_HIGH_TEMP] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.R_LOW_COOLANT] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.R_EXCESS_HC] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.R_EXCESS_WS] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.R_INSUFF_FUEL] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.R_PLC_FAULT] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.R_PLC_TIMEOUT] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.U_ALARM] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.U_EMER_COOL] = IO_MODE.DIGITAL_OUT,
|
|
|
|
[IO.F_MATRIX_CHG] = IO_MODE.ANALOG_OUT
|
2023-11-30 03:25:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-20 03:22:04 +00:00
|
|
|
assert(rsio.NUM_PORTS == #PORT_NAMES, "port names length incorrect")
|
2023-11-30 03:25:34 +00:00
|
|
|
assert(rsio.NUM_PORTS == #MODES, "modes length incorrect")
|
2023-10-20 03:22:04 +00:00
|
|
|
|
2022-12-01 04:31:14 +00:00
|
|
|
-- port to string
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-12-01 04:31:14 +00:00
|
|
|
---@param port IO_PORT
|
|
|
|
function rsio.to_string(port)
|
2023-10-20 03:22:04 +00:00
|
|
|
if util.is_int(port) and port > 0 and port <= #PORT_NAMES then
|
|
|
|
return PORT_NAMES[port]
|
2022-03-23 19:36:14 +00:00
|
|
|
else
|
2023-07-30 16:24:54 +00:00
|
|
|
return "UNKNOWN"
|
2022-03-23 19:36:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-06 15:11:53 +00:00
|
|
|
local _B_AND = bit.band
|
2022-03-23 19:36:14 +00:00
|
|
|
|
2022-12-01 04:31:14 +00:00
|
|
|
local function _I_ACTIVE_HIGH(level) return level == IO_LVL.HIGH end
|
|
|
|
local function _I_ACTIVE_LOW(level) return level == IO_LVL.LOW end
|
|
|
|
local function _O_ACTIVE_HIGH(active) if active then return IO_LVL.HIGH else return IO_LVL.LOW end end
|
|
|
|
local function _O_ACTIVE_LOW(active) if active then return IO_LVL.LOW else return IO_LVL.HIGH end end
|
2022-03-15 15:58:08 +00:00
|
|
|
|
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 = {
|
2024-04-20 20:32:18 +00:00
|
|
|
[IO.F_SCRAM] = { _in = _I_ACTIVE_LOW, _out = _O_ACTIVE_LOW, mode = IO_DIR.IN },
|
|
|
|
[IO.F_ACK] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.IN },
|
|
|
|
|
|
|
|
[IO.R_SCRAM] = { _in = _I_ACTIVE_LOW, _out = _O_ACTIVE_LOW, mode = IO_DIR.IN },
|
|
|
|
[IO.R_RESET] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.IN },
|
|
|
|
[IO.R_ENABLE] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.IN },
|
|
|
|
|
|
|
|
[IO.U_ACK] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.IN },
|
|
|
|
|
|
|
|
[IO.F_ALARM] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
|
|
|
[IO.F_ALARM_ANY] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
|
|
|
[IO.F_MATRIX_LOW] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
|
|
|
[IO.F_MATRIX_HIGH] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
|
|
|
|
|
|
|
[IO.WASTE_PU] = { _in = _I_ACTIVE_LOW, _out = _O_ACTIVE_LOW, mode = IO_DIR.OUT },
|
|
|
|
[IO.WASTE_PO] = { _in = _I_ACTIVE_LOW, _out = _O_ACTIVE_LOW, mode = IO_DIR.OUT },
|
|
|
|
[IO.WASTE_POPL] = { _in = _I_ACTIVE_LOW, _out = _O_ACTIVE_LOW, mode = IO_DIR.OUT },
|
|
|
|
[IO.WASTE_AM] = { _in = _I_ACTIVE_LOW, _out = _O_ACTIVE_LOW, mode = IO_DIR.OUT },
|
|
|
|
|
|
|
|
[IO.R_ACTIVE] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
|
|
|
[IO.R_AUTO_CTRL] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
|
|
|
[IO.R_SCRAMMED] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
|
|
|
[IO.R_AUTO_SCRAM] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
|
|
|
[IO.R_HIGH_DMG] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
|
|
|
[IO.R_HIGH_TEMP] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
|
|
|
[IO.R_LOW_COOLANT] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
|
|
|
[IO.R_EXCESS_HC] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
|
|
|
[IO.R_EXCESS_WS] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
|
|
|
[IO.R_INSUFF_FUEL] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
|
|
|
[IO.R_PLC_FAULT] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
|
|
|
[IO.R_PLC_TIMEOUT] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
|
|
|
|
|
|
|
[IO.U_ALARM] = { _in = _I_ACTIVE_HIGH, _out = _O_ACTIVE_HIGH, mode = IO_DIR.OUT },
|
|
|
|
[IO.U_EMER_COOL] = { _in = _I_ACTIVE_LOW, _out = _O_ACTIVE_LOW, mode = IO_DIR.OUT }
|
2022-03-15 15:58:08 +00:00
|
|
|
}
|
|
|
|
|
2024-04-20 20:32:18 +00:00
|
|
|
assert(rsio.NUM_DIG_PORTS == #RS_DIO_MAP, "RS_DIO_MAP length incorrect")
|
2023-11-30 03:25:34 +00:00
|
|
|
|
|
|
|
-- get the I/O direction of a port
|
|
|
|
---@nodiscard
|
|
|
|
---@param port IO_PORT
|
|
|
|
---@return IO_DIR
|
|
|
|
function rsio.get_io_dir(port)
|
2024-04-20 20:32:18 +00:00
|
|
|
if rsio.is_valid_port(port) then
|
|
|
|
return util.trinary(MODES[port] == IO_MODE.DIGITAL_OUT or MODES[port] == IO_MODE.ANALOG_OUT, IO_DIR.OUT, IO_DIR.IN)
|
2023-11-30 03:25:34 +00:00
|
|
|
else return IO_DIR.IN end
|
|
|
|
end
|
|
|
|
|
2022-12-01 04:31:14 +00:00
|
|
|
-- get the mode of a port
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-12-01 04:31:14 +00:00
|
|
|
---@param port IO_PORT
|
2022-05-10 21:06:27 +00:00
|
|
|
---@return IO_MODE
|
2022-12-01 04:31:14 +00:00
|
|
|
function rsio.get_io_mode(port)
|
2023-12-18 01:43:08 +00:00
|
|
|
if rsio.is_valid_port(port) then return MODES[port]
|
2023-11-30 03:25:34 +00:00
|
|
|
else return IO_MODE.ANALOG_IN end
|
2022-03-23 19:36:14 +00:00
|
|
|
end
|
|
|
|
|
2023-10-04 21:26:07 +00:00
|
|
|
--#endregion
|
|
|
|
|
|
|
|
--#region Generic Checks
|
2022-05-06 15:11:53 +00:00
|
|
|
|
|
|
|
local RS_SIDES = rs.getSides()
|
|
|
|
|
2022-12-01 04:31:14 +00:00
|
|
|
-- check if a port is valid
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-12-01 04:31:14 +00:00
|
|
|
---@param port IO_PORT
|
2022-05-10 21:06:27 +00:00
|
|
|
---@return boolean valid
|
2022-12-01 04:31:14 +00:00
|
|
|
function rsio.is_valid_port(port)
|
2023-11-30 03:25:34 +00:00
|
|
|
return util.is_int(port) and port > 0 and port <= rsio.NUM_PORTS
|
2022-05-06 15:11:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- check if a side is valid
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param side string
|
|
|
|
---@return boolean valid
|
2022-05-31 20:09:06 +00:00
|
|
|
function rsio.is_valid_side(side)
|
2022-05-06 15:11:53 +00:00
|
|
|
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
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2023-11-05 18:23:22 +00:00
|
|
|
---@param color any
|
2022-05-10 21:06:27 +00:00
|
|
|
---@return boolean valid
|
2022-05-31 20:09:06 +00:00
|
|
|
function rsio.is_color(color)
|
2022-10-20 17:59:35 +00:00
|
|
|
return util.is_int(color) and (color > 0) and (_B_AND(color, (color - 1)) == 0)
|
2022-05-06 15:11:53 +00:00
|
|
|
end
|
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
-- color to string
|
|
|
|
---@nodiscard
|
|
|
|
---@param color color
|
|
|
|
---@return string
|
|
|
|
function rsio.color_name(color)
|
|
|
|
local color_name_map = { [colors.red] = "red", [colors.orange] = "orange", [colors.yellow] = "yellow", [colors.lime] = "lime", [colors.green] = "green", [colors.cyan] = "cyan", [colors.lightBlue] = "lightBlue", [colors.blue] = "blue", [colors.purple] = "purple", [colors.magenta] = "magenta", [colors.pink] = "pink", [colors.white] = "white", [colors.lightGray] = "lightGray", [colors.gray] = "gray", [colors.black] = "black", [colors.brown] = "brown" }
|
|
|
|
|
|
|
|
if rsio.is_color(color) then
|
|
|
|
return color_name_map[color]
|
|
|
|
else return "unknown" end
|
|
|
|
end
|
|
|
|
|
2023-10-04 21:26:07 +00:00
|
|
|
--#endregion
|
|
|
|
|
|
|
|
--#region Digital I/O
|
2022-05-06 15:11:53 +00:00
|
|
|
|
2024-04-20 20:32:18 +00:00
|
|
|
-- check if a port is digital
|
|
|
|
---@nodiscard
|
|
|
|
---@param port IO_PORT
|
|
|
|
function rsio.is_digital(port)
|
|
|
|
return rsio.is_valid_port(port) and (MODES[port] == IO_MODE.DIGITAL_IN or MODES[port] == IO_MODE.DIGITAL_OUT)
|
|
|
|
end
|
|
|
|
|
2022-12-01 04:31:14 +00:00
|
|
|
-- get digital I/O level reading from a redstone boolean input value
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
|
|
|
---@param rs_value boolean raw value from redstone
|
2022-05-10 21:06:27 +00:00
|
|
|
---@return IO_LVL
|
2022-05-31 20:09:06 +00:00
|
|
|
function rsio.digital_read(rs_value)
|
2023-02-19 05:14:27 +00:00
|
|
|
if rs_value then return IO_LVL.HIGH else return IO_LVL.LOW end
|
2022-03-15 15:58:08 +00:00
|
|
|
end
|
|
|
|
|
2022-12-01 04:31:14 +00:00
|
|
|
-- get redstone boolean output value corresponding to a digital I/O level
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
|
|
|
---@param level IO_LVL logic level
|
2022-05-27 22:10:06 +00:00
|
|
|
---@return boolean
|
2023-10-04 21:26:07 +00:00
|
|
|
function rsio.digital_write(level) return level == IO_LVL.HIGH end
|
2022-12-01 04:31:14 +00:00
|
|
|
|
|
|
|
-- returns the level corresponding to active
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
|
|
|
---@param port IO_PORT port (to determine active high/low)
|
|
|
|
---@param active boolean state to convert to logic level
|
2022-12-01 04:31:14 +00:00
|
|
|
---@return IO_LVL|false
|
|
|
|
function rsio.digital_write_active(port, active)
|
2024-04-20 20:32:18 +00:00
|
|
|
if not rsio.is_digital(port) then
|
2022-05-27 22:10:06 +00:00
|
|
|
return false
|
2022-03-15 15:58:08 +00:00
|
|
|
else
|
2022-12-01 04:31:14 +00:00
|
|
|
return RS_DIO_MAP[port]._out(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
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
|
|
|
---@param port IO_PORT port (to determine active low/high)
|
|
|
|
---@param level IO_LVL logic level
|
|
|
|
---@return boolean|nil state true for active, false for inactive, or nil if invalid port or level provided
|
2022-12-01 04:31:14 +00:00
|
|
|
function rsio.digital_is_active(port, level)
|
2024-04-20 20:32:18 +00:00
|
|
|
if (not rsio.is_digital(port)) or level == IO_LVL.FLOATING or level == IO_LVL.DISCONNECT then
|
2022-12-01 04:31:14 +00:00
|
|
|
return nil
|
2022-03-15 15:58:08 +00:00
|
|
|
else
|
2022-12-01 04:31:14 +00:00
|
|
|
return RS_DIO_MAP[port]._in(level)
|
2022-03-15 15:58:08 +00:00
|
|
|
end
|
|
|
|
end
|
2022-05-04 17:37:01 +00:00
|
|
|
|
2023-10-04 21:26:07 +00:00
|
|
|
--#endregion
|
|
|
|
|
|
|
|
--#region Analog I/O
|
2022-05-16 14:38:47 +00:00
|
|
|
|
2024-04-20 20:32:18 +00:00
|
|
|
-- check if a port is analog
|
|
|
|
---@nodiscard
|
|
|
|
---@param port IO_PORT
|
|
|
|
function rsio.is_analog(port)
|
|
|
|
return rsio.is_valid_port(port) and (MODES[port] == IO_MODE.ANALOG_IN or MODES[port] == IO_MODE.ANALOG_OUT)
|
|
|
|
end
|
|
|
|
|
2022-05-16 14:38:47 +00:00
|
|
|
-- read an analog value scaled from min to max
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-16 14:38:47 +00:00
|
|
|
---@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)
|
2022-05-31 20:09:06 +00:00
|
|
|
function rsio.analog_read(rs_value, min, max)
|
2022-05-16 14:38:47 +00:00
|
|
|
local value = rs_value / 15
|
|
|
|
return (value * (max - min)) + min
|
|
|
|
end
|
|
|
|
|
|
|
|
-- write an analog value from the provided scale range
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-16 14:38:47 +00:00
|
|
|
---@param value number value to write (from min to max range)
|
|
|
|
---@param min number minimum of range
|
|
|
|
---@param max number maximum of range
|
2024-04-20 20:32:18 +00:00
|
|
|
---@return integer rs_value scaled redstone reading (0 to 15)
|
2022-05-31 20:09:06 +00:00
|
|
|
function rsio.analog_write(value, min, max)
|
2022-05-16 14:38:47 +00:00
|
|
|
local scaled_value = (value - min) / (max - min)
|
2023-02-19 05:14:27 +00:00
|
|
|
return math.floor(scaled_value * 15)
|
2022-05-16 14:38:47 +00:00
|
|
|
end
|
|
|
|
|
2023-10-04 21:26:07 +00:00
|
|
|
--#endregion
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
return rsio
|