52840 按键-bsp案例

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 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值