mirror of
https://github.com/anklimov/lighthub
synced 2025-12-12 14:49:50 +03:00
Color temperature
This commit is contained in:
@@ -510,6 +510,9 @@ st.setSuffix(suffixCode);
|
|||||||
case S_SAT:
|
case S_SAT:
|
||||||
st.setS(Par[0]);
|
st.setS(Par[0]);
|
||||||
break;
|
break;
|
||||||
|
case S_TEMP:
|
||||||
|
st.setColorTemp(Par[0]);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
switch (i) //Number of params
|
switch (i) //Number of params
|
||||||
{
|
{
|
||||||
@@ -584,7 +587,7 @@ int Item::Ctrl(itemCmd cmd, char* subItem)
|
|||||||
suffixCode = defaultSuffixCode;
|
suffixCode = defaultSuffixCode;
|
||||||
|
|
||||||
|
|
||||||
debugSerial<<F("RAM=")<<freeRam()<<F(" Item=")<<itemArr->name<<F(" Sub=")<<subItem;
|
debugSerial<<F("RAM=")<<freeRam()<<F(" Item=")<<itemArr->name<<F(" Sub=")<<subItem<<F(" Cmd=");
|
||||||
cmd.debugOut();
|
cmd.debugOut();
|
||||||
if (!itemArr) return -1;
|
if (!itemArr) return -1;
|
||||||
|
|
||||||
@@ -739,6 +742,11 @@ int Item::Ctrl(itemCmd cmd, char* subItem)
|
|||||||
st.saveItem(this);
|
st.saveItem(this);
|
||||||
SendStatus(SEND_PARAMETERS | SEND_DEFFERED);
|
SendStatus(SEND_PARAMETERS | SEND_DEFFERED);
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
case S_TEMP:
|
||||||
|
st.setColorTemp(cmd.getColorTemp());
|
||||||
|
st.saveItem(this);
|
||||||
|
SendStatus(SEND_PARAMETERS | SEND_DEFFERED);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -160,24 +160,24 @@ bool itemCmd::setS(uint8_t s)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
//! Setup color tempetature parameter for HSV or HSV255 types. It must be 0..100 value.
|
//! Setup color tempetature parameter for HSV or HSV255 types. It must be 153..500 (mireds) value.
|
||||||
//! 0 - cold, 100 - warm light
|
//! Internally 1 - cold, 101 - warm light
|
||||||
bool itemCmd::setColorTemp(uint8_t t)
|
bool itemCmd::setColorTemp(int t)
|
||||||
{
|
{
|
||||||
int par=t;
|
int par=map(t,153,500,0,100);
|
||||||
switch (cmd.itemArgType)
|
switch (cmd.itemArgType)
|
||||||
{
|
{
|
||||||
case ST_VOID:
|
case ST_VOID:
|
||||||
cmd.itemArgType=ST_HSV;
|
cmd.itemArgType=ST_HSV;
|
||||||
case ST_HSV:
|
case ST_HSV:
|
||||||
case ST_HS:
|
case ST_HS:
|
||||||
if (par>100) par=100;
|
case ST_PERCENTS:
|
||||||
// value 0 is reserved for default/uninitialized value. Internally data stored in 1..101 range (7 bits)
|
case ST_PERCENTS255:
|
||||||
param.colorTemp=par+1;
|
|
||||||
break;
|
|
||||||
case ST_HSV255:
|
case ST_HSV255:
|
||||||
if (par>100) par=100;
|
if (par>100) par=100;
|
||||||
if (par<0) par=0;
|
if (par<0) par=0;
|
||||||
|
//debugSerial<<F("Assign color temp:")<<par<<endl;
|
||||||
|
// value 0 is reserved for default/uninitialized value. Internally data stored in 1..101 range (7 bits)
|
||||||
param.colorTemp=par+1;
|
param.colorTemp=par+1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -188,10 +188,22 @@ bool itemCmd::setColorTemp(uint8_t t)
|
|||||||
|
|
||||||
//! Setup color tempetature parameter from HSV or HSV255 types. return 0..100 value in success.
|
//! Setup color tempetature parameter from HSV or HSV255 types. return 0..100 value in success.
|
||||||
//! -1 - if no value stored
|
//! -1 - if no value stored
|
||||||
int8_t itemCmd::getColorTemp()
|
int itemCmd::getColorTemp()
|
||||||
{
|
{
|
||||||
if (cmd.itemArgType!=ST_HSV and cmd.itemArgType!=ST_HSV255 and cmd.itemArgType!=ST_HS) return -1;
|
if (!param.colorTemp) return -1;
|
||||||
return param.colorTemp-1;
|
|
||||||
|
switch (cmd.itemArgType)
|
||||||
|
{
|
||||||
|
case ST_HSV:
|
||||||
|
case ST_HS:
|
||||||
|
case ST_PERCENTS:
|
||||||
|
case ST_PERCENTS255:
|
||||||
|
case ST_HSV255:
|
||||||
|
|
||||||
|
return map(param.colorTemp-1,0,100,153,500);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t itemCmd::getH()
|
uint16_t itemCmd::getH()
|
||||||
@@ -273,8 +285,15 @@ itemCmd itemCmd::assignFrom(itemCmd from)
|
|||||||
{
|
{
|
||||||
bool RGBW_flag = false;
|
bool RGBW_flag = false;
|
||||||
bool HSV255_flag = false;
|
bool HSV255_flag = false;
|
||||||
cmd.suffixCode=from.cmd.suffixCode;
|
int t=from.getColorTemp();
|
||||||
|
|
||||||
|
if (t>=0)
|
||||||
|
{
|
||||||
|
setColorTemp(t);
|
||||||
|
}
|
||||||
|
cmd.suffixCode=from.cmd.suffixCode;
|
||||||
|
|
||||||
|
|
||||||
switch (cmd.itemArgType){ //Destination
|
switch (cmd.itemArgType){ //Destination
|
||||||
case ST_HSV:
|
case ST_HSV:
|
||||||
case ST_PERCENTS:
|
case ST_PERCENTS:
|
||||||
@@ -293,6 +312,7 @@ itemCmd itemCmd::assignFrom(itemCmd from)
|
|||||||
case ST_HS:
|
case ST_HS:
|
||||||
param.h=from.param.h;
|
param.h=from.param.h;
|
||||||
param.s=from.param.s;
|
param.s=from.param.s;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case ST_PERCENTS:
|
case ST_PERCENTS:
|
||||||
param.v=from.param.v;
|
param.v=from.param.v;
|
||||||
@@ -325,9 +345,9 @@ itemCmd itemCmd::assignFrom(itemCmd from)
|
|||||||
param.b=from.param.b;
|
param.b=from.param.b;
|
||||||
cmd.itemArgType=from.cmd.itemArgType;
|
cmd.itemArgType=from.cmd.itemArgType;
|
||||||
break;
|
break;
|
||||||
case ST_HS:
|
|
||||||
param.v=map(from.param.v,0,100,0,255);
|
|
||||||
case ST_HSV:
|
case ST_HSV:
|
||||||
|
param.v=map(from.param.v,0,100,0,255);
|
||||||
|
case ST_HS:
|
||||||
param.h=from.param.h;
|
param.h=from.param.h;
|
||||||
param.s=from.param.s;
|
param.s=from.param.s;
|
||||||
//param.s=map(from.param.s,0,100,0,255);
|
//param.s=map(from.param.s,0,100,0,255);
|
||||||
@@ -415,7 +435,7 @@ itemCmd itemCmd::assignFrom(itemCmd from)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Converting current obj to HSV
|
//Converting current obj to HSV
|
||||||
|
|
||||||
debugSerial<<F("Updated:"); from.debugOut();
|
debugSerial<<F("Updated:"); from.debugOut();
|
||||||
param=from.param;
|
param=from.param;
|
||||||
cmd=from.cmd;
|
cmd=from.cmd;
|
||||||
@@ -439,23 +459,51 @@ itemCmd itemCmd::assignFrom(itemCmd from)
|
|||||||
rgbSaturation =map(from.param.s, 0, 100, 0, 255);
|
rgbSaturation =map(from.param.s, 0, 100, 0, 255);
|
||||||
rgbValue = map(from.param.v, 0, 100, 0, 255);
|
rgbValue = map(from.param.v, 0, 100, 0, 255);
|
||||||
}
|
}
|
||||||
|
short colorT=from.param.colorTemp-1;
|
||||||
|
|
||||||
|
|
||||||
if (RGBW_flag)
|
if (RGBW_flag)
|
||||||
{
|
|
||||||
if (rgbSaturation < 128) { // Using white
|
{
|
||||||
param.w=map((127 - rgbSaturation) * rgbValue, 0, 127*255, 0, 255);
|
if (colorT<0)
|
||||||
int rgbvLevel = map (rgbSaturation,0,127,0,255*2);
|
{ //ColorTemperature not set
|
||||||
rgbValue = map(rgbValue, 0, 255, 0, rgbvLevel);
|
if (rgbSaturation < 128) { // Using white
|
||||||
rgbSaturation = map(rgbSaturation, 0, 127, 100, 255);
|
param.w=map((127 - rgbSaturation) * rgbValue, 0, 127*255, 0, 255);
|
||||||
if (rgbValue>255) rgbValue = 255;
|
int rgbvLevel = map (rgbSaturation,0,127,0,255*2);
|
||||||
}
|
rgbValue = map(rgbValue, 0, 255, 0, rgbvLevel);
|
||||||
else
|
rgbSaturation = map(rgbSaturation, 0, 127, 100, 255);
|
||||||
{
|
if (rgbValue>255) rgbValue = 255;
|
||||||
rgbSaturation = map(rgbSaturation, 128, 255, 100, 255);
|
}
|
||||||
param.w=0;
|
else
|
||||||
}
|
{
|
||||||
debugSerial<<F("Converted S:")<<rgbSaturation<<F(" Converted V:")<<rgbValue<<endl;
|
rgbSaturation = map(rgbSaturation, 128, 255, 100, 255);
|
||||||
}
|
param.w=0;
|
||||||
|
};
|
||||||
|
debugSerial<<F("Converted S:")<<rgbSaturation<<F(" Converted V:")<<rgbValue<<endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
long coldPercent = map (colorT,0,100,100,30);
|
||||||
|
long hotPercent = map (colorT,0,100,30,100);
|
||||||
|
int rgbvLevel;
|
||||||
|
|
||||||
|
if (rgbSaturation < 128) { // Using white
|
||||||
|
param.w=map((127 - rgbSaturation) * rgbValue *hotPercent, 0, 127*255*100, 0, 255);
|
||||||
|
rgbvLevel = map (rgbSaturation+coldPercent,0,127+100,0,255*2);
|
||||||
|
rgbValue = map(rgbValue, 0, 255, 0, rgbvLevel);
|
||||||
|
rgbSaturation = map(rgbSaturation, 0, 127, 0, 255);
|
||||||
|
if (rgbValue>255) rgbValue = 255;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rgbSaturation = map(rgbSaturation, 128, 255, 100, 255);
|
||||||
|
param.w=0;
|
||||||
|
};
|
||||||
|
debugSerial<<F("Cold:")<<coldPercent<<F(" Hot:")<<hotPercent<<F(" ConvS:")<<rgbSaturation<<F(" ConvV:")<<rgbValue<<F(" vLevel:")<<rgbvLevel<<endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#ifdef ADAFRUIT_LED
|
#ifdef ADAFRUIT_LED
|
||||||
Adafruit_NeoPixel strip(0, 0, 0);
|
Adafruit_NeoPixel strip(0, 0, 0);
|
||||||
uint32_t rgb = strip.ColorHSV(map(from.param.h, 0, 365, 0, 65535), rgbSaturation, rgbValue);
|
uint32_t rgb = strip.ColorHSV(map(from.param.h, 0, 365, 0, 65535), rgbSaturation, rgbValue);
|
||||||
|
|||||||
@@ -192,8 +192,8 @@ public:
|
|||||||
itemCmd RGBW(uint8_t r, uint8_t g, uint8_t b, uint8_t w);
|
itemCmd RGBW(uint8_t r, uint8_t g, uint8_t b, uint8_t w);
|
||||||
bool setH(uint16_t);
|
bool setH(uint16_t);
|
||||||
bool setS(uint8_t);
|
bool setS(uint8_t);
|
||||||
bool setColorTemp(uint8_t);
|
bool setColorTemp(int);
|
||||||
int8_t getColorTemp();
|
int getColorTemp();
|
||||||
uint16_t getH();
|
uint16_t getH();
|
||||||
uint16_t getS();
|
uint16_t getS();
|
||||||
itemCmd setArgType(uint8_t);
|
itemCmd setArgType(uint8_t);
|
||||||
|
|||||||
Reference in New Issue
Block a user