Fork of the espurna firmware for `mhsw` switches
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.

92 lines
2.7 KiB

  1. #if GARLAND_SUPPORT
  2. #include <vector>
  3. #include "../anim.h"
  4. #include "../color.h"
  5. #include "../palette.h"
  6. //------------------------------------------------------------------------------
  7. class AnimSalut : public Anim {
  8. public:
  9. AnimSalut() : Anim("Salut") {
  10. }
  11. void SetupImpl() override {
  12. shots.clear();
  13. // There can be more then one shot at the moment
  14. // but looks like one is enough
  15. // for (int i = 0; i < 3; ++i)
  16. shots.emplace_back(palette, numLeds);
  17. }
  18. void Run() override {
  19. for (int i = 0; i < numLeds; ++i) leds[i] = 0;
  20. for (auto& c : shots) {
  21. if (!c.Run(leds)) {
  22. Shot new_shot(palette, numLeds);
  23. std::swap(c, new_shot);
  24. }
  25. }
  26. }
  27. private:
  28. struct Shot {
  29. private:
  30. struct Spark {
  31. bool done = false;
  32. float speed = ((float)secureRandom(1, 25)) / 10;
  33. float speed_dec = ((float)secureRandom(1, 3)) / 10;
  34. float pos;
  35. int dir;
  36. Color color;
  37. uint16_t numLeds;
  38. Spark(int pos, Palette* pal, uint16_t numLeds) : pos(pos), dir(secureRandom(10) > 5 ? -1 : 1), color(pal->getRndInterpColor()), numLeds(numLeds) {}
  39. void Run(Color* leds) {
  40. if (pos >= 0 && pos < numLeds) {
  41. leds[(int)pos] = color;
  42. if (speed > 0) {
  43. pos += speed * dir;
  44. speed -= speed_dec;
  45. } else {
  46. color.fade(5);
  47. if (color.empty()) {
  48. if (secureRandom(10) > 8)
  49. leds[(int)pos] = 0xFFFFFF;
  50. done = true;
  51. }
  52. }
  53. } else {
  54. done = true;
  55. }
  56. }
  57. };
  58. public:
  59. int spark_num = secureRandom(30, 40);
  60. int center;
  61. std::vector<Spark> sparks;
  62. Shot(Palette* pal, uint16_t numLeds) : center(secureRandom(15, numLeds - 15)) {
  63. // DEBUG_MSG_P(PSTR("[GARLAND] Shot created center = %d spark_num = %d\n"), center, spark_num);
  64. sparks.reserve(spark_num);
  65. for (int i = 0; i < spark_num; ++i) {
  66. sparks.emplace_back(center, pal, numLeds);
  67. }
  68. }
  69. bool Run(Color* leds) {
  70. bool done = true;
  71. for (auto& s : sparks) {
  72. if (!s.done) {
  73. done = false;
  74. s.Run(leds);
  75. }
  76. }
  77. return !done;
  78. }
  79. };
  80. std::vector<Shot> shots;
  81. };
  82. #endif // GARLAND_SUPPORT