Sound2Light auf RGB(W)-LED-Leisten
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pulsetunnel.lua 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. COOLDOWN_FACTOR = 0.9998
  2. num_modules = 20
  3. center_module = 10
  4. -- maximum energy values for each band
  5. maxRedEnergy = 1
  6. maxGreenEnergy = 1
  7. maxBlueEnergy = 1
  8. -- output color buffers
  9. red = {}
  10. green = {}
  11. blue = {}
  12. tmpRed = {}
  13. tmpGreen = {}
  14. tmpBlue = {}
  15. function weighted_avg(array, centerIndex)
  16. return 0.2 * array[centerIndex - 1] +
  17. 0.6 * array[centerIndex] +
  18. 0.2 * array[centerIndex + 1]
  19. end
  20. function periodic()
  21. local redEnergy = get_energy_in_band(0, 400);
  22. local greenEnergy = get_energy_in_band(400, 4000);
  23. local blueEnergy = get_energy_in_band(4000, 22000);
  24. local centerIndex = 2 * center_module + 1;
  25. maxRedEnergy = maxRedEnergy * COOLDOWN_FACTOR
  26. if redEnergy > maxRedEnergy then
  27. maxRedEnergy = redEnergy
  28. end
  29. maxGreenEnergy = maxGreenEnergy * COOLDOWN_FACTOR
  30. if greenEnergy > maxGreenEnergy then
  31. maxGreenEnergy = greenEnergy
  32. end
  33. maxBlueEnergy = maxBlueEnergy * COOLDOWN_FACTOR
  34. if blueEnergy > maxBlueEnergy then
  35. maxBlueEnergy = blueEnergy
  36. end
  37. -- move the color buffers on by one in each direction
  38. for i = 2,centerIndex,1 do
  39. tmpRed[i-1] = tmpRed[i]
  40. tmpGreen[i-1] = tmpGreen[i]
  41. tmpBlue[i-1] = tmpBlue[i]
  42. end
  43. for i = #tmpRed-1,centerIndex,-1 do
  44. tmpRed[i+1] = tmpRed[i]
  45. tmpGreen[i+1] = tmpGreen[i]
  46. tmpBlue[i+1] = tmpBlue[i]
  47. end
  48. -- set the new value for the center module
  49. tmpRed[centerIndex] = redEnergy / maxRedEnergy
  50. tmpGreen[centerIndex] = greenEnergy / maxGreenEnergy
  51. tmpBlue[centerIndex] = blueEnergy / maxBlueEnergy
  52. for i = 1,num_modules do
  53. red[i] = weighted_avg(tmpRed, 2*i)
  54. green[i] = weighted_avg(tmpGreen, 2*i)
  55. blue[i] = weighted_avg(tmpBlue, 2*i)
  56. end
  57. -- return the 3 color arrays
  58. return red, green, blue
  59. end
  60. function init(nmod, cmod)
  61. num_modules = nmod
  62. center_module = cmod
  63. for i = 1,nmod do
  64. red[i] = 0
  65. green[i] = 0
  66. blue[i] = 0
  67. end
  68. for i = 1,2*(nmod+1) do
  69. tmpRed[i] = 0
  70. tmpGreen[i] = 0
  71. tmpBlue[i] = 0
  72. end
  73. -- don't use fading
  74. return 0
  75. end