瀏覽代碼

send HASS-compatable kelvin value; fix inverted scales for mireds/kelvins

Chris Mullins 8 年之前
父節點
當前提交
bdbd868344
共有 4 個文件被更改,包括 27 次插入12 次删除
  1. 5 6
      lib/Helpers/Units.h
  2. 2 2
      lib/MiLight/MiLightClient.cpp
  3. 16 4
      lib/MiLight/RgbCctPacketFormatter.cpp
  4. 4 0
      lib/MiLight/RgbCctPacketFormatter.h

+ 5 - 6
lib/Helpers/Units.h

@@ -14,9 +14,8 @@ public:
     return round(value * (newMax / oldMax));
   }
 
-  static uint8_t miredsToBrightness(uint8_t mireds, uint8_t maxValue = 255) {
-      // MiLight CCT bulbs range from 2700K-6500K, or ~370.3-153.8 mireds. Note
-      // that mireds are inversely correlated with color temperature.
+  static uint8_t miredsToWhiteVal(uint16_t mireds, uint8_t maxValue = 255) {
+      // MiLight CCT bulbs range from 2700K-6500K, or ~370.3-153.8 mireds.
       uint32_t tempMireds = constrain(mireds, COLOR_TEMP_MIN_MIREDS, COLOR_TEMP_MAX_MIREDS);
 
       uint8_t scaledTemp = round(
@@ -26,12 +25,12 @@ public:
         static_cast<double>(COLOR_TEMP_MAX_MIREDS - COLOR_TEMP_MIN_MIREDS)
       );
 
-      return (maxValue - scaledTemp);
+      return scaledTemp;
   }
 
-  static uint8_t brightnessToMireds(uint8_t value, uint8_t maxValue = 255) {
+  static uint16_t whiteValToMireds(uint8_t value, uint8_t maxValue = 255) {
     uint8_t reverseValue = maxValue - value;
-    uint8_t scaled = rescale(reverseValue, (COLOR_TEMP_MAX_MIREDS - COLOR_TEMP_MIN_MIREDS), maxValue);
+    uint16_t scaled = rescale<uint16_t, uint16_t>(reverseValue, (COLOR_TEMP_MAX_MIREDS - COLOR_TEMP_MIN_MIREDS), maxValue);
 
     return COLOR_TEMP_MIN_MIREDS + scaled;
   }

+ 2 - 2
lib/MiLight/MiLightClient.cpp

@@ -284,7 +284,7 @@ void MiLightClient::update(const JsonObject& request) {
   }
   // HomeAssistant
   if (request.containsKey("brightness")) {
-    uint8_t scaledBrightness = round(request.get<uint8_t>("brightness") * (100/255.0));
+    uint8_t scaledBrightness = Units::rescale(request.get<uint8_t>("brightness"), 100, 255);
     this->updateBrightness(scaledBrightness);
   }
 
@@ -294,7 +294,7 @@ void MiLightClient::update(const JsonObject& request) {
   // HomeAssistant
   if (request.containsKey("color_temp")) {
     this->updateTemperature(
-      Units::miredsToBrightness(request["color_temp"], 100)
+      Units::miredsToWhiteVal(request["color_temp"], 100)
     );
   }
 

+ 16 - 4
lib/MiLight/RgbCctPacketFormatter.cpp

@@ -137,10 +137,22 @@ void RgbCctPacketFormatter::parsePacket(const uint8_t *packet, JsonObject& resul
     uint16_t hue = Units::rescale<uint16_t, uint16_t>(rescaledColor, 360, 255.0);
     result["hue"] = hue;
   } else if (command == RGB_CCT_KELVIN) {
-    uint8_t temperature = RGB_CCT_KELVIN_OFFSET;
-    temperature -= arg;
-    temperature /= 2;
-    result["temperature"] = temperature;
+    uint8_t temperature =
+        static_cast<uint8_t>(
+          // Range in packets is 180 - 220 or something like that. Shift to
+          // 0..224. Then strip out values out of range [0..24), and (224..255]
+          constrain(
+            static_cast<uint8_t>(arg + RGB_CCT_KELVIN_REMOTE_OFFSET),
+            24,
+            224
+          )
+            +
+          // Shift 24 down to 0
+          RGB_CCT_KELVIN_REMOTE_START
+        )/2; // values are in increments of 2
+        printf("%u\n", temperature);
+
+    result["color_temp"] = Units::whiteValToMireds(temperature, 100);
   // brightness == saturation
   } else if (command == RGB_CCT_BRIGHTNESS && arg >= (RGB_CCT_BRIGHTNESS_OFFSET - 15)) {
     uint8_t level = constrain(arg - RGB_CCT_BRIGHTNESS_OFFSET, 0, 100);

+ 4 - 0
lib/MiLight/RgbCctPacketFormatter.h

@@ -11,6 +11,10 @@
 #define RGB_CCT_SATURATION_OFFSET 0xD
 #define RGB_CCT_KELVIN_OFFSET 0x94
 
+// Remotes have a larger range
+#define RGB_CCT_KELVIN_REMOTE_OFFSET 0x4C
+#define RGB_CCT_KELVIN_REMOTE_START  0xE8
+
 #ifndef _RGB_CCT_PACKET_FORMATTER_H
 #define _RGB_CCT_PACKET_FORMATTER_H