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.

pulsecircle.lua 2.0KB

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