hal_led_ws2812.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. void WS2812_Pwm_Play(void)
  28. {
  29. SetSimplePwmPlayBack(led_color_seq, LED_DEFAULT_NUMBER, PWM_FLAG_STOP);
  30. }
  31. /**
  32. @brief 设置灯的时序
  33. @param color - [in] 灯的颜色
  34. @return 无
  35. */
  36. void WS2812_DisplayDot(uint32_t col)
  37. {
  38. uint32_t col_r = (col & 0x00FF0000) >> 16;
  39. uint32_t col_g = (col & 0x0000FF00) >> 8;
  40. uint32_t col_b = col & 0x000000FF;
  41. uint32_t t = 0x80;
  42. uint32_t index = 0;
  43. for(int i=0;i<8;i++)
  44. {
  45. index = i*4;
  46. if(col_g & t){
  47. led_color_seq_values[0][index+0] = WS_H[0];
  48. led_color_seq_values[0][index+1] = WS_H[1];
  49. led_color_seq_values[0][index+2] = WS_H[2];
  50. led_color_seq_values[0][index+3] = WS_H[3];
  51. }else{
  52. led_color_seq_values[0][index+0] = WS_L[0];
  53. led_color_seq_values[0][index+1] = WS_L[1];
  54. led_color_seq_values[0][index+2] = WS_L[2];
  55. led_color_seq_values[0][index+3] = WS_L[3];
  56. }
  57. index = i*4;
  58. if(col_r & t){
  59. led_color_seq_values[1][index+0] = WS_H[0];
  60. led_color_seq_values[1][index+1] = WS_H[1];
  61. led_color_seq_values[1][index+2] = WS_H[2];
  62. led_color_seq_values[1][index+3] = WS_H[3];
  63. }else{
  64. led_color_seq_values[1][index+0] = WS_L[0];
  65. led_color_seq_values[1][index+1] = WS_L[1];
  66. led_color_seq_values[1][index+2] = WS_L[2];
  67. led_color_seq_values[1][index+3] = WS_L[3];
  68. }
  69. index = i*4;
  70. if(col_b & t){
  71. led_color_seq_values[2][index+0] = WS_H[0];
  72. led_color_seq_values[2][index+1] = WS_H[1];
  73. led_color_seq_values[2][index+2] = WS_H[2];
  74. led_color_seq_values[2][index+3] = WS_H[3];
  75. }else{
  76. led_color_seq_values[2][index+0] = WS_L[0];
  77. led_color_seq_values[2][index+1] = WS_L[1];
  78. led_color_seq_values[2][index+2] = WS_L[2];
  79. led_color_seq_values[2][index+3] = WS_L[3];
  80. }
  81. t = t>>1;
  82. }
  83. }
  84. static uint8_t reflash =0 ;
  85. //强制关闭LED
  86. void LED_Close_Enforce(void){
  87. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE);
  88. reflash =0 ;
  89. }
  90. void LED_SetColor(uint8_t n,uint32_t color)
  91. {
  92. if(n>=LED_NUM_OF_LED) return;
  93. m_wsled[n].color = color;
  94. reflash = 1;
  95. DEBUG_LOG("WS2812_SetColor[%d]:0x%4X\n",n,color);
  96. }
  97. void LED_Stop(uint8_t n)
  98. {
  99. if(n>=LED_NUM_OF_LED) return;
  100. if(0 == m_wsled[n].onoff) return;
  101. DEBUG_LOG("LED_Stop:%d\n",n);
  102. m_wsled[n].onoff = 0;
  103. reflash =1;
  104. }
  105. void LED_Start(uint8_t n,uint32_t color)
  106. {
  107. if(n>=LED_NUM_OF_LED) return;
  108. if(1 == m_wsled[n].onoff && m_wsled[n].color == color) return;
  109. m_wsled[n].onoff = 1;
  110. m_wsled[n].color = color;
  111. reflash =1;
  112. DEBUG_LOG("LED_Start:%d,%X\n",n,color);
  113. }
  114. void LED_Process(void)
  115. {
  116. static uint8_t state =0;
  117. uint8_t i =0;
  118. switch(state){
  119. case 0:
  120. if(reflash){reflash=0;
  121. for(i=LED_NUM_OF_LED-1;i>0;i--){ //显示优先级最高的灯
  122. if(m_wsled[i].onoff>0){
  123. // DEBUG_LOG("hal_WS2812_Process->play[%d]\n",i);
  124. WS2812_DisplayDot(m_wsled[i].color);
  125. break;
  126. }
  127. }
  128. if(i==0) nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE); //无灯显示,关闭电源
  129. else{
  130. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_ENABLE); //打开灯电源
  131. state = 1;
  132. }
  133. }
  134. break;
  135. case 1:
  136. if(reflash){
  137. state =0;
  138. // Process_Start(5,"LED",LED_Process);
  139. }
  140. else SetSimplePwmPlayBack(led_color_seq, LED_DEFAULT_NUMBER, PWM_FLAG_STOP);
  141. break;
  142. default:state = 0;break;
  143. }
  144. }
  145. #if DEBUG_LEDRGB
  146. static uint32_t ledbuff[]={
  147. COLOR_WHITE
  148. ,COLOR_RED
  149. ,COLOR_GREEN
  150. ,COLOR_BLUE
  151. ,COLOR_ORANGE
  152. ,COLOR_PURPLE
  153. ,COLOR_YELLOW
  154. };
  155. static void WS2812_Test(void)
  156. {
  157. static uint8_t flag = 0;
  158. static uint8_t i = 0;
  159. if(flag==0){ flag = 1;
  160. if(i < 6){
  161. LED_Start(LED_RESTART,ledbuff[i]);
  162. i++;
  163. }
  164. else i =0;
  165. }else{ flag = 0;
  166. LED_Stop(LED_RESTART);
  167. }
  168. }
  169. #endif
  170. void cb_LED_Wakeup(uint32_t t)
  171. {
  172. Pwm_Initialize();
  173. DEBUG_LOG("Pwm_Initialize:%d,%X\n");
  174. }
  175. void cb_LED_Sleep(uint32_t t)
  176. {
  177. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE);
  178. Pwm_UnInitialize();
  179. DEBUG_LOG("Pwm_UnInitialize\n");
  180. }
  181. void LED_Init(void)
  182. {
  183. SetPwm_BaseClock(NRF_PWM_CLK_8MHz);
  184. SetPwm_Channels(PIN_LED_CONTROL,NRF_DRV_PWM_PIN_NOT_USED,NRF_DRV_PWM_PIN_NOT_USED,NRF_DRV_PWM_PIN_NOT_USED);
  185. SetPwm_DutyCycleThreshold(3);
  186. Pwm_Initialize();
  187. led_color_seq = Pwm_SetComSequence(led_color_seq_values[0], PWM_SEQUENCE_VALUES_LEN(led_color_seq_values),0,0);
  188. nrf_gpio_cfg(
  189. PIN_LED_ENABLE,
  190. NRF_GPIO_PIN_DIR_OUTPUT,
  191. NRF_GPIO_PIN_INPUT_DISCONNECT,
  192. NRF_GPIO_PIN_NOPULL,
  193. NRF_GPIO_PIN_S0D1,
  194. NRF_GPIO_PIN_NOSENSE);
  195. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_ENABLE);
  196. Process_Start(5,"LED",LED_Process);
  197. #if DEBUG_LEDRGB
  198. Process_Start(1000,"WS2812",WS2812_Test);
  199. Process_SetHoldOn(WS2812_Test,1);
  200. #endif
  201. Sleep_Regist(cb_LED_Sleep);
  202. Wakeup_Regist(cb_LED_Wakeup);
  203. }