hal_led_ws2812 - 副本.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. DEBUG_LOG("!!!!!!LED_Close_Enforce\n");
  84. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE);
  85. reflash =0 ;
  86. }
  87. void LED_SetColor(uint8_t n,uint32_t color)
  88. {
  89. if(n>=LED_NUM_OF_LED) return;
  90. m_wsled[n].color = color;
  91. reflash = 1;
  92. DEBUG_LOG("WS2812_SetColor[%d]:0x%4X\n",n,color);
  93. }
  94. void LED_Stop(uint8_t n)
  95. {
  96. if(n>=LED_NUM_OF_LED) return;
  97. if(0 == m_wsled[n].onoff) return;
  98. DEBUG_LOG("LED_Stop:%d\n",n);
  99. m_wsled[n].onoff = 0;
  100. reflash =1;
  101. }
  102. void LED_Start(uint8_t n,uint32_t color)
  103. {
  104. if(n>=LED_NUM_OF_LED) return;
  105. if(1 == m_wsled[n].onoff && m_wsled[n].color == color) return;
  106. m_wsled[n].onoff = 1;
  107. m_wsled[n].color = color;
  108. reflash =1;
  109. DEBUG_LOG("LED_Start:%d,%X\n",n,color);
  110. }
  111. void LED_Process(void)
  112. {
  113. static uint8_t state =0;
  114. uint8_t i =0;
  115. switch(state){
  116. case 0:
  117. if(reflash){reflash=0;
  118. for(i=LED_NUM_OF_LED-1;i>0;i--){ //显示优先级最高的灯
  119. if(m_wsled[i].onoff>0){
  120. DEBUG_LOG("!!!!!hal_WS2812_Process->play[%d]\n",i);
  121. WS2812_DisplayDot(m_wsled[i].color);
  122. break;
  123. }
  124. }
  125. if(i==0) {
  126. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE); //无灯显示,关闭电源
  127. DEBUG_LOG("!!!!LED_DISABLE\n");
  128. }
  129. else{
  130. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_ENABLE); //打开灯电源
  131. state = 1;
  132. }
  133. }
  134. break;
  135. case 1:
  136. DEBUG_LOG("!!!!!!SetSimplePwmPlayBack\n",i);
  137. SetSimplePwmPlayBack(led_color_seq, LED_DEFAULT_NUMBER, PWM_FLAG_STOP);
  138. state =0;
  139. break;
  140. default:state = 0;break;
  141. }
  142. }
  143. #if DEBUG_LEDRGB
  144. void WS2812_Test(void)
  145. {
  146. static uint8_t flag = 0;
  147. if(flag==0){ flag = 1;
  148. LED_Start(WS2812_TEST,WS2812_COLOR_ORANGE);
  149. }else{ flag = 0;
  150. LED_Stop(WS2812_TEST);
  151. }
  152. }
  153. #endif
  154. void cb_LED_Wakeup(uint32_t t)
  155. {
  156. Pwm_Initialize();
  157. // DEBUG_LOG("!!!!cb_LED_Wakeup\n");
  158. }
  159. void cb_LED_Sleep(uint32_t t)
  160. {
  161. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE);
  162. Pwm_UnInitialize();
  163. // DEBUG_LOG("!!!!cb_LED_Sleep\n");
  164. }
  165. void LED_Init(void)
  166. {
  167. SetPwm_BaseClock(NRF_PWM_CLK_8MHz);
  168. SetPwm_Channels(PIN_LED_CONTROL,NRF_DRV_PWM_PIN_NOT_USED,NRF_DRV_PWM_PIN_NOT_USED,NRF_DRV_PWM_PIN_NOT_USED);
  169. SetPwm_DutyCycleThreshold(3);
  170. Pwm_Initialize();
  171. led_color_seq = Pwm_SetComSequence(led_color_seq_values[0], PWM_SEQUENCE_VALUES_LEN(led_color_seq_values),0,0);
  172. nrf_gpio_cfg_output(PIN_LED_ENABLE); nrf_gpio_pin_write(PIN_LED_ENABLE,LED_ENABLE);
  173. Process_Start(5,"LED_Process",LED_Process);
  174. #if DEBUG_LEDRGB
  175. Process_Start(2000,"WS2812_Test",WS2812_Test);
  176. #endif
  177. Sleep_Regist(cb_LED_Sleep);
  178. Wakeup_Regist(cb_LED_Wakeup);
  179. }