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

fix reversed range for color temp

Christopher Mullins 7 роки тому
батько
коміт
e82539f41f

+ 2 - 4
lib/MiLight/FUT091PacketFormatter.cpp

@@ -7,7 +7,7 @@ void FUT091PacketFormatter::updateBrightness(uint8_t value) {
 }
 
 void FUT091PacketFormatter::updateTemperature(uint8_t value) {
-  command(static_cast<uint8_t>(FUT091Command::KELVIN), V2PacketFormatter::tov2scale(value, 0xCD, 2));
+  command(static_cast<uint8_t>(FUT091Command::KELVIN), V2PacketFormatter::tov2scale(value, 0xCD, 2, false));
 }
 
 void FUT091PacketFormatter::enableNightMode() {
@@ -42,10 +42,8 @@ BulbId FUT091PacketFormatter::parsePacket(const uint8_t *packet, JsonObject& res
   } else if (command == (uint8_t)FUT091Command::BRIGHTNESS) {
     uint8_t level = V2PacketFormatter::fromv2scale(arg, 0x97, 2);
     result["brightness"] = Units::rescale<uint8_t, uint8_t>(level, 255, 100);
-  // saturation == kelvin. arg ranges are the same, so can't distinguish
-  // without using state
   } else if (command == (uint8_t)FUT091Command::KELVIN) {
-    uint8_t kelvin = V2PacketFormatter::fromv2scale(arg, 0xCD, 2);
+    uint8_t kelvin = V2PacketFormatter::fromv2scale(arg, 0xCD, 2, false);
     result["color_temp"] = Units::whiteValToMireds(kelvin, 100);
   } else {
     result["button_id"] = command;

+ 12 - 4
lib/MiLight/V2PacketFormatter.cpp

@@ -104,10 +104,18 @@ void V2PacketFormatter::switchMode(GroupState currentState, BulbMode desiredMode
   
 }
 
-uint8_t V2PacketFormatter::tov2scale(uint8_t value, uint8_t endValue, uint8_t interval) {
-  return ((100 - value) * interval) + endValue;
+uint8_t V2PacketFormatter::tov2scale(uint8_t value, uint8_t endValue, uint8_t interval, bool reverse) {
+  if (reverse) {
+    value = 100 - value;
+  }
+
+  return (value * interval) + endValue;
 }
 
-uint8_t V2PacketFormatter::fromv2scale(uint8_t value, uint8_t endValue, uint8_t interval) {
-  return 100 - (((value + (0x100 - endValue))%0x100) / 2);
+uint8_t V2PacketFormatter::fromv2scale(uint8_t value, uint8_t endValue, uint8_t interval, bool reverse) {
+  value = (((value + (0x100 - endValue))%0x100) / interval);
+  if (reverse) {
+    value = 100 - value;
+  }
+  return value;
 }

+ 2 - 2
lib/MiLight/V2PacketFormatter.h

@@ -33,8 +33,8 @@ public:
    *   0x8F, 0x8D, ..., 0, 0x2, ..., 0x20
    * This is a parameterized method to convert from [0, 100] TO this scale
    */
-  static uint8_t tov2scale(uint8_t value, uint8_t endValue, uint8_t interval);
-  static uint8_t fromv2scale(uint8_t value, uint8_t endValue, uint8_t interval);
+  static uint8_t tov2scale(uint8_t value, uint8_t endValue, uint8_t interval, bool reverse = true);
+  static uint8_t fromv2scale(uint8_t value, uint8_t endValue, uint8_t interval, bool reverse = true);
 
 protected:
   const uint8_t protocolId;