hal_led_ws2812 - 副本 (2).c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. static uint8_t Led_ChangFlag_PRO =0 ;
  84. static uint8_t Led_PRO_Current =LED_NONE ;
  85. //强制关闭LED
  86. void LED_Close_Enforce(void){
  87. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE);
  88. Led_PRO_Current = LED_NONE;
  89. Led_ChangFlag_PRO =0 ;
  90. }
  91. void LED_Stop(uint8_t n)
  92. {
  93. if(n>=MEMBER_NUM_OF_LED) return;
  94. if(n >= Led_PRO_Current) {
  95. m_wsled[n].onoff = 0;
  96. m_wsled[n].color = COLOR_BLACK;
  97. Led_ChangFlag_PRO =1;
  98. }
  99. }
  100. void LED_Start(uint8_t n,uint32_t color)
  101. {
  102. if(n>=MEMBER_NUM_OF_LED) return;
  103. if(n > Led_PRO_Current) {
  104. // DEBUG_LOG(">>>>>>>LED_Start%d\n",n);
  105. m_wsled[n].onoff = 1;
  106. m_wsled[n].color = color;
  107. Led_PRO_Current =n;
  108. Led_ChangFlag_PRO =1;
  109. }
  110. // DEBUG_LOG("LED_Start:%02x\n",color);
  111. }
  112. void LED_Process(void)
  113. {
  114. static uint8_t state =0;
  115. uint8_t i =0;
  116. switch(state){
  117. case 0:
  118. if(Led_ChangFlag_PRO){
  119. state =1;
  120. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_ENABLE);
  121. }
  122. break;
  123. case 1:
  124. if(Led_PRO_Current < MEMBER_NUM_OF_LED ){
  125. if(m_wsled[Led_PRO_Current].color != COLOR_BLACK){ //关闭所有灯
  126. WS2812_DisplayDot(m_wsled[Led_PRO_Current].color);
  127. SetSimplePwmPlayBack(led_color_seq, LED_DEFAULT_NUMBER, led_pwm_mode);
  128. }
  129. else{
  130. Led_PRO_Current = LED_NONE;
  131. for(i =Led_PRO_Current;i > 0;i--){
  132. if(1 == m_wsled[i].onoff){
  133. WS2812_DisplayDot(m_wsled[i].color);
  134. SetSimplePwmPlayBack(led_color_seq, LED_DEFAULT_NUMBER, led_pwm_mode);
  135. break;
  136. }
  137. }
  138. if(0 == i)nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE);
  139. }
  140. // DEBUG_LOG("i:%d LED_Start:%02x\n",i,m_wsled[i].color);
  141. }
  142. Led_ChangFlag_PRO = 0;
  143. state =0;
  144. break;
  145. }
  146. }
  147. #if DEBUG_LEDRGB
  148. void LED_Run(void)
  149. {
  150. #if DEBUG_LEDRGB
  151. static uint32_t temp=0;
  152. if(temp==0){
  153. LED_Start(LED_RUN,COLOR_BLACK);
  154. }else if(temp==1){
  155. LED_Start(LED_RUN,COLOR_WHITE);
  156. }else if(temp==2){
  157. LED_Start(LED_RUN,COLOR_RED);
  158. }else if(temp==3){
  159. LED_Start(LED_RUN,COLOR_BLUE);
  160. }else if(temp==4){
  161. LED_Start(LED_RUN,COLOR_ORANGE);
  162. }else if(temp==5){
  163. LED_Start(LED_RUN,COLOR_PURPLE);
  164. }else if(temp==6){
  165. LED_Start(LED_RUN,COLOR_YELLOW);
  166. }else if(temp==7){
  167. LED_Start(LED_RUN,COLOR_CYAN);
  168. }
  169. if(++temp>=8) temp = 0;
  170. // DEBUG_LOG("LED_Run,%d\n",temp);;
  171. #else
  172. static uint32_t flag = 1;
  173. if(flag){
  174. nrf_gpio_pin_write(PIN_LED_RUN,LED_SMALL_ENABLE);
  175. flag = 0;
  176. }else{
  177. nrf_gpio_pin_write(PIN_LED_RUN,LED_SMALL_DISABLE);
  178. flag = 1;
  179. }
  180. #endif
  181. }
  182. void LED_RGB_Init(void)
  183. {
  184. Process_Start(500,"LED_Run",LED_Run);
  185. Process_SetHoldOn(LED_Run,1);
  186. }
  187. #endif
  188. void cb_LED_Wakeup(uint32_t t)
  189. {
  190. Pwm_Initialize();
  191. }
  192. void cb_LED_Sleep(uint32_t t)
  193. {
  194. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE);
  195. Pwm_UnInitialize();
  196. }
  197. void LED_Init(void)
  198. {
  199. SetPwm_BaseClock(NRF_PWM_CLK_8MHz);
  200. SetPwm_Channels(led_channel[0], led_channel[1], led_channel[2], led_channel[3]);
  201. SetPwm_DutyCycleThreshold(led_pwm_cycle_time);
  202. Pwm_Initialize();
  203. led_color_seq = Pwm_SetComSequence(led_color_seq_values[0], PWM_SEQUENCE_VALUES_LEN(led_color_seq_values),0,0);
  204. nrf_gpio_cfg_output(PIN_LED_ENABLE); nrf_gpio_pin_write(PIN_LED_ENABLE,LED_ENABLE);
  205. nrf_gpio_cfg_output(PIN_LED_RUN); nrf_gpio_pin_write(PIN_LED_RUN,LED_SMALL_DISABLE);
  206. Process_Start(10,"LED_Process",LED_Process);
  207. #if DEBUG_LEDRGB
  208. LED_RGB_Init();
  209. #endif
  210. Sleep_Regist(cb_LED_Sleep);
  211. Wakeup_Regist(cb_LED_Wakeup);
  212. }