LightCluster.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // Created by emilr on 2022-04-13.
  3. //
  4. #include "LightCluster.h"
  5. #include "main.h"
  6. int animationI;
  7. int maxAnimationI;
  8. int delayTimeMS;
  9. int animationSetting1;
  10. int animationSetting2;
  11. int numLeds;
  12. extern void rainbow(light lights[]);
  13. extern void setupRainbow();
  14. void (*animationTable[])(light lights[]) = {rainbow};
  15. void (*setup_table[])() = {setupRainbow};
  16. void LightCluster::runAnimation() {
  17. if (shouldRun()) {
  18. //Serial.println("Animation begin");
  19. animationTable[animationNumber](lights);
  20. lastRun = millis();
  21. for (int i = 0; i < numLights; i++) {
  22. // Serial.print("lights mapped = ");
  23. // Serial.println(lights[i].mapped);
  24. // Serial.print("color = ");
  25. // Serial.println(lights[i].color, HEX);
  26. leds.setPixelColor(lights[i].mapped, lights[i].color);
  27. }
  28. //Serial.println("Animation has run");
  29. //return true;
  30. }
  31. //return false;
  32. }
  33. void LightCluster::runSetup() {
  34. setup_table[animationNumber]();
  35. }
  36. void LightCluster::changeAnimation(int newAnimationNumber) {
  37. animationNumber = newAnimationNumber;
  38. runSetup();
  39. runAnimation();
  40. }
  41. LightCluster::LightCluster(light *incomingLights, int size, int animation) {
  42. // for (int i = 0; i < sizeof(lights); i++) {
  43. // light data;
  44. // data.mapped = lights[i];
  45. // this->lights[i] = data;
  46. // }
  47. animationNumber = animation;
  48. numLights = size;
  49. numLeds = size;
  50. lights = incomingLights;
  51. runSetup();
  52. runAnimation();
  53. }
  54. bool LightCluster::shouldRun() {
  55. //Serial.println("ShouldRun started");
  56. if (millis() - lastRun >= delayTimeMS) {
  57. //Serial.println("ShouldRun true");
  58. return true;
  59. }
  60. //Serial.println("ShouldRun false");
  61. return false;
  62. }
  63. void rainbow(light lights[]) {
  64. for (int light = 0; light < numLeds; light++) {
  65. lights[light].color = leds.ColorHSV(((light * animationSetting1 + animationI*65536/maxAnimationI) % 65536), 255, 255);
  66. // Serial.print("Set ");
  67. // Serial.print(light);
  68. // Serial.print(" to ");
  69. // Serial.println(leds.ColorHSV(((light * animationSetting1 + animationI) * 360), 255, 255));
  70. // Serial.println(light * animationSetting1);
  71. // Serial.println((light * animationSetting1 + animationI) * 360);
  72. }
  73. //Serial.print("AnimationI = ");
  74. //Serial.println(animationI);
  75. animationI += 1;
  76. animationI %= maxAnimationI; //Don't go all way to 255 because 0*65536/256 = 0 and 256*65536/256 = 65536. It will result in the same color for 2 cycles.
  77. }
  78. void setupRainbow() {
  79. animationI = 0;
  80. delayTimeMS = 100;
  81. maxAnimationI = 256;
  82. animationSetting1 = 65536/(numLeds+1);
  83. }