GroupState.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include <stddef.h>
  2. #include <inttypes.h>
  3. #include <MiLightConstants.h>
  4. #include <ArduinoJson.h>
  5. #ifndef _GROUP_STATE_H
  6. #define _GROUP_STATE_H
  7. struct BulbId {
  8. uint16_t deviceId;
  9. uint8_t groupId;
  10. MiLightRemoteType deviceType;
  11. BulbId();
  12. BulbId(const BulbId& other);
  13. BulbId(const uint16_t deviceId, const uint8_t groupId, const MiLightRemoteType deviceType);
  14. bool operator==(const BulbId& other);
  15. void operator=(const BulbId& other);
  16. };
  17. enum BulbMode {
  18. BULB_MODE_WHITE,
  19. BULB_MODE_COLOR,
  20. BULB_MODE_SCENE,
  21. BULB_MODE_NIGHT
  22. };
  23. static const char* BULB_MODE_NAMES[] = {
  24. "white",
  25. "color",
  26. "scene",
  27. "night"
  28. };
  29. class GroupState {
  30. public:
  31. GroupState();
  32. // 1 bit
  33. bool isSetState() const;
  34. MiLightStatus getState() const;
  35. bool setState(const MiLightStatus on);
  36. // 7 bits
  37. bool isSetBrightness() const;
  38. uint8_t getBrightness() const;
  39. bool setBrightness(uint8_t brightness);
  40. // 8 bits
  41. bool isSetHue() const;
  42. uint16_t getHue() const;
  43. bool setHue(uint16_t hue);
  44. // 7 bits
  45. bool isSetSaturation() const;
  46. uint8_t getSaturation() const;
  47. bool setSaturation(uint8_t saturation);
  48. // 5 bits
  49. bool isSetMode() const;
  50. uint8_t getMode() const;
  51. bool setMode(uint8_t mode);
  52. // 7 bits
  53. bool isSetKelvin() const;
  54. uint8_t getKelvin() const;
  55. uint16_t getMireds() const;
  56. bool setKelvin(uint8_t kelvin);
  57. bool setMireds(uint16_t mireds);
  58. // 3 bits
  59. bool isSetBulbMode() const;
  60. BulbMode getBulbMode() const;
  61. bool setBulbMode(BulbMode mode);
  62. bool isDirty() const;
  63. inline bool setDirty();
  64. bool clearDirty();
  65. bool isMqttDirty() const;
  66. inline bool setMqttDirty();
  67. bool clearMqttDirty();
  68. bool patch(const JsonObject& state);
  69. void applyState(JsonObject& state);
  70. void load(Stream& stream);
  71. void dump(Stream& stream) const;
  72. static const GroupState& defaultState(MiLightRemoteType remoteType);
  73. private:
  74. static const size_t DATA_BYTES = 2;
  75. union Data {
  76. uint32_t data[DATA_BYTES];
  77. struct Fields {
  78. uint32_t
  79. _state : 1,
  80. _brightness : 7,
  81. _hue : 8,
  82. _saturation : 7,
  83. _mode : 4,
  84. _bulbMode : 3,
  85. _isSetState : 1,
  86. _isSetHue : 1;
  87. uint32_t
  88. _kelvin : 7,
  89. _isSetBrightness : 1,
  90. _isSetSaturation : 1,
  91. _isSetMode : 1,
  92. _isSetKelvin : 1,
  93. _isSetBulbMode : 1,
  94. _brightnessColor : 7,
  95. _brightnessMode : 7,
  96. _isSetBrightnessColor : 1,
  97. _isSetBrightnessMode : 1,
  98. _dirty : 1,
  99. _mqttDirty : 1,
  100. : 4;
  101. } fields;
  102. };
  103. Data state;
  104. };
  105. extern const BulbId DEFAULT_BULB_ID;
  106. #endif