changed redstone I/O capabilities, added analog read/write scaling functions

This commit is contained in:
Mikayla Fischler 2022-05-16 10:38:47 -04:00
parent 374bfb7a19
commit 4834dbf781

View File

@ -23,10 +23,10 @@ local IO_DIR = {
---@alias IO_MODE integer
local IO_MODE = {
DIGITAL_OUT = 0,
DIGITAL_IN = 1,
ANALOG_OUT = 2,
ANALOG_IN = 3
DIGITAL_IN = 0,
DIGITAL_OUT = 1,
ANALOG_IN = 2,
ANALOG_OUT = 3
}
---@alias RS_IO integer
@ -35,37 +35,35 @@ local RS_IO = {
-- facility
F_SCRAM = 1, -- active low, facility-wide scram
F_AE2_LIVE = 2, -- active high, indicates whether AE2 network is online (hint: use redstone P2P)
-- reactor
R_SCRAM = 3, -- active low, reactor scram
R_ENABLE = 4, -- active high, reactor enable
R_SCRAM = 2, -- active low, reactor scram
R_ENABLE = 3, -- active high, reactor enable
-- digital outputs --
-- facility
F_ALARM = 4, -- active high, facility safety alarm
-- waste
WASTE_PO = 5, -- active low, polonium routing
WASTE_PU = 6, -- active low, plutonium routing
WASTE_AM = 7, -- active low, antimatter routing
-- reactor
R_SCRAMMED = 8, -- active high, if the reactor is scrammed
R_AUTO_SCRAM = 9, -- active high, if the reactor was automatically scrammed
R_ACTIVE = 10, -- active high, if the reactor is active
R_AUTO_CTRL = 11, -- active high, if the reactor burn rate is automatic
R_DMG_CRIT = 12, -- active high, if the reactor damage is critical
R_HIGH_TEMP = 13, -- active high, if the reactor is at a high temperature
R_NO_COOLANT = 14, -- active high, if the reactor has no coolant
R_EXCESS_HC = 15, -- active high, if the reactor has excess heated coolant
R_EXCESS_WS = 16, -- active high, if the reactor has excess waste
R_INSUFF_FUEL = 17, -- active high, if the reactor has insufficent fuel
R_PLC_TIMEOUT = 18, -- active high, if the reactor PLC has not been heard from
-- analog outputs --
A_R_BURN_RATE = 19, -- reactor burn rate percentage
A_B_BOIL_RATE = 20, -- boiler boil rate percentage
A_T_FLOW_RATE = 21 -- turbine flow rate percentage
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
}
rsio.IO_LVL = IO_LVL
@ -82,12 +80,13 @@ rsio.IO = RS_IO
rsio.to_string = function (channel)
local names = {
"F_SCRAM",
"F_AE2_LIVE",
"R_SCRAM",
"R_ENABLE",
"F_ALARM",
"WASTE_PO",
"WASTE_PU",
"WASTE_AM",
"R_ALARM",
"R_SCRAMMED",
"R_AUTO_SCRAM",
"R_ACTIVE",
@ -98,10 +97,8 @@ rsio.to_string = function (channel)
"R_EXCESS_HC",
"R_EXCESS_WS",
"R_INSUFF_FUEL",
"R_PLC_TIMEOUT",
"A_R_BURN_RATE",
"A_B_BOIL_RATE",
"A_T_FLOW_RATE"
"R_PLC_FAULT",
"R_PLC_TIMEOUT"
}
if channel > 0 and channel <= #names then
@ -124,18 +121,20 @@ local _DO_ACTIVE_LOW = function (on) return _TRINARY(on, IO_LVL.LOW, IO_LVL.HIGH
local RS_DIO_MAP = {
-- F_SCRAM
{ _f = _DI_ACTIVE_LOW, mode = IO_DIR.IN },
-- F_AE2_LIVE
{ _f = _DI_ACTIVE_HIGH, mode = IO_DIR.IN },
-- R_SCRAM
{ _f = _DI_ACTIVE_LOW, mode = IO_DIR.IN },
-- R_ENABLE
{ _f = _DI_ACTIVE_HIGH, mode = IO_DIR.IN },
-- F_ALARM
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
-- WASTE_PO
{ _f = _DO_ACTIVE_LOW, mode = IO_DIR.OUT },
-- WASTE_PU
{ _f = _DO_ACTIVE_LOW, mode = IO_DIR.OUT },
-- WASTE_AM
{ _f = _DO_ACTIVE_LOW, mode = IO_DIR.OUT },
-- R_ALARM
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
-- R_SCRAMMED
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
-- R_AUTO_SCRAM
@ -156,6 +155,8 @@ local RS_DIO_MAP = {
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
-- R_INSUFF_FUEL
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
-- R_PLC_FAULT
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT },
-- R_PLC_TIMEOUT
{ _f = _DO_ACTIVE_HIGH, mode = IO_DIR.OUT }
}
@ -166,12 +167,13 @@ local RS_DIO_MAP = {
rsio.get_io_mode = function (channel)
local modes = {
IO_MODE.DIGITAL_IN, -- F_SCRAM
IO_MODE.DIGITAL_IN, -- F_AE2_LIVE
IO_MODE.DIGITAL_IN, -- R_SCRAM
IO_MODE.DIGITAL_IN, -- R_ENABLE
IO_MODE.DIGITAL_OUT, -- F_ALARM
IO_MODE.DIGITAL_OUT, -- WASTE_PO
IO_MODE.DIGITAL_OUT, -- WASTE_PU
IO_MODE.DIGITAL_OUT, -- WASTE_AM
IO_MODE.DIGITAL_OUT, -- R_ALARM
IO_MODE.DIGITAL_OUT, -- R_SCRAMMED
IO_MODE.DIGITAL_OUT, -- R_AUTO_SCRAM
IO_MODE.DIGITAL_OUT, -- R_ACTIVE
@ -182,10 +184,8 @@ rsio.get_io_mode = function (channel)
IO_MODE.DIGITAL_OUT, -- R_EXCESS_HC
IO_MODE.DIGITAL_OUT, -- R_EXCESS_WS
IO_MODE.DIGITAL_OUT, -- R_INSUFF_FUEL
IO_MODE.DIGITAL_OUT, -- R_PLC_TIMEOUT
IO_MODE.ANALOG_OUT, -- A_R_BURN_RATE
IO_MODE.ANALOG_OUT, -- A_B_BOIL_RATE
IO_MODE.ANALOG_OUT -- A_T_FLOW_RATE
IO_MODE.DIGITAL_OUT, -- R_PLC_FAULT
IO_MODE.DIGITAL_OUT -- R_PLC_TIMEOUT
}
if channel > 0 and channel <= #modes then
@ -205,7 +205,7 @@ local RS_SIDES = rs.getSides()
---@param channel RS_IO
---@return boolean valid
rsio.is_valid_channel = function (channel)
return (channel ~= nil) and (channel > 0) and (channel <= RS_IO.A_T_FLOW_RATE)
return (channel ~= nil) and (channel > 0) and (channel <= #RS_IO)
end
-- check if a side is valid
@ -266,4 +266,28 @@ rsio.digital_is_active = function (channel, level)
end
end
----------------
-- 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
return rsio