Преглед изворни кода

fix errors, separate modes for color, white, and scene modes

Chris Mullins пре 8 година
родитељ
комит
6a21e61b7d

+ 83 - 23
lib/MiLightState/GroupState.cpp

@@ -1,9 +1,21 @@
 #include <GroupState.h>
 #include <Units.h>
+#include <MiLightRemoteConfig.h>
+
+const GroupState& GroupState::defaultState(MiLightRemoteType remoteType) {
+  static GroupState instances[MiLightRemoteConfig::NUM_REMOTES];
+  GroupState& state = instances[remoteType];
+
+  switch (remoteType) {
+    case REMOTE_TYPE_RGB:
+      state.setBulbMode(BULB_MODE_COLOR);
+      break;
+    case REMOTE_TYPE_CCT:
+      state.setBulbMode(BULB_MODE_WHITE);
+      break;
+  }
 
-const GroupState& GroupState::defaultState() {
-  static GroupState instance;
-  return instance;
+  return state;
 }
 
 GroupId::GroupId()
@@ -39,34 +51,82 @@ bool GroupId::operator==(const GroupId &other) {
 }
 
 GroupState::GroupState() {
-  _state           = 0;
-  _brightness      = 0;
-  _hue             = 0;
-  _saturation      = 0;
-  _mode            = 0;
-  _bulbMode        = 0;
-  _kelvin          = 0;
-  _isSetState      = 0;
-  _isSetHue        = 0;
-  _isSetBrightness = 0;
-  _isSetSaturation = 0;
-  _isSetMode       = 0;
-  _isSetKelvin     = 0;
-  _isSetBulbMode   = 0;
+  _state                = 0;
+  _brightness           = 0;
+  _brightnessColor      = 0;
+  _brightnessMode       = 0;
+  _hue                  = 0;
+  _saturation           = 0;
+  _mode                 = 0;
+  _bulbMode             = 0;
+  _kelvin               = 0;
+  _isSetState           = 0;
+  _isSetHue             = 0;
+  _isSetBrightness      = 0;
+  _isSetBrightnessColor = 0;
+  _isSetBrightnessMode  = 0;
+  _isSetSaturation      = 0;
+  _isSetMode            = 0;
+  _isSetKelvin          = 0;
+  _isSetBulbMode        = 0;
 }
 
 bool GroupState::isSetState() const { return _isSetState; }
 MiLightStatus GroupState::getState() const { return _state ? ON : OFF; }
-void GroupState::setState(const MiLightStatus& state) {
+void GroupState::setState(const MiLightStatus state) {
   _isSetState = 1;
   _state = state == ON ? 1 : 0;
 }
 
-bool GroupState::isSetBrightness() const { return _isSetBrightness; }
-uint8_t GroupState::getBrightness() const { return _brightness; }
+bool GroupState::isSetBrightness() const {
+  if (! _isSetBulbMode) {
+    return _isSetBrightness;
+  }
+
+  switch (_bulbMode) {
+    case BULB_MODE_WHITE:
+      return _isSetBrightness;
+    case BULB_MODE_COLOR:
+      return _isSetBrightnessColor;
+    case BULB_MODE_SCENE:
+      return _isSetBrightnessMode;
+  }
+
+  return false;
+}
+
+uint8_t GroupState::getBrightness() const {
+  switch (_bulbMode) {
+    case BULB_MODE_WHITE:
+      return _brightness;
+    case BULB_MODE_COLOR:
+      return _brightnessColor;
+    case BULB_MODE_SCENE:
+      return _brightnessMode;
+  }
+
+  return 0;
+}
+
 void GroupState::setBrightness(uint8_t brightness) {
-  _isSetBrightness = 1;
-  _brightness = brightness;
+  uint8_t bulbMode = _bulbMode;
+  if (! _isSetBulbMode) {
+    bulbMode = BULB_MODE_WHITE;
+  }
+
+  switch (bulbMode) {
+    case BULB_MODE_WHITE:
+      _isSetBrightness = 1;
+      _brightness = brightness;
+      break;
+    case BULB_MODE_COLOR:
+      _isSetBrightnessColor = 1;
+      _brightnessColor = brightness;
+      break;
+    case BULB_MODE_SCENE:
+      _isSetBrightnessMode = 1;
+      _brightnessMode = brightness;
+  }
 }
 
 bool GroupState::isSetHue() const { return _isSetHue; }
@@ -109,7 +169,7 @@ void GroupState::patch(const JsonObject& state) {
     setState(state["state"] == "ON" ? ON : OFF);
   }
   if (state.containsKey("brightness")) {
-    setBrightness(Units::rescale(state["brightness"], 100, 255));
+    setBrightness(Units::rescale(state.get<uint8_t>("brightness"), 100, 255));
   }
   if (state.containsKey("hue")) {
     setHue(Units::rescale<uint8_t, uint16_t>(state["hue"], 255, 360));

+ 14 - 10
lib/MiLightState/GroupState.h

@@ -38,7 +38,7 @@ public:
   // 1 bit
   bool isSetState() const;
   MiLightStatus getState() const;
-  void setState(const MiLightStatus& on);
+  void setState(const MiLightStatus on);
 
   // 7 bits
   bool isSetBrightness() const;
@@ -73,7 +73,7 @@ public:
   void patch(const JsonObject& state);
   void applyState(JsonObject& state);
 
-  static const GroupState& defaultState();
+  static const GroupState& defaultState(MiLightRemoteType remoteType);
 
 private:
   uint32_t
@@ -86,14 +86,18 @@ private:
     _isSetState : 1,
     _isSetHue   : 1;
 
-  uint16_t
-    _kelvin          : 7,
-    _isSetBrightness : 1,
-    _isSetSaturation : 1,
-    _isSetMode       : 1,
-    _isSetKelvin     : 1,
-    _isSetBulbMode   : 1,
-                     : 4;
+  uint32_t
+    _kelvin               : 7,
+    _isSetBrightness      : 1,
+    _isSetSaturation      : 1,
+    _isSetMode            : 1,
+    _isSetKelvin          : 1,
+    _isSetBulbMode        : 1,
+    _brightnessColor      : 7,
+    _brightnessMode       : 7,
+    _isSetBrightnessColor : 1,
+    _isSetBrightnessMode  : 1,
+                          : 5;
 };
 
 struct GroupStateNode {

+ 12 - 4
lib/MiLightState/GroupStateCache.cpp

@@ -4,34 +4,42 @@ GroupStateCache::GroupStateCache(const size_t maxSize)
   : maxSize(maxSize)
 { }
 
-const GroupState* GroupStateCache::get(const GroupId& id) {
+GroupState* GroupStateCache::get(const GroupId& id) {
   GroupState* state = getInternal(id);
 
   if (state == NULL) {
-    return &GroupState::defaultState();
+    state = set(id, GroupState::defaultState(id.deviceType));
+    Serial.println(state->getBrightness());
+    return state;
   } else {
     return state;
   }
 }
 
-void GroupStateCache::set(const GroupId& id, const GroupState& state) {
+GroupState* GroupStateCache::set(const GroupId& id, const GroupState& state) {
   GroupCacheNode* pushedNode = NULL;
   if (cache.size() >= maxSize) {
     pushedNode = cache.pop();
   }
 
   GroupState* cachedState = getInternal(id);
+
   if (cachedState == NULL) {
     if (pushedNode == NULL) {
-      cache.unshift(new GroupCacheNode(id, state));
+      GroupCacheNode* newNode = new GroupCacheNode(id, state);
+      cachedState = &newNode->state;
+      cache.unshift(newNode);
     } else {
       pushedNode->id = id;
       pushedNode->state = state;
+      cachedState = &pushedNode->state;
       cache.unshift(pushedNode);
     }
   } else {
     *cachedState = state;
   }
+
+  return cachedState;
 }
 
 GroupState* GroupStateCache::getInternal(const GroupId& id) {

+ 2 - 2
lib/MiLightState/GroupStateCache.h

@@ -17,8 +17,8 @@ class GroupStateCache : public GroupStateStore {
 public:
   GroupStateCache(const size_t maxSize);
 
-  const GroupState* get(const GroupId& id);
-  void set(const GroupId& id, const GroupState& state);
+  GroupState* get(const GroupId& id);
+  GroupState* set(const GroupId& id, const GroupState& state);
 
 private:
   LinkedList<GroupCacheNode*> cache;

+ 2 - 2
lib/MiLightState/GroupStateStore.h

@@ -5,8 +5,8 @@
 
 class GroupStateStore {
 public:
-  virtual const GroupState* get(const GroupId& id) = 0;
-  virtual void set(const GroupId& id, const GroupState& state) = 0;
+  virtual GroupState* get(const GroupId& id) = 0;
+  virtual GroupState* set(const GroupId& id, const GroupState& state) = 0;
 };
 
 #endif

+ 6 - 0
src/main.cpp

@@ -81,6 +81,12 @@ void onPacketSentHandler(uint8_t* packet, const MiLightRemoteConfig& config) {
 
   uint16_t deviceId = result["device_id"];
   uint16_t groupId = result["group_id"];
+  const MiLightRemoteConfig* remoteConfig = MiLightRemoteConfig::fromType(result.get<String>("device_type"));
+
+  GroupId bulbId(deviceId, groupId, remoteConfig->type);
+  GroupState* groupState = stateCache->get(bulbId);
+  groupState->patch(result);
+  groupState->applyState(result);
 
   char output[200];
   result.printTo(output);