瀏覽代碼

add CCT support to UDP server

Chris Mullins 8 年之前
父節點
當前提交
32e45e9595
共有 4 個文件被更改,包括 125 次插入49 次删除
  1. 10 8
      lib/MiLight/MiLightClient.cpp
  2. 6 3
      lib/MiLight/MiLightClient.h
  3. 72 13
      lib/MiLight/MiLightUdpServer.cpp
  4. 37 25
      lib/MiLight/MiLightUdpServer.h

+ 10 - 8
lib/MiLight/MiLightClient.cpp

@@ -66,7 +66,8 @@ void MiLightClient::writeRgbw(
   const uint8_t color,
   const uint8_t brightness,
   const uint8_t groupId,
-  const uint8_t button) {
+  const uint8_t button,
+  const unsigned int resendCount) {
   
   uint8_t packet[MilightRgbwConfig.packetLength];
   size_t packetPtr = 0;
@@ -79,12 +80,13 @@ void MiLightClient::writeRgbw(
   packet[packetPtr++] = button;
   packet[packetPtr++] = nextSequenceNum();
   
-  write(RGBW, packet);
+  write(RGBW, packet, resendCount);
 }
 
 void MiLightClient::writeCct(const uint16_t deviceId,
   const uint8_t groupId,
-  const uint8_t button) {
+  const uint8_t button,
+  const unsigned int resendCount) {
     
   uint8_t packet[MilightRgbwConfig.packetLength];
   uint8_t sequenceNum = nextSequenceNum();
@@ -98,7 +100,7 @@ void MiLightClient::writeCct(const uint16_t deviceId,
   packet[packetPtr++] = sequenceNum;
   packet[packetPtr++] = sequenceNum;
   
-  write(CCT, packet);
+  write(CCT, packet, resendCount);
 }
     
 void MiLightClient::updateColorRaw(const uint16_t deviceId, const uint8_t groupId, const uint16_t color) {
@@ -183,11 +185,11 @@ void MiLightClient::allOff(const MiLightRadioType type, const uint16_t deviceId)
 }
 
 void MiLightClient::increaseCctBrightness(const uint16_t deviceId, const uint8_t groupId) {
-  writeCct(deviceId, groupId, CCT_BRIGHTNESS_UP);
+  writeCct(deviceId, groupId, CCT_BRIGHTNESS_UP, 10);
 }
 
 void MiLightClient::decreaseCctBrightness(const uint16_t deviceId, const uint8_t groupId) {
-  writeCct(deviceId, groupId, CCT_BRIGHTNESS_DOWN);
+  writeCct(deviceId, groupId, CCT_BRIGHTNESS_DOWN, 10);
 }
 
 void MiLightClient::updateCctBrightness(const uint16_t deviceId, const uint8_t groupId, const uint8_t brightness) {
@@ -200,11 +202,11 @@ void MiLightClient::updateCctBrightness(const uint16_t deviceId, const uint8_t g
 }
 
 void MiLightClient::increaseTemperature(const uint16_t deviceId, const uint8_t groupId) {
-  writeCct(deviceId, groupId, CCT_TEMPERATURE_UP);
+  writeCct(deviceId, groupId, CCT_TEMPERATURE_UP, 10);
 }
 
 void MiLightClient::decreaseTemperature(const uint16_t deviceId, const uint8_t groupId) {
-  writeCct(deviceId, groupId, CCT_TEMPERATURE_DOWN);
+  writeCct(deviceId, groupId, CCT_TEMPERATURE_DOWN, 10);
 }
 
 void MiLightClient::updateTemperature(const uint16_t deviceId, const uint8_t groupId, const uint8_t temperature) {

+ 6 - 3
lib/MiLight/MiLightClient.h

@@ -9,6 +9,7 @@
 
 #define MILIGHT_PACKET_LENGTH 7
 #define MILIGHT_CCT_INTERVALS 10
+#define MILIGHT_DEFAULT_RESEND_COUNT 50
 
 enum MiLightRadioType {
   UNKNOWN = 0,
@@ -65,20 +66,22 @@ class MiLightClient {
     
     bool available(const MiLightRadioType radioType);
     void read(const MiLightRadioType radioType, uint8_t packet[]);
-    void write(const MiLightRadioType radioType, uint8_t packet[], const unsigned int resendCount = 50);
+    void write(const MiLightRadioType radioType, uint8_t packet[], const unsigned int resendCount = MILIGHT_DEFAULT_RESEND_COUNT);
     
     void writeRgbw(
       const uint16_t deviceId,
       const uint8_t color,
       const uint8_t brightness,
       const uint8_t groupId,
-      const uint8_t button
+      const uint8_t button,
+      const unsigned int resendCount = MILIGHT_DEFAULT_RESEND_COUNT
     );
     
     void writeCct(
       const uint16_t deviceId,
       const uint8_t groupId,
-      const uint8_t button
+      const uint8_t button,
+      const unsigned int resendCount = MILIGHT_DEFAULT_RESEND_COUNT
     );
     
     // Common methods

+ 72 - 13
lib/MiLight/MiLightUdpServer.cpp

@@ -34,45 +34,54 @@ void MiLightUdpServer::handleClient() {
 }
 
 void MiLightUdpServer::handleCommand(uint8_t command, uint8_t commandArg) {
-  if (command >= UDP_GROUP_1_ON && command <= UDP_GROUP_4_OFF) {
+  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_GROUP_1_ON + 2)/2;
+    const uint8_t groupId = (command - UDP_RGBW_GROUP_1_ON + 2)/2;
     
     client->updateStatus(RGBW, deviceId, groupId, status);
     
     this->lastGroup = groupId;
-  } else if (command >= UDP_GROUP_ALL_WHITE && command <= UDP_GROUP_4_WHITE) {
-    const uint8_t groupId = (command - UDP_GROUP_ALL_WHITE)/2;
+  } 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->updateColorWhite(deviceId, groupId);
     this->lastGroup = groupId;
-  } else {
+  } else if (uint8_t cctGroup = cctCommandIdToGroup(command)) {
+    client->updateStatus(
+      CCT,
+      deviceId,
+      cctGroup,
+      cctCommandToStatus(command)
+    );
+    this->lastGroup = cctGroup;
+  }
+  else {
     switch (command) {
-      case UDP_ALL_ON:
+      case UDP_RGBW_ALL_ON:
         client->allOn(RGBW, deviceId);
         break;
       
-      case UDP_ALL_OFF:
+      case UDP_RGBW_ALL_OFF:
         client->allOff(RGBW, deviceId);
         break;
       
-      case UDP_COLOR:
+      case UDP_RGBW_COLOR:
         // UDP color is shifted by 0xC8 from 2.4 GHz color ...
         client->updateColorRaw(deviceId, this->lastGroup, commandArg + 0xC8);
         break;
         
-      case UDP_DISCO_MODE:
+      case UDP_RGBW_DISCO_MODE:
         pressButton(this->lastGroup, RGBW_DISCO_MODE);
         break;
         
-      case UDP_SPEED_DOWN:
+      case UDP_RGBW_SPEED_DOWN:
         pressButton(this->lastGroup, RGBW_SPEED_DOWN);
         break;
         
-      case UDP_SPEED_UP:
+      case UDP_RGBW_SPEED_UP:
         pressButton(this->lastGroup, RGBW_SPEED_UP);
         break;
         
-      case UDP_BRIGHTNESS:
+      case UDP_RGBW_BRIGHTNESS:
         // map [2, 27] --> [0, 100]
         client->updateBrightness(
           deviceId, 
@@ -81,6 +90,22 @@ void MiLightUdpServer::handleCommand(uint8_t command, uint8_t commandArg) {
         );
         break;
         
+      case UDP_CCT_BRIGHTNESS_DOWN:
+        client->decreaseCctBrightness(deviceId, this->lastGroup);
+        break;
+        
+      case UDP_CCT_BRIGHTNESS_UP:
+        client->increaseCctBrightness(deviceId, this->lastGroup);
+        break;
+        
+      case UDP_CCT_TEMPERATURE_DOWN:
+        client->decreaseTemperature(deviceId, this->lastGroup);
+        break;
+        
+      case UDP_CCT_TEMPERATURE_UP:
+        client->increaseTemperature(deviceId, this->lastGroup);
+        break;
+        
       default:
         Serial.print("MiLightUdpServer - Unhandled command: ");
         Serial.println(command);
@@ -90,4 +115,38 @@ void MiLightUdpServer::handleCommand(uint8_t command, uint8_t commandArg) {
 
 void MiLightUdpServer::pressButton(uint8_t group, uint8_t button) {
   client->writeRgbw(deviceId, 0, 0, group, button);
-}  
+}  
+
+uint8_t MiLightUdpServer::cctCommandIdToGroup(uint8_t command) {
+  switch (command) {
+    case UDP_CCT_GROUP_1_ON:
+    case UDP_CCT_GROUP_1_OFF:
+      return 1;
+    case UDP_CCT_GROUP_2_ON:
+    case UDP_CCT_GROUP_2_OFF:
+      return 2;
+    case UDP_CCT_GROUP_3_ON:
+    case UDP_CCT_GROUP_3_OFF:
+      return 3;
+    case UDP_CCT_GROUP_4_ON:
+    case UDP_CCT_GROUP_4_OFF:
+      return 4;
+  }
+  
+  return 0;
+}  
+  
+MiLightStatus MiLightUdpServer::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:
+      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:
+      return OFF;
+  }
+}

+ 37 - 25
lib/MiLight/MiLightUdpServer.h

@@ -2,38 +2,48 @@
 #include <MiLightClient.h>
 #include <WiFiUdp.h>
 
+// This protocol is documented here:
+// http://www.limitlessled.com/dev/
+
 #define MILIGHT_PACKET_BUFFER_SIZE 10
 
 #ifndef _MILIGHT_UDP_SERVER
 #define _MILIGHT_UDP_SERVER 
 
-// These are mostly a remapping of MiLightButton
 enum MiLightUdpCommands {
-  UDP_ALL_ON            = 0x41,
-  UDP_ALL_OFF           = 0x42,
-  
-  UDP_SPEED_UP          = 0x43, 
-  UDP_SPEED_DOWN        = 0x44, 
-  
-  UDP_GROUP_1_ON        = 0x45,
-  UDP_GROUP_1_OFF       = 0x46,
-  UDP_GROUP_2_ON        = 0x47,
-  UDP_GROUP_2_OFF       = 0x48,
-  UDP_GROUP_3_ON        = 0x49,
-  UDP_GROUP_3_OFF       = 0x4A,
-  UDP_GROUP_4_ON        = 0x4B,
-  UDP_GROUP_4_OFF       = 0x4C,
-  
-  UDP_DISCO_MODE        = 0x4D,
-  
-  UDP_GROUP_ALL_WHITE   = 0xC2,
-  UDP_GROUP_1_WHITE     = 0xC5,
-  UDP_GROUP_2_WHITE     = 0xC7,
-  UDP_GROUP_3_WHITE     = 0xC9,
-  UDP_GROUP_4_WHITE     = 0xCB,
+  UDP_CCT_GROUP_1_ON         = 0x38,
+  UDP_CCT_GROUP_1_OFF        = 0x3B,
+  UDP_CCT_GROUP_2_ON         = 0x3D,
+  UDP_CCT_GROUP_2_OFF        = 0x33,
+  UDP_CCT_GROUP_3_ON         = 0x37,
+  UDP_CCT_GROUP_3_OFF        = 0x3A,
+  UDP_CCT_GROUP_4_ON         = 0x32,
+  UDP_CCT_GROUP_4_OFF        = 0x36,
+  UDP_CCT_TEMPERATURE_DOWN   = 0x3F,
+  UDP_CCT_TEMPERATURE_UP     = 0x3E,
+  UDP_CCT_BRIGHTNESS_DOWN    = 0x34,
+  UDP_CCT_BRIGHTNESS_UP      = 0x3C,
   
-  UDP_BRIGHTNESS        = 0x4E,
-  UDP_COLOR             = 0x40
+  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,
+  UDP_RGBW_GROUP_2_OFF       = 0x48,
+  UDP_RGBW_GROUP_3_ON        = 0x49,
+  UDP_RGBW_GROUP_3_OFF       = 0x4A,
+  UDP_RGBW_GROUP_4_ON        = 0x4B,
+  UDP_RGBW_GROUP_4_OFF       = 0x4C,
+  UDP_RGBW_DISCO_MODE        = 0x4D,
+  UDP_RGBW_GROUP_ALL_WHITE   = 0xC2,
+  UDP_RGBW_GROUP_1_WHITE     = 0xC5,
+  UDP_RGBW_GROUP_2_WHITE     = 0xC7,
+  UDP_RGBW_GROUP_3_WHITE     = 0xC9,
+  UDP_RGBW_GROUP_4_WHITE     = 0xCB,
+  UDP_RGBW_BRIGHTNESS        = 0x4E,
+  UDP_RGBW_COLOR             = 0x40
 };
 
 class MiLightUdpServer {
@@ -55,6 +65,8 @@ protected:
   
   void handleCommand(uint8_t command, uint8_t commandArg);
   void pressButton(uint8_t group, uint8_t button);
+  uint8_t cctCommandIdToGroup(uint8_t command);
+  MiLightStatus cctCommandToStatus(uint8_t command);
 };
 
 #endif