123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- #include "hal_ws2812.h"
- #include "bsp_gpio.h"
- #include "nrf_gpio.h"
- #include "usr_config.h"
- //#include "bsp_time.h"
- #include "system.h"
- #include "hal_ble_client.h"
- #include "drv_pwm.h"
- #include "nrf_delay.h"
- #include "hal_imu.h"
- #define WS2812_FLASH_ALWAYS 1 //是否一直刷新
- #define WS2812_ENABLE 0
- #define WS2812_DISABLE 1
- #define WS2812_DISPLAY_NUMBER 4
- #define ONE_WS2812_LIGHT_TIME (3*4*8*4*0.125) //us
- //高位先发,按照GRB的顺序发送数据
- //50us以上reset
- //#define WS_L 0,0x8000,0x8000,0x8000
- //#define WS_H 0,0,0,0x8000
- static pwm_values_common_t led_color_seq_values[3][4*8] = {0};
- static nrf_pwm_sequence_t *led_color_seq = NULL;
- static const uint16_t WS_H[4] = {0,0,0,0x8000};
- static const uint16_t WS_L[4] = {0,0x8000,0x8000,0x8000};
- static uint8_t reflash = 1 ;
- struct WS_t{
- uint8_t onoff;
- uint32_t color; //颜色
- };
- static struct WS_t m_wsled[MEMBER_NUM_OF_WS2812] = {0};
- /**
- @brief 设置灯的时序
- @param color - [in] 灯的颜色
- @return 无
- */
- static void WS2812_DisplayDot(uint32_t col)
- {
- uint32_t col_r = (col & 0x00FF0000) >> 16;
- uint32_t col_g = (col & 0x0000FF00) >> 8;
- uint32_t col_b = col & 0x000000FF;
-
- uint32_t t = 0x80;
- uint32_t index = 0;
- for(int i=0;i<8;i++)
- {
- index = i*4;
- if(col_g & t){
- led_color_seq_values[0][index+0] = WS_H[0];
- led_color_seq_values[0][index+1] = WS_H[1];
- led_color_seq_values[0][index+2] = WS_H[2];
- led_color_seq_values[0][index+3] = WS_H[3];
- }else{
- led_color_seq_values[0][index+0] = WS_L[0];
- led_color_seq_values[0][index+1] = WS_L[1];
- led_color_seq_values[0][index+2] = WS_L[2];
- led_color_seq_values[0][index+3] = WS_L[3];
- }
- index = i*4;
- if(col_r & t){
- led_color_seq_values[1][index+0] = WS_H[0];
- led_color_seq_values[1][index+1] = WS_H[1];
- led_color_seq_values[1][index+2] = WS_H[2];
- led_color_seq_values[1][index+3] = WS_H[3];
- }else{
- led_color_seq_values[1][index+0] = WS_L[0];
- led_color_seq_values[1][index+1] = WS_L[1];
- led_color_seq_values[1][index+2] = WS_L[2];
- led_color_seq_values[1][index+3] = WS_L[3];
- }
- index = i*4;
- if(col_b & t){
- led_color_seq_values[2][index+0] = WS_H[0];
- led_color_seq_values[2][index+1] = WS_H[1];
- led_color_seq_values[2][index+2] = WS_H[2];
- led_color_seq_values[2][index+3] = WS_H[3];
- }else{
- led_color_seq_values[2][index+0] = WS_L[0];
- led_color_seq_values[2][index+1] = WS_L[1];
- led_color_seq_values[2][index+2] = WS_L[2];
- led_color_seq_values[2][index+3] = WS_L[3];
- }
- t = t>>1;
- }
- }
- void WS2812_SetColor(uint8_t n,uint32_t color)
- {
- if(n>=MEMBER_NUM_OF_WS2812) return;
- m_wsled[n].color = color;
- reflash = 1;
- }
- void WS2812_Start(uint8_t n,uint32_t color)
- {
- if(n>=MEMBER_NUM_OF_WS2812) return;
- if(m_wsled[n].onoff>0) return;
- m_wsled[n].onoff = 1;
- m_wsled[n].color = color;
- reflash = 1;
- }
- void WS2812_Resum(uint8_t n)
- {
- if(n>=MEMBER_NUM_OF_WS2812) return;
- if(m_wsled[n].onoff>0) return;
- m_wsled[n].onoff = 1;
- reflash = 1;
- }
- void WS2812_Stop(uint8_t n)
- {
- if(n>=MEMBER_NUM_OF_WS2812) return;
- if(m_wsled[n].onoff==0) return;
- m_wsled[n].onoff = 0;
- reflash = 1;
- }
- static void hal_WS2812_Process(void)
- {
- int i;
- static uint8_t state = 0;
- switch(state){
- case 0:{
- if(reflash>0){ reflash = 0;
- for(i=MEMBER_NUM_OF_WS2812-1;i>0;i--){ //显示优先级最高的灯
- if(m_wsled[i].onoff>0){ //SEGGER_RTT_printf(0,"hal_WS2812_Process->play[%d]\n",i);
- WS2812_DisplayDot(m_wsled[i].color);
- break;
- }
- }
- if(i==0) nrf_gpio_pin_write(PIN_RGB_ENABLE,WS2812_DISABLE); //无灯显示,关闭电源
- else{
- nrf_gpio_pin_write(PIN_RGB_ENABLE,WS2812_ENABLE); //打开灯电源
- state = 1;
- }
- }
-
- break;}
- case 1:{
- PWM_SetSimplePwmPlayBack(led_color_seq, WS2812_DISPLAY_NUMBER, PWM_FLAG_STOP);
-
- #if WS2812_FLASH_ALWAYS
- if(reflash>0) state = 0;
- #else
- state = 0;
- #endif
- break;}
- default:state = 0;break;
- }
- }
- #if DEBUG_LEDRUN
- void WS2812_Test(void)
- {
- static uint8_t flag = 0;
- if(flag==0){ flag = 1;
- WS2812_SetColor(WS2812_TEST,WS2812_COLOR_BLACK);
- }else if(flag==1){ flag = 2;
- WS2812_SetColor(WS2812_TEST,WS2812_COLOR_RED);
- }else if(flag==2){ flag = 3;
- WS2812_SetColor(WS2812_TEST,WS2812_COLOR_GREEN);
- }else if(flag==3){ flag = 4;
- WS2812_SetColor(WS2812_TEST,WS2812_COLOR_BLUE);
- }else if(flag==4){ flag = 5;
- WS2812_SetColor(WS2812_TEST,WS2812_COLOR_ORANGE);
- }else if(flag==5){ flag = 6;
- WS2812_SetColor(WS2812_TEST,WS2812_COLOR_WHITE);
- }else if(flag==6){ flag = 7;
- WS2812_Stop(WS2812_TEST);
- }else if(flag==7){ flag = 0;
- WS2812_Resum(WS2812_TEST);
- }
- }
- #endif
- void WS2812_Initialize(void)
- {
- PWM_SetBaseClock(NRF_PWM_CLK_8MHz);
- PWM_SetChannels(PIN_RGB_DAT, NRF_DRV_PWM_PIN_NOT_USED, NRF_DRV_PWM_PIN_NOT_USED, NRF_DRV_PWM_PIN_NOT_USED);
- PWM_SetDutyCycleThreshold(3); //static uint16_t led_pwm_cycle_time = 3; //1 -> 0.125us
- PWM_Initialize();
-
- led_color_seq = PWM_SetComSequence(led_color_seq_values[0], PWM_SEQUENCE_VALUES_LEN(led_color_seq_values),0,0);
-
- nrf_gpio_cfg_output(PIN_RGB_ENABLE);nrf_gpio_pin_write(PIN_RGB_ENABLE,WS2812_ENABLE);
-
- // #if WS2812_FLASH_ALWAYS
- // Process_Start(50,"hal_WS2812_Process",hal_WS2812_Process);
- // #else
- // Process_Start(5,"hal_WS2812_Process",hal_WS2812_Process);
- // #endif
-
- Process_Start(5,"hal_WS2812_Process",hal_WS2812_Process);
-
- #if DEBUG_LEDRUN
- Process_Start(500,"WS2812_Test",WS2812_Test);
- WS2812_Start(WS2812_TEST,WS2812_COLOR_BLACK);
- #endif
- }
|