ESP32的长按、短按、双击、多击 按键检测实现

乐鑫官方有个仓库叫做esp-iot-solution,里面有很多常用外设的驱动和物联网场景的实现代码。其中就有一个button模块来实现按键的长按、短按检测(双击多击实现请看文末):
espressif/esp-iot-solution › components › general › button › button
README翻译如下:

  • 本模块封装了一个按键对象
  • 一个按键设备定义如下:
    • GPIO 端口号
    • 信号有效电平
    • 触发模式:触发模式决定了是否调用连续触发
    • 多少秒后开始进行进行连续触发
  • 一个按键设备提供如下回调:
    • 一个按下事件回调
    • 一个释放事件回调
    • 一个短时触摸事件回调
    • 一个连续触发事件回调
    • 多个长按事件回调

所有事件可以设置不同的去抖时长。
当任意一个长按回调触发时,短时触摸事件不会被触发。
本模块基于idf的GPIO控制和freeRTOS的软件定时器实现。

  • 按键设备使用方法 :

    • 通过 iot_button_create() 创建按键对象
    • 然后为按键对象设置回调
    • 调用iot_button_delete 释放对象内存
  • Todo:

    • 增加硬件定时器模式(有时候软件定时器有些限制)

NOTE:

所有事件回调基于freeRTOS的软件定时器API实现,因此必须遵循如下规则:
由于按键回调在定时器服务任务中执行,因此不应进行任何阻塞操作。例如,按键回调中不可以调用vTaskDelay(),vTaskDelayUntil() ,以及访问队列或信号量时指定非零阻塞时间。

另外:
可以对如下宏进行重定义来调整定时器服务的栈深度/队列长度/任务优先级。

#define configUSE_TIMERS //enable soft-timer
#define configTIMER_TASK_PRIORITY // priority of the timers service task
#define configQueue_LENGTH // length of timer command queue
#define configTIMER_TASK_STACK_DEPTH // stack depth of the soft-timer

在模块的test子文件夹下面有参考例程。
如下代码进行了一些简化:

// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#define BUTTON_IO_NUM  16
#define BUTTON_ACTIVE_LEVEL   0
#include "stdio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/timers.h"

#include "iot_button.h"
#include "esp_system.h"
#include "esp_log.h"

static const char* TAG_BTN = "BTN_TEST";

void button_tap_cb(void* arg)
{
    char* pstr = (char*) arg;
    ESP_EARLY_LOGI(TAG_BTN, "tap cb (%s), heap: %d\n", pstr, esp_get_free_heap_size());
}

void button_press_serial_cb(void* arg)
{
    ESP_EARLY_LOGI(TAG_BTN, "serial push, heap: %d\n", esp_get_free_heap_size());
}

void button_press_5s_cb(void* arg)
{
    ESP_EARLY_LOGI(TAG_BTN, "press 5s, heap: %d\n", esp_get_free_heap_size());
}

void button_test()
{
    printf("before btn init, heap: %d\n", esp_get_free_heap_size());
    button_handle_t btn_handle = iot_button_create(BUTTON_IO_NUM, BUTTON_ACTIVE_LEVEL);
    // iot_button_set_evt_cb(btn_handle, BUTTON_CB_PUSH, button_tap_cb, "PUSH");
    // iot_button_set_evt_cb(btn_handle, BUTTON_CB_RELEASE, button_tap_cb, "RELEASE");
    iot_button_set_evt_cb(btn_handle, BUTTON_CB_TAP, button_tap_cb, "TAP");
    iot_button_set_serial_cb(btn_handle, 1, 1000/portTICK_RATE_MS, button_press_serial_cb, "SERIAL");

    iot_button_add_custom_cb(btn_handle, 5, button_press_5s_cb, NULL);
    printf("after btn init, heap: %d\n", esp_get_free_heap_size());

}

void app_main()
{
    button_test();
}

LOG如下:

before btn init, heap: 298560
use esp timer !!!
I (250) gpio: GPIO[16]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:3 
after btn init, heap: 297752
#单次按键
I (3190) BTN_TEST: tap cb (TAP), heap: 303336
#按住不放
I (5920) BTN_TEST: serial push, heap: 303336

I (6920) BTN_TEST: serial push, heap: 303336

I (7920) BTN_TEST: serial push, heap: 303336

I (8920) BTN_TEST: serial push, heap: 303336
#到达5s时打印如下两行内容:
I (9870) BTN_TEST: press 5s, heap: 303336

I (9920) BTN_TEST: serial push, heap: 303336

Note:

单击+长按使用iot_button_set_evt_cb()设置BUTTON_CB_TAP事件+iot_button_add_custom_cb()设置长按事件实现。BUTTON_CB_PUSHBUTTON_CB_RELEASE在长按的时候也会被触发。

不过乐鑫这个模块并未实现双击或者多击。

但是有一个红旭的教程实现了长按+短按+单击+多击的参考代码。大家也可以参考一下。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值