Просмотр исходного кода

extract unit conversion code; always send state

Chris Mullins лет назад: 8
Родитель
Сommit
f83bb2f172

+ 4 - 0
lib/MiLight/CctPacketFormatter.cpp

@@ -157,6 +157,10 @@ void CctPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result)
   if (onOffGroupId < 255) {
     result["state"] = cctCommandToStatus(command) == ON ? "ON" : "OFF";
   }
+
+  if (! result.containsKey("state")) {
+    result["state"] = "ON";
+  }
 }
 
 void CctPacketFormatter::format(uint8_t const* packet, char* buffer) {

+ 3 - 16
lib/MiLight/MiLightClient.cpp

@@ -2,9 +2,7 @@
 #include <MiLightRadioConfig.h>
 #include <Arduino.h>
 #include <RGBConverter.h>
-
-#define COLOR_TEMP_MAX_MIREDS 370
-#define COLOR_TEMP_MIN_MIREDS 153
+#include <Units.h>
 
 MiLightClient::MiLightClient(MiLightRadioFactory* radioFactory)
   : resendCount(MILIGHT_DEFAULT_RESEND_COUNT),
@@ -295,20 +293,9 @@ void MiLightClient::update(const JsonObject& request) {
   }
   // HomeAssistant
   if (request.containsKey("color_temp")) {
-    // MiLight CCT bulbs range from 2700K-6500K, or ~370.3-153.8 mireds. Note
-    // that mireds are inversely correlated with color temperature.
-    uint32_t tempMireds = request["color_temp"];
-    tempMireds = tempMireds > COLOR_TEMP_MAX_MIREDS ? COLOR_TEMP_MAX_MIREDS : tempMireds;
-    tempMireds = tempMireds < COLOR_TEMP_MIN_MIREDS ? COLOR_TEMP_MIN_MIREDS : tempMireds;
-
-    uint8_t scaledTemp = round(
-      100*
-      (tempMireds - COLOR_TEMP_MIN_MIREDS)
-        /
-      static_cast<double>(COLOR_TEMP_MAX_MIREDS - COLOR_TEMP_MIN_MIREDS)
+    this->updateTemperature(
+      Units::miredsToBrightness(request["color_temp"], 100)
     );
-
-    this->updateTemperature(100 - scaledTemp);
   }
 
   if (request.containsKey("mode")) {

+ 0 - 5
lib/MiLight/PacketFormatter.h

@@ -71,11 +71,6 @@ public:
 
   static void formatV1Packet(uint8_t const* packet, char* buffer);
 
-  template <typename T, typename V>
-  static T rescale(T value, V newMax, float oldMax = 255.0) {
-    return round(value * (newMax / oldMax));
-  }
-
   size_t getPacketLength() const;
 
 protected:

+ 8 - 3
lib/MiLight/RgbCctPacketFormatter.cpp

@@ -1,4 +1,5 @@
 #include <RgbCctPacketFormatter.h>
+#include <Units.h>
 
 #define V2_OFFSET(byte, key, jumpStart) ( \
   pgm_read_byte(&V2_OFFSETS[byte-1][key%4]) \
@@ -80,7 +81,7 @@ void RgbCctPacketFormatter::updateBrightness(uint8_t brightness) {
 }
 
 void RgbCctPacketFormatter::updateHue(uint16_t value) {
-  uint8_t remapped = rescale(value, 255, 360);
+  uint8_t remapped = Units::rescale(value, 255, 360);
   updateColorRaw(remapped);
 }
 
@@ -133,7 +134,7 @@ void RgbCctPacketFormatter::parsePacket(const uint8_t *packet, JsonObject& resul
     }
   } else if (command == RGB_CCT_COLOR) {
     uint8_t rescaledColor = (arg - RGB_CCT_COLOR_OFFSET) % 0x100;
-    uint16_t hue = rescale<uint16_t, uint16_t>(rescaledColor, 360, 255.0);
+    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;
@@ -143,10 +144,14 @@ void RgbCctPacketFormatter::parsePacket(const uint8_t *packet, JsonObject& resul
   // 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);
-    result["brightness"] = rescale<uint8_t, uint8_t>(level, 255, 100);
+    result["brightness"] = Units::rescale<uint8_t, uint8_t>(level, 255, 100);
   } else if (command == RGB_CCT_SATURATION) {
     result["saturation"] = constrain(arg - RGB_CCT_SATURATION_OFFSET, 0, 100);
   }
+
+  if (! result.containsKey("state")) {
+    result["state"] = "ON";
+  }
 }
 
 uint8_t RgbCctPacketFormatter::xorKey(uint8_t key) {

+ 7 - 2
lib/MiLight/RgbPacketFormatter.cpp

@@ -1,4 +1,5 @@
 #include <RgbPacketFormatter.h>
+#include <Units.h>
 
 void RgbPacketFormatter::initializePacket(uint8_t *packet) {
   size_t packetPtr = 0;
@@ -37,7 +38,7 @@ void RgbPacketFormatter::command(uint8_t command, uint8_t arg) {
 
 void RgbPacketFormatter::updateHue(uint16_t value) {
   const int16_t remappedColor = (value + 40) % 360;
-  updateColorRaw(rescale(remappedColor, 255, 360));
+  updateColorRaw(Units::rescale(remappedColor, 255, 360));
 }
 
 void RgbPacketFormatter::updateColorRaw(uint8_t value) {
@@ -90,10 +91,14 @@ void RgbPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result)
   } else if (command == RGB_OFF) {
     result["state"] = "OFF";
   } else if (command == 0) {
-    uint16_t remappedColor = rescale<uint16_t, uint16_t>(packet[RGB_COLOR_INDEX], 360.0, 255.0);
+    uint16_t remappedColor = Units::rescale<uint16_t, uint16_t>(packet[RGB_COLOR_INDEX], 360.0, 255.0);
     remappedColor = (remappedColor + 320) % 360;
     result["hue"] = remappedColor;
   }
+
+  if (! result.containsKey("state")) {
+    result["state"] = "ON";
+  }
 }
 
 void RgbPacketFormatter::format(uint8_t const* packet, char* buffer) {

+ 9 - 4
lib/MiLight/RgbwPacketFormatter.cpp

@@ -1,5 +1,6 @@
 #include <RgbwPacketFormatter.h>
 #include <MiLightButtons.h>
+#include <Units.h>
 
 #define STATUS_COMMAND(status, groupId) ( RGBW_GROUP_1_ON + ((groupId - 1)*2) + status )
 
@@ -49,7 +50,7 @@ void RgbwPacketFormatter::updateStatus(MiLightStatus status, uint8_t groupId) {
 
 void RgbwPacketFormatter::updateBrightness(uint8_t value) {
   // Expect an input value in [0, 100]. Map it down to [0, 25].
-  const uint8_t adjustedBrightness = rescale(value, 25, 100);
+  const uint8_t adjustedBrightness = Units::rescale(value, 25, 100);
 
   // The actual protocol uses a bizarre range where min is 16, max is 23:
   // [16, 15, ..., 0, 31, ..., 23]
@@ -71,7 +72,7 @@ void RgbwPacketFormatter::command(uint8_t command, uint8_t arg) {
 
 void RgbwPacketFormatter::updateHue(uint16_t value) {
   const int16_t remappedColor = (value + 40) % 360;
-  updateColorRaw(rescale(remappedColor, 255, 360));
+  updateColorRaw(Units::rescale(remappedColor, 255, 360));
 }
 
 void RgbwPacketFormatter::updateColorRaw(uint8_t value) {
@@ -105,12 +106,16 @@ void RgbwPacketFormatter::parsePacket(const uint8_t* packet, JsonObject& result)
     brightness -= packet[RGBW_BRIGHTNESS_GROUP_INDEX] >> 3;
     brightness += 17;
     brightness %= 32;
-    result["level"] = rescale<uint8_t, uint8_t>(brightness, 100, 25);
+    result["level"] = Units::rescale<uint8_t, uint8_t>(brightness, 100, 25);
   } else if (command == RGBW_COLOR) {
-    uint16_t remappedColor = rescale<uint16_t, uint16_t>(packet[RGBW_COLOR_INDEX], 360.0, 255.0);
+    uint16_t remappedColor = Units::rescale<uint16_t, uint16_t>(packet[RGBW_COLOR_INDEX], 360.0, 255.0);
     remappedColor = (remappedColor + 320) % 360;
     result["hue"] = remappedColor;
   }
+
+  if (! result.containsKey("state")) {
+    result["state"] = "ON";
+  }
 }
 
 void RgbwPacketFormatter::format(uint8_t const* packet, char* buffer) {