Переглянути джерело

Merge pull request #41 from sidoh/udp_presets

Add support for UDP presets
Chris Mullins 8 роки тому
батько
коміт
500d5dca74

+ 19 - 15
lib/Udp/V6CctCommandHandler.cpp

@@ -1,49 +1,53 @@
 #include <V6CctCommandHandler.h>
 
+bool V6CctCommandHandler::handlePreset(
+    MiLightClient* client,
+    uint8_t commandLsb,
+    uint32_t commandArg)
+{
+  return false;
+}
+
 bool V6CctCommandHandler::handleCommand(
-    MiLightClient* client, 
-    uint16_t deviceId,
-    uint8_t group,
+    MiLightClient* client,
     uint32_t command,
     uint32_t commandArg)
 {
   const uint8_t cmd = command & 0xFF;
   const uint8_t arg = commandArg >> 24;
-  
-  client->prepare(MilightCctConfig, deviceId, group);
-  
+
   if (cmd == V2_CCT_COMMAND_PREFIX) {
     switch (arg) {
       case V2_CCT_ON:
         client->updateStatus(ON);
         break;
-        
+
       case V2_CCT_OFF:
         client->updateStatus(OFF);
         break;
-        
+
       case V2_CCT_BRIGHTNESS_DOWN:
         client->decreaseBrightness();
         break;
-        
+
       case V2_CCT_BRIGHTNESS_UP:
         client->increaseBrightness();
         break;
-        
+
       case V2_CCT_TEMPERATURE_DOWN:
         client->decreaseTemperature();
         break;
-        
+
       case V2_CCT_TEMPERATURE_UP:
         client->increaseTemperature();
         break;
-        
+
       default:
         return false;
     }
-    
+
     return true;
   }
-  
+
   return false;
-}
+}

+ 12 - 8
lib/Udp/V6CctCommandHandler.h

@@ -1,11 +1,11 @@
 #include <V6CommandHandler.h>
 
 #ifndef _V6_CCT_COMMAND_HANDLER_H
-#define _V6_CCT_COMMAND_HANDLER_H 
+#define _V6_CCT_COMMAND_HANDLER_H
 
 enum CctCommandIds {
   V2_CCT_COMMAND_PREFIX   = 0x01,
-  
+
   V2_CCT_BRIGHTNESS_UP    = 0x01,
   V2_CCT_BRIGHTNESS_DOWN  = 0x02,
   V2_CCT_TEMPERATURE_UP   = 0x03,
@@ -20,15 +20,19 @@ public:
   V6CctCommandHandler()
     : V6CommandHandler(0x0100, MilightCctConfig)
   { }
-  
+
   virtual bool handleCommand(
-    MiLightClient* client, 
-    uint16_t deviceId,
-    uint8_t group,
+    MiLightClient* client,
     uint32_t command,
     uint32_t commandArg
   );
-  
+
+  virtual bool handlePreset(
+    MiLightClient* client,
+    uint8_t commandLsb,
+    uint32_t commandArg
+  );
+
 };
 
-#endif
+#endif

+ 20 - 13
lib/Udp/V6ComamndHandler.cpp

@@ -13,30 +13,32 @@ V6CommandHandler* V6CommandHandler::ALL_HANDLERS[] = {
 };
 
 const size_t V6CommandHandler::NUM_HANDLERS = size(ALL_HANDLERS);
-  
-bool V6CommandHandler::handleCommand(MiLightClient* client, 
+
+bool V6CommandHandler::handleCommand(MiLightClient* client,
   uint16_t deviceId,
   uint8_t group,
   uint8_t commandType,
   uint32_t command,
-  uint32_t commandArg) 
+  uint32_t commandArg)
 {
   client->prepare(radioConfig, deviceId, group);
-  
+
   if (commandType == V6_PAIR) {
     client->pair();
   } else if (commandType == V6_UNPAIR) {
     client->unpair();
+  } else if (commandType == V6_PRESET) {
+    return this->handlePreset(client, command, commandArg);
   } else if (commandType == V6_COMMAND) {
-    return this->handleCommand(client, deviceId, group, command, commandArg);
+    return this->handleCommand(client, command, commandArg);
   } else {
     return false;
   }
-  
+
   return true;
 }
 
-bool V6CommandDemuxer::handleCommand(MiLightClient* client, 
+bool V6CommandDemuxer::handleCommand(MiLightClient* client,
   uint16_t deviceId,
   uint8_t group,
   uint8_t commandType,
@@ -49,15 +51,20 @@ bool V6CommandDemuxer::handleCommand(MiLightClient* client,
       return true;
     }
   }
-  
+
   return false;
 }
 
