소스 검색

Handle night mode state better.

Christopher Mullins 8 년 전
부모
커밋
093285214f
4개의 변경된 파일58개의 추가작업 그리고 6개의 파일을 삭제
  1. 3 1
      lib/MiLight/FUT089PacketFormatter.cpp
  2. 3 1
      lib/MiLight/RgbCctPacketFormatter.cpp
  3. 44 3
      lib/MiLightState/GroupState.cpp
  4. 8 1
      lib/MiLightState/GroupState.h

+ 3 - 1
lib/MiLight/FUT089PacketFormatter.cpp

@@ -60,7 +60,9 @@ BulbId FUT089PacketFormatter::parsePacket(const uint8_t *packet, JsonObject& res
   uint8_t arg = packetCopy[V2_ARGUMENT_INDEX];
 
   if (command == FUT089_ON) {
-    if (arg == FUT089_MODE_SPEED_DOWN) {
+    if ((packetCopy[V2_COMMAND_INDEX] & 0x80) == 0x80) {
+      result["command"] = "night_mode";
+    } else if (arg == FUT089_MODE_SPEED_DOWN) {
       result["command"] = "mode_speed_down";
     } else if (arg == FUT089_MODE_SPEED_UP) {
       result["command"] = "mode_speed_up";

+ 3 - 1
lib/MiLight/RgbCctPacketFormatter.cpp

@@ -78,7 +78,9 @@ BulbId RgbCctPacketFormatter::parsePacket(const uint8_t *packet, JsonObject& res
   uint8_t arg = packetCopy[V2_ARGUMENT_INDEX];
 
   if (command == RGB_CCT_ON) {
-    if (arg == RGB_CCT_MODE_SPEED_DOWN) {
+    if ((packetCopy[V2_COMMAND_INDEX] & 0x80) == 0x80) {
+      result["command"] = "night_mode";
+    } else if (arg == RGB_CCT_MODE_SPEED_DOWN) {
       result["command"] = "mode_speed_down";
     } else if (arg == RGB_CCT_MODE_SPEED_UP) {
       result["command"] = "mode_speed_up";

+ 44 - 3
lib/MiLightState/GroupState.cpp

@@ -74,6 +74,8 @@ GroupState::GroupState() {
   state.fields._isSetBulbMode        = 0;
   state.fields._dirty                = 1;
   state.fields._mqttDirty            = 0;
+  state.fields._isSetNightMode       = 0;
+  state.fields._isNightMode          = 0;
 }
 
 bool GroupState::isSetField(GroupStateField field) const {
@@ -245,15 +247,46 @@ bool GroupState::setMireds(uint16_t mireds) {
 }
 
 bool GroupState::isSetBulbMode() const { return state.fields._isSetBulbMode; }
-BulbMode GroupState::getBulbMode() const { return static_cast<BulbMode>(state.fields._bulbMode); }
+BulbMode GroupState::getBulbMode() const {
+  BulbMode mode;
+
+  // Night mode is a transient state.  When power is toggled, the bulb returns
+  // to the state it was last in.  To handle this case, night mode state is
+  // stored separately.
+  if (isSetNightMode() && isNightMode()) {
+    return BULB_MODE_NIGHT;
+  } else {
+    return static_cast<BulbMode>(state.fields._bulbMode);
+  }
+}
 bool GroupState::setBulbMode(BulbMode bulbMode) {
   if (isSetBulbMode() && getBulbMode() == bulbMode) {
     return false;
   }
 
   setDirty();
-  state.fields._isSetBulbMode = 1;
-  state.fields._bulbMode = bulbMode;
+
+  // As mentioned in isSetBulbMode, NIGHT_MODE is stored separately.
+  if (bulbMode == BULB_MODE_NIGHT) {
+    setNightMode(true);
+  } else {
+    state.fields._isSetBulbMode = 1;
+    state.fields._bulbMode = bulbMode;
+  }
+
+  return true;
+}
+
+bool GroupState::isSetNightMode() const { return state.fields._isSetNightMode; }
+bool GroupState::isNightMode() const { return state.fields._isNightMode; }
+bool GroupState::setNightMode(bool nightMode) {
+  if (isSetNightMode() && isNightMode() == nightMode) {
+    return false;
+  }
+
+  setDirty();
+  state.fields._isSetNightMode = 1;
+  state.fields._isNightMode = nightMode;
 
   return true;
 }
@@ -305,11 +338,19 @@ bool GroupState::patch(const JsonObject& state) {
     changes |= setMireds(state["color_temp"]);
     changes |= setBulbMode(BULB_MODE_WHITE);
   }
+
+  // Any changes other than setting mode to night should take device out of
+  // night mode.
+  if (changes && getBulbMode() == BULB_MODE_NIGHT) {
+    setNightMode(false);
+  }
+
   if (state.containsKey("command")) {
     const String& command = state["command"];
 
     if (command == "white_mode") {
       changes |= setBulbMode(BULB_MODE_WHITE);
+      setNightMode(false);
     } else if (command == "night_mode") {
       changes |= setBulbMode(BULB_MODE_NIGHT);
     }

+ 8 - 1
lib/MiLightState/GroupState.h

@@ -77,6 +77,11 @@ public:
   BulbMode getBulbMode() const;
   bool setBulbMode(BulbMode mode);
 
+  // 1 bit
+  bool isSetNightMode() const;
+  bool isNightMode() const;
+  bool setNightMode(bool nightMode);
+
   bool isDirty() const;
   inline bool setDirty();
   bool clearDirty();
@@ -121,7 +126,9 @@ private:
         _isSetBrightnessMode  : 1,
         _dirty                : 1,
         _mqttDirty            : 1,
-                              : 4;
+        _isSetNightMode       : 1,
+        _isNightMode          : 1,
+                              : 2;
     } fields;
   };