hal_led.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 2
  13. //高位先发,按照GRB的顺序发送数据
  14. //50us以上reset
  15. //#define WS_L 0,0x8000,0x8000,0x8000
  16. //#define WS_H 0,0,0,0x8000
  17. static uint32_t led_channel[4] = {PIN_LED_R|NRF_DRV_PWM_PIN_INVERTED,PIN_LED_G|NRF_DRV_PWM_PIN_INVERTED,PIN_LED_B|NRF_DRV_PWM_PIN_INVERTED,NRF_DRV_PWM_PIN_NOT_USED};
  18. static uint16_t led_pwm_cycle_time = 0xFF; //1 -> 0.125us
  19. static uint32_t led_pwm_mode = PWM_FLAG_LOOP;
  20. static pwm_values_individual_t led_color_seq_values = {0xFF,0xFF,0xFF,0xFF};
  21. static nrf_pwm_sequence_t *led_color_seq = NULL;
  22. struct WS_t{
  23. uint8_t onoff;
  24. uint32_t color; //颜色
  25. };
  26. static struct WS_t m_wsled[MEMBER_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. led_color_seq_values.channel_0 = ~col_r;
  38. led_color_seq_values.channel_1 = ~col_g;
  39. led_color_seq_values.channel_2 = ~col_b;
  40. }
  41. /**
  42. @brief 设置灯的数量
  43. @param color - [in] 颜色
  44. @param led_num - [in] 灯的数量
  45. @return 无
  46. */
  47. static void SetLedColor(uint32_t color, uint32_t led_num)
  48. {
  49. WS2812_DisplayDot(color);
  50. SetSimplePwmPlayBack(led_color_seq, led_num, led_pwm_mode);
  51. }
  52. //强制关闭LED
  53. void LED_Close_Enforce(void){
  54. SetLedColor(COLOR_BLACK,LED_DEFAULT_NUMBER);
  55. }
  56. void LED_Start(uint8_t n)
  57. {
  58. if(n>=MEMBER_NUM_OF_LED) return;
  59. if(m_wsled[n].onoff!=1) m_wsled[n].onoff = 1;
  60. }
  61. void LED_Stop(uint8_t n)
  62. {
  63. if(n>=MEMBER_NUM_OF_LED) return;
  64. if(m_wsled[n].onoff!=0) {
  65. m_wsled[n].onoff = 0;
  66. }
  67. }
  68. void LED_SetColor(uint8_t n,uint32_t color)
  69. {
  70. if(n>=MEMBER_NUM_OF_LED) return;
  71. m_wsled[n].color = color;
  72. // SEGGER_RTT_printf(0,"LED_SetColor:%02x\n",color);
  73. }
  74. void LED_Process(void)
  75. {
  76. for(int i=MEMBER_NUM_OF_LED-1;i>0;i--){
  77. if(m_wsled[i].onoff>0){
  78. SetLedColor(m_wsled[i].color, LED_DEFAULT_NUMBER);
  79. // SEGGER_RTT_printf(0,"LED_SetColor:%02x\n",m_wsled[i].color);
  80. return;
  81. }
  82. }
  83. SetLedColor(COLOR_BLACK,LED_DEFAULT_NUMBER);
  84. }
  85. void LED_Run(void)
  86. {
  87. #if DEBUG_LEDRGB
  88. static uint32_t temp=0;
  89. if(temp==0){
  90. LED_SetColor(LED_RUN,COLOR_RED);
  91. }else if(temp==1){
  92. LED_SetColor(LED_RUN,COLOR_GREEN);
  93. }else if(temp==2){
  94. LED_SetColor(LED_RUN,COLOR_BLUE);
  95. // }else if(temp==3){
  96. // LED_SetColor(LED_RUN,COLOR_ORANGE);
  97. // }else if(temp==4){
  98. // LED_SetColor(LED_RUN,COLOR_YELLOW);
  99. // }else if(temp==5){
  100. // LED_SetColor(LED_RUN,COLOR_PURPLE);
  101. }
  102. if(++temp>=3) temp = 0;
  103. // SEGGER_RTT_printf(0,"LED_Run,%d\n",temp);;
  104. #else
  105. static uint32_t flag = 1;
  106. if(flag){
  107. // LED_SetColor(LED_RUN,COLOR_GREEN);
  108. nrf_gpio_pin_write(PIN_LED_RUN,0);
  109. flag = 0;
  110. }else{
  111. // LED_SetColor(LED_RUN, COLOR_BLACK);
  112. nrf_gpio_pin_write(PIN_LED_RUN,1);
  113. flag = 1;
  114. }
  115. #endif
  116. }
  117. void LED_Run_Init(void)
  118. {
  119. LED_Start(LED_RUN);
  120. #if DEBUG_LEDRGB
  121. Process_Start(500,"LED_Run",LED_Run);
  122. #else
  123. Process_Start(20,"LED_Run",LED_Run);
  124. #endif
  125. Process_SetHoldOn(LED_Run,1);
  126. }
  127. void cb_LED_Wakeup(uint32_t t)
  128. {
  129. Pwm_Initialize();
  130. }
  131. void cb_LED_Sleep(uint32_t t)
  132. {
  133. SetLedColor(COLOR_BLACK, LED_DEFAULT_NUMBER);
  134. Pwm_UnInitialize();
  135. }
  136. void LED_Init(void)
  137. {
  138. SetPwm_BaseClock(NRF_PWM_CLK_8MHz);
  139. SetPwm_Channels(led_channel[0], led_channel[1], led_channel[2], led_channel[3]);
  140. SetPwm_DutyCycleThreshold(led_pwm_cycle_time);
  141. Pwm_Initialize();
  142. led_color_seq = Pwm_SetIndSequence(&led_color_seq_values, PWM_SEQUENCE_VALUES_LEN(led_color_seq_values),0,0);
  143. LED_Start(LED_NONE);
  144. nrf_gpio_cfg_output(PIN_LED_RUN); nrf_gpio_pin_write(PIN_LED_RUN,1);
  145. Process_Start(10,"LED_Process",LED_Process);
  146. #if DEBUG_LEDRUN
  147. LED_Run_Init();
  148. #endif
  149. Sleep_Regist(cb_LED_Sleep);
  150. Wakeup_Regist(cb_LED_Wakeup);
  151. }