Skip to content

Commit

Permalink
send HASS-compatable kelvin value; fix inverted scales for mireds/kel…
Browse files Browse the repository at this point in the history
…vins
  • Loading branch information
Chris Mullins committed Jul 4, 2017
1 parent ddfe239 commit bdbd868
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
11 changes: 5 additions & 6 deletions lib/Helpers/Units.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ class Units {
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(
Expand All @@ -26,12 +25,12 @@ class Units {
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;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/MiLight/MiLightClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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)
);
}

Expand Down
20 changes: 16 additions & 4 deletions lib/MiLight/RgbCctPacketFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions lib/MiLight/RgbCctPacketFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit bdbd868

Please sign in to comment.