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.

45 lines
929B

  1. -- Rainbow RGB LED example
  2. -- by Andrew Belt
  3. -- Call process() every 256 audio samples
  4. config.frameDivider = 256
  5. -- From https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB
  6. function hsvToRgb(h, s, v)
  7. h = h * 6
  8. c = v * s
  9. x = c * (1 - math.abs(h % 2 - 1))
  10. if (h < 1) then rgb = {c, x, 0}
  11. elseif (h < 2) then rgb = {x, c, 0}
  12. elseif (h < 3) then rgb = {0, c, x}
  13. elseif (h < 4) then rgb = {0, x, c}
  14. elseif (h < 5) then rgb = {x, 0, c}
  15. else rgb = {c, 0, x}
  16. end
  17. m = v - c
  18. rgb[1] = rgb[1] + m
  19. rgb[2] = rgb[2] + m
  20. rgb[3] = rgb[3] + m
  21. return rgb
  22. end
  23. phase = 0
  24. function process(block)
  25. phase = phase + block.sampleTime * config.frameDivider * 0.5
  26. phase = phase % 1
  27. for i=1,6 do
  28. h = (1 - i / 6 + phase) % 1
  29. rgb = hsvToRgb(h, 1, 1)
  30. for c=1,3 do
  31. block.lights[i][c] = rgb[c]
  32. block.switchLights[i][c] = rgb[c]
  33. end
  34. block.outputs[i][1] = math.sin(2 * math.pi * h) * 5 + 5
  35. end
  36. end
  37. display("Hello, world!")