GroupState.h 6.7 KB

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