spi 推跑马灯,来电时跑马灯效果 归零码
1 主控spi单次最少是发一个U8 也就是8个bit位,所以软件充分利用 这个u8的每一个bit位组合成一个 灯需要的0码 或者1码
2 遇到的问题,主频改变的时候数据就不对了,发送spi数据前需要判断当前主频来设置spi的波特率来保持波形
3 开机第一次发送波形的时候有概率的波形中间会被中断一次-----丢弃第一次发送的波形,第一次发送波形的时候,led使能脚不要使能
代码
void ws2812_power_open(void)
{
printf("%s\n",func);
GPIOFSET = BIT(4);
}
void ws2812_power_close(void)
{
printf("%s\n",func);
GPIOFCLR = BIT(4);
}
void ws2812_set_led_buf_offset(rgb_t* buf_p) //����buf��ʾ
{
rgb_t offset_buf[WS2812_LED_NUM];
u8 k=0,i=0;
static u8 offset=1;
k = offset++;
if(offset>9)
offset=1;
while(i<WS2812_LED_NUM)
{
offset_buf[k] = *(buf_p+i);
if(++k>=WS2812_LED_NUM)
k = 0;
i++;
}
ws2812_send_data((u8*)offset_buf,WS2812_LED_NUM*3);
}
void ws2812_set_blue_led_on(void)
{
u8 rgb_data[3*10]={0x55,0x00,0x55,
0x55,0x00,0x55,
0x55,0x00,0x55,
0x55,0x00,0x55,
0x55,0x00,0x55,
0x55,0x00,0x55,//
0x55,0x00,0x55,
0x55,0x00,0x55,
0x55,0x00,0x55,
0x55,0x00,0x55,
};
ws2812_set_led_buf((rgb_t *)rgb_data);
}
void ws2812_set_red_led_on(void)
{
u8 rgb_data[3*10]={0x00,0xAA,0x00,
0x00,0xAA,0x00,
0x00,0xAA,0x00,
0x00,0xAA,0x00,
0x00,0xAA,0x00,
0x00,0xAA,0x00,//
0x00,0xAA,0x00,
0x00,0xAA,0x00,
0x00,0xAA,0x00,
0x00,0xAA,0x00,
};
ws2812_set_led_buf((rgb_t *)rgb_data);
}
void ws2812_set_red_blue_led_on(void)
{
u8 rgb_data[3*10]={0x00,0xAA,0xAA,
0x00,0xAA,0xAA,
0x00,0xAA,0xAA,
0x00,0xAA,0xAA,
0x00,0xAA,0xAA,
0x00,0xAA,0xAA,//
0x00,0xAA,0xAA,
0x00,0xAA,0xAA,
0x00,0xAA,0xAA,
0x00,0xAA,0xAA,
};
ws2812_power_open();
ws2812_set_led_buf((rgb_t *)rgb_data);
}
WS2812_DRIVER_SECTION
void ws2812_set_blue_breath(void)
{
u8 rgb_data[3*10]={0x00,0x00,0x00,
0x00,0x00,0x00,
0x00,0x00,0x00,
0x00,0x00,0x00,
0x00,0x00,0x00,
0x00,0x00,0x00,//
0x00,0x00,0x00,
0x00,0x00,0x00,
0x00,0x00,0x00,
0x00,0x00,0x00,
};
u8 step=5;
u8 i;
bool dir_up = sys_cb.dir_up;
u8 curValue=sys_cb.curValue;
static u8 offCnt=0;
if(dir_up)
{
if(curValue+step<100+10)//100
curValue+=step;
else{
if(offCnt++==4)
{
dir_up=false;
offCnt=0;
}
}
}
else {
if(curValue>10+10) //step
curValue-=step;
else{
if(offCnt++==4)
{
dir_up=true;
offCnt=0;
}
}
}
for(i=0;i<3*WS2812_LED_NUM;i++)
{
//j=i/3;
if((i)%3==2)
{
rgb_data[i]=curValue;
}
//20230818
else if((i)%3==0)//g
{
rgb_data[i]=curValue;//5+curValue/5;
}
//
}
sys_cb.dir_up=dir_up;
sys_cb.curValue=curValue;
ws2812_set_led_buf((rgb_t *)rgb_data);
}
void ws2812_set_red_breath(void)
{
u8 rgb_data[3*10]={0x00,0x00,0x00,// g r b
0x00,0x00,0x00,
0x00,0x00,0x00,
0x00,0x00,0x00,
0x00,0x00,0x00,
0x00,0x00,0x00,//
0x00,0x00,0x00,
0x00,0x00,0x00,
0x00,0x00,0x00,
0x00,0x00,0x00,
};
u8 step=2;//5
u8 i,j;
bool dir_up = sys_cb.dir_up;
u8 curValue=sys_cb.curValue;
static u8 offCnt=0;
if(dir_up)
{
if(curValue+step<50)//100
curValue+=step;
else{
if(offCnt++==4)
{
dir_up=false;
offCnt=0;
}
}
}
else {
if(curValue>step)
curValue-=step;
else{
if(offCnt++==4)
{
dir_up=true;
offCnt=0;
}
}
}
for(i=1;i<3*WS2812_LED_NUM;i++)
{
j=i/3;
if((i-j*3)%3==1)
{
rgb_data[i]=curValue;
}
}
sys_cb.dir_up=dir_up;
sys_cb.curValue=curValue;
//printf("ws2812_set_red_breath:%d\n",sys_cb.curValue);
ws2812_set_led_buf((rgb_t *)rgb_data);
}
void ws2812_set_call_led(void)
{
printf("ws2812_set_call_led:%d\n",sys_cb.ledStatus);
if(sys_cb.ledStatus!=LOW_BAT_FLASH)
{
sys_cb.ledStatus=THIRD_CALL_FLASH;
ws2812_power_open();
}
}
void ws2812_set_low_led(void)
{
//printf("ws2812_set_low_led:%d\n",sys_cb.ledStatus);
if(sys_cb.ledStatus==ALL_LED_OFF)
sys_cb.ledStatus=LOW_BAT_FLASH;
}
void ws2812_set_off_led(void)
{
printf("ws2812_set_off_led:%d\n",sys_cb.ledStatus);
//if(sys_cb.ledStatus!=LOW_BAT_FLASH)
{
sys_cb.ledStatus=ALL_LED_OFF;
ws2812_led_clear();
ws2812_power_close();
}
}
void ws2812_set_blue_breath_start(void)
{
printf("ws2812_set_blue_breath_start:%d\n",sys_cb.ledStatus);
sys_cb.ledStatus=BLUE_BREATH;
sys_cb.curValue=0x8;
sys_cb.dir_up=true;
ws2812_power_open();
}
void ws2812_set_blue_breath_stop(void)
{
printf("ws2812_set_off_led:%d\n",sys_cb.ledStatus);
sys_cb.ledStatus=ALL_LED_OFF;
ws2812_led_clear();
ws2812_power_close();
}
void ws2812_set_red_breath_start(void)
{
printf("ws2812_set_off_led:%d\n",sys_cb.ledStatus);
sys_cb.ledStatus=RED_BREATH;
sys_cb.curValue=0x0;
sys_cb.dir_up=true;
ws2812_power_open();
}
void ws2812_set_red_breath_stop(void)
{
printf("ws2812_set_off_led:%d\n",sys_cb.ledStatus);
sys_cb.ledStatus=ALL_LED_OFF;
ws2812_led_clear();
ws2812_power_close();
}
void ws2812_set_horce_race_ring(void)
{
sys_cb.ledStatus=RING_HORCE_RACE_LED;
ws2812_power_open();
}
void ws2812_horce_race_ring_run(void)
{
u8 rgb_data[3*10]={0x00,0xAA,0xAA,
0x00,0x88,0x88,
0x00,0x66,0x66,
0xAA,0x00,0x00,
0x88,0x00,0x00,
0x66,0x00,0x00,//
0x55,0x00,0x00,
0xAA,0x00,0xAA,
0x88,0x00,0x88,
0x66,0x00,0x66,
};
ws2812_set_led_buf_offset((rgb_t *)rgb_data);
}
void ws2812_set_horce_race_ring_stop(void)
{
sys_cb.ledStatus=ALL_LED_OFF;
ws2812_led_clear();
ws2812_power_close();
}
void ws2812_set_low_bat_led_on(void)
{
printf("%s,%d\n",func,tick_get());
ws2812_power_open();
ws2812_set_red_led_on();
}
void ws2812_set_low_bat_led_off(void)
{
printf("%s,%d\n",func,tick_get());
ws2812_led_clear();
ws2812_power_close();
}
void ws2812_set_poweroff_led_on(void)
{
sys_cb.ledStatus=0;
ws2812_power_open();
ws2812_set_red_led_on();
}