GroupState.h 6.9 KB

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