GroupState.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. // Convenience constructor that patches transient state from a previous GroupState,
  43. // and defaults with JSON state
  44. GroupState(const GroupState* previousState, const JsonObject& jsonState);
  45. void initFields();
  46. bool operator==(const GroupState& other) const;
  47. bool isEqualIgnoreDirty(const GroupState& other) const;
  48. void print(Stream& stream) const;
  49. bool isSetField(GroupStateField field) const;
  50. uint16_t getFieldValue(GroupStateField field) const;
  51. void setFieldValue(GroupStateField field, uint16_t value);
  52. bool clearField(GroupStateField field);
  53. bool isSetScratchField(GroupStateField field) const;
  54. uint16_t getScratchFieldValue(GroupStateField field) const;
  55. void setScratchFieldValue(GroupStateField field, uint16_t value);
  56. // 1 bit
  57. bool isSetState() const;
  58. MiLightStatus getState() const;
  59. bool setState(const MiLightStatus on);
  60. // Return true if status is ON or if the field is unset (i.e., defaults to ON)
  61. bool isOn() const;
  62. // 7 bits
  63. bool isSetBrightness() const;
  64. uint8_t getBrightness() const;
  65. bool setBrightness(uint8_t brightness);
  66. bool clearBrightness();
  67. // 8 bits
  68. bool isSetHue() const;
  69. uint16_t getHue() const;
  70. bool setHue(uint16_t hue);
  71. // 7 bits
  72. bool isSetSaturation() const;
  73. uint8_t getSaturation() const;
  74. bool setSaturation(uint8_t saturation);
  75. // 5 bits
  76. bool isSetMode() const;
  77. bool isSetEffect() const;
  78. uint8_t getMode() const;
  79. bool setMode(uint8_t mode);
  80. // 7 bits
  81. bool isSetKelvin() const;
  82. uint8_t getKelvin() const;
  83. uint16_t getMireds() const;
  84. bool setKelvin(uint8_t kelvin);
  85. bool setMireds(uint16_t mireds);
  86. // 3 bits
  87. bool isSetBulbMode() const;
  88. BulbMode getBulbMode() const;
  89. bool setBulbMode(BulbMode mode);
  90. // 1 bit
  91. bool isSetNightMode() const;
  92. bool isNightMode() const;
  93. bool setNightMode(bool nightMode);
  94. bool isDirty() const;
  95. inline bool setDirty();
  96. bool clearDirty();
  97. bool isMqttDirty() const;
  98. inline bool setMqttDirty();
  99. bool clearMqttDirty();
  100. // Clears all of the fields in THIS GroupState that have different values
  101. // than the provided group state.
  102. bool clearNonMatchingFields(const GroupState& other);
  103. // Patches this state with ONLY the set fields in the other. Returns
  104. // true if there were any changes.
  105. bool patch(const GroupState& other);
  106. // Patches this state with the fields defined in the JSON state. Returns
  107. // true if there were any changes.
  108. bool patch(const JsonObject& state);
  109. // It's a little weird to need to pass in a BulbId here. The purpose is to
  110. // support fields like DEVICE_ID, which aren't otherweise available to the
  111. // state in this class. The alternative is to have every GroupState object
  112. // keep a reference to its BulbId, which feels too heavy-weight.
  113. void applyField(JsonObject& state, const BulbId& bulbId, GroupStateField field) const;
  114. void applyState(JsonObject& state, const BulbId& bulbId, GroupStateField* fields, size_t numFields) const;
  115. // Attempt to keep track of increment commands in such a way that we can
  116. // know what state it's in. When we get an increment command (like "increase
  117. // brightness"):
  118. // 1. If there is no value in the scratch state: assume real state is in
  119. // the furthest value from the direction of the command. For example,
  120. // if we get "increase," assume the value was 0.
  121. // 2. If there is a value in the scratch state, apply the command to it.
  122. // For example, if we get "decrease," subtract 1 from the scratch.
  123. // 3. When scratch reaches a known extreme (either min or max), set the
  124. // persistent field to that value
  125. // 4. If there is already a known value for the state, apply it rather
  126. // than messing with scratch state.
  127. //
  128. // returns true if a (real, not scratch) state change was made
  129. bool applyIncrementCommand(GroupStateField field, IncrementDirection dir);
  130. void load(Stream& stream);
  131. void dump(Stream& stream) const;
  132. void debugState(char const *debugMessage) const;
  133. static const GroupState& defaultState(MiLightRemoteType remoteType);
  134. private:
  135. static const size_t DATA_LONGS = 2;
  136. union StateData {
  137. uint32_t rawData[DATA_LONGS];
  138. struct Fields {
  139. uint32_t
  140. _state : 1,
  141. _brightness : 7,
  142. _hue : 8,
  143. _saturation : 7,
  144. _mode : 4,
  145. _bulbMode : 3,
  146. _isSetState : 1,
  147. _isSetHue : 1;
  148. uint32_t
  149. _kelvin : 7,
  150. _isSetBrightness : 1,
  151. _isSetSaturation : 1,
  152. _isSetMode : 1,
  153. _isSetKelvin : 1,
  154. _isSetBulbMode : 1,
  155. _brightnessColor : 7,
  156. _brightnessMode : 7,
  157. _isSetBrightnessColor : 1,
  158. _isSetBrightnessMode : 1,
  159. _dirty : 1,
  160. _mqttDirty : 1,
  161. _isSetNightMode : 1,
  162. _isNightMode : 1;
  163. } fields;
  164. };
  165. // Transient scratchpad that is never persisted. Used to track and compute state for
  166. // protocols that only have increment commands (like CCT).
  167. union TransientData {
  168. uint16_t rawData;
  169. struct Fields {
  170. uint16_t
  171. _isSetKelvinScratch : 1,
  172. _kelvinScratch : 7,
  173. _isSetBrightnessScratch : 1,
  174. _brightnessScratch : 8;
  175. } fields;
  176. };
  177. StateData state;
  178. TransientData scratchpad;
  179. // State is constructed from individual command packets. A command packet is parsed in
  180. // isolation, and the result is patched onto previous state. There are a few cases where
  181. // it's necessary to know some things from the previous state, so we keep a reference to
  182. // it here.
  183. const GroupState* previousState;
  184. void applyColor(JsonObject& state, uint8_t r, uint8_t g, uint8_t b) const;
  185. void applyColor(JsonObject& state) const;
  186. // Apply OpenHAB-style color, e.g., {"color":"0,0,0"}
  187. void applyOhColor(JsonObject& state) const;
  188. };
  189. extern const BulbId DEFAULT_BULB_ID;
  190. #endif