--[[ The function does curve conversion f(inputValue) = outputValue Example of a curve, having multiple ranges of kx + b, set in a register of a String type with Json [{"k":4.167,"b":0,"range":[0,3]},{"k":0,"b":12.5,"range":[3,27]},{"k":-4.167,"b":125,"range":[27,30]}] --]] function getCurveValue (curveReg, inputValue) local s, e = 1, 2 -- start & end indexes of the subranges local tab = cjson.decode( R(curveReg) ) local outputValue, stickOutflag for _, sub_ in ipairs(tab) do if ( (sub_.range[s] <= inputValue) and (inputValue < sub_.range[e]) ) then return (inputValue * sub_.k + sub_.b), nil -- normal sequence end end -- edge cases if (inputValue < tab[s].range[s]) then outputValue = tab[s].range[s] * tab[s].k + tab[s].b stickOutflag = 1 elseif (inputValue > tab[#tab].range[e]) then outputValue = tab[#tab].range[e] * tab[#tab].k + tab[#tab].b stickOutflag = #tab end return outputValue, stickOutflag end