util.is_int
This commit is contained in:
parent
1c819779c7
commit
0bc0decbf2
@ -25,7 +25,7 @@ local imatrix_rtu = require("rtu.dev.imatrix_rtu")
|
|||||||
local turbine_rtu = require("rtu.dev.turbine_rtu")
|
local turbine_rtu = require("rtu.dev.turbine_rtu")
|
||||||
local turbinev_rtu = require("rtu.dev.turbinev_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
|
local rtu_t = types.rtu_t
|
||||||
|
|
||||||
@ -121,7 +121,7 @@ local function configure()
|
|||||||
local io_reactor = rtu_redstone[entry_idx].for_reactor
|
local io_reactor = rtu_redstone[entry_idx].for_reactor
|
||||||
|
|
||||||
-- CHECK: reactor ID must be >= to 1
|
-- 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"))
|
println(util.c("configure> redstone entry #", entry_idx, " : ", io_reactor, " isn't an integer >= 1"))
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
@ -244,13 +244,13 @@ local function configure()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- CHECK: index is an integer >= 1
|
-- 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"))
|
println(util.c("configure> device entry #", i, ": index ", index, " isn't an integer >= 1"))
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- CHECK: reactor is an integer >= 1
|
-- 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"))
|
println(util.c("configure> device entry #", i, ": reactor ", for_reactor, " isn't an integer >= 1"))
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|||||||
@ -2,6 +2,8 @@
|
|||||||
-- Redstone I/O
|
-- Redstone I/O
|
||||||
--
|
--
|
||||||
|
|
||||||
|
local util = require("scada-common.util")
|
||||||
|
|
||||||
local rsio = {}
|
local rsio = {}
|
||||||
|
|
||||||
----------------------
|
----------------------
|
||||||
@ -101,7 +103,7 @@ function rsio.to_string(channel)
|
|||||||
"R_PLC_TIMEOUT"
|
"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]
|
return names[channel]
|
||||||
else
|
else
|
||||||
return ""
|
return ""
|
||||||
@ -184,7 +186,7 @@ function rsio.get_io_mode(channel)
|
|||||||
IO_MODE.DIGITAL_OUT -- R_PLC_TIMEOUT
|
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]
|
return modes[channel]
|
||||||
else
|
else
|
||||||
return IO_MODE.ANALOG_IN
|
return IO_MODE.ANALOG_IN
|
||||||
@ -201,7 +203,7 @@ local RS_SIDES = rs.getSides()
|
|||||||
---@param channel RS_IO
|
---@param channel RS_IO
|
||||||
---@return boolean valid
|
---@return boolean valid
|
||||||
function rsio.is_valid_channel(channel)
|
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
|
end
|
||||||
|
|
||||||
-- check if a side is valid
|
-- check if a side is valid
|
||||||
@ -220,7 +222,7 @@ end
|
|||||||
---@param color integer
|
---@param color integer
|
||||||
---@return boolean valid
|
---@return boolean valid
|
||||||
function rsio.is_color(color)
|
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
|
end
|
||||||
|
|
||||||
-----------------
|
-----------------
|
||||||
@ -243,7 +245,7 @@ end
|
|||||||
---@param level IO_LVL
|
---@param level IO_LVL
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function rsio.digital_write(channel, level)
|
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
|
return false
|
||||||
else
|
else
|
||||||
return RS_DIO_MAP[channel]._f(level)
|
return RS_DIO_MAP[channel]._f(level)
|
||||||
@ -255,7 +257,7 @@ end
|
|||||||
---@param level IO_LVL
|
---@param level IO_LVL
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function rsio.digital_is_active(channel, level)
|
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
|
return false
|
||||||
else
|
else
|
||||||
return RS_DIO_MAP[channel]._f(level)
|
return RS_DIO_MAP[channel]._f(level)
|
||||||
|
|||||||
@ -79,6 +79,13 @@ end
|
|||||||
|
|
||||||
-- MATH --
|
-- 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
|
-- round a number to an integer
|
||||||
---@return integer rounded
|
---@return integer rounded
|
||||||
function util.round(x)
|
function util.round(x)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user