refactored helper function to the beginning

This commit is contained in:
Ollo 2021-03-21 14:48:09 +01:00
parent 1ef3cf4f53
commit b06ee392ab

View File

@ -2,6 +2,21 @@
local M local M
do do
local data={}
-- Utility function for round
local round = function(num)
under = math.floor(num)
upper = math.floor(num) + 1
underV = -(under - num)
upperV = upper - num
if (upperV > underV) then
return under
else
return upper
end
end
-- @fn generateLEDs -- @fn generateLEDs
-- Module displaying of the words -- Module displaying of the words
-- @param data struct with the following paramter: -- @param data struct with the following paramter:
@ -10,16 +25,19 @@ do
-- dC drawn characters -- dC drawn characters
local updateColor = function (data) local updateColor = function (data)
if (data.aoC > 0) and (data.mC ~= nil) and (data.mC > 0) then if (data.aoC > 0) and (data.mC ~= nil) and (data.mC > 0) then
local div = tonumber(data.dC/data.mC) local div = tonumber(data.dC / data.mC)
if (div < 1) then local divRound = round(div)
local perCentColor = tonumber(100 * data.mC / data.aoC)
print (tostring(data.dC) .. " -> " .. tostring(div) .. " " .. tostring(divRound) .. "")
if (divRound < 1) then
return data.colorFg return data.colorFg
elseif (div < 2) then elseif (divRound < 2) then
return data.colorM1 return data.colorM1
elseif (div < 3) then elseif (divRound < 3) then
return data.colorM2 return data.colorM2
elseif (div < 4) then elseif (divRound < 4) then
return data.colorM3 return data.colorM3
elseif (div < 5) then elseif (divRound < 5) then
return data.colorM4 return data.colorM4
else else
return data.colorFg return data.colorFg
@ -37,26 +55,11 @@ local drawLEDs = function(data, offset, numberNewChars)
return return
end end
for i=1,numberNewChars do for i=1,numberNewChars do
data.rgbBuffer:set(offset + i - 1, updateColor(data))
data.dC=data.dC+1 data.dC=data.dC+1
data.rgbBuffer:set(offset + i - 1, updateColor(data))
end end
end end
-- Utility function for round
local round = function(num)
under = math.floor(num)
upper = math.floor(num) + 1
underV = -(under - num)
upperV = upper - num
if (upperV > underV) then
return under
else
return upper
end
end
local data={}
-- @fn swapLine -- @fn swapLine
-- @param lineOffset offset (starting at 1) where the line is located to be swapped -- @param lineOffset offset (starting at 1) where the line is located to be swapped
-- works on the rgbBuffer, defined in data struct -- works on the rgbBuffer, defined in data struct
@ -113,7 +116,7 @@ local generateLEDs = function(rgbBuffer, words, colorBg, colorFg, colorM1, color
if (aoC ~= nil) then if (aoC ~= nil) then
data.aoC = aoC data.aoC = aoC
data.mC = aoC/minutes data.mC = round(aoC/minutes)
else else
data.aoC = 0 data.aoC = 0
end end