GroupState.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. uint16_t getHue() const;
  43. void setHue(uint16_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. uint16_t getMireds() const;
  56. void setKelvin(uint8_t kelvin);
  57. void setMireds(uint16_t mireds);
  58. // 3 bits
  59. bool isSetBulbMode() const;
  60. BulbMode getBulbMode() const;
  61. void setBulbMode(BulbMode mode);
  62. bool isDirty() const;
  63. bool setDirty();
  64. bool clearDirty();
  65. void patch(const JsonObject& state);
  66. void applyState(JsonObject& state);
  67. void load(Stream& stream);
  68. void dump(Stream& stream) const;
  69. static const GroupState& defaultState(MiLightRemoteType remoteType);
  70. private:
  71. static const size_t DATA_BYTES = 2;
  72. union Data {
  73. uint32_t data[DATA_BYTES];
  74. struct Fields {
  75. uint32_t
  76. _state : 1,
  77. _brightness : 7,
  78. _hue : 8,
  79. _saturation : 7,
  80. _mode : 4,
  81. _bulbMode : 3,
  82. _isSetState : 1,
  83. _isSetHue : 1;
  84. uint32_t
  85. _kelvin : 7,
  86. _isSetBrightness : 1,
  87. _isSetSaturation : 1,
  88. _isSetMode : 1,
  89. _isSetKelvin : 1,
  90. _isSetBulbMode : 1,
  91. _brightnessColor : 7,
  92. _brightnessMode : 7,
  93. _isSetBrightnessColor : 1,
  94. _isSetBrightnessMode : 1,
  95. _dirty : 1,
  96. : 5;
  97. } fields;
  98. };
  99. Data state;
  100. };
  101. struct GroupStateNode {
  102. GroupState state;
  103. GroupId nextNode;
  104. GroupId prevNode;
  105. };
  106. #endif