Przeglądaj źródła

use extern constant

Chris Mullins 8 lat temu
rodzic
commit
b2c603f0b5

+ 8 - 4
lib/MiLight/CctPacketFormatter.cpp

@@ -146,12 +146,14 @@ MiLightStatus CctPacketFormatter::cctCommandToStatus(uint8_t command) {
   }
 }
 
-void CctPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore) {
+GroupId CctPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore) {
   uint8_t command = packet[CCT_COMMAND_INDEX] & 0x7F;
 
-  result["device_id"] = (packet[1] << 8) | packet[2];
-  result["device_type"] = "cct";
-  result["group_id"] = packet[3];
+  GroupId groupId(
+    (packet[1] << 8) | packet[2],
+    packet[3],
+    REMOTE_TYPE_CCT
+  );
 
   uint8_t onOffGroupId = cctCommandIdToGroup(command);
   if (onOffGroupId < 255) {
@@ -167,6 +169,8 @@ void CctPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result,
   } else {
     result["button_id"] = command;
   }
+
+  return groupId;
 }
 
 void CctPacketFormatter::format(uint8_t const* packet, char* buffer) {

+ 1 - 1
lib/MiLight/CctPacketFormatter.h

@@ -43,7 +43,7 @@ public:
 
   virtual void format(uint8_t const* packet, char* buffer);
   virtual void initializePacket(uint8_t* packet);
-  virtual void parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore);
+  virtual GroupId parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore);
 
   static uint8_t getCctStatusButton(uint8_t groupId, MiLightStatus status);
   static uint8_t cctCommandIdToGroup(uint8_t command);

+ 11 - 11
lib/MiLight/FUT089PacketFormatter.cpp

@@ -45,17 +45,16 @@ void FUT089PacketFormatter::enableNightMode() {
   command(FUT089_ON | 0x80, arg);
 }
 
-void FUT089PacketFormatter::parsePacket(const uint8_t *packet, JsonObject& result, GroupStateStore* stateStore) {
+GroupId FUT089PacketFormatter::parsePacket(const uint8_t *packet, JsonObject& result, GroupStateStore* stateStore) {
   uint8_t packetCopy[V2_PACKET_LEN];
   memcpy(packetCopy, packet, V2_PACKET_LEN);
   V2RFEncoding::decodeV2Packet(packetCopy);
 
-  const uint16_t deviceId = (packetCopy[2] << 8) | packetCopy[3];
-  const uint8_t groupId = packetCopy[7];
-
-  result["device_id"] = deviceId;
-  result["group_id"] = groupId;
-  result["device_type"] = "fut089";
+  GroupId groupId(
+    (packetCopy[2] << 8) | packetCopy[3],
+    packetCopy[7],
+    REMOTE_TYPE_FUT089
+  );
 
   uint8_t command = (packetCopy[V2_COMMAND_INDEX] & 0x7F);
   uint8_t arg = packetCopy[V2_ARGUMENT_INDEX];
@@ -69,10 +68,10 @@ void FUT089PacketFormatter::parsePacket(const uint8_t *packet, JsonObject& resul
       result["command"] = "white_mode";
     } else if (arg <= 8) { // Group is not reliably encoded in group byte. Extract from arg byte
       result["state"] = "ON";
-      result["group_id"] = arg;
+      groupId.groupId = arg;
     } else if (arg >= 9 && arg <= 17) {
       result["state"] = "OFF";
-      result["group_id"] = arg-9;
+      groupId.groupId = arg-9;
     }
   } else if (command == FUT089_COLOR) {
     uint8_t rescaledColor = (arg - FUT089_COLOR_OFFSET) % 0x100;
@@ -84,8 +83,7 @@ void FUT089PacketFormatter::parsePacket(const uint8_t *packet, JsonObject& resul
   // saturation == kelvin. arg ranges are the same, so can't distinguish
   // without using state
   } else if (command == FUT089_SATURATION) {
-    GroupId group(deviceId, groupId, REMOTE_TYPE_FUT089);
-    GroupState& state = stateStore->get(group);
+    GroupState& state = stateStore->get(groupId);
 
     if (state.getBulbMode() == BULB_MODE_COLOR) {
       result["saturation"] = 100 - constrain(arg, 0, 100);
@@ -98,4 +96,6 @@ void FUT089PacketFormatter::parsePacket(const uint8_t *packet, JsonObject& resul
     result["button_id"] = command;
     result["argument"] = arg;
   }
+
+  return groupId;
 }

+ 1 - 1
lib/MiLight/FUT089PacketFormatter.h

@@ -39,7 +39,7 @@ public:
   virtual void modeSpeedUp();
   virtual void updateMode(uint8_t mode);
 
-  virtual void parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore);
+  virtual GroupId parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore);
 };
 
 #endif

