Commit 6e3c8b10 authored by chen.weican's avatar chen.weican

【修改内容】支持彩带灯模式的设置和RGB色的设置

【提交人】陈伟灿
parent 47e91b73
#include "kk_color_space.h" #include "kk_color_space.h"
static float hue_to_rgb(float m1, float m2, float h) void RGB_to_HSL_base(unsigned char red, unsigned char green, unsigned char blue,double *H,double *S,double *L)
{ {
h = (h < 0) ? h + 1 : ((h > 1) ? h - 1 : h); double min=0, max=0, delta=0;
if (h * 6 < 1) double R=0, G=0, B=0;
{
return (m1 + (m2 - m1) * h * 6); R = red / 255.0;
} G = green / 255.0;
else if (h * 2 < 1) B = blue / 255.0;
{
return m2; min = (R < G) ? ((R < B) ? R : B):((G < B) ? G : B);
} max = (R > G) ? ((R > B) ? R : B):((G > B) ? G : B);
else if (h * 3 < 2)
{ delta = max - min;
return m1 + (m2 - m1) * (0.66666 - h) * 6;
*L = (min + max) / 2.0;
if(*L==0.0 || (max-min) == 0.0){
*S = 0;
}else if(*L > 0.5){
*S = delta / (2 - 2 * *L);
}else{
*S = delta / (2 * *L);
} }
else
{ if((max-min) == 0.0){
return m1; *H = 0;
}else if((max-R == 0.0) && (G >= B)){
*H = (G - B) / delta;
}else if((max-R == 0.0) && (G < B)){
*H = (G - B) / delta + 6;
}else if((max-G) == 0.0){
*H = (B - R) / delta + 2;
}else if((max-B) == 0.0){
*H = (R - G) / delta + 4;
}else{
} }
*H /= 6.0;
}
void RGB_to_HSL(unsigned char red, unsigned char green, unsigned char blue,unsigned short *H,unsigned char *S,unsigned char *L)
{
double h, s, l;
RGB_to_HSL_base(red,green,blue,&h,&s,&l);
*H = h * 360 + ((int)(h * 3600) % 10 >= 5 ? 1 : 0);
*S = s * 100 + ((int)(s * 1000) % 10 >= 5 ? 1 : 0);
*L = l * 100 + ((int)(l * 1000) % 10 >= 5 ? 1 : 0);
} }
void hsl_to_rgb(float h,float s,float l,unsigned char* r,unsigned char * g,unsigned char * b) double HSL_convert_color_to_RGB(double p, double q, double tC)
{ {
float m1, m2; tC = (tC < 0) ? (tC + 1) : ((tC > 1) ? (tC - 1) : tC);
m2 = (l <= 0.5) ? l * (s + 1) : l + s - l*s; if(tC * 6 < 1){
m1 = l * 2 - m2; return (p + (q - p) * 6 * tC );
*r = 255 * hue_to_rgb(m1, m2, h + 0.33333); }else if(tC * 2 < 1){
*g = 255 * hue_to_rgb(m1, m2, h); return q;
*b = 255 * hue_to_rgb(m1, m2, h - 0.33333); }else if(tC * 3 < 2){
return p + ((q - p) * 6 * (2 / 3.0 - tC));
}
else{
return p;
}
} }
void rgb_to_hsl(unsigned char r, unsigned char g, unsigned char b,unsigned short* H,unsigned char*S,unsigned char* L) void HSL_to_RGB_base(double H,double S,double L,unsigned char *red, unsigned char *green, unsigned char *blue)
{ {
float min = 0, max = 0, delta = 0; double q, p;
float h = 0, s = 0, l = 0, R = 0, G = 0, B = 0; double tR, tG, tB;
R = r / 255.0; double r, g, b;
G = g / 255.0;
B = b / 255.0;
min = R < G ? R : G;
min = B < min ? B : min;
max = R > G ? R : G;
max = B > max ? B : max;
delta = max - min; q = (L <= 0.5) ? L * (S + 1) : L + S - (L * S);
l = (min + max) / 2.0; p = 2 * L - q;
s = 0; tR = H + 1/3.0;
if (l > 0 && l < 1) tG = H;
{ tB = H - 1/3.0;
s = delta / (l < 0.5 ? (2 * l) : (2 - 2 * l));
}
h = 0; r = 255 * HSL_convert_color_to_RGB(p, q, tR);
if (delta > 0) g = 255 * HSL_convert_color_to_RGB(p, q, tG);
{ b = 255 * HSL_convert_color_to_RGB(p, q, tB);
if (max == R && max != G)
{
h += (G - B) / delta;
}
else if (max == G && max != B)
{
h += (2 + (B - R) / delta);
}
else if (max == B && max != R)
{
h += (4 + (R - G) / delta);
}
{ *red = r + (((int)(r * 10) % 10 >= 5) ? 1 : 0);
h /= 6.0; *green = g + (((int)(g * 10) % 10 >= 5) ? 1 :0);
} *blue = b + (((int)(b * 10) % 10 >= 5) ? 1 : 0);
} }
*H = (unsigned char)h; void HSL_to_RGB(unsigned short H,unsigned char S,unsigned char L,unsigned char *red, unsigned char *green, unsigned char *blue)
*S = (unsigned char)s; {
*L = (unsigned char)l; double h = H / 360.0;
double s = S / 100.0;
double l = L / 100.0;
HSL_to_RGB_base(h,s,l,red,green,blue);
} }
...@@ -5,8 +5,10 @@ ...@@ -5,8 +5,10 @@
void rgb_to_hsl(unsigned char r, unsigned char g, unsigned char b,unsigned short* H,unsigned char* S,unsigned char* L); void RGB_to_HSL_base(unsigned char red, unsigned char green, unsigned char blue,double *H,double *S,double *L);
void hsl_to_rgb(float h,float s,float l,unsigned char* r,unsigned char* g,unsigned char* b); void RGB_to_HSL(unsigned char red, unsigned char green, unsigned char blue,unsigned short *H,unsigned char *S,unsigned char *L);
......
...@@ -368,33 +368,13 @@ int kk_tsl_report_CO2(EmberEUI64 eui64,uint8_t EP,EmberAfClusterId clusterId,Emb ...@@ -368,33 +368,13 @@ int kk_tsl_report_CO2(EmberEUI64 eui64,uint8_t EP,EmberAfClusterId clusterId,Emb
return kk_tsl_report_Concentration(eui64,EP,clusterId,attributeId,dataType,len,data); return kk_tsl_report_Concentration(eui64,EP,clusterId,attributeId,dataType,len,data);
} }
int kk_tsl_report_colorControl_Brightness(EmberEUI64 eui64,uint8_t EP,EmberAfClusterId clusterId,EmberAfAttributeId attributeId,uint8_t dataType,uint8_t len,uint8_t *data)
{
uint8_t value = data[0];
emberAfAppPrintln("[tsl report:kk_tsl_report_global_Brightness] value:%d\n",value);
if(dataType == ZCL_INT8U_ATTRIBUTE_TYPE){
if(len==1){
kk_tsl_report(eui64,EP,value,clusterId,attributeId);
return tsl_rpt_success;
}
return tsl_rpt_invaild_len;
}
return tsl_rpt_invaild_type;
}
static int s_HSLCount = 0; static int s_HSLCount = 0;
static unsigned short s_H = 0;
static unsigned char s_S = 0;
static unsigned char s_L = 0;
COLOR_HSL g_hsl = {0,0,0}; static int kk_tsl_report_colorControl_RGB_Str(EmberEUI64 eui64)
int kk_tsl_report_colorControl_RGB(EmberEUI64 eui64,uint8_t EP,EmberAfClusterId clusterId,EmberAfAttributeId attributeId,uint8_t dataType,uint8_t len,uint8_t *data)
{ {
uint8_t value = data[0];
kk_device_table_s *dev; kk_device_table_s *dev;
kk_dev_config_map *dev_info = NULL; kk_dev_config_map *dev_info = NULL;
kk_dev_config_item *item = NULL; kk_dev_config_item *item = NULL;
...@@ -403,22 +383,14 @@ int kk_tsl_report_colorControl_RGB(EmberEUI64 eui64,uint8_t EP,EmberAfClusterId ...@@ -403,22 +383,14 @@ int kk_tsl_report_colorControl_RGB(EmberEUI64 eui64,uint8_t EP,EmberAfClusterId
int startIdx = 0; int startIdx = 0;
cJSON* root_color = NULL; cJSON* root_color = NULL;
char tmp_Identity[64] = {0}; char tmp_Identity[64] = {0};
COLOR_RGB g_rgb = {0,0,0}; unsigned char r,g,b;
emberAfAppPrintln("[tsl report:kk_tsl_report_global_RGB] value:%d\n",value); if(s_HSLCount >= 3 && s_H != 0 && s_S != 0){
if(dataType == ZCL_INT8U_ATTRIBUTE_TYPE){ HSL_to_RGB(s_H*360/254,s_S*100/254,s_L,&r,&g,&b);
if(attributeId == 0x0001){
g_hsl.saturation = value;
s_HSLCount++;
}
else if(attributeId == 0x0){
g_hsl.hue = value;
s_HSLCount++;
}
}
if(s_HSLCount >= 2){
s_HSLCount = 0; s_HSLCount = 0;
HSLtoRGB(&g_hsl,&g_rgb); s_H = 0;
s_S = 0;
//printf("[%s][%d]%d %d %d\n",__FUNCTION__,__LINE__,r,g,b);
dev = kk_device_find_by_mac(eui64); dev = kk_device_find_by_mac(eui64);
if(dev == NULL){ if(dev == NULL){
return tsl_rpt_err; return tsl_rpt_err;
...@@ -436,7 +408,7 @@ int kk_tsl_report_colorControl_RGB(EmberEUI64 eui64,uint8_t EP,EmberAfClusterId ...@@ -436,7 +408,7 @@ int kk_tsl_report_colorControl_RGB(EmberEUI64 eui64,uint8_t EP,EmberAfClusterId
rev = kk_tsl_utils_memtok(item->identity,'.',1,&startIdx); rev = kk_tsl_utils_memtok(item->identity,'.',1,&startIdx);
if(!rev){ if(!rev){
memcpy(tmp_Identity,item->identity,startIdx); memcpy(tmp_Identity,item->identity,startIdx);
rpc_cJSON_AddNumberToObject(root_color, item->identity + 1 + startIdx,g_rgb.red); rpc_cJSON_AddNumberToObject(root_color, item->identity + 1 + startIdx,r);
} }
} }
else if(strstr(item->identity,".green") != NULL){ else if(strstr(item->identity,".green") != NULL){
...@@ -446,7 +418,7 @@ int kk_tsl_report_colorControl_RGB(EmberEUI64 eui64,uint8_t EP,EmberAfClusterId ...@@ -446,7 +418,7 @@ int kk_tsl_report_colorControl_RGB(EmberEUI64 eui64,uint8_t EP,EmberAfClusterId
rev = kk_tsl_utils_memtok(item->identity,'.',1,&startIdx); rev = kk_tsl_utils_memtok(item->identity,'.',1,&startIdx);
if(!rev){ if(!rev){
memcpy(tmp_Identity,item->identity,startIdx); memcpy(tmp_Identity,item->identity,startIdx);
rpc_cJSON_AddNumberToObject(root_color, item->identity + 1 + startIdx,g_rgb.green); rpc_cJSON_AddNumberToObject(root_color, item->identity + 1 + startIdx,g);
} }
} }
else if(strstr(item->identity,".blue") != NULL){ else if(strstr(item->identity,".blue") != NULL){
...@@ -456,7 +428,7 @@ int kk_tsl_report_colorControl_RGB(EmberEUI64 eui64,uint8_t EP,EmberAfClusterId ...@@ -456,7 +428,7 @@ int kk_tsl_report_colorControl_RGB(EmberEUI64 eui64,uint8_t EP,EmberAfClusterId
rev = kk_tsl_utils_memtok(item->identity,'.',1,&startIdx); rev = kk_tsl_utils_memtok(item->identity,'.',1,&startIdx);
if(!rev){ if(!rev){
memcpy(tmp_Identity,item->identity,startIdx); memcpy(tmp_Identity,item->identity,startIdx);
rpc_cJSON_AddNumberToObject(root_color, item->identity + 1 + startIdx,g_rgb.blue); rpc_cJSON_AddNumberToObject(root_color, item->identity + 1 + startIdx,b);
} }
} }
item = item->next; item = item->next;
...@@ -469,6 +441,49 @@ int kk_tsl_report_colorControl_RGB(EmberEUI64 eui64,uint8_t EP,EmberAfClusterId ...@@ -469,6 +441,49 @@ int kk_tsl_report_colorControl_RGB(EmberEUI64 eui64,uint8_t EP,EmberAfClusterId
} }
} }
return tsl_rpt_err; return tsl_rpt_err;
}
int kk_tsl_report_colorControl_Brightness(EmberEUI64 eui64,uint8_t EP,EmberAfClusterId clusterId,EmberAfAttributeId attributeId,uint8_t dataType,uint8_t len,uint8_t *data)
{
uint8_t value = data[0];
emberAfAppPrintln("[tsl report:kk_tsl_report_global_Brightness] value:%d\n",value);
if(dataType == ZCL_INT8U_ATTRIBUTE_TYPE){
if(len==1){
kk_tsl_report(eui64,EP,value,clusterId,attributeId);
s_L = value;
s_HSLCount++;
kk_tsl_report_colorControl_RGB_Str(eui64);
if(s_HSLCount >= 3){
s_HSLCount = 0;
}
}
return tsl_rpt_success;
}
return tsl_rpt_invaild_type;
}
int kk_tsl_report_colorControl_RGB(EmberEUI64 eui64,uint8_t EP,EmberAfClusterId clusterId,EmberAfAttributeId attributeId,uint8_t dataType,uint8_t len,uint8_t *data)
{
uint8_t value = data[0];
emberAfAppPrintln("[tsl report:kk_tsl_report_global_RGB] value:%d\n",value);
if(dataType == ZCL_INT8U_ATTRIBUTE_TYPE){
if(attributeId == 0x0001){
s_S = value;
s_HSLCount++;
kk_tsl_report_colorControl_RGB_Str(eui64);
}
else if(attributeId == 0x0){
s_H = value;
s_HSLCount++;
kk_tsl_report_colorControl_RGB_Str(eui64);
}
}
return tsl_rpt_success;
} }
......
...@@ -140,8 +140,10 @@ cJSON *kk_tsl_property_operation(jrpc_context * ctx, cJSON *params, cJSON *id,cJ ...@@ -140,8 +140,10 @@ cJSON *kk_tsl_property_operation(jrpc_context * ctx, cJSON *params, cJSON *id,cJ
} }
else{ else{
propertyItem = rpc_cJSON_GetObjectItem(params, item->identity); propertyItem = rpc_cJSON_GetObjectItem(params, item->identity);
if(propertyItem != NULL){ if(propertyItem != NULL){
// printf("[%s][%d]item->identity:%s\n",__FUNCTION__,__LINE__,item->identity);
findFlag = 1; findFlag = 1;
} }
} }
...@@ -154,7 +156,12 @@ cJSON *kk_tsl_property_operation(jrpc_context * ctx, cJSON *params, cJSON *id,cJ ...@@ -154,7 +156,12 @@ cJSON *kk_tsl_property_operation(jrpc_context * ctx, cJSON *params, cJSON *id,cJ
value = propertyItem->valueint; value = propertyItem->valueint;
} }
func = item->controlFunc; func = item->controlFunc;
if(func != NULL){
res = func(ctx,nodeId,item->endpoint,&value); res = func(ctx,nodeId,item->endpoint,&value);
}
else{
// printf("[%s][%d] can not find the func!!!\n",__FUNCTION__,__LINE__);
}
findFlag = 0; findFlag = 0;
} }
item = item->next; item = item->next;
...@@ -302,21 +309,24 @@ error_return: ...@@ -302,21 +309,24 @@ error_return:
static uint8_t s_RgbCount = 0; static uint8_t s_RgbCount = 0;
static COLOR_RGB s_rgb; static COLOR_RGB s_rgb;
static int kk_zclColorControlMovetohueandsat(EmberNodeId node,unsigned char ep) static int kk_zclColorControlMovetohueandsat(EmberNodeId node,unsigned char ep)
{ {
int h,s,l; unsigned short h;
unsigned short s,l;
COLOR_HSL hsl; COLOR_HSL hsl;
EmberStatus status = 0; EmberStatus status = 0;
if(s_RgbCount >= 3){ if(s_RgbCount >= 3){
RGBtoHSL(&s_rgb,&hsl); //RGBtoHSL(&s_rgb,&hsl);
emberAfAppPrintln("[kk_zclColorControlMovetohueandsat]"); emberAfAppPrintln("[kk_zclColorControlMovetohueandsat]");
rgb_to_hsl(s_rgb.red, s_rgb.green, s_rgb.blue,h,s,l); RGB_to_HSL(s_rgb.red, s_rgb.green, s_rgb.blue,&h,&s,&l);
emberAfAppPrintln("[kk_zclColorControlMovetohueandsat] h:%d,s:%d,l:%d\n,",h,s,l);
status = zclColorControlMovetohueandsat(node,ep,h*254,s*254,0,0,0,0); status = zclColorControlMovetohueandsat(node,ep,h*254/360,s*254/100,0,0,0,0);
status |= zclLevel_MoveToLevel(node,ep,l,0,NULL,NULL);
s_RgbCount = 0; s_RgbCount = 0;
} }
return 0; return status;
} }
int kk_tsl_set_colorlight_RGB_red(jrpc_context * ctx,EmberNodeId node,unsigned char ep,void* data) int kk_tsl_set_colorlight_RGB_red(jrpc_context * ctx,EmberNodeId node,unsigned char ep,void* data)
{ {
...@@ -384,6 +394,7 @@ int kk_tsl_set_colorlight_mode(jrpc_context * ctx,EmberNodeId node,unsigned char ...@@ -384,6 +394,7 @@ int kk_tsl_set_colorlight_mode(jrpc_context * ctx,EmberNodeId node,unsigned char
mode = kk_find_extra_data(node,"Mode"); mode = kk_find_extra_data(node,"Mode");
while(mode!=NULL){ while(mode!=NULL){
if(ix++==value){ if(ix++==value){
kk_global_len = mode->len + 1; kk_global_len = mode->len + 1;
kk_global_buffer[0] = mode->len; kk_global_buffer[0] = mode->len;
memcpy(&kk_global_buffer[1],mode->data,kk_global_buffer[0] - 1); memcpy(&kk_global_buffer[1],mode->data,kk_global_buffer[0] - 1);
...@@ -394,7 +405,7 @@ int kk_tsl_set_colorlight_mode(jrpc_context * ctx,EmberNodeId node,unsigned char ...@@ -394,7 +405,7 @@ int kk_tsl_set_colorlight_mode(jrpc_context * ctx,EmberNodeId node,unsigned char
emberAfCorePrintln("\n"); emberAfCorePrintln("\n");
break; break;
} }
mode->next; mode = mode->next;
} }
......
...@@ -26,13 +26,14 @@ void kk_rpc_test(void); ...@@ -26,13 +26,14 @@ void kk_rpc_test(void);
{"kk_tsl_set_colorlight_RGB_green",kk_tsl_set_colorlight_RGB_green},\ {"kk_tsl_set_colorlight_RGB_green",kk_tsl_set_colorlight_RGB_green},\
{"kk_tsl_set_colorlight_RGB_blue",kk_tsl_set_colorlight_RGB_blue},\ {"kk_tsl_set_colorlight_RGB_blue",kk_tsl_set_colorlight_RGB_blue},\
{"kk_tsl_set_colorlight_Brightness",kk_tsl_set_colorlight_Brightness},\ {"kk_tsl_set_colorlight_Brightness",kk_tsl_set_colorlight_Brightness},\
{"kk_tsl_report_colorControl_mode",kk_tsl_set_colorlight_mode},\ {"kk_tsl_set_colorlight_mode",kk_tsl_set_colorlight_mode},\
} }
#define KK_RPC_REPORT_FUNCTION_TABLE {\ #define KK_RPC_REPORT_FUNCTION_TABLE {\
{"kk_tsl_report_global_onoff",kk_tsl_report_global_onoff},\ {"kk_tsl_report_global_onoff",kk_tsl_report_global_onoff},\
{"kk_tsl_report_colorControl_Brightness",kk_tsl_report_colorControl_Brightness},\ {"kk_tsl_report_colorControl_Brightness",kk_tsl_report_colorControl_Brightness},\
{"kk_tsl_report_colorControl_RGB",kk_tsl_report_colorControl_RGB},\
} }
kk_rpc_set_api_s kk_rpc_set_api[]; kk_rpc_set_api_s kk_rpc_set_api[];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment