如何解决esp8266 + arduino + Map替代Map
下午,
我正在为使用esp8266
的设备编写一些定制的筒灯代码。
我想知道目前是否有更快的方法可以在颜色和白色或色温之间进行转换。我正在使用map
并翻转值,但是在处理时注意到这很慢。
是否有另一种无需使用map
即可做到这一点的方法,或者还有更正式的方式可以编写此代码吗?
SetBrightness = set brightness level overall
SetSaturation = mix between color and white
CT = set white color temp
long map2(long x,long in_min,long in_max,long out_min,long out_max)
{
if ( x == in_max)
return out_max;
else if (out_min < out_max)
return (x - in_min) * (out_max - out_min + 1) / (in_max - in_min) + out_min;
else
return (x - in_min) * (out_max - out_min - 1) / (in_max - in_min) + out_min;
}
void updateLights()
{
byte R_TEMP = map2(r,255,255);
byte G_TEMP = map2(g,255);
byte B_TEMP = map2(b,255);
byte DIM_TEMP = map2(SetBrightness,100,255);
byte Sat_TEMP = map2(SetSaturation,255);
//Mix color to white
byte ST1_TEMP = map2(Sat_TEMP,255);
byte ST2_TEMP = map2(Sat_TEMP,0);
// color temp from white TO warm WHITE
byte CT1_TEMP = map2(CT,0);
byte CT2_TEMP = map2(CT,255);
// blend the biritnes and blen to getther
byte Blend1 = (ST1_TEMP * DIM_TEMP / 255);
byte Blend2 = (ST2_TEMP * DIM_TEMP / 255);
r_show = (R_TEMP * Blend1 / 255);
g_show = (G_TEMP * Blend1 / 255);
b_show = (B_TEMP * Blend1 / 255);
w_show = (CT1_TEMP * Blend2 / 255);
wc_show = (CT2_TEMP * Blend2 / 255);
//MQTTClient.publish((stat + "/color2").c_str(),(String(r_show) + "," + String(g_show) + "," + String(b_show) + "," + String(w_show) + "," + String(wc_show)).c_str());
analogWrite(RED_PIN,r_show);
analogWrite(GREEN_PIN,g_show);
analogWrite(BLUE_PIN,b_show);
analogWrite(CWHITE_PIN,w_show);
analogWrite(WWHITE_PIN,wc_show);
}
void FadeSaturation() {
if (saturation != SetSaturation) {
if (saturation > SetSaturation) {
SetSaturation++;
if (SetSaturation > 100) {
SetSaturation = 100;
}
}
else if (saturation < SetSaturation) {
SetSaturation--;
if (SetSaturation < 0) {
SetSaturation = 0;
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。