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 (将#修改为@)