GroupState.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. bool isSetField(GroupStateField field) const;
  41. uint16_t getFieldValue(GroupStateField field) const;
  42. void setFieldValue(GroupStateField field, uint16_t value);
  43. bool isSetScratchField(GroupStateField field) const;
  44. uint16_t getScratchFieldValue(GroupStateField field) const;
  45. void setScratchFieldValue(GroupStateField field, uint16_t value);
  46. // 1 bit
  47. bool isSetState() const;
  48. MiLightStatus getState() const;
  49. bool setState(const MiLightStatus on);
  50. // Return true if status is ON or if the field is unset (i.e., defaults to ON)
  51. bool isOn() const;
  52. // 7 bits
  53. bool isSetBrightness() const;
  54. uint8_t getBrightness() const;
  55. bool setBrightness(uint8_t brightness);
  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. bool patch(const JsonObject& state);
  90. // It's a little weird to need to pass in a BulbId here. The purpose is to
  91. // support fields like DEVICE_ID, which aren't otherweise available to the
  92. // state in this class. The alternative is to have every GroupState object
  93. // keep a reference to its BulbId, which feels too heavy-weight.
  94. void applyField(JsonObject& state, const BulbId& bulbId, GroupStateField field);
  95. void applyState(JsonObject& state, const BulbId& bulbId, GroupStateField* fields, size_t numFields);
  96. // Attempt to keep track of increment commands in such a way that we can
  97. // know what state it's in. When we get an increment command (like "increase
  98. // brightness"):
  99. // 1. If there is no value in the scratch state: assume real state is in
  100. // the furthest value from the direction of the command. For example,
  101. // if we get "increase," assume the value was 0.
  102. // 2. If there is a value in the scratch state, apply the command to it.
  103. // For example, if we get "decrease," subtract 1 from the scratch.
  104. // 3. When scratch reaches a known extreme (either min or max), set the
  105. // persistent field to that value
  106. // 4. If there is already a known value for the state, apply it rather
  107. // than messing with scratch state.
  108. //
  109. // returns true if a (real, not scratch) state change was made
  110. bool applyIncrementCommand(GroupStateField field, IncrementDirection dir);
  111. void load(Stream& stream);
  112. void dump(Stream& stream) const;
  113. void debugState(char const *debugMessage);
  114. static const GroupState& defaultState(MiLightRemoteType remoteType);
  115. private:
  116. static const size_t DATA_LONGS = 3;
  117. union StateData {
  118. uint32_t rawData[DATA_LONGS];
  119. struct Fields {
  120. uint32_t
  121. _state : 1,
  122. _brightness : 7,
  123. _hue : 8,
  124. _saturation : 7,
  125. _mode : 4,
  126. _bulbMode : 3,
  127. _isSetState : 1,
  128. _isSetHue : 1;
  129. uint32_t
  130. _kelvin : 7,
  131. _isSetBrightness : 1,
  132. _isSetSaturation : 1,
  133. _isSetMode : 1,
  134. _isSetKelvin : 1,
  135. _isSetBulbMode : 1,
  136. _brightnessColor : 7,
  137. _brightnessMode : 7,
  138. _isSetBrightnessColor : 1,
  139. _isSetBrightnessMode : 1,
  140. _dirty : 1,
  141. _mqttDirty : 1,
  142. _isSetNightMode : 1,
  143. _isNightMode : 1;
  144. } fields;
  145. };
  146. // Transient scratchpad that is never persisted. Used to track and compute state for
  147. // protocols that only have increment commands (like CCT).
  148. union TransientData {
  149. uint16_t rawData;
  150. struct Fields {
  151. uint16_t
  152. _isSetKelvinScratch : 1,
  153. _kelvinScratch : 7,
  154. _isSetBrightnessScratch : 1,
  155. _brightnessScratch : 8;
  156. } fields;
  157. };
  158. StateData state;
  159. TransientData scratchpad;
  160. void applyColor(JsonObject& state, uint8_t r, uint8_t g, uint8_t b);
  161. void applyColor(JsonObject& state);
  162. };
  163. extern const BulbId DEFAULT_BULB_ID;
  164. #endif