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. Process_Start(100,"LED",LED_Process);
  133. }
  134. }
  135. break;
  136. case 1:
  137. if(reflash){
  138. state =0;
  139. Process_Start(5,"LED",LED_Process);
  140. }
  141. else SetSimplePwmPlayBack(led_color_seq, LED_DEFAULT_NUMBER, PWM_FLAG_STOP);
  142. break;
  143. default:state = 0;break;
  144. }
  145. }
  146. #if DEBUG_LEDRGB
  147. static uint32_t ledbuff[]={
  148. COLOR_WHITE
  149. ,COLOR_RED
  150. ,COLOR_GREEN
  151. ,COLOR_BLUE
  152. ,COLOR_ORANGE
  153. ,COLOR_PURPLE
  154. ,COLOR_YELLOW
  155. };
  156. static void WS2812_Test(void)
  157. {
  158. static uint8_t flag = 0;
  159. static uint8_t i = 0;
  160. if(flag==0){ flag = 1;
  161. if(i < 6){
  162. LED_Start(LED_RESTART,ledbuff[i]);
  163. i++;
  164. }
  165. else i =0;
  166. }else{ flag = 0;
  167. LED_Stop(LED_RESTART);
  168. }
  169. }
  170. #endif
  171. void cb_LED_Wakeup(uint32_t t)
  172. {
  173. Pwm_Initialize();
  174. DEBUG_LOG("Pwm_Initialize:%d,%X\n");
  175. }
  176. void cb_LED_Sleep(uint32_t t)
  177. {
  178. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE);
  179. Pwm_UnInitialize();
  180. DEBUG_LOG("Pwm_UnInitialize\n");
  181. }
  182. void LED_Init(void)
  183. {
  184. SetPwm_BaseClock(NRF_PWM_CLK_8MHz);
  185. SetPwm_Channels(PIN_LED_CONTROL,NRF_DRV_PWM_PIN_NOT_USED,NRF_DRV_PWM_PIN_NOT_USED,NRF_DRV_PWM_PIN_NOT_USED);
  186. SetPwm_DutyCycleThreshold(3);
  187. Pwm_Initialize();
  188. led_color_seq = Pwm_SetComSequence(led_color_seq_values[0], PWM_SEQUENCE_VALUES_LEN(led_color_seq_values),0,0);
  189. nrf_gpio_cfg(
  190. PIN_LED_ENABLE,
  191. NRF_GPIO_PIN_DIR_OUTPUT,
  192. NRF_GPIO_PIN_INPUT_DISCONNECT,
  193. NRF_GPIO_PIN_NOPULL,
  194. NRF_GPIO_PIN_S0D1,
  195. NRF_GPIO_PIN_NOSENSE);
  196. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_ENABLE);
  197. Process_Start(5,"LED",LED_Process);
  198. #if DEBUG_LEDRGB
  199. Process_Start(1000,"WS2812",WS2812_Test);
  200. Process_SetHoldOn(WS2812_Test,1);
  201. #endif
  202. Sleep_Regist(cb_LED_Sleep);
  203. Wakeup_Regist(cb_LED_Wakeup);
  204. }