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; } }帮我详细注释这段代码
时间: 2024-03-16 19:44:53 浏览: 89
这段代码是一个按键处理函数,主要用于处理按键的状态和事件。下面是对代码的详细注释:
```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;
}
}
```
阅读全文