-bool V6CommandDemuxer::handleCommand(MiLightClient* client, 
-  uint16_t deviceId,
-  uint8_t group,
-  uint32_t command,
+bool V6CommandDemuxer::handleCommand(MiLightClient* client,
+  uint32_t commandLsb,
   uint32_t commandArg)
 {
   return false;
-}
+}
+
+bool V6CommandDemuxer::handlePreset(MiLightClient* client,
+  uint8_t commandLsb,
+  uint32_t commandArg)
+{
+  return false;
+}

+ 28 - 19
lib/Udp/V6CommandHandler.h

@@ -2,11 +2,12 @@
 #include <MiLightRadioConfig.h>
 
 #ifndef _V6_COMMAND_HANDLER_H
-#define _V6_COMMAND_HANDLER_H 
+#define _V6_COMMAND_HANDLER_H
 
 enum V6CommandTypes {
   V6_PAIR = 0x3D,
   V6_UNPAIR = 0x3E,
+  V6_PRESET = 0x3F,
   V6_COMMAND = 0x31
 };
 
@@ -14,63 +15,71 @@ class V6CommandHandler {
 public:
   static V6CommandHandler* ALL_HANDLERS[] PROGMEM;
   static const size_t NUM_HANDLERS;
-  
+
   V6CommandHandler(uint16_t commandId, MiLightRadioConfig& radioConfig)
     : commandId(commandId),
       radioConfig(radioConfig)
   { }
-  
+
   virtual bool handleCommand(
-    MiLightClient* client, 
+    MiLightClient* client,
     uint16_t deviceId,
     uint8_t group,
     uint8_t commandType,
     uint32_t command,
     uint32_t commandArg
   );
-  
+
   const uint16_t commandId;
   MiLightRadioConfig& radioConfig;
-  
+
 protected:
-  
+
   virtual bool handleCommand(
-    MiLightClient* client, 
-    uint16_t deviceId,
-    uint8_t group,
+    MiLightClient* client,
     uint32_t command,
     uint32_t commandArg
   ) = 0;
+
+  virtual bool handlePreset(
+    MiLightClient* client,
+    uint8_t commandLsb,
+    uint32_t commandArg
+  ) = 0;
 };
 
 class V6CommandDemuxer : public V6CommandHandler {
 public:
   V6CommandDemuxer(V6CommandHandler* handlers[], size_t numHandlers)
-    : V6CommandHandler(0, MilightRgbwConfig),  
+    : V6CommandHandler(0, MilightRgbwConfig),
       handlers(handlers),
       numHandlers(numHandlers)
   { }
-  
+
   virtual bool handleCommand(
-    MiLightClient* client, 
+    MiLightClient* client,
     uint16_t deviceId,
     uint8_t group,
     uint8_t commandType,
     uint32_t command,
     uint32_t commandArg
   );
-  
+
 protected:
   V6CommandHandler** handlers;
   size_t numHandlers;
-  
+
   virtual bool handleCommand(
-    MiLightClient* client, 
-    uint16_t deviceId,
-    uint8_t group,
+    MiLightClient* client,
     uint32_t command,
     uint32_t commandArg
   );
+
+  virtual bool handlePreset(
+    MiLightClient* client,
+    uint8_t commandLsb,
+    uint32_t commandArg
+  );
 };
 
-#endif
+#endif

+ 44 - 22
lib/Udp/V6RgbCctCommandHandler.cpp

@@ -1,83 +1,105 @@
 #include <V6RgbCctCommandHandler.h>
 
+bool V6RgbCctCommandHandler::handlePreset(
+    MiLightClient* client,
+    uint8_t commandLsb,
+    uint32_t commandArg)
+{
+  if (commandLsb == 0) {
+    const uint8_t saturation = commandArg >> 24;
+    const uint8_t color = (commandArg >> 16);
+    const uint8_t brightness = (commandArg >> 8);
+
+    client->updateBrightness(brightness);
+    client->updateColorRaw(color);
+    client->updateSaturation(saturation);
+  } else if (commandLsb == 1) {
+    const uint8_t brightness = (commandArg >> 16);
+    const uint8_t kelvin = (commandArg >> 8);
+
+    client->updateBrightness(brightness);
+    client->updateTemperature(0x64 - kelvin);
+  } else {
+    return false;
+  }
+
+  return true;
+}
+
 bool V6RgbCctCommandHandler::handleCommand(
-    MiLightClient* client, 
-    uint16_t deviceId,
-    uint8_t group,
+    MiLightClient* client,
     uint32_t command,
     uint32_t commandArg)
 {
   const uint8_t cmd = command & 0xFF;
   const uint8_t arg = commandArg >> 24;
-  
-  client->prepare(MilightRgbCctConfig, deviceId, group);
-  
+
   if (cmd == V2_STATUS) {
     switch (arg) {
       case V2_RGB_CCT_ON:
       case V2_RGB_CCT_OFF:
         client->updateStatus(arg == V2_RGB_CCT_ON ? ON : OFF);
         break;
-        
+
       case V2_RGB_NIGHT_MODE:
         client->updateBrightness(0);
         break;
-        
+
       case V2_RGB_CCT_SPEED_DOWN:
         client->modeSpeedDown();
         break;
-        
+
       case V2_RGB_CCT_SPEED_UP:
         client->modeSpeedUp();
         break;
-        
-      default: 
+
+      default:
         return false;
     }
-    
+
     return true;
   }
-  
+
   switch (cmd) {
     case V2_COLOR:
       handleUpdateColor(client, commandArg);
       break;
-      
+
     case V2_KELVIN:
       client->updateTemperature(100 - arg);
       break;
-      
+
     case V2_BRIGHTNESS:
       client->updateBrightness(arg);
       break;
-      
+
     case V2_SATURATION:
       client->updateSaturation(100 - arg);
       break;
-      
+
     case V2_MODE:
       client->updateMode(arg-1);
       break;
-      
+
     default:
       return false;
   }
-  
+
   return true;
 }
 
