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
926B

  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 *= 6
  8. let c = v * s
  9. let x = c * (1 - Math.abs(h % 2 - 1))
  10. let rgb;
  11. if (h < 1) rgb = [c, x, 0]
  12. else if (h < 2) rgb = [x, c, 0]
  13. else if (h < 3) rgb = [0, c, x]
  14. else if (h < 4) rgb = [0, x, c]
  15. else if (h < 5) rgb = [x, 0, c]
  16. else rgb = [c, 0, x]
  17. let m = v - c
  18. rgb[0] += m
  19. rgb[1] += m
  20. rgb[2] += m
  21. return rgb
  22. }
  23. let phase = 0
  24. function process(block) {
  25. phase += block.sampleTime * config.frameDivider * 0.5
  26. phase %= 1
  27. for (let i = 0; i < 6; i++) {
  28. let h = (1 - i / 6 + phase) % 1
  29. let rgb = hsvToRgb(h, 1, 1)
  30. for (let c = 0; c < 3; c++) {
  31. block.lights[i][c] = rgb[c]
  32. block.switchLights[i][c] = rgb[c]
  33. }
  34. block.outputs[i][0] = Math.sin(2 * Math.PI * h) * 5 + 5
  35. }
  36. }
  37. display("Hello, world!")