美文网首页
ESP32 能识别出单击、双击、三击、。。。。

ESP32 能识别出单击、双击、三击、。。。。

作者: 昨天剩下的一杯冷茶 | 来源:发表于2019-08-13 19:47 被阅读0次

看了他的ESP32实现双击和单击识别,太复杂了。
https://blog.csdn.net/xiaolongba/article/details/80791148

image.png

看看我的原创,如果您觉得我写的有意思,麻烦点赞,让我更有动力写更好的代码。
代码下载地址:
https://download.csdn.net/download/qq_31806069/11541239

1、 初始化GPIO

static void IRAM_ATTR gpio_isr_handler(void* arg)
{
    uint32_t gpio_num = (uint32_t) arg;
    if (gpio_num ==GPIO_INPUT_IO_0  && 0 == gpio_get_level(GPIO_INPUT_IO_0))
    {
        my_time_start(true);
    }
}


void my_gpio_init(void)
{
   static gpio_config_t io_conf;
    
    //interrupt of rising edge
    io_conf.intr_type = GPIO_INTR_ANYEDGE;
    //bit mask of the pins, use GPIO4/5 here
    io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
    //set as input mode    
    io_conf.mode = GPIO_MODE_INPUT;
    //enable pull-up mode
    io_conf.pull_up_en = 1;
    gpio_config(&io_conf);
    
    //install gpio isr service
    gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
    //hook isr handler for specific gpio pin
    gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
    //hook isr handler for specific gpio pin
    gpio_isr_handler_add(GPIO_INPUT_IO_1, gpio_isr_handler, (void*) GPIO_INPUT_IO_1);
    
    printf("caicai hello world\n");

}

2、 设置定时器

static void periodic_timer_callback(void* arg)
{
#define MY_BUTTON_ERROR_FALG 0xff;
    key_work_time +=TIME_INTER_MS;

    //printf("key_work_time:%d\n",key_work_time);
    //printf("key_state:%d-%d\n",key_state,key_work_time/1000);

    switch(key_state)
    {
     case 0x00://after 20ms
        if (  0 == gpio_get_level(GPIO_INPUT_IO_0) )//press button
        {
            key_state = 1;
            key_press_times++;
            key_neg_time+=TIME_INTER_MS;
        }else
        {
            key_press_times = MY_BUTTON_ERROR_FALG;
            goto time_error;

        }
    break;

    case 0x01:
        if (  0 == gpio_get_level(GPIO_INPUT_IO_0) )//press button
        {
            key_neg_time+=TIME_INTER_MS;
        }else
        {
            key_neg_time = 0;
            key_pos_time+=TIME_INTER_MS;
            key_state = 2;
        }

        if(key_neg_time > TIME_NEG_TIMEOU_MS)
        {
            key_press_times = MY_BUTTON_ERROR_FALG;
            goto time_error;
        }
    break;


    case 0x02:
        if (  0 == gpio_get_level(GPIO_INPUT_IO_0) )
        {
            key_neg_time+=TIME_INTER_MS;
            if (key_neg_time > 20)
            {
                key_state= 0x01;
                key_press_times++;
                key_pos_time =0;
            }
        }else
        {
            key_pos_time+=TIME_INTER_MS;
        }
        if(key_pos_time > TIME_POS_TIMEOU_MS)
        {
        //over
        goto time_error;
        }

    break;
    }

return;
    time_error:
    printf("--->key_press_times:%d\n",key_press_times);
    xQueueSendFromISR(gpio_evt_queue, &key_press_times, NULL);
     key_work_time =0;
    key_pos_time = 0;
    key_neg_time = 0;
    key_press_times =0;
    key_state = 0;
    my_time_start(false);
    
    
}

void my_time_init(void)
{
    const esp_timer_create_args_t periodic_timer_args = {
            .callback = &periodic_timer_callback,
            /* name is optional, but may help identify the timer when debugging */
            .name = "periodic"
    };

    
    ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));


}
void my_time_start(bool is_start)
{
    static bool my_time_state = false;

    if (my_time_state ==  is_start) return;
    my_time_state = is_start;
    //printf("my time open:%d\r\n",is_start);
    if (is_start)
    {
        /* Start the timers */
        ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer,  TIME_INTER_MS));
    }else
    {
        ESP_ERROR_CHECK(esp_timer_stop(periodic_timer));

    }
}

3、 主函数

static xQueueHandle gpio_evt_queue = NULL;

#define GPIO_INPUT_IO_0     4
#define GPIO_INPUT_IO_1     5
#define GPIO_INPUT_PIN_SEL  ((1ULL<<GPIO_INPUT_IO_0) | (1ULL<<GPIO_INPUT_IO_1))
#define ESP_INTR_FLAG_DEFAULT 0

#define TIME_INTER_MS (20*1000)
#define TIME_WORK_TIMEOU_MS (250*1000)
#define TIME_NEG_TIMEOU_MS (250*1000)
#define TIME_POS_TIMEOU_MS (150*1000)

uint32_t key_work_time =0;
uint32_t key_pos_time = 0;
uint32_t key_neg_time = 0;
uint32_t key_press_times =0;
uint8_t key_state = 0;

esp_timer_handle_t periodic_timer;
void my_time_start(bool is_start);



void app_main()
{
    //create a queue to handle gpio event from isr
    gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));

    my_gpio_init();
    my_time_init();

    uint32_t show_key_times;
  
    
    while(1) 
    {
        if(xQueueReceive(gpio_evt_queue, &show_key_times, portMAX_DELAY)) {
            printf("press key times %d\n", show_key_times);
        }
    }

}

效果图:


image.png

能识别出单击、双击、三击、。。。。
牛逼!

相关文章

  • ESP32 能识别出单击、双击、三击、。。。。

    看了他的ESP32实现双击和单击识别,太复杂了。https://blog.csdn.net/xiaolongba/...

  • Word使用技巧——快速选择文本

    一、 鼠标点选 1. 单击选行 2. 双击选段 3. 三击选全文 二、键盘配合 1. ctrl+鼠标点击:选中某行...

  • 实现html/js同时启用单双击事件

    在同一个dom元素同时启用了单击和双击事件时,双击会触发两次单击事件和一次双击事件,并且触发顺序是:单击1 -> ...

  • JS中的事件

    概念:行为。鼠标事件:onclick 左键单击ondblclick 双击oncontextmenu 右键单击...

  • js基本事件

    单击事件:onclick:单击事件ondbclick:双击事件 鼠标事件:onmousemove:鼠标移动事件on...

  • 五、jQuery事件

    一、鼠标事件 1、鼠标点击 注意1、双击事件伴随着单击事件,因此双击事件会触发两次单击事件2、单击事件又伴随着mo...

  • touch事件单击双击区分响应

    如果您的 iPhone 应用里有个 view,既有单击操作又有双击操作。用户双击 view 时,总是先执行一遍单击...

  • 按键单击与双击

    按键的单击与双击与长按短按实现方式相近,以下代码仅供参考,写得不是很好 适用于蓝桥杯开发板 程序扩展性不强,更重要...

  • UITabBarItem 双击事件

    业务需求:在选中某一Tab后,为其添加单击和双击事件(注意单击双击事件必须需独立,不能同时触发) 方案构思: 方案...

  • jQuery系列(四) -- 事件

    鼠标事件 .click() ==> 单击事件 .dblclick() ==> 双击事件 .mousedown() ...

网友评论

      本文标题:ESP32 能识别出单击、双击、三击、。。。。

      本文链接:https://www.haomeiwen.com/subject/lxfijctx.html