-/* 
+/*
  * Arguments are 32 bits. Most commands use the first byte, but color arguments
  * can use all four. Triggered in app when quickly transitioning through colors.
  */
 void V6RgbCctCommandHandler::handleUpdateColor(MiLightClient *client, uint32_t color) {
   for (int i = 3; i >= 0; i--) {
     const uint8_t argValue = (color >> (i*8)) & 0xFF;
-    
+
     if (argValue == 0) {
       return;
     }
-    
+
     client->updateColorRaw(argValue);
   }
 }

+ 12 - 8
lib/Udp/V6RgbCctCommandHandler.h

@@ -1,7 +1,7 @@
 #include <V6CommandHandler.h>
 
 #ifndef _V6_RGB_CCT_COMMAND_HANDLER_H
-#define _V6_RGB_CCT_COMMAND_HANDLER_H 
+#define _V6_RGB_CCT_COMMAND_HANDLER_H
 
 enum V2CommandIds {
   V2_COLOR = 0x01,
@@ -25,17 +25,21 @@ public:
   V6RgbCctCommandHandler()
     : V6CommandHandler(0x0800, MilightRgbCctConfig)
   { }
-  
+
   virtual bool handleCommand(
-    MiLightClient* client, 
-    uint16_t deviceId,
-    uint8_t group,
+    MiLightClient* client,
     uint32_t command,
     uint32_t commandArg
   );
-  
+
+  virtual bool handlePreset(
+    MiLightClient* client,
+    uint8_t commandLsb,
+    uint32_t commandArg
+  );
+
   void handleUpdateColor(MiLightClient* client, uint32_t color);
-  
+
 };
 
-#endif
+#endif

+ 20 - 17
lib/Udp/V6RgbCommandHandler.cpp

@@ -1,60 +1,63 @@
 #include <V6RgbCommandHandler.h>
 
+bool V6RgbCommandHandler::handlePreset(
+    MiLightClient* client,
+    uint8_t commandLsb,
+    uint32_t commandArg)
+{
+}
+
 bool V6RgbCommandHandler::handleCommand(
-    MiLightClient* client, 
-    uint16_t deviceId,
-    uint8_t group,
+    MiLightClient* client,
     uint32_t command,
     uint32_t commandArg)
 {
   const uint8_t cmd = command & 0xFF;
   const uint8_t arg = commandArg >> 24;
-  
-  client->prepare(MilightRgbConfig, deviceId, 0);
-  
+
   if (cmd == V2_RGB_COMMAND_PREFIX) {
     switch (arg) {
       case V2_RGB_ON:
         client->updateStatus(ON);
         break;
-        
+
       case V2_RGB_OFF:
         client->updateStatus(OFF);
         break;
-        
+
       case V2_RGB_BRIGHTNESS_DOWN:
         client->decreaseBrightness();
         break;
-        
+
       case V2_RGB_BRIGHTNESS_UP:
         client->increaseBrightness();
         break;
-        
+
       case V2_RGB_MODE_DOWN:
         client->previousMode();
         break;
-        
+
       case V2_RGB_MODE_UP:
         client->nextMode();
         break;
-        
+
       case V2_RGB_SPEED_DOWN:
         client->modeSpeedDown();
         break;
-        
+
       case V2_RGB_SPEED_UP:
         client->modeSpeedUp();
         break;
-        
+
       default:
         return false;
     }
-    
+
     return true;
   } else if (cmd == V2_RGB_COLOR_PREFIX) {
     client->updateColorRaw(arg);
     return true;
   }
-  
+
   return false;
-}
+}

