From 6618f8aebf49c9d25e3d4b4c5435241c5b56bd9e Mon Sep 17 00:00:00 2001 From: Mark Hendriksen Date: Mon, 5 Aug 2024 20:34:50 +0200 Subject: [PATCH] Add rgb_multiplier to Generic_RGB_CCT_Light.ino Requested in issue https://github.com/diyhue/Lights/issues/149. --- .../Generic_RGB_CCT_Light.ino | 60 +++++++++++++++---- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/ESP8266/Generic_RGB_CCT_Light/Generic_RGB_CCT_Light.ino b/ESP8266/Generic_RGB_CCT_Light/Generic_RGB_CCT_Light.ino index 655ff48..e47943d 100644 --- a/ESP8266/Generic_RGB_CCT_Light/Generic_RGB_CCT_Light.ino +++ b/ESP8266/Generic_RGB_CCT_Light/Generic_RGB_CCT_Light.ino @@ -37,6 +37,7 @@ unsigned long lastEPMillis; char *lightName = "New Hue RGB-CCT light"; uint8_t scene = 0, startup = false, onPin = 1, offPin = 3, pins[] = {12, 13, 14, 4, 5}; //red, green, blue, could white, warm white bool hwSwitch = false; +uint8_t rgb_multiplier[] = {100, 100, 100}; // light multiplier in percentage /R, G, B/ ESP8266WebServer server(80); WiFiUDP Udp; @@ -123,15 +124,34 @@ void convert_xy() g = g <= 0.0031308f ? 12.92f * g : (1.0f + 0.055f) * pow(g, (1.0f / 2.4f)) - 0.055f; b = b <= 0.0031308f ? 12.92f * b : (1.0f + 0.055f) * pow(b, (1.0f / 2.4f)) - 0.055f; - float maxv = 0;// calc the maximum value of r g and b - if (r > maxv) maxv = r; - if (g > maxv) maxv = g; - if (b > maxv) maxv = b; - - if (maxv > 0) {// only if maximum value is greater than zero, otherwise there would be division by zero - r /= maxv; // scale to maximum so the brightest light is always 1.0 - g /= maxv; - b /= maxv; + // Apply multiplier for white correction + r = r * rgb_multiplier[0] / 100; + g = g * rgb_multiplier[1] / 100; + b = b * rgb_multiplier[2] / 100; + + if (r > b && r > g) { + // red is biggest + if (r > 1.0f) { + g = g / r; + b = b / r; + r = 1.0f; + } + } + else if (g > b && g > r) { + // green is biggest + if (g > 1.0f) { + r = r / g; + b = b / g; + g = 1.0f; + } + } + else if (b > r && b > g) { + // blue is biggest + if (b > 1.0f) { + r = r / b; + g = g / b; + b = 1.0f; + } } r = r < 0 ? 0 : r; @@ -333,6 +353,9 @@ bool saveConfig() { json["off"] = offPin; json["hw"] = hwSwitch; json["dhcp"] = useDhcp; + json["rpct"] = rgb_multiplier[0]; + json["gpct"] = rgb_multiplier[1]; + json["bpct"] = rgb_multiplier[2]; JsonArray addr = json.createNestedArray("addr"); addr.add(address[0]); addr.add(address[1]); @@ -385,6 +408,11 @@ bool loadConfig() { pins[2] = (uint8_t) json["b"]; pins[3] = (uint8_t) json["c"]; pins[4] = (uint8_t) json["w"]; + if (json.containsKey("rpct")) { + rgb_multiplier[0] = (uint8_t) json["rpct"]; + rgb_multiplier[1] = (uint8_t) json["gpct"]; + rgb_multiplier[2] = (uint8_t) json["bpct"]; + } onPin = (uint8_t) json["on"]; offPin = (uint8_t) json["off"]; hwSwitch = json["hw"]; @@ -411,8 +439,8 @@ void handleNotFound() { } void setup() { - //Serial.begin(9600); - //Serial.println(); + Serial.begin(74880); + Serial.println(); delay(1000); //Serial.println("mounting FS..."); @@ -598,6 +626,9 @@ void setup() { root["blue"] = pins[2]; root["cw"] = pins[3]; root["ww"] = pins[4]; + root["rpct"] = rgb_multiplier[0]; + root["gpct"] = rgb_multiplier[1]; + root["bpct"] = rgb_multiplier[2]; root["hw"] = hwSwitch; root["on"] = onPin; root["off"] = offPin; @@ -621,6 +652,9 @@ void setup() { pins[2] = server.arg("blue").toInt(); pins[3] = server.arg("cw").toInt(); pins[4] = server.arg("ww").toInt(); + rgb_multiplier[0] = server.arg("rpct").toInt(); + rgb_multiplier[1] = server.arg("gpct").toInt(); + rgb_multiplier[2] = server.arg("bpct").toInt(); hwSwitch = server.arg("hwswitch").toInt(); onPin = server.arg("on").toInt(); offPin = server.arg("off").toInt(); @@ -633,7 +667,7 @@ void setup() { saveConfig(); } - const char * htmlContent = " Hue Light




"; + const char * htmlContent = " Hue Light




"; server.send(200, "text/html", htmlContent); if (server.args()) { delay(100); @@ -667,7 +701,7 @@ void entertainment() { Udp.read(packetBuffer, packetSize); for (uint8_t color = 0; color < 3; color++) { light.currentColors[color] = packetBuffer[color + 1]; - analogWrite(pins[color], (int)(packetBuffer[color + 1] * 4)); + analogWrite(pins[color], (int)(packetBuffer[color + 1] * 4 * (rgb_multiplier[color] / 100))); } } }