/**\n * @brief Button driver core function, driver state machine.\n * @param handle: the button handle struct.\n * @retval None\n /\nstatic void button_handler(struct Button handle)\n{\n\t// Read the level of the button GPIO pin\n\tuint8_t read_gpio_level = handle->hal_button_Level(handle->button_id);\n\n\t//ticks counter working..\n\tif((handle->state) > 0) handle->ticks++;\n\n\t/------------button debounce handle---------------/\n\tif(read_gpio_level != handle->button_level) { //not equal to prev one\n\t\t//continue read 3 times same new level change\n\t\tif(++(handle->debounce_cnt) >= DEBOUNCE_TICKS) {\n\t\t\t// Update the button level and reset debounce counter\n\t\t\thandle->button_level = read_gpio_level;\n\t\t\thandle->debounce_cnt = 0;\n\t\t}\n\t} else { //level not change ,counter reset.\n\t\t// Reset debounce counter\n\t\thandle->debounce_cnt = 0;\n\t}\n\n\t/-----------------State machine-------------------/\n\tswitch (handle->state) {\n\tcase 0:\n\t\t// Check if the button is pressed down\n\t\tif(handle->button_level == handle->active_level) {\t//start press down\n\t\t\t// Set the event as PRESS_DOWN and call the event callback function\n\t\t\thandle->event = (uint8_t)PRESS_DOWN;\n\t\t\tEVENT_CB(PRESS_DOWN);\n\t\t\t// Reset ticks counter and set repeat as 1\n\t\t\thandle->ticks = 0;\n\t\t\thandle->repeat = 1;\n\t\t\t// Update the state as 1\n\t\t\thandle->state = 1;\n\t\t} else {\n\t\t\t// Set the event as NONE_PRESS\n\t\t\thandle->event = (uint8_t)NONE_PRESS;\n\t\t}\n\t\tbreak;\n\n\tcase 1:\n\t\t// Check if the button is released\n\t\tif(handle->button_level != handle->active_level) { //released press up\n\t\t\t// Set the event as PRESS_UP and call the event callback function\n\t\t\thandle->event = (uint8_t)PRESS_UP;\n\t\t\tEVENT_CB(PRESS_UP);\n\t\t\t// Reset ticks counter and update the state as 2\n\t\t\thandle->ticks = 0;\n\t\t\thandle->state = 2;\n\t\t} else if(handle->ticks > LONG_TICKS) {\n\t\t\t// Set the event as LONG_PRESS_START and call the event callback function\n\t\t\thandle->event = (uint8_t)LONG_PRESS_START;\n\t\t\tEVENT_CB(LONG_PRESS_START);\n\t\t\t// Update the state as 5\n\t\t\thandle->state = 5;\n\t\t}\n\t\tbreak;\n\n\tcase 2:\n\t\t// Check if the button is pressed down again\n\t\tif(handle->button_level == handle->active_level) { //press down again\n\t\t\t// Set the event as PRESS_DOWN and call the event callback function\n\t\t\thandle->event = (uint8_t)PRESS_DOWN;\n\t\t\tEVENT_CB(PRESS_DOWN);\n\t\t\t// Increment the repeat count and call the event callback function for PRESS_REPEAT\n\t\t\thandle->repeat++;\n\t\t\tEVENT_CB(PRESS_REPEAT); // repeat hit\n\t\t\t// Reset ticks counter and update the state as 3\n\t\t\thandle->ticks = 0;\n\t\t\thandle->state = 3;\n\t\t} else if(handle->ticks > SHORT_TICKS) { //released timeout\n\t\t\tif(handle->repeat == 1) {\n\t\t\t\t// Set the event as SINGLE_CLICK and call the event callback function\n\t\t\t\thandle->event = (uint8_t)SINGLE_CLICK;\n\t\t\t\tEVENT_CB(SINGLE_CLICK);\n\t\t\t} else if(handle->repeat == 2) {\n\t\t\t\t// Set the event as DOUBLE_CLICK and call the event callback function\n\t\t\t\thandle->event = (uint8_t)DOUBLE_CLICK;\n\t\t\t\tEVENT_CB(DOUBLE_CLICK); // repeat hit\n\t\t\t}\n\t\t\t// Update the state as 0\n\t\t\thandle->state = 0;\n\t\t}\n\t\tbreak;\n\n\tcase 3:\n\t\t// Check if the button is released\n\t\tif(handle->button_level != handle->active_level) { //released press up\n\t\t\t// Set the event as PRESS_UP and call the event callback function\n\t\t\thandle->event = (uint8_t)PRESS_UP;\n\t\t\tEVENT_CB(PRESS_UP);\n\t\t\t// Check if ticks counter is less than SHORT_TICKS\n\t\t\tif(handle->ticks < SHORT_TICKS) {\n\t\t\t\t// Reset ticks counter and update the state as 2 for repeat press\n\t\t\t\thandle->ticks = 0;\n\t\t\t\thandle->state = 2; //repeat press\n\t\t\t} else {\n\t\t\t\t// Update the state as 0\n\t\t\t\thandle->state = 0;\n\t\t\t}\n\t\t} else if(handle->ticks > SHORT_TICKS) { // long press up\n\t\t\t// Update the state as 0\n\t\t\thandle->state = 0;\n\t\t}\n\t\tbreak;\n\n\tcase 5:\n\t\t// Check if the button is held down\n\t\tif(handle->button_level == handle->active_level) {\n\t\t\t// Set the event as LONG_PRESS_HOLD and call the event callback function\n\t\t\thandle->event = (uint8_t)LONG_PRESS_HOLD;\n\t\t\tEVENT_CB(LONG_PRESS_HOLD);\n\t\t} else { //releasd\n\t\t\t// Set the event as PRESS_UP and call the event callback function\n\t\t\thandle->event = (uint8_t)PRESS_UP;\n\t\t\tEVENT_CB(PRESS_UP);\n\t\t\t// Update the state as 0\n\t\t\thandle->state = 0; //reset\n\t\t}\n\t\tbreak;\n\tdefault:\n\t\t// Update the state as 0\n\t\thandle->state = 0; //reset\n\t\tbreak;\n\t}\n}\n

C 语言按钮驱动程序状态机:详细注释

原文地址: https://www.cveoy.top/t/topic/ns0p 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录