From 0bc0decbf2688a73c348e5c41a5fb6863819ed1f Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Sun, 5 Jun 2022 16:51:38 -0400 Subject: [PATCH] util.is_int --- rtu/startup.lua | 8 ++++---- scada-common/rsio.lua | 14 ++++++++------ scada-common/util.lua | 7 +++++++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/rtu/startup.lua b/rtu/startup.lua index 209fed6..ed39c9a 100644 --- a/rtu/startup.lua +++ b/rtu/startup.lua @@ -25,7 +25,7 @@ local imatrix_rtu = require("rtu.dev.imatrix_rtu") local turbine_rtu = require("rtu.dev.turbine_rtu") local turbinev_rtu = require("rtu.dev.turbinev_rtu") -local RTU_VERSION = "beta-v0.7.8" +local RTU_VERSION = "beta-v0.7.9" local rtu_t = types.rtu_t @@ -121,7 +121,7 @@ local function configure() local io_reactor = rtu_redstone[entry_idx].for_reactor -- CHECK: reactor ID must be >= to 1 - if type(io_reactor) ~= "number" or io_reactor <= 0 or io_reactor ~= math.floor(io_reactor) then + if (not util.is_int(io_reactor)) or (io_reactor <= 0) then println(util.c("configure> redstone entry #", entry_idx, " : ", io_reactor, " isn't an integer >= 1")) return false end @@ -244,13 +244,13 @@ local function configure() end -- CHECK: index is an integer >= 1 - if type(index) ~= "number" or index <= 0 or index ~= math.floor(index) then + if (not util.is_int(index)) or (index <= 0) then println(util.c("configure> device entry #", i, ": index ", index, " isn't an integer >= 1")) return false end -- CHECK: reactor is an integer >= 1 - if type(for_reactor) ~= "number" or for_reactor <= 0 or for_reactor ~= math.floor(for_reactor) then + if (not util.is_int(for_reactor)) or (for_reactor <= 0) then println(util.c("configure> device entry #", i, ": reactor ", for_reactor, " isn't an integer >= 1")) return false end diff --git a/scada-common/rsio.lua b/scada-common/rsio.lua index 05f078c..04ab23c 100644 --- a/scada-common/rsio.lua +++ b/scada-common/rsio.lua @@ -2,6 +2,8 @@ -- Redstone I/O -- +local util = require("scada-common.util") + local rsio = {} ---------------------- @@ -101,7 +103,7 @@ function rsio.to_string(channel) "R_PLC_TIMEOUT" } - if type(channel) == "number" and channel > 0 and channel <= #names then + if util.is_int(channel) and channel > 0 and channel <= #names then return names[channel] else return "" @@ -184,7 +186,7 @@ function rsio.get_io_mode(channel) IO_MODE.DIGITAL_OUT -- R_PLC_TIMEOUT } - if type(channel) == "number" and channel > 0 and channel <= #modes then + if util.is_int(channel) and channel > 0 and channel <= #modes then return modes[channel] else return IO_MODE.ANALOG_IN @@ -201,7 +203,7 @@ local RS_SIDES = rs.getSides() ---@param channel RS_IO ---@return boolean valid function rsio.is_valid_channel(channel) - return (type(channel) == "number") and (channel > 0) and (channel <= RS_IO.R_PLC_TIMEOUT) + return util.is_int(channel) and (channel > 0) and (channel <= RS_IO.R_PLC_TIMEOUT) end -- check if a side is valid @@ -220,7 +222,7 @@ end ---@param color integer ---@return boolean valid function rsio.is_color(color) - return (type(color) == "number") and (color > 0) and (_B_AND(color, (color - 1)) == 0); + return util.is_int(color) and (color > 0) and (_B_AND(color, (color - 1)) == 0); end ----------------- @@ -243,7 +245,7 @@ end ---@param level IO_LVL ---@return boolean function rsio.digital_write(channel, level) - if type(channel) ~= "number" or channel < RS_IO.F_ALARM or channel > RS_IO.R_PLC_TIMEOUT then + if (not util.is_int(channel)) or (channel < RS_IO.F_ALARM) or (channel > RS_IO.R_PLC_TIMEOUT) then return false else return RS_DIO_MAP[channel]._f(level) @@ -255,7 +257,7 @@ end ---@param level IO_LVL ---@return boolean function rsio.digital_is_active(channel, level) - if type(channel) ~= "number" or channel > RS_IO.R_ENABLE then + if (not util.is_int(channel)) or (channel > RS_IO.R_ENABLE) then return false else return RS_DIO_MAP[channel]._f(level) diff --git a/scada-common/util.lua b/scada-common/util.lua index ba417ff..c155922 100644 --- a/scada-common/util.lua +++ b/scada-common/util.lua @@ -79,6 +79,13 @@ end -- MATH -- +-- is a value an integer +---@param x any value +---@return boolean if the number is an integer +function util.is_int(x) + return type(x) == "number" and x == math.floor(x) +end + -- round a number to an integer ---@return integer rounded function util.round(x)