蓝牙芯片nRF5283的按键实现

开发板默认的有4个button,实际使用中,有可能只设置一个button,button的行为定义为:短按休眠和唤醒,长按关机。对应的nRF5283 SDK中的 BSP_EVENT_SLEEP和BSP_EVENT_SYSOFF。

1. 修改BUTTONS_NUMBER及pin脚

#define BUTTONS_NUMBER 1

#define BUTTON_START   23
#define BUTTON_1       23
#define BUTTON_STOP    23
#define BUTTON_PULL    NRF_GPIO_PIN_PULLUP

2. 应用中注释掉默认的bsp_btn_ble_init

默认的buttons_leds_init中注释掉bsp_btn_ble_init,该模块注册了过多的event事件,或者修改自己所需要的,比如按键断开链接等。

static void buttons_leds_init(bool * p_erase_bonds)
{
    bsp_event_t startup_event;

    uint32_t err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_event_handler);
    APP_ERROR_CHECK(err_code);

   // err_code = bsp_btn_ble_init(NULL, &startup_event);
   // APP_ERROR_CHECK(err_code);

    *p_erase_bonds = (startup_event == BSP_EVENT_CLEAR_BONDING_DATA);
}

注释掉bsp_btn_ble.c中NRF_SDH_BLE_OBSERVER,否则长按会配置的断开链接。或者在sdk_config.h中取消该observer的配置。

//NRF_SDH_BLE_OBSERVER(m_ble_observer, BSP_BTN_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);

3. 修改bsp.c的bsp_init中事件注册函数

将默认的BSP_EVENT_DEFAULT注册为期望的BSP_EVENT_SLEEP和BSP_EVENT_SYSOFF。

        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);
        }

改为

        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_SLEEP);
            err_code = bsp_event_to_button_action_assign(num, BSP_BUTTON_ACTION_LONG_PUSH , BSP_EVENT_SYSOFF);
        }

4. bsp_event_handler中增加事件响应

在BSP_EVENT_SLEEP和BSP_EVENT_SYSOFF响应对应的事件

void bsp_event_handler(bsp_event_t event)
{
    uint32_t err_code;
    switch (event)
    {
        case BSP_EVENT_SLEEP:
            sleep_mode_enter();
            break;
    case BSP_EVENT_SYSOFF:                       /**< The device should enter system off mode (without wakeup). */
        break;
        default:
            break;
    }
}

5. 修改长按时间

#define BSP_LONG_PUSH_TIMEOUT_MS (1000) /**< The time to hold for a long push (in milliseconds). */

至此,从长按时间,到事件的注册,最终事件响应完成,效果如下。

你可能感兴趣的:(蓝牙芯片nRF5283的按键实现)