hal_led_ws2812 - 副本.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 1
  11. #define LED_DISABLE 0
  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 uint32_t led_channel[4] = {PIN_LED_CONTROL,NRF_DRV_PWM_PIN_NOT_USED,NRF_DRV_PWM_PIN_NOT_USED,NRF_DRV_PWM_PIN_NOT_USED};
  19. static uint16_t led_pwm_cycle_time = 3; //1 -> 0.125us
  20. static uint32_t led_pwm_mode = PWM_FLAG_STOP;
  21. static pwm_values_common_t led_color_seq_values[3][4*8] = {0};
  22. static nrf_pwm_sequence_t *led_color_seq = NULL;
  23. static const uint16_t WS_H[4] = {0,0,0,0x8000};
  24. static const uint16_t WS_L[4] = {0,0x8000,0x8000,0x8000};
  25. struct WS_t{
  26. uint8_t onoff;
  27. uint32_t color; //颜色
  28. };
  29. static struct WS_t m_wsled[MEMBER_NUM_OF_LED] = {0};
  30. /**
  31. @brief 设置灯的时序
  32. @param color - [in] 灯的颜色
  33. @return 无
  34. */
  35. static void WS2812_DisplayDot(uint32_t col)
  36. {
  37. uint32_t col_r = (col & 0x00FF0000) >> 16;
  38. uint32_t col_g = (col & 0x0000FF00) >> 8;
  39. uint32_t col_b = col & 0x000000FF;
  40. uint32_t t = 0x80;
  41. uint32_t index = 0;
  42. for(int i=0;i<8;i++)
  43. {
  44. index = i*4;
  45. if(col_g & t){
  46. led_color_seq_values[0][index+0] = WS_H[0];
  47. led_color_seq_values[0][index+1] = WS_H[1];
  48. led_color_seq_values[0][index+2] = WS_H[2];
  49. led_color_seq_values[0][index+3] = WS_H[3];
  50. }else{
  51. led_color_seq_values[0][index+0] = WS_L[0];
  52. led_color_seq_values[0][index+1] = WS_L[1];
  53. led_color_seq_values[0][index+2] = WS_L[2];
  54. led_color_seq_values[0][index+3] = WS_L[3];
  55. }
  56. index = i*4;
  57. if(col_r & t){
  58. led_color_seq_values[1][index+0] = WS_H[0];
  59. led_color_seq_values[1][index+1] = WS_H[1];
  60. led_color_seq_values[1][index+2] = WS_H[2];
  61. led_color_seq_values[1][index+3] = WS_H[3];
  62. }else{
  63. led_color_seq_values[1][index+0] = WS_L[0];
  64. led_color_seq_values[1][index+1] = WS_L[1];
  65. led_color_seq_values[1][index+2] = WS_L[2];
  66. led_color_seq_values[1][index+3] = WS_L[3];
  67. }
  68. index = i*4;
  69. if(col_b & t){
  70. led_color_seq_values[2][index+0] = WS_H[0];
  71. led_color_seq_values[2][index+1] = WS_H[1];
  72. led_color_seq_values[2][index+2] = WS_H[2];
  73. led_color_seq_values[2][index+3] = WS_H[3];
  74. }else{
  75. led_color_seq_values[2][index+0] = WS_L[0];
  76. led_color_seq_values[2][index+1] = WS_L[1];
  77. led_color_seq_values[2][index+2] = WS_L[2];
  78. led_color_seq_values[2][index+3] = WS_L[3];
  79. }
  80. t = t>>1;
  81. }
  82. }
  83. /**
  84. @brief 设置灯的数量
  85. @param color - [in] 颜色
  86. @param led_num - [in] 灯的数量
  87. @return 无
  88. */
  89. static void SetLedColor(uint32_t color, uint32_t led_num)
  90. {
  91. if(color == COLOR_BLACK && led_num == LED_DEFAULT_NUMBER) //关闭所有灯
  92. {
  93. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE);nrf_delay_ms(1);
  94. // nrf_gpio_pin_write(PIN_LED_ENABLE,LED_ENABLE);nrf_delay_ms(1);
  95. }else{
  96. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_ENABLE);nrf_delay_ms(1);
  97. WS2812_DisplayDot(color);
  98. SetSimplePwmPlayBack(led_color_seq, led_num, led_pwm_mode);
  99. }
  100. }
  101. //强制关闭LED
  102. void LED_Close_Enforce(void){
  103. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE);nrf_delay_ms(1);
  104. }
  105. void LED_Start(uint8_t n)
  106. {
  107. if(n>=MEMBER_NUM_OF_LED) return;
  108. if(m_wsled[n].onoff!=1) m_wsled[n].onoff = 1;
  109. }
  110. void LED_Stop(uint8_t n)
  111. {
  112. if(n>=MEMBER_NUM_OF_LED) return;
  113. if(m_wsled[n].onoff!=0) {
  114. m_wsled[n].onoff = 0;
  115. }
  116. }
  117. void LED_SetColor(uint8_t n,uint32_t color)
  118. {
  119. if(n>=MEMBER_NUM_OF_LED) return;
  120. m_wsled[n].color = color;
  121. // DEBUG_LOG("LED_SetColor:%02x\n",color);
  122. }
  123. void LED_Process(void)
  124. {
  125. for(int i=MEMBER_NUM_OF_LED-1;i>0;i--){
  126. if(m_wsled[i].onoff>0){
  127. SetLedColor(m_wsled[i].color, LED_DEFAULT_NUMBER);
  128. // DEBUG_LOG("i:%d LED_SetColor:%02x\n",i,m_wsled[i].color);
  129. return;
  130. }
  131. }
  132. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE);
  133. }
  134. void LED_Run(void)
  135. {
  136. #if DEBUG_LEDRGB
  137. static uint32_t temp=0;
  138. if(temp==0){
  139. LED_SetColor(LED_RUN,COLOR_RED);
  140. }else if(temp==1){
  141. LED_SetColor(LED_RUN,COLOR_GREEN);
  142. }else if(temp==2){
  143. LED_SetColor(LED_RUN,COLOR_BLUE);
  144. // }else if(temp==3){
  145. // LED_SetColor(LED_RUN,COLOR_ORANGE);
  146. // }else if(temp==4){
  147. // LED_SetColor(LED_RUN,COLOR_YELLOW);
  148. // }else if(temp==5){
  149. // LED_SetColor(LED_RUN,COLOR_PURPLE);
  150. }
  151. if(++temp>=3) temp = 0;
  152. // DEBUG_LOG("LED_Run,%d\n",temp);;
  153. #else
  154. static uint32_t flag = 1;
  155. if(flag){
  156. nrf_gpio_pin_write(PIN_LED_RUN,LED_SMALL_ENABLE);
  157. flag = 0;
  158. }else{
  159. nrf_gpio_pin_write(PIN_LED_RUN,LED_SMALL_DISABLE);
  160. flag = 1;
  161. }
  162. #endif
  163. }
  164. void LED_Run_Init(void)
  165. {
  166. LED_Start(LED_RUN);
  167. #if DEBUG_LEDRGB
  168. Process_Start(500,"LED_Run",LED_Run);
  169. #else
  170. Process_Start(20,"LED_Run",LED_Run);
  171. #endif
  172. Process_SetHoldOn(LED_Run,1);
  173. }
  174. void cb_LED_Wakeup(uint32_t t)
  175. {
  176. Pwm_Initialize();
  177. }
  178. void cb_LED_Sleep(uint32_t t)
  179. {
  180. SetLedColor(COLOR_BLACK, LED_DEFAULT_NUMBER);
  181. Pwm_UnInitialize();
  182. }
  183. void LED_Init(void)
  184. {
  185. SetPwm_BaseClock(NRF_PWM_CLK_8MHz);
  186. SetPwm_Channels(led_channel[0], led_channel[1], led_channel[2], led_channel[3]);
  187. SetPwm_DutyCycleThreshold(led_pwm_cycle_time);
  188. Pwm_Initialize();
  189. led_color_seq = Pwm_SetComSequence(led_color_seq_values[0], PWM_SEQUENCE_VALUES_LEN(led_color_seq_values),0,0);
  190. LED_Start(LED_NONE);
  191. nrf_gpio_cfg_output(PIN_LED_ENABLE); nrf_gpio_pin_write(PIN_LED_ENABLE,LED_ENABLE);
  192. nrf_gpio_cfg_output(PIN_LED_RUN); nrf_gpio_pin_write(PIN_LED_RUN,LED_SMALL_DISABLE);
  193. Process_Start(10,"LED_Process",LED_Process);
  194. #if DEBUG_LEDRUN
  195. LED_Run_Init();
  196. #endif
  197. Sleep_Regist(cb_LED_Sleep);
  198. Wakeup_Regist(cb_LED_Wakeup);
  199. }