Skip to content

Commit

Permalink
(2.4.4) Fix discovery
Browse files Browse the repository at this point in the history
Improve white color handling
  • Loading branch information
Aircoookie committed Jan 6, 2020
1 parent fa40ecc commit 247afb1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Espalexa
version=2.4.3
version=2.4.4
author=Christian Schwinne
maintainer=Christian Schwinne
sentence=Library to control an ESP module with the Alexa voice assistant
Expand Down
12 changes: 6 additions & 6 deletions src/Espalexa.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
/*
* @title Espalexa library
* @version 2.4.3
* @version 2.4.4
* @author Christian Schwinne
* @license MIT
* @contributors d-999
Expand Down Expand Up @@ -49,7 +49,7 @@
#include <WiFiUdp.h>

#ifdef ESPALEXA_DEBUG
#pragma message "Espalexa 2.4.3 debug mode"
#pragma message "Espalexa 2.4.4 debug mode"
#define EA_DEBUG(x) Serial.print (x)
#define EA_DEBUGLN(x) Serial.println (x)
#else
Expand Down Expand Up @@ -164,7 +164,7 @@ class Espalexa {
json += "\",\"modelid\":\"" + modelidString(dev->getType());
json += "\",\"manufacturername\":\"Philips\",\"productname\":\"E" + String(static_cast<uint8_t>(dev->getType()));
json += "\",\"uniqueid\":\"" + String(encodeLightId(deviceId+1));
json += "\",\"swversion\":\"espalexa-2.4.3\"}";
json += "\",\"swversion\":\"espalexa-2.4.4\"}";

return json;
}
Expand All @@ -188,7 +188,7 @@ class Espalexa {
}
res += "\r\nFree Heap: " + (String)ESP.getFreeHeap();
res += "\r\nUptime: " + (String)millis();
res += "\r\n\r\nEspalexa library v2.4.3 by Christian Schwinne 2019";
res += "\r\n\r\nEspalexa library v2.4.4 by Christian Schwinne 2020";
server->send(200, "text/plain", res);
}
#endif
Expand Down Expand Up @@ -298,7 +298,7 @@ class Espalexa {
"SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/1.17.0\r\n" // _modelName, _modelNumber
"hue-bridgeid: "+ escapedMac +"\r\n"
"ST: urn:schemas-upnp-org:device:basic:1\r\n" // _deviceType
"USN: uuid:2f402f80-da50-11e1-9b23-"+ escapedMac +"::upnp:rootdevice\r\n" // _uuid::_deviceType
"USN: uuid:2f402f80-da50-11e1-9b23-"+ escapedMac +"::ssdp:all\r\n" // _uuid::_deviceType
"\r\n";

espalexaUdp.beginPacket(espalexaUdp.remoteIP(), espalexaUdp.remotePort());
Expand Down Expand Up @@ -372,7 +372,7 @@ class Espalexa {
String request = packetBuffer;
if(request.indexOf("M-SEARCH") >= 0) {
EA_DEBUGLN(request);
if(request.indexOf("upnp:rootdevice") > 0 || request.indexOf("asic:1") > 0) {
if(request.indexOf("upnp:rootdevice") > 0 || request.indexOf("asic:1") > 0 || request.indexOf("ssdp:all") > 0) {
EA_DEBUGLN("Responding search req...");
respondToSearch();
}
Expand Down
60 changes: 38 additions & 22 deletions src/EspalexaDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ uint32_t EspalexaDevice::getKelvin()
uint32_t EspalexaDevice::getRGB()
{
if (_rgb != 0) return _rgb; //color has not changed
uint8_t rgb[3];
float r, g, b;
byte rgb[4]{0, 0, 0, 0};
float r, g, b, w;

if (_mode == EspalexaColorMode::none) return 0;

Expand All @@ -122,27 +122,37 @@ uint32_t EspalexaDevice::getRGB()
float temp = 10000/ _ct; //kelvins = 1,000,000/mired (and that /100)
float r, g, b;

if( temp <= 66 ){
r = 255;
g = temp;
g = 99.470802 * log(g) - 161.119568;
if( temp <= 19){
b = 0;
} else {
b = temp-10;
b = 138.517731 * log(b) - 305.044793;
// Cold white to warm white receiving from Alexa: _ct = 199, 234, 284, 350, 383 (from cold white to warm white)
switch (_ct) {
case 199: rgb[0]=255,rgb[1]=255,rgb[2]=255;rgb[3]=255;break;
case 234: rgb[0]=127,rgb[1]=127,rgb[2]=127;rgb[3]=255;break;
case 284: rgb[0]=0,rgb[1]=0,rgb[2]=0;rgb[3]=255;break;
case 350: rgb[0]=130,rgb[1]=90,rgb[2]=0;rgb[3]=255;break;
case 383: rgb[0]=255,rgb[1]=153,rgb[2]=0;rgb[3]=255;break;
default: {
if( temp <= 66 ){
r = 255;
g = temp;
g = 99.470802 * log(g) - 161.119568;
if( temp <= 19){
b = 0;
} else {
b = temp-10;
b = 138.517731 * log(b) - 305.044793;
}
} else {
r = temp - 60;
r = 329.698727 * pow(r, -0.13320476);
g = temp - 60;
g = 288.12217 * pow(g, -0.07551485 );
b = 255;
}

rgb[0] = (byte)constrain(r,0.1,255.1);
rgb[1] = (byte)constrain(g,0.1,255.1);
rgb[2] = (byte)constrain(b,0.1,255.1);
}
} else {
r = temp - 60;
r = 329.698727 * pow(r, -0.13320476);
g = temp - 60;
g = 288.12217 * pow(g, -0.07551485 );
b = 255;
}

rgb[0] = (byte)constrain(r,0.1,255.1);
rgb[1] = (byte)constrain(g,0.1,255.1);
rgb[2] = (byte)constrain(b,0.1,255.1);
} else if (_mode == EspalexaColorMode::hs)
{
float h = ((float)_hue)/65535.0;
Expand Down Expand Up @@ -216,10 +226,16 @@ uint32_t EspalexaDevice::getRGB()
rgb[1] = 255.0*g;
rgb[2] = 255.0*b;
}
_rgb = ((rgb[0] << 16) | (rgb[1] << 8) | (rgb[2]));
_rgb = ((rgb[3] << 24) | (rgb[0] << 16) | (rgb[1] << 8) | (rgb[2])); //white value is only >0 if Alexa did provide a CT value, RGB colors will not be touched.
return _rgb;
}

//white channel for RGBW lights. Always 0 unless colormode is ct
uint8_t EspalexaDevice::getW()
{
return (getRGB() >> 24) & 0xFF;
}

uint8_t EspalexaDevice::getR()
{
return (getRGB() >> 16) & 0xFF;
Expand Down
1 change: 1 addition & 0 deletions src/EspalexaDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class EspalexaDevice {
uint8_t getR();
uint8_t getG();
uint8_t getB();
uint8_t getW();
EspalexaColorMode getColorMode();
EspalexaDeviceType getType();

Expand Down

0 comments on commit 247afb1

Please sign in to comment.