+ 3 - 1
lib/MiLight/PacketFormatter.cpp

@@ -64,7 +64,9 @@ void PacketFormatter::enableNightMode() { }
 void PacketFormatter::updateTemperature(uint8_t value) { }
 void PacketFormatter::updateSaturation(uint8_t value) { }
 
-void PacketFormatter::parsePacket(const uint8_t *packet, JsonObject &result, GroupStateStore* stateStore) { }
+GroupId PacketFormatter::parsePacket(const uint8_t *packet, JsonObject &result, GroupStateStore* stateStore) {
+  return DEFAULT_GROUP_ID;
+}
 
 void PacketFormatter::pair() {
   for (size_t i = 0; i < 5; i++) {

+ 1 - 1
lib/MiLight/PacketFormatter.h

@@ -71,7 +71,7 @@ public:
   virtual void prepare(uint16_t deviceId, uint8_t groupId);
   virtual void format(uint8_t const* packet, char* buffer);
 
-  virtual void parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore);
+  virtual GroupId parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore);
 
   static void formatV1Packet(uint8_t const* packet, char* buffer);
 

+ 10 - 6
lib/MiLight/RgbCctPacketFormatter.cpp

@@ -63,14 +63,16 @@ void RgbCctPacketFormatter::enableNightMode() {
   command(RGB_CCT_ON | 0x80, arg);
 }
 
-void RgbCctPacketFormatter::parsePacket(const uint8_t *packet, JsonObject& result, GroupStateStore* stateStore) {
+GroupId RgbCctPacketFormatter::parsePacket(const uint8_t *packet, JsonObject& result, GroupStateStore* stateStore) {
   uint8_t packetCopy[V2_PACKET_LEN];
   memcpy(packetCopy, packet, V2_PACKET_LEN);
   V2RFEncoding::decodeV2Packet(packetCopy);
 
-  result["device_id"] = (packetCopy[2] << 8) | packetCopy[3];
-  result["group_id"] = packetCopy[7];
-  result["device_type"] = "rgb_cct";
+  GroupId groupId(
+    (packetCopy[2] << 8) | packetCopy[3],
+    packetCopy[7],
+    REMOTE_TYPE_RGB_CCT
+  );
 
   uint8_t command = (packetCopy[V2_COMMAND_INDEX] & 0x7F);
   uint8_t arg = packetCopy[V2_ARGUMENT_INDEX];
@@ -82,10 +84,10 @@ void RgbCctPacketFormatter::parsePacket(const uint8_t *packet, JsonObject& resul
       result["command"] = "mode_speed_up";
     } else if (arg < 5) { // Group is not reliably encoded in group byte. Extract from arg byte
       result["state"] = "ON";
-      result["group_id"] = arg;
+      groupId.groupId = arg;
     } else {
       result["state"] = "OFF";
-      result["group_id"] = arg-5;
+      groupId.groupId = arg-5;
     }
   } else if (command == RGB_CCT_COLOR) {
     uint8_t rescaledColor = (arg - RGB_CCT_COLOR_OFFSET) % 0x100;
@@ -119,4 +121,6 @@ void RgbCctPacketFormatter::parsePacket(const uint8_t *packet, JsonObject& resul
     result["button_id"] = command;
     result["argument"] = arg;
   }
+
+  return groupId;
 }

+ 1 - 1
lib/MiLight/RgbCctPacketFormatter.h

@@ -50,7 +50,7 @@ public:
   virtual void nextMode();
   virtual void previousMode();
 
-  virtual void parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore);
+  virtual GroupId parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore);
 
 protected:
 

