第五篇 按键

实际上在蓝牙样板程序中已经包含了按键部分的初始化,我们只需要根据自己的板子做很少的配置工作,buttons_init()函数如下:
static void buttons_init(void)
{
    // Note: Array must be static because a pointer to it will be saved in the Button handler
    //       module.
    static app_button_cfg_t buttons[] =
    {
        {WAKEUP_BUTTON_PIN, APP_BUTTON_ACTIVE_LOW, BUTTON_PULL, NULL},
        // YOUR_JOB: Add other buttons to be used:
        // {MY_BUTTON_PIN,     false, BUTTON_PULL, button_event_handler}
        {BUTTON_0, APP_BUTTON_ACTIVE_LOW, BUTTON_PULL, button_event_handler},
        {BUTTON_1, APP_BUTTON_ACTIVE_LOW, BUTTON_PULL, button_event_handler},
        {BUTTON_2, APP_BUTTON_ACTIVE_LOW, BUTTON_PULL, button_event_handler},
        {BUTTON_3, APP_BUTTON_ACTIVE_LOW, BUTTON_PULL, button_event_handler},
    };

    APP_BUTTON_INIT(buttons, sizeof(buttons) / sizeof(buttons[0]), BUTTON_DETECTION_DELAY, true);

    // Note: If the only use of buttons is to wake up, the app_button module can be omitted, and
    //       the wakeup button can be configured by
    // GPIO_WAKEUP_BUTTON_CONFIG(WAKEUP_BUTTON_PIN);
}

这里我们新增了4个按键,这4个按键所对应的GPIO口分别是30、21、22和23,这4个按键的中断处理函数为button_event_handler(),代码如下:

void button_event_handler(uint8_t pin_no, uint8_t button_action)
{
	if (button_action == APP_BUTTON_PUSH) {
		switch (pin_no) {
		case BUTTON_0:
			printf("button_0 down\r\n");
			break;
		case BUTTON_1:
			printf("button_1 down\r\n");
			break;
		case BUTTON_2:
			printf("button_2 down\r\n");
			break;
		case BUTTON_3:
			printf("button_3 down\r\n");
			break;
		default:
			break;
		}
	} else {
		switch (pin_no) {
		case BUTTON_0:
			printf("button_0 up\r\n");
			break;
		case BUTTON_1:
			printf("button_1 up\r\n");
			break;
		case BUTTON_2:
			printf("button_2 up\r\n");
			break;
		case BUTTON_3:
			printf("button_3 up\r\n");
			break;
		default:
			break;
		}
	}
}
在按键的中断处理函数中,收到按键按下、抬起事件后打印一点消息,方便我们调试。

最后,按键初始化好之后,还需要调用app_button_enable()函数,按键才能正常使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值