Przeglądaj źródła

Use V2 range helpers

Christopher Mullins 7 lat temu
rodzic
commit
f5443ace17

+ 2 - 2
lib/MiLight/FUT091PacketFormatter.cpp

@@ -43,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, BRIGHTNESS_SCALE_MAX, 2, true, 0x13);
+    uint8_t level = V2PacketFormatter::fromv2scale(arg, BRIGHTNESS_SCALE_MAX, 2, true);
     result["brightness"] = Units::rescale<uint8_t, uint8_t>(level, 255, 100);
   } else if (command == (uint8_t)FUT091Command::KELVIN) {
-    uint8_t kelvin = V2PacketFormatter::fromv2scale(arg, KELVIN_SCALE_MAX, 2, false, 0x13);
+    uint8_t kelvin = V2PacketFormatter::fromv2scale(arg, KELVIN_SCALE_MAX, 2, false);
     result["color_temp"] = Units::whiteValToMireds(kelvin, 100);
   } else {
     result["button_id"] = command;

+ 3 - 20
lib/MiLight/RgbCctPacketFormatter.cpp

@@ -40,11 +40,7 @@ void RgbCctPacketFormatter::updateColorRaw(uint8_t value) {
 void RgbCctPacketFormatter::updateTemperature(uint8_t value) {
   // Packet scale is [0x94, 0x92, .. 0, .., 0xCE, 0xCC]. Increments of 2.
   // From coolest to warmest.
-  // To convert from [0, 100] scale:
-  //   * Multiply by 2
-  //   * Reverse direction (increasing values should be cool -> warm)
-  //   * Start scale at 0xCC
-  uint8_t cmdValue = ((100 - value) * 2) + RGB_CCT_KELVIN_REMOTE_END;
+  uint8_t cmdValue = V2PacketFormatter::tov2scale(value, RGB_CCT_KELVIN_REMOTE_END, 2);
 
   // when updating temperature, the bulb switches to white.  If we are not already
   // in white mode, that makes changing temperature annoying because the current hue/mode
@@ -87,7 +83,7 @@ void RgbCctPacketFormatter::updateColorWhite() {
   // there is no direct white command, so let's look up our prior temperature and set that, which
   // causes the bulb to go white 
   GroupState ourState = this->stateStore->get(this->deviceId, this->groupId, REMOTE_TYPE_RGB_CCT);
-  uint8_t value = ((100 - ourState.getKelvin()) * 2) + RGB_CCT_KELVIN_REMOTE_END;
+  uint8_t value = V2PacketFormatter::tov2scale(ourState.getKelvin(), RGB_CCT_KELVIN_REMOTE_END, 2);
 
   // issue command to set kelvin to prior value, which will drive to white
   command(RGB_CCT_KELVIN, value);
@@ -131,20 +127,7 @@ BulbId RgbCctPacketFormatter::parsePacket(const uint8_t *packet, JsonObject& res
     uint16_t hue = Units::rescale<uint16_t, uint16_t>(rescaledColor, 360, 255.0);
     result["hue"] = hue;
   } else if (command == RGB_CCT_KELVIN) {
-    // Packet range is [0x94, 0x92, ..., 0xCC]. Remote sends values outside this
-    // range, so normalize.
-    uint8_t temperature = arg;
-    if (arg < 0xCC && arg >= 0xB0) {
-      temperature = 0xCC;
-    } else if (arg > 0x94 && arg <= 0xAF) {
-      temperature = 0x94;
-    }
-
-    temperature = (temperature + (0x100 - RGB_CCT_KELVIN_REMOTE_END)) % 0x100;
-    temperature /= 2;
-    temperature = (100 - temperature);
-    temperature = constrain(temperature, 0, 100);
-
+    uint8_t temperature = V2PacketFormatter::fromv2scale(arg, RGB_CCT_KELVIN_REMOTE_END, 2);
     result["color_temp"] = Units::whiteValToMireds(temperature, 100);
   // brightness == saturation
   } else if (command == RGB_CCT_BRIGHTNESS && arg >= (RGB_CCT_BRIGHTNESS_OFFSET - 15)) {

+ 4 - 1
lib/MiLight/V2PacketFormatter.h

@@ -10,6 +10,9 @@
 #define V2_COMMAND_INDEX 4
 #define V2_ARGUMENT_INDEX 5
 
+// Default number of values to allow before and after strictly defined range for V2 scales
+#define V2_DEFAULT_RANGE_BUFFER 0x13
+
 class V2PacketFormatter : public PacketFormatter {
 public:
   V2PacketFormatter(uint8_t protocolId, uint8_t numGroups);
@@ -41,7 +44,7 @@ public:
    * 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);
+  static uint8_t fromv2scale(uint8_t value, uint8_t endValue, uint8_t interval, bool reverse = true, uint8_t buffer = V2_DEFAULT_RANGE_BUFFER);
 
 protected:
   const uint8_t protocolId;