Pārlūkot izejas kodu

fan out group 0 to all groups

Chris Mullins 8 gadi atpakaļ
vecāks
revīzija
8373bb388c

+ 23 - 24
lib/MiLight/MiLightRemoteConfig.cpp

@@ -1,11 +1,14 @@
 #include <MiLightRemoteConfig.h>
 
+/**
+ * IMPORTANT NOTE: These should be in the same order as MiLightRemoteType.
+ */
 const MiLightRemoteConfig* MiLightRemoteConfig::ALL_REMOTES[] = {
-  &FUT096Config,
-  &FUT091Config,
-  &FUT092Config,
-  &FUT089Config,
-  &FUT098Config
+  &FUT096Config, // rgbw
+  &FUT091Config, // cct
+  &FUT092Config, // rgb+cct
+  &FUT098Config, // rgb
+  &FUT089Config  // 8-group rgb+cct (b8, fut089)
 };
 
 const MiLightRemoteConfig* MiLightRemoteConfig::fromType(const String& type) {
@@ -35,21 +38,12 @@ const MiLightRemoteConfig* MiLightRemoteConfig::fromType(const String& type) {
 }
 
 const MiLightRemoteConfig* MiLightRemoteConfig::fromType(MiLightRemoteType type) {
-  switch (type) {
-    case REMOTE_TYPE_RGBW:
-      return &FUT096Config;
-    case REMOTE_TYPE_RGB:
-      return &FUT098Config;
-    case REMOTE_TYPE_CCT:
-      return &FUT091Config;
-    case REMOTE_TYPE_RGB_CCT:
-      return &FUT092Config;
-    case REMOTE_TYPE_FUT089:
-      return &FUT089Config;
-    default:
-      Serial.println(F("ERROR - tried to fetch remote config for unknown type"));
-      return NULL;
+  if (type == REMOTE_TYPE_UNKNOWN || type >= size(ALL_REMOTES)) {
+    Serial.println(F("ERROR - tried to fetch remote config for unknown type"));
+    return NULL;
   }
+
+  return ALL_REMOTES[type];
 }
 
 const MiLightRemoteConfig* MiLightRemoteConfig::fromReceivedPacket(
@@ -74,33 +68,38 @@ const MiLightRemoteConfig FUT096Config( //rgbw
   new RgbwPacketFormatter(),
   MiLightRadioConfig::ALL_CONFIGS[0],
   REMOTE_TYPE_RGBW,
-  "rgbw"
+  "rgbw",
+  4
 );
 
 const MiLightRemoteConfig FUT091Config( //cct
   new CctPacketFormatter(),
   MiLightRadioConfig::ALL_CONFIGS[1],
   REMOTE_TYPE_CCT,
-  "cct"
+  "cct",
+  4
 );
 
 const MiLightRemoteConfig FUT092Config( //rgb+cct
   new RgbCctPacketFormatter(),
   MiLightRadioConfig::ALL_CONFIGS[2],
   REMOTE_TYPE_RGB_CCT,
-  "rgb_cct"
+  "rgb_cct",
+  4
 );
 
 const MiLightRemoteConfig FUT089Config( //rgb+cct B8 / FUT089
   new FUT089PacketFormatter(),
   MiLightRadioConfig::ALL_CONFIGS[2],
   REMOTE_TYPE_FUT089,
-  "fut089"
+  "fut089",
+  8
 );
 
 const MiLightRemoteConfig FUT098Config( //rgb
   new RgbPacketFormatter(),
   MiLightRadioConfig::ALL_CONFIGS[3],
   REMOTE_TYPE_RGB,
-  "rgb"
+  "rgb",
+  0
 );

+ 5 - 2
lib/MiLight/MiLightRemoteConfig.h

@@ -17,17 +17,20 @@ public:
     PacketFormatter* packetFormatter,
     MiLightRadioConfig& radioConfig,
     const MiLightRemoteType type,
-    const String name
+    const String name,
+    const size_t numGroups
   ) : packetFormatter(packetFormatter),
       radioConfig(radioConfig),
       type(type),
-      name(name)
+      name(name),
+      numGroups(numGroups)
   { }
 
   PacketFormatter* const packetFormatter;
   const MiLightRadioConfig& radioConfig;
   const MiLightRemoteType type;
   const String name;
+  const size_t numGroups;
 
   static const MiLightRemoteConfig* fromType(MiLightRemoteType type);
   static const MiLightRemoteConfig* fromType(const String& type);

+ 12 - 0
lib/MiLightState/GroupStateStore.cpp

@@ -1,4 +1,5 @@
 #include <GroupStateStore.h>
+#include <MiLightRemoteConfig.h>
 
 GroupStateStore::GroupStateStore(const size_t maxSize)
   : cache(GroupStateCache(maxSize))
@@ -21,6 +22,17 @@ GroupState& GroupStateStore::get(const BulbId& id) {
 GroupState& GroupStateStore::set(const BulbId &id, const GroupState& state) {
   GroupState& storedState = get(id);
   storedState = state;
+
+  if (id.groupId == 0) {
+    const MiLightRemoteConfig* remote = MiLightRemoteConfig::fromType(id.deviceType);
+    BulbId individualBulb(id);
+
+    for (size_t i = 1; i < remote->numGroups; i++) {
+      individualBulb.groupId = i;
+      set(individualBulb, state);
+    }
+  }
+
   return storedState;
 }
 

+ 6 - 6
lib/Radio/MiLightConstants.h

@@ -2,12 +2,12 @@
 #define _MILIGHT_BUTTONS
 
 enum MiLightRemoteType {
-  REMOTE_TYPE_UNKNOWN,
-  REMOTE_TYPE_RGBW,
-  REMOTE_TYPE_CCT,
-  REMOTE_TYPE_RGB_CCT,
-  REMOTE_TYPE_RGB,
-  REMOTE_TYPE_FUT089
+  REMOTE_TYPE_UNKNOWN = 255,
+  REMOTE_TYPE_RGBW    = 0,
+  REMOTE_TYPE_CCT     = 1,
+  REMOTE_TYPE_RGB_CCT = 2,
+  REMOTE_TYPE_RGB     = 3,
+  REMOTE_TYPE_FUT089  = 4
 };
 
 enum MiLightStatus {

+ 1 - 0
src/main.cpp

@@ -98,6 +98,7 @@ void onPacketSentHandler(uint8_t* packet, const MiLightRemoteConfig& config) {
   if (mqttClient) {
     GroupState& groupState = stateStore.get(bulbId);
     groupState.patch(result);
+    stateStore.set(bulbId, groupState);
 
     // Sends the state delta derived from the raw packet
     char output[200];