hal_led_ws2812.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "hal_led.h"
  2. #include "bsp_gpio.h"
  3. #include "nrf_gpio.h"
  4. #include "usr_config.h"
  5. #include "bsp_time.h"
  6. #include "system.h"
  7. #include "hal_ble_client.h"
  8. #include "bsp_pwm.h"
  9. #include "nrf_delay.h"
  10. #define LED_ENABLE 0
  11. #define LED_DISABLE 1
  12. #define LED_DEFAULT_NUMBER 4
  13. #define ONE_LED_LIGHT_TIME (3*4*8*4*0.125) //us
  14. //高位先发,按照GRB的顺序发送数据
  15. //50us以上reset
  16. //#define WS_L 0,0x8000,0x8000,0x8000
  17. //#define WS_H 0,0,0,0x8000
  18. static pwm_values_common_t led_color_seq_values[3][4*8] = {0};
  19. static nrf_pwm_sequence_t *led_color_seq = NULL;
  20. static const uint16_t WS_H[4] = {0,0,0,0x8000};
  21. static const uint16_t WS_L[4] = {0,0x8000,0x8000,0x8000};
  22. struct WS_t{
  23. uint8_t onoff;
  24. uint32_t color; //颜色
  25. };
  26. static struct WS_t m_wsled[LED_NUM_OF_LED] = {0};
  27. /**
  28. @brief 设置灯的时序
  29. @param color - [in] 灯的颜色
  30. @return 无
  31. */
  32. static void WS2812_DisplayDot(uint32_t col)
  33. {
  34. uint32_t col_r = (col & 0x00FF0000) >> 16;
  35. uint32_t col_g = (col & 0x0000FF00) >> 8;
  36. uint32_t col_b = col & 0x000000FF;
  37. uint32_t t = 0x80;
  38. uint32_t index = 0;
  39. for(int i=0;i<8;i++)
  40. {
  41. index = i*4;
  42. if(col_g & t){
  43. led_color_seq_values[0][index+0] = WS_H[0];
  44. led_color_seq_values[0][index+1] = WS_H[1];
  45. led_color_seq_values[0][index+2] = WS_H[2];
  46. led_color_seq_values[0][index+3] = WS_H[3];
  47. }else{
  48. led_color_seq_values[0][index+0] = WS_L[0];
  49. led_color_seq_values[0][index+1] = WS_L[1];
  50. led_color_seq_values[0][index+2] = WS_L[2];
  51. led_color_seq_values[0][index+3] = WS_L[3];
  52. }
  53. index = i*4;
  54. if(col_r & t){
  55. led_color_seq_values[1][index+0] = WS_H[0];
  56. led_color_seq_values[1][index+1] = WS_H[1];
  57. led_color_seq_values[1][index+2] = WS_H[2];
  58. led_color_seq_values[1][index+3] = WS_H[3];
  59. }else{
  60. led_color_seq_values[1][index+0] = WS_L[0];
  61. led_color_seq_values[1][index+1] = WS_L[1];
  62. led_color_seq_values[1][index+2] = WS_L[2];
  63. led_color_seq_values[1][index+3] = WS_L[3];
  64. }
  65. index = i*4;
  66. if(col_b & t){
  67. led_color_seq_values[2][index+0] = WS_H[0];
  68. led_color_seq_values[2][index+1] = WS_H[1];
  69. led_color_seq_values[2][index+2] = WS_H[2];
  70. led_color_seq_values[2][index+3] = WS_H[3];
  71. }else{
  72. led_color_seq_values[2][index+0] = WS_L[0];
  73. led_color_seq_values[2][index+1] = WS_L[1];
  74. led_color_seq_values[2][index+2] = WS_L[2];
  75. led_color_seq_values[2][index+3] = WS_L[3];
  76. }
  77. t = t>>1;
  78. }
  79. }
  80. static uint8_t reflash =0 ;
  81. //强制关闭LED
  82. void LED_Close_Enforce(void){
  83. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE);
  84. reflash =0 ;
  85. }
  86. void LED_SetColor(uint8_t n,uint32_t color)
  87. {
  88. if(n>=LED_NUM_OF_LED) return;
  89. m_wsled[n].color = color;
  90. reflash = 1;
  91. // DEBUG_LOG("WS2812_SetColor[%d]:0x%4X\n",n,color);
  92. }
  93. void LED_Stop(uint8_t n)
  94. {
  95. if(n>=LED_NUM_OF_LED) return;
  96. if(0 == m_wsled[n].onoff) return;
  97. // DEBUG_LOG("LED_Stop:%d\n",n);
  98. m_wsled[n].onoff = 0;
  99. reflash =1;
  100. }
  101. void LED_Start(uint8_t n,uint32_t color)
  102. {
  103. if(n>=LED_NUM_OF_LED) return;
  104. if(1 == m_wsled[n].onoff && m_wsled[n].color == color) return;
  105. m_wsled[n].onoff = 1;
  106. m_wsled[n].color = color;
  107. reflash =1;
  108. // DEBUG_LOG("LED_Start:%d,%X\n",n,color);
  109. }
  110. void LED_Process(void)
  111. {
  112. static uint8_t state =0;
  113. uint8_t i =0;
  114. switch(state){
  115. case 0:
  116. if(reflash){reflash=0;
  117. for(i=LED_NUM_OF_LED-1;i>0;i--){ //显示优先级最高的灯
  118. if(m_wsled[i].onoff>0){
  119. // DEBUG_LOG("hal_WS2812_Process->play[%d]\n",i);
  120. WS2812_DisplayDot(m_wsled[i].color);
  121. break;
  122. }
  123. }
  124. if(i==0) nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE); //无灯显示,关闭电源
  125. else{
  126. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_ENABLE); //打开灯电源
  127. state = 1;
  128. Process_Start(100,"LED_Process",LED_Process);
  129. }
  130. }
  131. break;
  132. case 1:
  133. if(reflash){
  134. state =0;
  135. Process_Start(5,"LED_Process",LED_Process);
  136. }
  137. else SetSimplePwmPlayBack(led_color_seq, LED_DEFAULT_NUMBER, PWM_FLAG_STOP);
  138. break;
  139. default:state = 0;break;
  140. }
  141. }
  142. #if DEBUG_LEDRGB
  143. void WS2812_Test(void)
  144. {
  145. static uint8_t flag = 0;
  146. if(flag==0){ flag = 1;
  147. LED_Start(WS2812_TEST,WS2812_COLOR_ORANGE);
  148. }else{ flag = 0;
  149. LED_Stop(WS2812_TEST);
  150. }
  151. }
  152. #endif
  153. void cb_LED_Wakeup(uint32_t t)
  154. {
  155. Pwm_Initialize();
  156. }
  157. void cb_LED_Sleep(uint32_t t)
  158. {
  159. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE);
  160. Pwm_UnInitialize();
  161. }
  162. void LED_Init(void)
  163. {
  164. SetPwm_BaseClock(NRF_PWM_CLK_8MHz);
  165. SetPwm_Channels(PIN_LED_CONTROL,NRF_DRV_PWM_PIN_NOT_USED,NRF_DRV_PWM_PIN_NOT_USED,NRF_DRV_PWM_PIN_NOT_USED);
  166. SetPwm_DutyCycleThreshold(3);
  167. Pwm_Initialize();
  168. led_color_seq = Pwm_SetComSequence(led_color_seq_values[0], PWM_SEQUENCE_VALUES_LEN(led_color_seq_values),0,0);
  169. nrf_gpio_cfg(
  170. PIN_LED_ENABLE,
  171. NRF_GPIO_PIN_DIR_OUTPUT,
  172. NRF_GPIO_PIN_INPUT_DISCONNECT,
  173. NRF_GPIO_PIN_NOPULL,
  174. NRF_GPIO_PIN_S0D1,
  175. NRF_GPIO_PIN_NOSENSE);
  176. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_ENABLE);
  177. Process_Start(5,"LED_Process",LED_Process);
  178. #if DEBUG_LEDRGB
  179. Process_Start(2000,"WS2812_Test",WS2812_Test);
  180. #endif
  181. Sleep_Regist(cb_LED_Sleep);
  182. Wakeup_Regist(cb_LED_Wakeup);
  183. }