+ 11 - 7
lib/Udp/V6RgbCommandHandler.h

@@ -1,7 +1,7 @@
 #include <V6CommandHandler.h>
 
 #ifndef _V6_RGB_COMMAND_HANDLER_H
-#define _V6_RGB_COMMAND_HANDLER_H 
+#define _V6_RGB_COMMAND_HANDLER_H
 
 enum RgbCommandIds {
   V2_RGB_COMMAND_PREFIX  = 0x02,
@@ -21,15 +21,19 @@ public:
   V6RgbCommandHandler()
     : V6CommandHandler(0x0500, MilightRgbConfig)
   { }
-  
+
   virtual bool handleCommand(
-    MiLightClient* client, 
-    uint16_t deviceId,
-    uint8_t group,
+    MiLightClient* client,
     uint32_t command,
     uint32_t commandArg
   );
-  
+
+  virtual bool handlePreset(
+    MiLightClient* client,
+    uint8_t commandLsb,
+    uint32_t commandArg
+  );
+
 };
 
-#endif
+#endif

+ 29 - 15
lib/Udp/V6RgbwCommandHandler.cpp

@@ -1,48 +1,62 @@
 #include <V6RgbwCommandHandler.h>
 
+bool V6RgbwCommandHandler::handlePreset(
+    MiLightClient* client,
+    uint8_t commandLsb,
+    uint32_t commandArg)
+{
+  if (commandLsb == 0) {
+    client->updateColorRaw(commandArg >> 24);
+    client->updateBrightness(commandArg >> 16);
+  } else if (commandLsb == 1) {
+    client->updateColorWhite();
+    client->updateBrightness(commandArg >> 16);
+  } else {
+    return false;
+  }
+
+  return true;
+}
+
 bool V6RgbwCommandHandler::handleCommand(
-    MiLightClient* client, 
-    uint16_t deviceId,
-    uint8_t group,
+    MiLightClient* client,
     uint32_t command,
     uint32_t commandArg)
 {
   const uint8_t cmd = command & 0xFF;
   const uint8_t arg = commandArg >> 24;
-  
-  client->prepare(MilightRgbwConfig, deviceId, 0);
-  
+
   if (cmd == V2_RGBW_COMMAND_PREFIX) {
     switch (arg) {
       case V2_RGBW_ON:
         client->updateStatus(ON);
         break;
-        
+
       case V2_RGBW_OFF:
         client->updateStatus(OFF);
         break;
-        
+
       case V2_RGBW_WHITE_ON:
         client->updateColorWhite();
         break;
-        
+
       case V2_RGBW_NIGHT_LIGHT:
         client->updateColorWhite();
         client->updateBrightness(0);
         break;
-        
+
       case V2_RGBW_SPEED_DOWN:
         client->modeSpeedDown();
         break;
-        
+
       case V2_RGBW_SPEED_UP:
         client->modeSpeedUp();
         break;
-        
+
       default:
         return false;
     }
-    
+
     return true;
   } else if (cmd == V2_RGBW_COLOR_PREFIX) {
     client->updateColorRaw(arg);
@@ -54,6 +68,6 @@ bool V6RgbwCommandHandler::handleCommand(
     client->updateMode(arg);
     return true;
   }
-  
+
   return false;
-}
+}

+ 12 - 8
lib/Udp/V6RgbwCommandHandler.h

@@ -1,14 +1,14 @@
 #include <V6CommandHandler.h>
 
 #ifndef _V6_RGBW_COMMAND_HANDLER_H
-#define _V6_RGBW_COMMAND_HANDLER_H 
+#define _V6_RGBW_COMMAND_HANDLER_H
 
 enum RgbwCommandIds {
   V2_RGBW_COLOR_PREFIX      = 0x01,
   V2_RGBW_BRIGHTNESS_PREFIX = 0x02,
   V2_RGBW_COMMAND_PREFIX    = 0x03,
   V2_RGBW_MODE_PREFIX       = 0x04,
-  
+
   V2_RGBW_ON                = 0x01,
   V2_RGBW_OFF               = 0x02,
   V2_RGBW_SPEED_DOWN        = 0x03,
@@ -22,15 +22,19 @@ public:
   V6RgbwCommandHandler()
     : V6CommandHandler(0x0700, MilightRgbwConfig)
   { }
-  
+
   virtual bool handleCommand(
-    MiLightClient* client, 
-    uint16_t deviceId,
-    uint8_t group,
+    MiLightClient* client,
     uint32_t command,
     uint32_t commandArg
   );
-  
+
+  virtual bool handlePreset(
+    MiLightClient* client,
+    uint8_t commandLsb,
+    uint32_t commandArg
+  );
+
 };
 
-#endif
+#endif