Button press & debounce

一、先看下原始代码流程,按键1中断进入函数:

void usr_button1_cb(void)
{    ke_evt_set(1UL << EVENT_BUTTON1_PRESS_ID);
}
建立事件 EVENT_BUTTON1_PRESS_ID


而在事件初始化里定义了EVENT_BUTTON1_PRESS_ID 对应的函数是app_event_button1_press_handler:

    if(KE_EVENT_OK != ke_evt_callback_set(EVENT_BUTTON1_PRESS_ID, 
                                            app_event_button1_press_handler))
    {
        ASSERT_ERR(0);
    }

所以按键1的处理函数是app_event_button1_press_handler

void app_event_button1_press_handler(void)
{
#if ((QN_DEEP_SLEEP_EN) && (!QN_32K_RCO))
    if (sleep_env.deep_sleep)
    {
        sleep_env.deep_sleep = false;
        // start 32k xtal wakeup timer
        wakeup_32k_xtal_start_timer();
    }
#endif

    // delay 20ms to debounce
    ke_timer_set(APP_SYS_BUTTON_1_TIMER, TASK_APP
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
void button_handler(struct Button* handle) { uint8_t read_gpio_level = handle->hal_button_Level(handle->button_id); //ticks counter working.. if((handle->state) > 0) handle->ticks++; /*------------button debounce handle---------------*/ if(read_gpio_level != handle->button_level) { //not equal to prev one //continue read 3 times same new level change if(++(handle->debounce_cnt) >= DEBOUNCE_TICKS) { handle->button_level = read_gpio_level; handle->debounce_cnt = 0; } } else { //leved not change ,counter reset. handle->debounce_cnt = 0; } /*-----------------State machine-------------------*/ switch (handle->state) { case 0: if(handle->button_level == handle->active_level) { //start press down handle->event = (uint8_t)PRESS_DOWN; EVENT_CB(PRESS_DOWN); handle->ticks = 0; handle->repeat = 1; handle->state = 1; } else { handle->event = (uint8_t)NONE_PRESS; } break; case 1: if(handle->button_level != handle->active_level) { //released press up handle->event = (uint8_t)PRESS_UP; EVENT_CB(PRESS_UP); handle->ticks = 0; handle->state = 2; } else if(handle->ticks > LONG_TICKS) { handle->event = (uint8_t)LONG_PRESS_START; EVENT_CB(LONG_PRESS_START); handle->state = 5; } break; case 2: if(handle->button_level == handle->active_level) { //press down again handle->event = (uint8_t)PRESS_DOWN; EVENT_CB(PRESS_DOWN); handle->repeat++; EVENT_CB(PRESS_REPEAT); // repeat hit handle->ticks = 0; handle->state = 3; } else if(handle->ticks > SHORT_TICKS) { //released timeout if(handle->repeat == 1) { handle->event = (uint8_t)SINGLE_CLICK; EVENT_CB(SINGLE_CLICK); } else if(handle->repeat == 2) { handle->event = (uint8_t)DOUBLe_CLICK; EVENT_CB(DOUBLe_CLICK); // repeat hit } handle->state = 0; } break; case 3: if(handle->button_level != handle->active_level) { //released press up handle->event = (uint8_t)PRESS_UP; EVENT_CB(PRESS_UP); if(handle->ticks < SHORT_TICKS) { handle->ticks = 0; handle->state = 2; //repeat press } else { handle->state = 0; } }else if(handle->ticks > SHORT_TICKS){ // long press up handle->state = 0; } break; case 5: if(handle->button_level == handle->active_level) { //continue hold trigger handle->event = (uint8_t)LONG_PRESS_HOLD; EVENT_CB(LONG_PRESS_HOLD); } else { //releasd handle->event = (uint8_t)PRESS_UP; EVENT_CB(PRESS_UP); handle->state = 0; //reset } break; default: handle->state = 0; //reset break; } }帮我详细注释这段代码
最新发布
06-09
这段代码是一个按键处理函数,主要用于处理按键的状态和事件。下面是对代码的详细注释: ```c void button_handler(struct Button* handle) { // 读取按键GPIO端口的电平值 uint8_t read_gpio_level = handle->hal_button_Level(handle->button_id); // 计数器开始工作 if((handle->state) > 0) handle->ticks++; /*------------按键消抖处理---------------*/ if(read_gpio_level != handle->button_level) { //如果读到的电平值不同于之前的 //连续读取3次相同的电平值 if(++(handle->debounce_cnt) >= DEBOUNCE_TICKS) { handle->button_level = read_gpio_level; //更新电平值 handle->debounce_cnt = 0; //重置消抖计数器 } } else { //如果电平值没有变化,重置消抖计数器 handle->debounce_cnt = 0; } /*-----------------状态机处理-------------------*/ switch (handle->state) { case 0: //初始状态 if(handle->button_level == handle->active_level) { //按键按下 handle->event = (uint8_t)PRESS_DOWN; //设置按键事件 EVENT_CB(PRESS_DOWN); //触发事件回调函数 handle->ticks = 0; //重置计数器 handle->repeat = 1; //重置按键重复次数 handle->state = 1; //进入下一个状态 } else { handle->event = (uint8_t)NONE_PRESS; //没有按键按下 } break; case 1: //按键按下状态 if(handle->button_level != handle->active_level) { //按键松开 handle->event = (uint8_t)PRESS_UP; //设置按键事件 EVENT_CB(PRESS_UP); //触发事件回调函数 handle->ticks = 0; //重置计数器 handle->state = 2; //进入下一个状态 } else if(handle->ticks > LONG_TICKS) { //按键按下超过长按时间 handle->event = (uint8_t)LONG_PRESS_START; //设置按键事件 EVENT_CB(LONG_PRESS_START); //触发事件回调函数 handle->state = 5; //进入下一个状态 } break; case 2: //单次按键按下后的状态 if(handle->button_level == handle->active_level) { //按键再次按下 handle->event = (uint8_t)PRESS_DOWN; //设置按键事件 EVENT_CB(PRESS_DOWN); //触发事件回调函数 handle->repeat++; //增加按键重复次数 EVENT_CB(PRESS_REPEAT); //触发按键重复事件回调函数 handle->ticks = 0; //重置计数器 handle->state = 3; //进入下一个状态 } else if(handle->ticks > SHORT_TICKS) { //按键松开超过短按时间 if(handle->repeat == 1) { //单击事件 handle->event = (uint8_t)SINGLE_CLICK; //设置按键事件 EVENT_CB(SINGLE_CLICK); //触发事件回调函数 } else if(handle->repeat == 2) { //双击事件 handle->event = (uint8_t)DOUBLE_CLICK; //设置按键事件 EVENT_CB(DOUBLE_CLICK); //触发事件回调函数 } handle->state = 0; //回到初始状态 } break; case 3: //按键重复状态 if(handle->button_level != handle->active_level) { //按键松开 handle->event = (uint8_t)PRESS_UP; //设置按键事件 EVENT_CB(PRESS_UP); //触发事件回调函数 if(handle->ticks < SHORT_TICKS) { //按键松开时间小于短按时间 handle->ticks = 0; //重置计数器 handle->state = 2; //回到单次按键按下后的状态 } else { //按键松开时间大于等于短按时间 handle->state = 0; //回到初始状态 } } else if(handle->ticks > SHORT_TICKS){ //按键按下时间大于短按时间 handle->state = 0; //回到初始状态 } break; case 5: //长按状态 if(handle->button_level == handle->active_level) { //按键继续按下 handle->event = (uint8_t)LONG_PRESS_HOLD; //设置按键事件 EVENT_CB(LONG_PRESS_HOLD); //触发事件回调函数 } else { //按键松开 handle->event = (uint8_t)PRESS_UP; //设置按键事件 EVENT_CB(PRESS_UP); //触发事件回调函数 handle->state = 0; //回到初始状态 } break; default: //其他状态,回到初始状态 handle->state = 0; break; } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值