admin管理员组

文章数量:1550647

int main(void)
    bsp_configuration();


只有一个函数  开始分析



void bsp_configuration()
     bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_evt_handler);

只有一个函数  开始分析
参数:
BSP_INIT_LEDS | BSP_INIT_BUTTONS  在内部拆分
bsp_evt_handler                   非常关键的回调函数

现在分两路研究

A---
void bsp_evt_handler(bsp_event_t evt)
{
    uint32_t err_code;
    switch (evt)
    {
        case BSP_EVENT_KEY_0:
            if (actual_state != BSP_INDICATE_FIRST)
                actual_state--;
            else
                actual_state = BSP_INDICATE_LAST;
            break;

        case BSP_EVENT_KEY_1:

            if (actual_state != BSP_INDICATE_LAST)
                actual_state++;
            else
                actual_state = BSP_INDICATE_FIRST;
            break;

        default:
            return; // no implementation needed
    }
    err_code = bsp_indication_set(actual_state);
}
它的actual_state是枚举值 函数内部发本质是actual_state在0-MAX之间循环加加
最后执行 bsp_indication_set(actual_state);


uint32_t bsp_indication_set(bsp_indication_t indicate)
        bsp_led_indication(indicate);
        这个函数里面actual_state也是每一个分来处理 这就是灯语
        
B--
 uint32_t bsp_init(uint32_t type, bsp_event_callback_t callback)
{

    m_registered_callback = callback;//全局 只用在 bsp_button_event_handler

    if (type & BSP_INIT_BUTTONS)
    {
        uint32_t num;

        for (num = 0; ((num < BUTTONS_NUMBER) && (err_code == NRF_SUCCESS)); num++)
        {
            err_code = bsp_event_to_button_action_assign(num, BSP_BUTTON_ACTION_PUSH, BSP_EVENT_DEFAULT);
        }

        if (err_code == NRF_SUCCESS)
        {
            err_code = app_button_init((app_button_cfg_t *)app_buttons,
                                       BUTTONS_NUMBER,
                                       APP_TIMER_TICKS(50));
        }

        if (err_code == NRF_SUCCESS)
        {
            err_code = app_button_enable();
        }

        if (err_code == NRF_SUCCESS)
        {
            err_code = app_timer_create(&m_bsp_button_tmr,
                                        APP_TIMER_MODE_SINGLE_SHOT,
                                        button_timer_handler);
        }
    }
#elif (BUTTONS_NUMBER > 0) && (defined BSP_SIMPLE)
    bsp_board_init(type);
#endif // (BUTTONS_NUMBER > 0) && !(defined BSP_SIMPLE)

#if LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)
    if (type & BSP_INIT_LEDS)
    {
      //handle LEDs only. Buttons are already handled.
      bsp_board_init(BSP_INIT_LEDS);

      // timers module must be already initialized!
      if (err_code == NRF_SUCCESS)
      {
          err_code =
              app_timer_create(&m_bsp_leds_tmr, APP_TIMER_MODE_SINGLE_SHOT, leds_timer_handler);
      }

      if (err_code == NRF_SUCCESS)
      {
          err_code =
              app_timer_create(&m_bsp_alert_tmr, APP_TIMER_MODE_REPEATED, alert_timer_handler);
      }
    }
#endif // LEDS_NUMBER > 0 && !(defined BSP_SIMPLE)

    return err_code;
}       
每个按键都是一个定时器 在轮询的

最简单的第一个案例 led

int main(void)
{
    /* Configure board. */
    bsp_board_init(BSP_INIT_LEDS);
    nrf_gpio_cfg_input(BUTTON, NRF_GPIO_PIN_PULLUP);//初始化按键
    /* Toggle LEDs. */
    while (true)
    {
//        for (int i = 0; i < LEDS_NUMBER; i++)
//        {
//            bsp_board_led_invert(i);
//            nrf_delay_ms(500);
//        }
        
        
        bool pin_set = nrf_gpio_pin_read(BUTTON);//读按键 正常是全灭 按着的时候红色亮起
        if(pin_set)
        {nrf_gpio_pin_set(LED_1);}
        else
        {nrf_gpio_pin_clear(LED_1);}
        nrf_delay_ms(5);
    }
}
#define LEDS_NUMBER    2

#define LED_1          NRF_GPIO_PIN_MAP(1,0)
#define LED_2          NRF_GPIO_PIN_MAP(0,11)
#define BUTTON         NRF_GPIO_PIN_MAP(0,16)
#define LED_START      LED_1
#define LED_STOP       LED_4

#define LEDS_ACTIVE_STATE 0

#define LEDS_LIST { LED_1, LED_2 }

本文标签: 按键案例bsp