123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- #include "hal_led.h"
- #include "bsp_gpio.h"
- #include "nrf_gpio.h"
- #include "usr_config.h"
- #include "bsp_time.h"
- #include "system.h"
- #include "hal_ble_client.h"
- #include "bsp_pwm.h"
- #include "nrf_delay.h"
- #define LED_ENABLE 1
- #define LED_DISABLE 0
- #define LED_DEFAULT_NUMBER 2
- //高位先发,按照GRB的顺序发送数据
- //50us以上reset
- //#define WS_L 0,0x8000,0x8000,0x8000
- //#define WS_H 0,0,0,0x8000
- 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};
- static uint16_t led_pwm_cycle_time = 0xFF; //1 -> 0.125us
- static uint32_t led_pwm_mode = PWM_FLAG_LOOP;
- static pwm_values_individual_t led_color_seq_values = {0xFF,0xFF,0xFF,0xFF};
- static nrf_pwm_sequence_t *led_color_seq = NULL;
- struct WS_t{
- uint8_t onoff;
- uint32_t color; //颜色
- };
- static struct WS_t m_wsled[MEMBER_NUM_OF_LED] = {0};
- /**
- @brief 设置灯的时序
- @param color - [in] 灯的颜色
- @return 无
- */
- static void WS2812_DisplayDot(uint32_t col)
- {
- uint32_t col_r = (col & 0x00FF0000) >> 16;
- uint32_t col_g = (col & 0x0000FF00) >> 8;
- uint32_t col_b = col & 0x000000FF;
-
- led_color_seq_values.channel_0 = ~col_r;
- led_color_seq_values.channel_1 = ~col_g;
- led_color_seq_values.channel_2 = ~col_b;
- }
- /**
- @brief 设置灯的数量
- @param color - [in] 颜色
- @param led_num - [in] 灯的数量
- @return 无
- */
- static void SetLedColor(uint32_t color, uint32_t led_num)
- {
- WS2812_DisplayDot(color);
- SetSimplePwmPlayBack(led_color_seq, led_num, led_pwm_mode);
- }
- //强制关闭LED
- void LED_Close_Enforce(void){
- SetLedColor(COLOR_BLACK,LED_DEFAULT_NUMBER);
- }
- void LED_Start(uint8_t n)
- {
- if(n>=MEMBER_NUM_OF_LED) return;
- if(m_wsled[n].onoff!=1) m_wsled[n].onoff = 1;
- }
- void LED_Stop(uint8_t n)
- {
- if(n>=MEMBER_NUM_OF_LED) return;
- if(m_wsled[n].onoff!=0) {
- m_wsled[n].onoff = 0;
- }
- }
- void LED_SetColor(uint8_t n,uint32_t color)
- {
- if(n>=MEMBER_NUM_OF_LED) return;
- m_wsled[n].color = color;
- // SEGGER_RTT_printf(0,"LED_SetColor:%02x\n",color);
- }
- void LED_Process(void)
- {
- for(int i=MEMBER_NUM_OF_LED-1;i>0;i--){
- if(m_wsled[i].onoff>0){
- SetLedColor(m_wsled[i].color, LED_DEFAULT_NUMBER);
- // SEGGER_RTT_printf(0,"LED_SetColor:%02x\n",m_wsled[i].color);
- return;
- }
- }
- SetLedColor(COLOR_BLACK,LED_DEFAULT_NUMBER);
-
- }
- void LED_Run(void)
- {
- #if DEBUG_LEDRGB
- static uint32_t temp=0;
- if(temp==0){
- LED_SetColor(LED_RUN,COLOR_RED);
- }else if(temp==1){
- LED_SetColor(LED_RUN,COLOR_GREEN);
- }else if(temp==2){
- LED_SetColor(LED_RUN,COLOR_BLUE);
- // }else if(temp==3){
- // LED_SetColor(LED_RUN,COLOR_ORANGE);
- // }else if(temp==4){
- // LED_SetColor(LED_RUN,COLOR_YELLOW);
- // }else if(temp==5){
- // LED_SetColor(LED_RUN,COLOR_PURPLE);
- }
- if(++temp>=3) temp = 0;
- // SEGGER_RTT_printf(0,"LED_Run,%d\n",temp);;
- #else
- static uint32_t flag = 1;
- if(flag){
- // LED_SetColor(LED_RUN,COLOR_GREEN);
- nrf_gpio_pin_write(PIN_LED_RUN,0);
- flag = 0;
- }else{
- // LED_SetColor(LED_RUN, COLOR_BLACK);
- nrf_gpio_pin_write(PIN_LED_RUN,1);
- flag = 1;
- }
- #endif
- }
- void LED_Run_Init(void)
- {
- LED_Start(LED_RUN);
- #if DEBUG_LEDRGB
- Process_Start(500,"LED_Run",LED_Run);
- #else
- Process_Start(20,"LED_Run",LED_Run);
- #endif
- Process_SetHoldOn(LED_Run,1);
- }
- void cb_LED_Wakeup(uint32_t t)
- {
- Pwm_Initialize();
- }
- void cb_LED_Sleep(uint32_t t)
- {
- SetLedColor(COLOR_BLACK, LED_DEFAULT_NUMBER);
- Pwm_UnInitialize();
- }
- void LED_Init(void)
- {
- SetPwm_BaseClock(NRF_PWM_CLK_8MHz);
- SetPwm_Channels(led_channel[0], led_channel[1], led_channel[2], led_channel[3]);
- SetPwm_DutyCycleThreshold(led_pwm_cycle_time);
- Pwm_Initialize();
-
- led_color_seq = Pwm_SetIndSequence(&led_color_seq_values, PWM_SEQUENCE_VALUES_LEN(led_color_seq_values),0,0);
-
- LED_Start(LED_NONE);
- nrf_gpio_cfg_output(PIN_LED_RUN); nrf_gpio_pin_write(PIN_LED_RUN,1);
- Process_Start(10,"LED_Process",LED_Process);
- #if DEBUG_LEDRUN
- LED_Run_Init();
- #endif
-
- Sleep_Regist(cb_LED_Sleep);
- Wakeup_Regist(cb_LED_Wakeup);
- }
|