GroupState.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include <stddef.h>
  2. #include <inttypes.h>
  3. #include <MiLightConstants.h>
  4. #include <MiLightRadioConfig.h>
  5. #include <GroupStateField.h>
  6. #include <ArduinoJson.h>
  7. #ifndef _GROUP_STATE_H
  8. #define _GROUP_STATE_H
  9. // enable to add debugging on state
  10. // #define DEBUG_STATE
  11. struct BulbId {
  12. uint16_t deviceId;
  13. uint8_t groupId;
  14. MiLightRemoteType deviceType;
  15. BulbId();
  16. BulbId(const BulbId& other);
  17. BulbId(const uint16_t deviceId, const uint8_t groupId, const MiLightRemoteType deviceType);
  18. bool operator==(const BulbId& other);
  19. void operator=(const BulbId& other);
  20. };
  21. enum BulbMode {
  22. BULB_MODE_WHITE,
  23. BULB_MODE_COLOR,
  24. BULB_MODE_SCENE,
  25. BULB_MODE_NIGHT
  26. };
  27. enum class IncrementDirection : unsigned {
  28. INCREASE = 1,
  29. DECREASE = -1U
  30. };
  31. static const char* BULB_MODE_NAMES[] = {
  32. "white",
  33. "color",
  34. "scene",
  35. "night"
  36. };
  37. class GroupState {
  38. public:
  39. GroupState();
  40. GroupState(const GroupState& other);
  41. GroupState& operator=(const GroupState& other);
  42. bool operator==(const GroupState& other) const;
  43. bool isEqualIgnoreDirty(const GroupState& other) const;
  44. void print(Stream& stream) const;
  45. bool isSetField(GroupStateField field) const;
  46. uint16_t getFieldValue(GroupStateField field) const;
  47. void setFieldValue(GroupStateField field, uint16_t value);
  48. bool isSetScratchField(GroupStateField field) const;
  49. uint16_t getScratchFieldValue(GroupStateField field) const;
  50. void setScratchFieldValue(GroupStateField field, uint16_t value);
  51. // 1 bit
  52. bool isSetState() const;
  53. MiLightStatus getState() const;
  54. bool setState(const MiLightStatus on);
  55. // Return true if status is ON or if the field is unset (i.e., defaults to ON)
  56. bool isOn() const;
  57. // 7 bits
  58. bool isSetBrightness() const;
  59. uint8_t getBrightness() const;
  60. bool setBrightness(uint8_t brightness);
  61. // 8 bits
  62. bool isSetHue() const;
  63. uint16_t getHue() const;
  64. bool setHue(uint16_t hue);
  65. // 7 bits
  66. bool isSetSaturation() const;
  67. uint8_t getSaturation() const;
  68. bool setSaturation(uint8_t saturation);
  69. // 5 bits
  70. bool isSetMode() const;
  71. bool isSetEffect() const;
  72. uint8_t getMode() const;
  73. bool setMode(uint8_t mode);
  74. // 7 bits
  75. bool isSetKelvin() const;
  76. uint8_t getKelvin() const;
  77. uint16_t getMireds() const;
  78. bool setKelvin(uint8_t kelvin);
  79. bool setMireds(uint16_t mireds);
  80. // 3 bits
  81. bool isSetBulbMode() const;
  82. BulbMode getBulbMode() const;
  83. bool setBulbMode(BulbMode mode);
  84. // 1 bit
  85. bool isSetNightMode() const;
  86. bool isNightMode() const;
  87. bool setNightMode(bool nightMode);
  88. bool isDirty() const;
  89. inline bool setDirty();
  90. bool clearDirty();
  91. bool isMqttDirty() const;
  92. inline bool setMqttDirty();
  93. bool clearMqttDirty();
  94. // Patches this state with ONLY the set fields in the other. Returns
  95. // true if there were any changes.
  96. bool patch(const GroupState& other);
  97. // Patches this state with the fields defined in the JSON state. Returns
  98. // true if there were any changes.
  99. bool patch(const JsonObject& state);
  100. // It's a little weird to need to pass in a BulbId here. The purpose is to
  101. // support fields like DEVICE_ID, which aren't otherweise available to the
  102. // state in this class. The alternative is to have every GroupState object
  103. // keep a reference to its BulbId, which feels too heavy-weight.
  104. void applyField(JsonObject& state, const BulbId& bulbId, GroupStateField field);
  105. void applyState(JsonObject& state, const BulbId& bulbId, GroupStateField* fields, size_t numFields);
  106. // Attempt to keep track of increment commands in such a way that we can
  107. // know what state it's in. When we get an increment command (like "increase
  108. // brightness"):
  109. // 1. If there is no value in the scratch state: assume real state is in
  110. // the furthest value from the direction of the command. For example,
  111. // if we get "increase," assume the value was 0.
  112. // 2. If there is a value in the scratch state, apply the command to it.
  113. // For example, if we get "decrease," subtract 1 from the scratch.
  114. // 3. When scratch reaches a known extreme (either min or max), set the
  115. // persistent field to that value
  116. // 4. If there is already a known value for the state, apply it rather
  117. // than messing with scratch state.
  118. //
  119. // returns true if a (real, not scratch) state change was made
  120. bool applyIncrementCommand(GroupStateField field, IncrementDirection dir);
  121. void load(Stream& stream);
  122. void dump(Stream& stream) const;
  123. void debugState(char const *debugMessage);
  124. static const GroupState& defaultState(MiLightRemoteType remoteType);
  125. private:
  126. static const size_t DATA_LONGS = 2;
  127. union StateData {
  128. uint32_t rawData[DATA_LONGS];
  129. struct Fields {
  130. uint32_t
  131. _state : 1,
  132. _brightness : 7,
  133. _hue : 8,
  134. _saturation : 7,
  135. _mode : 4,
  136. _bulbMode : 3,
  137. _isSetState : 1,
  138. _isSetHue : 1;
  139. uint32_t
  140. _kelvin : 7,
  141. _isSetBrightness : 1,
  142. _isSetSaturation : 1,
  143. _isSetMode : 1,
  144. _isSetKelvin : 1,
  145. _isSetBulbMode : 1,
  146. _brightnessColor : 7,
  147. _brightnessMode : 7,
  148. _isSetBrightnessColor : 1,
  149. _isSetBrightnessMode : 1,
  150. _dirty : 1,
  151. _mqttDirty : 1,
  152. _isSetNightMode : 1,
  153. _isNightMode : 1;
  154. } fields;
  155. };
  156. // Transient scratchpad that is never persisted. Used to track and compute state for
  157. // protocols that only have increment commands (like CCT).
  158. union TransientData {
  159. uint16_t rawData;
  160. struct Fields {
  161. uint16_t
  162. _isSetKelvinScratch : 1,
  163. _kelvinScratch : 7,
  164. _isSetBrightnessScratch : 1,
  165. _brightnessScratch : 8;
  166. } fields;
  167. };
  168. StateData state;
  169. TransientData scratchpad;
  170. void applyColor(JsonObject& state, uint8_t r, uint8_t g, uint8_t b);
  171. void applyColor(JsonObject& state);
  172. };
  173. extern const BulbId DEFAULT_BULB_ID;
  174. #endif