Skip to content
This repository was archived by the owner on Dec 8, 2019. It is now read-only.

Commit 0d25c0c

Browse files
add support for color_temp
1 parent 157ce9f commit 0d25c0c

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

Diff for: ESP_MQTT_Digital_LEDs/ESP_MQTT_Digital_LEDs.ino

+63
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,15 @@ bool processJson(char* message) {
381381
green = root["color"]["g"];
382382
blue = root["color"]["b"];
383383
}
384+
385+
if (root.containsKey("color_temp")) {
386+
//temp comes in as mireds, need to convert to kelvin then to RGB
387+
int color_temp = root["color_temp"];
388+
unsigned int kelvin = MILLION / color_temp;
389+
390+
temp2rgb(kelvin);
391+
392+
}
384393

385394
if (root.containsKey("brightness")) {
386395
brightness = root["brightness"];
@@ -1046,3 +1055,57 @@ void showleds() {
10461055
startFade = false;
10471056
}
10481057
}
1058+
void temp2rgb(unsigned int kelvin) {
1059+
int tmp_internal = kelvin / 100.0;
1060+
1061+
// red
1062+
if (tmp_internal <= 66) {
1063+
red = 255;
1064+
} else {
1065+
float tmp_red = 329.698727446 * pow(tmp_internal - 60, -0.1332047592);
1066+
if (tmp_red < 0) {
1067+
red = 0;
1068+
} else if (tmp_red > 255) {
1069+
red = 255;
1070+
} else {
1071+
red = tmp_red;
1072+
}
1073+
}
1074+
1075+
// green
1076+
if (tmp_internal <=66){
1077+
float tmp_green = 99.4708025861 * log(tmp_internal) - 161.1195681661;
1078+
if (tmp_green < 0) {
1079+
green = 0;
1080+
} else if (tmp_green > 255) {
1081+
green = 255;
1082+
} else {
1083+
green = tmp_green;
1084+
}
1085+
} else {
1086+
float tmp_green = 288.1221695283 * pow(tmp_internal - 60, -0.0755148492);
1087+
if (tmp_green < 0) {
1088+
green = 0;
1089+
} else if (tmp_green > 255) {
1090+
green = 255;
1091+
} else {
1092+
green = tmp_green;
1093+
}
1094+
}
1095+
1096+
// blue
1097+
if (tmp_internal >=66) {
1098+
blue = 255;
1099+
} else if (tmp_internal <= 19) {
1100+
blue = 0;
1101+
} else {
1102+
float tmp_blue = 138.5177312231 * log(tmp_internal - 10) - 305.0447927307;
1103+
if (tmp_blue < 0) {
1104+
blue = 0;
1105+
} else if (tmp_blue > 255) {
1106+
blue = 255;
1107+
} else {
1108+
blue = tmp_blue;
1109+
}
1110+
}
1111+
}

0 commit comments

Comments
 (0)