为了追求更加多样的色彩变化,由RGB三路PWM调光衍生出RGBWY五路PWM调光。与RGB一样,都是要配置相应的PWM,其实非常简单,如果你不需要用到手机APP或者tl-ble-phone-mesh来控制的话,那么你只需要配置好五个PWM,然后照着官方给的SDK代码,照葫芦画瓢地写一个light-adjust-CT函数就行了,然后在main-loop函数中调用一下,设置自己喜欢的比例和亮度,就ok了,很简单的,不过自己搞有一点点烦人,emm我直接放我的出来吧,可以抄作业。(●'◡'●)!!
//放在light.H文件
//配置自己硬件上的对应引脚
#if(PCBA_8258_SEL == PCBA_8258_DONGLE_48PIN)
#define PWM_R GPIO_PWMXX //red
#define PWM_G GPIO_PWMXX //green
#define PWM_B GPIO_PWMXX //blue
#define PWM_W GPIO_PWMXX //white
#define PWM_Y GPIO_PWMXX //yellow
//PWM宏定义
#define PWM_FUNC_R AS_PWM // AS_PWM_SECOND
#define PWM_FUNC_G AS_PWM // AS_PWM_SECOND
#define PWM_FUNC_B AS_PWM // AS_PWM_SECOND
#define PWM_FUNC_W AS_PWM // AS_PWM_SECOND
#define PWM_FUNC_Y AS_PWM // AS_PWM_SECOND
#define PWMID_R (GET_PWMID(PWM_R, PWM_FUNC_R))
#define PWMID_G (GET_PWMID(PWM_G, PWM_FUNC_G))
#define PWMID_B (GET_PWMID(PWM_B, PWM_FUNC_B))
#define PWMID_W (GET_PWMID(PWM_W, PWM_FUNC_W))
#define PWMID_Y (GET_PWMID(PWM_WW, PWM_FUNC_WW))
#define PWM_INV_R (GET_PWM_INVERT_VAL(PWM_R, PWM_FUNC_R))
#define PWM_INV_G (GET_PWM_INVERT_VAL(PWM_G, PWM_FUNC_G))
#define PWM_INV_B (GET_PWM_INVERT_VAL(PWM_B, PWM_FUNC_B))
#define PWM_INV_W (GET_PWM_INVERT_VAL(PWM_W, PWM_FUNC_W))
#define PWM_INV_Y (GET_PWM_INVERT_VAL(PWM_Y, PWM_FUNC_Y))
//放在main_light.c文件
//这里用来设置最大占空比,如果你的板子开了五路PWM导致功率过大可以改动一下
///没有就不用理会///
//#if 0
//const u16 rgb_lumen_map[101] = {
// 0,2*256+128,2*256+192,3*256,3*256+128,4*256,4*256+128,5*256,5*256+128,6*256, 7*256,
// 8*256, 9*256, 10*256, 11*256, 12*256, 13*256, 14*256, 15*256, 16*256, 17*256,
// 18*256, 19*256, 20*256, 21*256, 22*256, 24*256, 25*256, 26*256, 27*256, 28*256,
// 29*256, 30*256, 31*256, 32*256, 33*256, 34*256, 35*256, 36*256, 37*256, 38*256,
// 39*256, 40*256, 41*256, 42*256, 43*256, 44*256, 45*256, 46*256, 47*256, 48*256,
// 49*256, 50*256, 51*256, 52*256, 53*256, 54*256, 55*256, 56*256,57*256,58*256,
// 59*256,60*256,61*256,62*256,63*256,64*256,65*256,66*256,67*256,68*256,
// 69*256,70*256,71*256,72*256,73*256,74*256,75*256,76*256,78*256,80*256,
// 82*256,84*256,86*256,88*256,90*256,92*256,94*256,96*256,98*256,100*256,
// 102*256,104*256,106*256,108*256,110*256,112*256,114*256,116*256,118*256,120*256,// must less or equal than (255*256)
//};
// const u16 cct_lumen_map[101] = {
// 0,2*256+128,2*256+192,3*256,3*256+128,4*256,4*256+128,5*256,5*256+128,6*256, 7*256,
// 8*256, 9*256, 10*256, 11*256, 12*256, 13*256, 14*256, 15*256, 16*256, 17*256,
// 18*256, 19*256, 20*256, 21*256, 22*256, 24*256, 25*256, 26*256, 27*256, 28*256,
// 29*256, 30*256, 31*256, 32*256, 33*256, 34*256, 35*256, 36*256, 37*256, 38*256,
// 39*256, 40*256, 41*256, 42*256, 43*256, 44*256, 45*256, 46*256, 47*256, 48*256,
// 49*256, 50*256, 51*256, 52*256, 53*256, 54*256, 55*256, 56*256,57*256,58*256,
// 59*256,60*256,61*256,62*256,63*256,64*256,65*256,66*256,67*256,68*256,
// 69*256,70*256,71*256,72*256,73*256,74*256,75*256,76*256,78*256,80*256,
// 82*256,84*256,86*256,88*256,90*256,92*256,94*256,96*256,98*256,100*256,
// 102*256,104*256,106*256,108*256,110*256,112*256,114*256,116*256,118*256,120*256,// must less or equal than (255*256)
//};
//#else
// 0-100% (pwm's value index: this is pwm compare value, and the pwm cycle is 255*256)
// const u16 rgb_lumen_map[101] = {//********************************************************************************************************//
// 0, 5, 50, 130, 1*256, 2*256, 3*256, 4*256, 5*256, 6*256, 7*256,
// 8*256, 9*256, 10*256, 11*256, 12*256, 13*256, 14*256, 15*256, 16*256, 17*256,
// 18*256, 19*256, 21*256, 23*256, 25*256, 27*256, 29*256, 31*256, 33*256, 35*256,
// 37*256, 39*256, 41*256, 43*256, 45*256, 47*256, 49*256, 51*256, 53*256, 55*256,
// 57*256, 59*256, 61*256, 63*256, 65*256, 67*256, 69*256, 71*256, 73*256, 75*256,
// 78*256, 81*256, 84*256, 87*256, 90*256, 93*256, 96*256, 99*256,102*256,105*256,
// 108*256,111*256,114*256,117*256,120*256,123*256,126*256,129*256,132*256,135*256,
// 138*256,141*256,144*256,147*256,150*256,154*256,158*256,162*256,166*256,170*256,
// 174*256,178*256,182*256,186*256,190*256,194*256,198*256,202*256,206*256,210*256,
// 214*256,218*256,222*256,226*256,230*256,235*256,240*256,245*256,250*256,255*256,// must less or equal than (255*256)
//
// };
//const u16 cct_lumen_map[101] = {
// 0, 5, 50, 130, 1*256, 2*256, 3*256, 4*256, 5*256, 6*256, 7*256,
// 8*256, 9*256, 10*256, 11*256, 12*256, 13*256, 14*256, 15*256, 16*256, 17*256,
// 18*256, 19*256, 21*256, 23*256, 25*256, 27*256, 29*256, 31*256, 33*256, 35*256,
// 37*256, 39*256, 41*256, 43*256, 45*256, 47*256, 49*256, 51*256, 53*256, 55*256,
// 57*256, 59*256, 61*256, 63*256, 65*256, 67*256, 69*256, 71*256, 73*256, 75*256,
// 78*256, 81*256, 84*256, 87*256, 90*256, 93*256, 96*256, 99*256,102*256,105*256,
// 108*256,111*256,114*256,117*256,120*256,123*256,126*256,129*256,132*256,135*256,
// 138*256,141*256,144*256,147*256,150*256,154*256,158*256,162*256,166*256,170*256,
// 174*256,178*256,182*256,186*256,190*256,194*256,198*256,202*256,206*256,210*256,
// 214*256,218*256,222*256,226*256,230*256,235*256,240*256,245*256,250*256,255*256,// must less or equal than (255*256)
//};
设置RGBWY各个通道///
void light_adjust_R(u8 val, u8 lum){
pwm_set_lum (PWMID_R, get_pwm_cmp(val, lum), PWM_INV_R);
}
void light_adjust_G(u8 val, u8 lum){
pwm_set_lum (PWMID_G, get_pwm_cmp(val, lum), PWM_INV_G);
}
void light_adjust_B(u8 val, u8 lum){
pwm_set_lum (PWMID_B, get_pwm_cmp(val, lum), PWM_INV_B);
}
void light_adjust_W(u8 val, u8 lum){
pwm_set_lum (PWMID_W, get_pwm_cmp(val, lum), PWM_INV_W);
}
void light_adjust_Y(u8 val, u8 lum){
pwm_set_lum (PWMID_Y, get_pwm_cmp(val, lum), PWM_INV_Y);
}
定义RGB调光函数
void light_adjust_RGB_hw(u8 val_R, u8 val_G, u8 val_B, u8 lum){
light_adjust_R(val_R, lum);
light_adjust_G(val_G, lum);
light_adjust_B(val_B, lum);
}
void light_adjust_RGB(u8 val_R, u8 val_G, u8 val_B, u8 lum){
#if LIGHT_ADJUST_STEP_EN
light_onoff_step_init();
#endif
light_adjust_RGB_hw(val_R, val_G, val_B, lum);
}
/定义CT调光函数/这样写的原因主要是用来方便tl-ble-phone-mesh的调试
void light_adjust_CT( u8 val_W, u8 val_Y,u8 lum){
light_adjust_W(val_W, lum);
light_adjust_Y(val_Y, lum);
}
///或者可以这样写,这样可以一个函数全部设置到
//void light_adjust_CT_hw(u8 val_R, u8 val_G, u8 val_B, u8 val_W, u8 val_Y,u8 lum){
// light_adjust_R(val_R, lum);
// light_adjust_G(val_G, lum);
// light_adjust_B(val_B, lum);
// light_adjust_W(val_W, lum);
// light_adjust_Y(val_Y, lum);
//}
//void light_adjust_CT(u8 val_R, u8 val_G, u8 val_B, u8 val_W, u8 val_Y,u8 lum){
// #if LIGHT_ADJUST_STEP_EN
// light_onoff_step_init();
// #endif
//
// light_adjust_CT_hw(val_R, val_G, val_B, val_W, val_Y, lum);
//}
//设置引脚功能///
void pwm_led_en (int id, int en)
{
if (id == 0)
{
gpio_set_func (PWM_G, en ? PWM_FUNC_G : AS_GPIO);
}
else if (id == 1)
{
gpio_set_func (PWM_B, en ? PWM_FUNC_B : AS_GPIO);
}
else if (id == 2)
{
gpio_set_func (PWM_R, en ? PWM_FUNC_R : AS_GPIO);
}
else if (id == 3)
{
gpio_set_func (PWM_W, en ? PWM_FUNC_W : AS_GPIO);
}
else if (id == 4)
{
gpio_set_func (PWM_Y, en ? PWM_FUNC_Y : AS_GPIO);
}
}
/这段我也不知道要不要改//
if( led_off || led_on ){
if (led_sel & BIT(0))
{
light_adjust_G (LED_INDICATE_VAL * led_on, 100);
}
if (led_sel & BIT(1))
{
light_adjust_B (LED_INDICATE_VAL * led_on, 100);
}
if (led_sel & BIT(2))
{
light_adjust_R (LED_INDICATE_VAL * led_on, 100);
}
if (led_sel & BIT(3))
{
light_adjust_W (LED_INDICATE_VAL * led_on, 100);
}
if (led_sel & BIT(4))
{
light_adjust_Y (LED_INDICATE_VAL * led_on, 100);
}
if (led_sel & BIT(5))
{
}
}
//这里主要是用来设置手机蓝牙APP或者电脑tl-ble-phone-mesh软件控制的//
//还有开启,关闭,设置亮度这些大家可以自己去看看//
if(params[0] == 1){ //R
led_val[0] = params[1];
led_val[3] =0;
led_val[4]=0;
light_adjust_CT( led_val[3] ,led_val[4],led_lum);
light_adjust_R (led_val[0], led_lum);
}else if(params[0] == 2){ //G
led_val[1] = params[1];
led_val[3] =0;
led_val[4]=0;
light_adjust_CT( led_val[3] ,led_val[4],led_lum);
light_adjust_G (led_val[1], led_lum);
}else if(params[0] == 3){ //B
led_val[2] = params[1];
led_val[3] =0;
led_val[4]=0;
light_adjust_CT( led_val[3] ,led_val[4],led_lum);
light_adjust_B (led_val[2], led_lum);
}else if(params[0] == 4){ //RGB
led_val[0] = params[1];
led_val[1] = params[2];
led_val[2] = params[3];
led_val[3] =0;
led_val[4]=0;
light_adjust_CT( led_val[3] ,led_val[4],led_lum);
light_adjust_RGB(led_val[0], led_val[1], led_val[2], led_lum);
}else if(params[0] == 5){ //CT
led_val[0] = 0;
led_val[1] = 0;
led_val[2] = 0;
led_val[3] = params[4];
led_val[4] = params[5];
light_adjust_CT(led_val[3], led_val[4], led_lum);
light_adjust_RGB(led_val[0], led_val[1], led_val[2], led_lum);
//PWM口初始化/
void user_init_pwm(int retention_flag)
{
pwm_set (PWMID_R, PMW_MAX_TICK, PWM_INV_R ? 0 : PMW_MAX_TICK);
pwm_set (PWMID_G, PMW_MAX_TICK, PWM_INV_G ? 0 : PMW_MAX_TICK);
pwm_set (PWMID_B, PMW_MAX_TICK, PWM_INV_B ? 0 : PMW_MAX_TICK);
pwm_set (PWMID_W, PMW_MAX_TICK, PWM_INV_W ? 0 : PMW_MAX_TICK);
pwm_set (PWMID_Y, PMW_MAX_TICK, PWM_INV_Y ? 0 : PMW_MAX_TICK);
if(retention_flag){
light_onoff(!light_off);
}else{
//retrieve lumen value
light_lum_retrieve();
}
pwm_start (PWMID_R);
pwm_start (PWMID_G);
pwm_start (PWMID_B);
pwm_start (PWMID_W);
pwm_start (PWMID_Y);
gpio_set_func (PWM_R, PWM_FUNC_R);
gpio_set_func (PWM_G, PWM_FUNC_G);
gpio_set_func (PWM_B, PWM_FUNC_B);
gpio_set_func (PWM_W, PWM_FUNC_W);
gpio_set_func (PWM_Y, PWM_FUNC_Y);
pwm_io_input_disable();
}
最后你只需要在main-loop中调用就行了。
gpio_write(LED_ON_OFF_Pin,1);
light_adjust_RGB(100,100,30,30);
light_adjust_CT(100,100,30);
好了,五路PWM调光就完成了!下期再见,ヾ(•ω•`)o88!