Files
mapo-mono/MP.MONO.DECODER/lua/ParamsDecoder.lua
T
2022-03-23 20:07:26 +01:00

85 lines
2.1 KiB
Lua

--[[---------------------------------------------------
Procedura calcolo statistiche parametri:
Variabili IN:
- vcFunct(string): tipo di processing da effettuare tra [POINT/AVG/MEDIAN/MIN/MAX]
- valList(<double>): elenco VALORI double da processare
Variabili OUT:
- calcVal(double): valore calcolato finale
-----------------------------------------------------]]
-- Per eventuale debug
local ZBS = "c:/ZeroBraneStudio"
if not package.path:find(ZBS,1,true) then
package.path = ZBS .. "/lualibs/?/?.lua;" .. ZBS .. "/lualibs/?.lua;" .. package.path
package.cpath = ZBS .. "/bin/?.dll;" .. ZBS .. "/bin/clibs53/?.dll;" .. package.cpath
end
-- variabile semaforo callMode (locali o remote da NLua)
callMode = callMode or ''
vcFunct = vcFunct or ''
valList = valList or {}
calcVal = 0
calcOk = false
-- se non è da NLua inizializzo variabili accessorie
local function checkInit()
if (callMode ~= 'NLua') then
-- imposto valori test
valList = { 4.0, 5.0, 3.0, 6.0, 1.0, 2.0 }
vcFunct = 'MIN'
--POINT AVG MEDIAN MIN MAX
end
end
local function doCalc()
if(#valList>0) then
-- verifica il tipo di richiesta
if(vcFunct == 'AVG') then
s = 0
for i,v in ipairs(valList) do
s = s + v
end
calcVal = s / #valList
elseif(vcFunct == 'POINT') then
calcVal = valList[#valList]
elseif(vcFunct == 'MEDIAN') then
table.sort(valList)
calcVal = valList[#valList/2]
elseif(vcFunct == 'MIN') then
table.sort(valList)
calcVal = valList[1]
elseif(vcFunct == 'MAX') then
table.sort(valList)
calcVal = valList[#valList]
end
calcOk = true
else
calcVal = 0
calcOk = false
end
end
local function displayTestInfo()
if (callMode ~= 'NLua') then
print('------------------------------')
print('calcOk: ' .. tostring(calcOk))
print('vcFunct: ' .. vcFunct)
for i,val in pairs(valList) do
print("v_"..i.." | "..val)
end
print('calcVal: ' .. calcVal)
print('------------------------------')
end
end
-- MAIN
checkInit()
doCalc()
displayTestInfo()