Exact reimplementation of "old" C algo in LUA

This commit is contained in:
Thomas Kolb 2012-07-29 22:28:41 +02:00
parent d4a2e7ef4c
commit c15d815da9

View file

@ -13,10 +13,21 @@ red = {}
green = {} green = {}
blue = {} blue = {}
tmpRed = {}
tmpGreen = {}
tmpBlue = {}
function weighted_avg(array, centerIndex)
return 0.2 * array[centerIndex - 1] +
0.6 * array[centerIndex] +
0.2 * array[centerIndex + 1]
end
function periodic() function periodic()
local redEnergy = get_energy_in_band(0, 400); local redEnergy = get_energy_in_band(0, 400);
local greenEnergy = get_energy_in_band(400, 4000); local greenEnergy = get_energy_in_band(400, 4000);
local blueEnergy = get_energy_in_band(4000, 22000); local blueEnergy = get_energy_in_band(4000, 22000);
local centerIndex = 2 * center_module;
maxRedEnergy = maxRedEnergy * COOLDOWN_FACTOR maxRedEnergy = maxRedEnergy * COOLDOWN_FACTOR
if redEnergy > maxRedEnergy then if redEnergy > maxRedEnergy then
@ -34,22 +45,28 @@ function periodic()
end end
-- move the color buffers on by one in each direction -- move the color buffers on by one in each direction
for i = 2,center_module,1 do for i = 2,centerIndex,1 do
red[i-1] = red[i] tmpRed[i-1] = tmpRed[i]
green[i-1] = green[i] tmpGreen[i-1] = tmpGreen[i]
blue[i-1] = blue[i] tmpBlue[i-1] = tmpBlue[i]
end end
for i = num_modules-1,center_module,-1 do for i = #tmpRed-1,centerIndex,-1 do
red[i+1] = red[i] tmpRed[i+1] = tmpRed[i]
green[i+1] = green[i] tmpGreen[i+1] = tmpGreen[i]
blue[i+1] = blue[i] tmpBlue[i+1] = tmpBlue[i]
end end
-- set the new value for the center module -- set the new value for the center module
red[center_module] = redEnergy / maxRedEnergy tmpRed[centerIndex] = redEnergy / maxRedEnergy
green[center_module] = greenEnergy / maxGreenEnergy tmpGreen[centerIndex] = greenEnergy / maxGreenEnergy
blue[center_module] = blueEnergy / maxBlueEnergy tmpBlue[centerIndex] = blueEnergy / maxBlueEnergy
for i = 1,num_modules do
red[i] = weighted_avg(tmpRed, 2*i)
green[i] = weighted_avg(tmpGreen, 2*i)
blue[i] = weighted_avg(tmpBlue, 2*i)
end
-- return the 3 color arrays -- return the 3 color arrays
return red, green, blue return red, green, blue
@ -65,6 +82,12 @@ function init(nmod, cmod)
blue[i] = 0 blue[i] = 0
end end
for i = 1,2*(nmod+1) do
tmpRed[i] = 0
tmpGreen[i] = 0
tmpBlue[i] = 0
end
-- don't use fading -- don't use fading
return 0 return 0
end end