Transition.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <BulbId.h>
  2. #include <ArduinoJson.h>
  3. #include <GroupStateField.h>
  4. #include <stdint.h>
  5. #include <stddef.h>
  6. #include <functional>
  7. #include <memory>
  8. #pragma once
  9. class Transition {
  10. public:
  11. using TransitionFn = std::function<void(const BulbId& bulbId, GroupStateField field, uint16_t value)>;
  12. // transition commands are in seconds, convert to ms.
  13. static const uint16_t DURATION_UNIT_MULTIPLIER = 1000;
  14. class Builder {
  15. public:
  16. Builder(size_t id, const BulbId& bulbId, TransitionFn callback, size_t maxSteps);
  17. Builder& setDuration(float duration);
  18. Builder& setPeriod(size_t period);
  19. Builder& setNumPeriods(size_t numPeriods);
  20. /**
  21. * Users are typically defining transitions using:
  22. * 1. The desired end state (and implicitly the start state, assumed to be current)
  23. * 2. The duraiton
  24. * The user only cares about the period to the degree that it affects the smoothness of
  25. * the transition.
  26. *
  27. * For example, if the user wants to throttle brightness from 0 -> 100 over 5min, the
  28. * default period is going to be way too short to enable that. So we need to force the
  29. * period to be longer to fit the duration.
  30. */
  31. Builder& setDurationAwarePeriod(size_t desiredPeriod, size_t duration, size_t maxSteps);
  32. void setDurationRaw(size_t duration);
  33. bool isSetDuration() const;
  34. bool isSetPeriod() const;
  35. bool isSetNumPeriods() const;
  36. size_t getOrComputePeriod() const;
  37. size_t getOrComputeDuration() const;
  38. size_t getOrComputeNumPeriods() const;
  39. size_t getDuration() const;
  40. size_t getPeriod() const;
  41. size_t getNumPeriods() const;
  42. size_t getMaxSteps() const;
  43. std::shared_ptr<Transition> build();
  44. const size_t id;
  45. const BulbId& bulbId;
  46. const TransitionFn callback;
  47. private:
  48. size_t duration;
  49. size_t period;
  50. size_t numPeriods;
  51. size_t maxSteps;
  52. virtual std::shared_ptr<Transition> _build() const = 0;
  53. size_t numSetParams() const;
  54. };
  55. // Default time to wait between steps. Do this rather than having a fixed step size because it's
  56. // more capable of adapting to different situations.
  57. static const size_t DEFAULT_PERIOD = 225;
  58. static const size_t DEFAULT_NUM_PERIODS = 20;
  59. static const size_t DEFAULT_DURATION = DEFAULT_PERIOD*DEFAULT_NUM_PERIODS;
  60. // If period goes lower than this, throttle other parameters up to adjust.
  61. static const size_t MIN_PERIOD = 150;
  62. const size_t id;
  63. const BulbId bulbId;
  64. const TransitionFn callback;
  65. Transition(
  66. size_t id,
  67. const BulbId& bulbId,
  68. size_t period,
  69. TransitionFn callback
  70. );
  71. void tick();
  72. virtual bool isFinished() = 0;
  73. void serialize(JsonObject& doc);
  74. virtual void step() = 0;
  75. virtual void childSerialize(JsonObject& doc) = 0;
  76. static size_t calculatePeriod(int16_t distance, size_t stepSize, size_t duration);
  77. protected:
  78. const size_t period;
  79. unsigned long lastSent;
  80. static void stepValue(int16_t& current, int16_t end, int16_t stepSize);
  81. };