+ 8 - 4
lib/MiLight/RgbPacketFormatter.cpp

@@ -79,12 +79,14 @@ void RgbPacketFormatter::previousMode() {
   command(RGB_MODE_DOWN, 0);
 }
 
-void RgbPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore) {
+GroupId RgbPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore) {
   uint8_t command = packet[RGB_COMMAND_INDEX] & 0x7F;
 
-  result["group_id"] = 0;
-  result["device_id"] = (packet[1] << 8) | packet[2];
-  result["device_type"] = "rgb";
+  GroupId groupId(
+    (packet[1] << 8) | packet[2],
+    0,
+    REMOTE_TYPE_RGB
+  );
 
   if (command == RGB_ON) {
     result["state"] = "ON";
@@ -105,6 +107,8 @@ void RgbPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result,
   } else {
     result["button_id"] = command;
   }
+
+  return groupId;
 }
 
 void RgbPacketFormatter::format(uint8_t const* packet, char* buffer) {

+ 1 - 1
lib/MiLight/RgbPacketFormatter.h

@@ -39,7 +39,7 @@ public:
   virtual void modeSpeedUp();
   virtual void nextMode();
   virtual void previousMode();
-  virtual void parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore);
+  virtual GroupId parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore);
 
   virtual void initializePacket(uint8_t* packet);
 };

+ 9 - 5
lib/MiLight/RgbwPacketFormatter.cpp

@@ -93,19 +93,21 @@ void RgbwPacketFormatter::enableNightMode() {
   command(button | 0x10, 0);
 }
 
-void RgbwPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore) {
+GroupId RgbwPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore) {
   uint8_t command = packet[RGBW_COMMAND_INDEX] & 0x7F;
 
-  result["device_id"] = (packet[1] << 8) | packet[2];
-  result["device_type"] = "rgbw";
-  result["group_id"] = packet[RGBW_BRIGHTNESS_GROUP_INDEX] & 0x7;
+  GroupId groupId(
+    (packet[1] << 8) | packet[2],
+    packet[RGBW_BRIGHTNESS_GROUP_INDEX] & 0x7,
+    REMOTE_TYPE_RGBW
+  );
 
   if (command >= RGBW_ALL_ON && command <= RGBW_GROUP_4_OFF) {
     result["state"] = (command % 2) ? "ON" : "OFF";
     // Determine group ID from button ID for on/off. The remote's state is from
     // the last packet sent, not the current one, and that can be wrong for
     // on/off commands.
-    result["group_id"] = GROUP_FOR_STATUS_COMMAND(command);
+    groupId.groupId = GROUP_FOR_STATUS_COMMAND(command);
   } else if (command == RGBW_BRIGHTNESS) {
     uint8_t brightness = 31;
     brightness -= packet[RGBW_BRIGHTNESS_GROUP_INDEX] >> 3;
@@ -125,6 +127,8 @@ void RgbwPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result,
   } else {
     result["button_id"] = command;
   }
+
+  return groupId;
 }
 
 void RgbwPacketFormatter::format(uint8_t const* packet, char* buffer) {

+ 1 - 1
lib/MiLight/RgbwPacketFormatter.h

@@ -62,7 +62,7 @@ public:
   virtual void previousMode();
   virtual void updateMode(uint8_t mode);
   virtual void enableNightMode();
-  virtual void parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore);
+  virtual GroupId parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore);
 
   virtual void initializePacket(uint8_t* packet);
 

+ 2 - 0
lib/MiLightState/GroupState.cpp

@@ -3,6 +3,8 @@
 #include <MiLightRemoteConfig.h>
 #include <RGBConverter.h>
 
+const GroupId DEFAULT_GROUP_ID;
+
 const GroupState& GroupState::defaultState(MiLightRemoteType remoteType) {
   static GroupState instances[MiLightRemoteConfig::NUM_REMOTES];
   GroupState& state = instances[remoteType];

+ 2 - 0
lib/MiLightState/GroupState.h

@@ -123,4 +123,6 @@ struct GroupStateNode {
   GroupId prevNode;
 };
 
+extern const GroupId DEFAULT_GROUP_ID;
+
 #endif