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

Merge branch 'master' into patch-1

Chris Mullins пре 8 година
родитељ
комит
cc36bdd826
3 измењених фајлова са 60 додато и 35 уклоњено
  1. 6 0
      lib/MiLight/CctPacketFormatter.cpp
  2. 43 27
      lib/Udp/V5MiLightUdpServer.cpp
  3. 11 8
      lib/Udp/V5MiLightUdpServer.h

+ 6 - 0
lib/MiLight/CctPacketFormatter.cpp

@@ -68,6 +68,9 @@ uint8_t CctPacketFormatter::getCctStatusButton(uint8_t groupId, MiLightStatus st
 
   if (status == ON) {
     switch(groupId) {
+      case 0:
+        button = CCT_ALL_ON;
+        break;
       case 1:
         button = CCT_GROUP_1_ON;
         break;
@@ -83,6 +86,9 @@ uint8_t CctPacketFormatter::getCctStatusButton(uint8_t groupId, MiLightStatus st
     }
   } else {
     switch(groupId) {
+      case 0:
+        button = CCT_ALL_OFF;
+        break;
       case 1:
         button = CCT_GROUP_1_OFF;
         break;

+ 43 - 27
lib/Udp/V5MiLightUdpServer.cpp

@@ -5,90 +5,101 @@ void V5MiLightUdpServer::handlePacket(uint8_t* packet, size_t packetSize) {
     handleCommand(packet[0], packet[1]);
   } else {
     Serial.print(F("V5MilightUdpServer: unexpected packet length. Should always be 2-3, was: "));
-    Serial.println(packetSize);  
+    Serial.println(packetSize);
   }
 }
 
 void V5MiLightUdpServer::handleCommand(uint8_t command, uint8_t commandArg) {
+  // On/off for RGBW
   if (command >= UDP_RGBW_GROUP_1_ON && command <= UDP_RGBW_GROUP_4_OFF) {
     const MiLightStatus status = (command % 2) == 1 ? ON : OFF;
     const uint8_t groupId = (command - UDP_RGBW_GROUP_1_ON + 2)/2;
-    
+
     client->prepare(MilightRgbwConfig, deviceId, groupId);
     client->updateStatus(status);
-    
+
     this->lastGroup = groupId;
+  // Command set_white for RGBW
   } else if (command >= UDP_RGBW_GROUP_ALL_WHITE && command <= UDP_RGBW_GROUP_4_WHITE) {
     const uint8_t groupId = (command - UDP_RGBW_GROUP_ALL_WHITE)/2;
     client->prepare(MilightRgbwConfig, deviceId, groupId);
     client->updateColorWhite();
     this->lastGroup = groupId;
-  } else if (uint8_t cctGroup = cctCommandIdToGroup(command)) {
+  // On/off for CCT
+  } else if (cctCommandIdToGroup(command) != 255) {
+    uint8_t cctGroup = cctCommandIdToGroup(command);
     client->prepare(MilightCctConfig, deviceId, cctGroup);
     client->updateStatus(cctCommandToStatus(command));
     this->lastGroup = cctGroup;
-  }
-  else {
+  } else {
     client->prepare(MilightRgbwConfig, deviceId, lastGroup);
     bool handled = true;
-    
+
     switch (command) {
       case UDP_RGBW_ALL_ON:
         client->updateStatus(ON, 0);
         break;
-      
+
       case UDP_RGBW_ALL_OFF:
         client->updateStatus(OFF, 0);
         break;
-      
+
       case UDP_RGBW_COLOR:
         // UDP color is shifted by 0xC8 from 2.4 GHz color, and the spectrum is
         // flipped (R->B->G instead of R->G->B)
         client->updateColorRaw(0xFF-(commandArg + 0x35));
         break;
-        
+
       case UDP_RGBW_DISCO_MODE:
         pressButton(RGBW_DISCO_MODE);
         break;
-        
+
       case UDP_RGBW_SPEED_DOWN:
         pressButton(RGBW_SPEED_DOWN);
         break;
-        
+
       case UDP_RGBW_SPEED_UP:
         pressButton(RGBW_SPEED_UP);
         break;
-        
+
       case UDP_RGBW_BRIGHTNESS:
         // map [2, 27] --> [0, 100]
         client->updateBrightness(
           round(((commandArg - 2) / 25.0)*100)
         );
         break;
-        
+
       default:
         handled = false;
     }
-    
-    client->prepare(MilightCctConfig);
-    
+
+    if (handled) {
+      return;
+    }
+
+    client->prepare(MilightCctConfig, deviceId, lastGroup);
+
     switch(command) {
       case UDP_CCT_BRIGHTNESS_DOWN:
         client->decreaseBrightness();
         break;
-        
+
       case UDP_CCT_BRIGHTNESS_UP:
         client->increaseBrightness();
         break;
-        
+
       case UDP_CCT_TEMPERATURE_DOWN:
         client->decreaseTemperature();
         break;
-        
+
       case UDP_CCT_TEMPERATURE_UP:
         client->increaseTemperature();
         break;
-        
+
+      case UDP_CCT_NIGHT_MODE:
+        client->enableNightMode();
+        break;
+
       default:
         if (!handled) {
           Serial.print(F("V5MiLightUdpServer - Unhandled command: "));
@@ -100,7 +111,7 @@ void V5MiLightUdpServer::handleCommand(uint8_t command, uint8_t commandArg) {
 
 void V5MiLightUdpServer::pressButton(uint8_t button) {
   client->command(button, 0);
-}  
+}
 
 uint8_t V5MiLightUdpServer::cctCommandIdToGroup(uint8_t command) {
   switch (command) {
@@ -116,22 +127,27 @@ uint8_t V5MiLightUdpServer::cctCommandIdToGroup(uint8_t command) {
     case UDP_CCT_GROUP_4_ON:
     case UDP_CCT_GROUP_4_OFF:
       return 4;
+    case UDP_CCT_ALL_ON:
+    case UDP_CCT_ALL_OFF:
+      return 0;
   }
-  
-  return 0;
-}  
-  
+
+  return 255;
+}
+
 MiLightStatus V5MiLightUdpServer::cctCommandToStatus(uint8_t command) {
   switch (command) {
     case UDP_CCT_GROUP_1_ON:
     case UDP_CCT_GROUP_2_ON:
     case UDP_CCT_GROUP_3_ON:
     case UDP_CCT_GROUP_4_ON:
+    case UDP_CCT_ALL_ON:
       return ON;
     case UDP_CCT_GROUP_1_OFF:
     case UDP_CCT_GROUP_2_OFF:
     case UDP_CCT_GROUP_3_OFF:
     case UDP_CCT_GROUP_4_OFF:
+    case UDP_CCT_ALL_OFF:
       return OFF;
   }
-}
+}

+ 11 - 8
lib/Udp/V5MiLightUdpServer.h

@@ -7,9 +7,11 @@
 #include <MiLightUdpServer.h>
 
 #ifndef _V5_MILIGHT_UDP_SERVER
-#define _V5_MILIGHT_UDP_SERVER 
+#define _V5_MILIGHT_UDP_SERVER
 
 enum MiLightUdpCommands {
+  UDP_CCT_ALL_ON             = 0x35,
+  UDP_CCT_ALL_OFF            = 0x39,
   UDP_CCT_GROUP_1_ON         = 0x38,
   UDP_CCT_GROUP_1_OFF        = 0x3B,
   UDP_CCT_GROUP_2_ON         = 0x3D,
@@ -22,11 +24,12 @@ enum MiLightUdpCommands {
   UDP_CCT_TEMPERATURE_UP     = 0x3E,
   UDP_CCT_BRIGHTNESS_DOWN    = 0x34,
   UDP_CCT_BRIGHTNESS_UP      = 0x3C,
-  
-  UDP_RGBW_ALL_OFF           = 0x41,
-  UDP_RGBW_ALL_ON            = 0x42,
-  UDP_RGBW_SPEED_UP          = 0x43, 
-  UDP_RGBW_SPEED_DOWN        = 0x44, 
+  UDP_CCT_NIGHT_MODE         = 0xB9,
+
+  UDP_RGBW_ALL_ON            = 0x41,
+  UDP_RGBW_ALL_OFF           = 0x42,
+  UDP_RGBW_SPEED_UP          = 0x43,
+  UDP_RGBW_SPEED_DOWN        = 0x44,
   UDP_RGBW_GROUP_1_ON        = 0x45,
   UDP_RGBW_GROUP_1_OFF       = 0x46,
   UDP_RGBW_GROUP_2_ON        = 0x47,
@@ -50,10 +53,10 @@ public:
   V5MiLightUdpServer(MiLightClient*& client, uint16_t port, uint16_t deviceId)
     : MiLightUdpServer(client, port, deviceId)
   { }
-  
+
   // Should return size of the response packet
   virtual void handlePacket(uint8_t* packet, size_t packetSize);
-    
+
 protected:
   void handleCommand(uint8_t command, uint8_t commandArg);
   void pressButton(uint8_t button);