Christopher Mullins 7 anos atrás
pai
commit
69a2765065

+ 7 - 4
lib/MiLight/FUT091PacketFormatter.cpp

@@ -2,12 +2,15 @@
 #include <V2RFEncoding.h>
 #include <Units.h>
 
+static const uint8_t BRIGHTNESS_SCALE_MAX = 0x97;
+static const uint8_t KELVIN_SCALE_MAX = 0xCD;
+
 void FUT091PacketFormatter::updateBrightness(uint8_t value) {
-  command(static_cast<uint8_t>(FUT091Command::BRIGHTNESS), V2PacketFormatter::tov2scale(value, 0x97, 2));
+  command(static_cast<uint8_t>(FUT091Command::BRIGHTNESS), V2PacketFormatter::tov2scale(value, BRIGHTNESS_SCALE_MAX, 2));
 }
 
 void FUT091PacketFormatter::updateTemperature(uint8_t value) {
-  command(static_cast<uint8_t>(FUT091Command::KELVIN), V2PacketFormatter::tov2scale(value, 0xCD, 2, false));
+  command(static_cast<uint8_t>(FUT091Command::KELVIN), V2PacketFormatter::tov2scale(value, KELVIN_SCALE_MAX, 2, false));
 }
 
 void FUT091PacketFormatter::enableNightMode() {
@@ -40,10 +43,10 @@ BulbId FUT091PacketFormatter::parsePacket(const uint8_t *packet, JsonObject& res
       bulbId.groupId = arg-5;
     }
   } else if (command == (uint8_t)FUT091Command::BRIGHTNESS) {
-    uint8_t level = V2PacketFormatter::fromv2scale(arg, 0x97, 2);
+    uint8_t level = V2PacketFormatter::fromv2scale(arg, BRIGHTNESS_SCALE_MAX, 2, true, 0x13);
     result["brightness"] = Units::rescale<uint8_t, uint8_t>(level, 255, 100);
   } else if (command == (uint8_t)FUT091Command::KELVIN) {
-    uint8_t kelvin = V2PacketFormatter::fromv2scale(arg, 0xCD, 2, false);
+    uint8_t kelvin = V2PacketFormatter::fromv2scale(arg, KELVIN_SCALE_MAX, 2, false, 0x13);
     result["color_temp"] = Units::whiteValToMireds(kelvin, 100);
   } else {
     result["button_id"] = command;

+ 10 - 1
lib/MiLight/V2PacketFormatter.cpp

@@ -112,10 +112,19 @@ uint8_t V2PacketFormatter::tov2scale(uint8_t value, uint8_t endValue, uint8_t in
   return (value * interval) + endValue;
 }
 
-uint8_t V2PacketFormatter::fromv2scale(uint8_t value, uint8_t endValue, uint8_t interval, bool reverse) {
+uint8_t V2PacketFormatter::fromv2scale(uint8_t value, uint8_t endValue, uint8_t interval, bool reverse, uint8_t buffer) {
   value = (((value + (0x100 - endValue))%0x100) / interval);
   if (reverse) {
     value = 100 - value;
   }
+  if (value > 100) {
+    // overflow
+    if (value <= (100 + buffer)) {
+      value = 100;
+    // underflow (value is unsigned)
+    } else {
+      value = 0;
+    }
+  }
   return value;
 }

+ 8 - 1
lib/MiLight/V2PacketFormatter.h

@@ -34,7 +34,14 @@ public:
    * 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, bool reverse = true);
-  static uint8_t fromv2scale(uint8_t value, uint8_t endValue, uint8_t interval, bool reverse = true);
+
+  /*
+   * Method to convert FROM the scale described above to [0, 100].
+   * 
+   * An extra parameter is exposed: `buffer`, which allows for a range of values before/after the 
+   * max that will be mapped to 0 and 100, respectively.
+   */
+  static uint8_t fromv2scale(uint8_t value, uint8_t endValue, uint8_t interval, bool reverse = true, uint8_t buffer = 0);
 
 protected:
   const uint8_t protocolId;