GroupState.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <inttypes.h>
  2. #include <Arduino.h>
  3. #include <MiLightButtons.h>
  4. #include <ArduinoJson.h>
  5. #ifndef _GROUP_STATE_H
  6. #define _GROUP_STATE_H
  7. struct GroupId {
  8. uint16_t deviceId;
  9. uint8_t groupId;
  10. MiLightRemoteType deviceType;
  11. GroupId();
  12. GroupId(const GroupId& other);
  13. GroupId(const uint16_t deviceId, const uint8_t groupId, const MiLightRemoteType deviceType);
  14. bool operator==(const GroupId& other);
  15. void operator=(const GroupId& 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. void setState(const MiLightStatus on);
  36. // 7 bits
  37. bool isSetBrightness() const;
  38. uint8_t getBrightness() const;
  39. void setBrightness(uint8_t brightness);
  40. // 8 bits
  41. bool isSetHue() const;
  42. uint8_t getHue() const;
  43. void setHue(uint8_t hue);
  44. // 7 bits
  45. bool isSetSaturation() const;
  46. uint8_t getSaturation() const;
  47. void setSaturation(uint8_t saturation);
  48. // 5 bits
  49. bool isSetMode() const;
  50. uint8_t getMode() const;
  51. void setMode(uint8_t mode);
  52. // 7 bits
  53. bool isSetKelvin() const;
  54. uint8_t getKelvin() const;
  55. void setKelvin(uint8_t kelvin);
  56. // 3 bits
  57. bool isSetBulbMode() const;
  58. BulbMode getBulbMode() const;
  59. void setBulbMode(BulbMode mode);
  60. bool isDirty() const;
  61. bool setDirty();
  62. bool clearDirty();
  63. void patch(const JsonObject& state);
  64. void applyState(JsonObject& state);
  65. void load(Stream& stream);
  66. void dump(Stream& stream) const;
  67. static const GroupState& defaultState(MiLightRemoteType remoteType);
  68. private:
  69. static const size_t DATA_BYTES = 2;
  70. union Data {
  71. uint32_t data[DATA_BYTES];
  72. struct Fields {
  73. uint32_t
  74. _state : 1,
  75. _brightness : 7,
  76. _hue : 8,
  77. _saturation : 7,
  78. _mode : 4,
  79. _bulbMode : 3,
  80. _isSetState : 1,
  81. _isSetHue : 1;
  82. uint32_t
  83. _kelvin : 7,
  84. _isSetBrightness : 1,
  85. _isSetSaturation : 1,
  86. _isSetMode : 1,
  87. _isSetKelvin : 1,
  88. _isSetBulbMode : 1,
  89. _brightnessColor : 7,
  90. _brightnessMode : 7,
  91. _isSetBrightnessColor : 1,
  92. _isSetBrightnessMode : 1,
  93. _dirty : 1,
  94. : 5;
  95. } fields;
  96. };
  97. Data state;
  98. };
  99. struct GroupStateNode {
  100. GroupState state;
  101. GroupId nextNode;
  102. GroupId prevNode;
  103. };
  104. #endif