diff --git a/README.md b/README.md index f13d8b3..fa67d23 100644 --- a/README.md +++ b/README.md @@ -421,6 +421,13 @@ extern void mfbd_group_scan(const mfbd_group_t *_pbtn_group); ```c extern void mfbd_group_reset(const mfbd_group_t *_pbtn_group); ``` +### 如果一段时间没有调用按键检测,可以使用SKIP的API来跳过一段时间 + +可以在睡眠时调用此函数,此函数不会上报键值,只是更新计数时间,使用按键当前的状态直接进行计数`times`次时间。`tbtn`由于状态只有按下弹起,所以不支持此功能。 + +```c +extern void mfbd_group_skip(const mfbd_group_t *_pbtn_group, mfbd_btn_count_t times); +``` ## MFBD段定义 diff --git a/mfbd.c b/mfbd.c index e8fd63b..1ff7315 100644 --- a/mfbd.c +++ b/mfbd.c @@ -11,7 +11,7 @@ * 2022-08-05 smartmx add reset params function. * 2023-03-15 smartmx add state declaration. * 2023-07-03 smartmx add Section Definition option. - * 2023-07-15 smartmx add skip function. + * 2023-07-15 smartmx add skip function, to reduce calling of scan functions. * */ diff --git a/mfbd.h b/mfbd.h index b5bb40f..8ff667c 100644 --- a/mfbd.h +++ b/mfbd.h @@ -13,7 +13,7 @@ * 2022-09-07 smartmx add default define apis. * 2023-03-15 smartmx add state declaration. * 2023-07-03 smartmx add Section Definition option. - * 2023-07-15 smartmx add skip function. + * 2023-07-15 smartmx add skip function, to reduce calling of scan functions. * */ diff --git a/mfbd_sd.c b/mfbd_sd.c index 94a1e6c..d9fb6e9 100644 --- a/mfbd_sd.c +++ b/mfbd_sd.c @@ -6,6 +6,7 @@ * Change Logs: * Date Author Notes * 2023-07-03 smartmx the first version, Multi-Function Button Dectection with Section Definition. + * 2023-07-15 smartmx add skip function, to reduce calling of scan functions. * */ @@ -236,6 +237,60 @@ void mfbd_nbtn_scan(const mfbd_group_t *_pbtn_group, const mfbd_nbtn_info_t *_pb } } +/** + * @brief skip some times of mfbd_btn_count_t with last state. + * + * @param _pbtn_group is a pointer of mfbd_group_t. + * @param times is times need to skip. + * + * @return None. + */ +void mfbd_nbtn_skip(const mfbd_group_t *_pbtn_group, const mfbd_nbtn_info_t *_pbtn_info_start, const mfbd_nbtn_info_t *_pbtn_info_end, mfbd_btn_count_t times) +{ + const mfbd_nbtn_info_t *_pbtn_info = _pbtn_info_start; + + while (1) + { + if (_pbtn_info_end <= _pbtn_info) + { + break; + } + if(_pbtn_info->btn->state == MFBD_BTN_STATE_DOWN) + { + if (((MFBD_LONG_TIME_IN_FUC) > 0) && (_pbtn_info->btn_long_code != 0)) + { + /* if long_time is 0 or long_code is 0, disable long and repeat check. */ + if (_pbtn_info->btn->long_count < (MFBD_LONG_TIME_IN_FUC)) + { + if(((MFBD_LONG_TIME_IN_FUC) - 1 - _pbtn_info->btn->long_count) > times) + { + _pbtn_info->btn->long_count = _pbtn_info->btn->long_count + times; + } + else + { + _pbtn_info->btn->long_count = MFBD_LONG_TIME_IN_FUC - 1; + } + } + } + } + else if(_pbtn_info->btn->state == MFBD_BTN_STATE_LONG) + { + if (((MFBD_REPEAT_TIME_IN_FUC) > 0) && (_pbtn_info->btn_down_code[0] != 0)) + { + if(((MFBD_REPEAT_TIME_IN_FUC) - 1 - _pbtn_info->btn->repeat_count) > times) + { + _pbtn_info->btn->repeat_count = _pbtn_info->btn->repeat_count + times; + } + else + { + _pbtn_info->btn->repeat_count = MFBD_REPEAT_TIME_IN_FUC - 1; + } + } + } + _pbtn_info++; + } +} + /** * @brief reset all normal buttons' params. * @@ -408,6 +463,75 @@ void mfbd_mbtn_scan(const mfbd_group_t *_pbtn_group, const mfbd_mbtn_info_t *_pb } } +/** + * @brief skip some times of mfbd_btn_count_t with last state. + * + * @param _pbtn_group is a pointer of mfbd_group_t. + * @param times is times need to skip. + * + * @return None. + */ +void mfbd_mbtn_skip(const mfbd_group_t *_pbtn_group, const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd_mbtn_info_t *_pbtn_info_end, mfbd_btn_count_t times) +{ + const mfbd_mbtn_info_t *_pbtn_info = _pbtn_info_start; + while (1) + { + if(_pbtn_info->btn->state == MFBD_BTN_STATE_UP) + { + if (_pbtn_info->btn->multiclick_state != 0) + { + if (_pbtn_info->btn->multiclick_count < (MFBD_MULTICLICK_TIME_IN_FUC)) + { + if(((MFBD_MULTICLICK_TIME_IN_FUC) - _pbtn_info->btn->multiclick_count) > times) + { + _pbtn_info->btn->multiclick_count = _pbtn_info->btn->multiclick_count + times; + } + else + { + _pbtn_info->btn->multiclick_state = 0; + } + } + } + } + else if(_pbtn_info->btn->state == MFBD_BTN_STATE_DOWN) + { + if (_pbtn_info->btn->multiclick_state == 0) + { + if (((MFBD_LONG_TIME_IN_FUC) > 0) && (_pbtn_info->btn_long_code != 0)) + { + /* if long_time is 0 or long_code is 0, disable long and repeat check. */ + if (_pbtn_info->btn->long_count < (MFBD_LONG_TIME_IN_FUC)) + { + if(((MFBD_LONG_TIME_IN_FUC) - 1 - _pbtn_info->btn->long_count) > times) + { + _pbtn_info->btn->long_count = _pbtn_info->btn->long_count + times; + } + else + { + _pbtn_info->btn->long_count = MFBD_LONG_TIME_IN_FUC - 1; + } + } + } + } + } + else if(_pbtn_info->btn->state == MFBD_BTN_STATE_LONG) + { + if (((MFBD_REPEAT_TIME_IN_FUC) > 0) && (_pbtn_info->btn_down_code[0] != 0)) + { + if(((MFBD_REPEAT_TIME_IN_FUC) - 1 - _pbtn_info->btn->repeat_count) > times) + { + _pbtn_info->btn->repeat_count = _pbtn_info->btn->repeat_count + times; + } + else + { + _pbtn_info->btn->repeat_count = MFBD_REPEAT_TIME_IN_FUC - 1; + } + } + } + _pbtn_info++; + } +} + /** * @brief reset all multi-function buttons' params. * diff --git a/mfbd_sd.h b/mfbd_sd.h index 6982157..5794784 100644 --- a/mfbd_sd.h +++ b/mfbd_sd.h @@ -6,6 +6,7 @@ * Change Logs: * Date Author Notes * 2023-07-03 smartmx the first version, Multi-Function Button Dectection with Section Definition. + * 2023-07-15 smartmx add skip function, to reduce calling of scan functions. * */ @@ -452,9 +453,11 @@ extern void mfbd_tbtn_scan(const mfbd_group_t *_pbtn_group, const mfbd_tbtn_info extern void mfbd_tbtn_reset(const mfbd_tbtn_info_t *_pbtn_info_start, const mfbd_tbtn_info_t *_pbtn_info_end); extern void mfbd_nbtn_scan(const mfbd_group_t *_pbtn_group, const mfbd_nbtn_info_t *_pbtn_info_start, const mfbd_nbtn_info_t *_pbtn_info_end); +extern void mfbd_nbtn_skip(const mfbd_group_t *_pbtn_group, const mfbd_nbtn_info_t *_pbtn_info_start, const mfbd_nbtn_info_t *_pbtn_info_end, mfbd_btn_count_t times); extern void mfbd_nbtn_reset(const mfbd_nbtn_info_t *_pbtn_info_start, const mfbd_nbtn_info_t *_pbtn_info_end); extern void mfbd_mbtn_scan(const mfbd_group_t *_pbtn_group, const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd_mbtn_info_t *_pbtn_info_end); +extern void mfbd_mbtn_skip(const mfbd_group_t *_pbtn_group, const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd_mbtn_info_t *_pbtn_info_end, mfbd_btn_count_t times); extern void mfbd_mbtn_reset(const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd_mbtn_info_t *_pbtn_info_end); #if defined(__CC_ARM) || defined(__CLANG_ARM) /* ARM Compiler */ @@ -475,8 +478,8 @@ extern void mfbd_mbtn_reset(const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd mfbd_tbtn_reset((const mfbd_tbtn_info_t *)&(MFBD_SECTION_START(GROUP, tbtn)), (const mfbd_tbtn_info_t *)&(MFBD_SECTION_END(GROUP, tbtn))); \ } while (0) #else -#define MFBD_GROUP_SCAN_TBTN(GROUP) do{} while(0) -#define MFBD_GROUP_RESET_TBTN(GROUP) do{} while(0) +#define MFBD_GROUP_SCAN_TBTN(GROUP) do{} while(0) +#define MFBD_GROUP_RESET_TBTN(GROUP) do{} while(0) #endif #if MFBD_USE_NORMAL_BUTTON @@ -487,6 +490,13 @@ extern void mfbd_mbtn_reset(const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd extern const int MFBD_SECTION_END(GROUP, nbtn); \ mfbd_nbtn_scan((const mfbd_group_t *)&(MFBD_GROUP_NAME(GROUP)), (const mfbd_nbtn_info_t *)&(MFBD_SECTION_START(GROUP, nbtn)), (const mfbd_nbtn_info_t *)&(MFBD_SECTION_END(GROUP, nbtn))); \ } while (0) +#define MFBD_GROUP_SKIP_NBTN(GROUP, TIMES) \ + do \ + { \ + extern const int MFBD_SECTION_START(GROUP, nbtn); \ + extern const int MFBD_SECTION_END(GROUP, nbtn); \ + mfbd_nbtn_skip((const mfbd_group_t *)&(MFBD_GROUP_NAME(GROUP)), (const mfbd_nbtn_info_t *)&(MFBD_SECTION_START(GROUP, nbtn)), (const mfbd_nbtn_info_t *)&(MFBD_SECTION_END(GROUP, nbtn)), TIMES); \ + } while (0) #define MFBD_GROUP_RESET_NBTN(GROUP) \ do \ { \ @@ -495,8 +505,9 @@ extern void mfbd_mbtn_reset(const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd mfbd_nbtn_reset((const mfbd_nbtn_info_t *)&(MFBD_SECTION_START(GROUP, nbtn)), (const mfbd_nbtn_info_t *)&(MFBD_SECTION_END(GROUP, nbtn))); \ } while (0) #else -#define MFBD_GROUP_SCAN_NBTN(GROUP) do{} while(0) -#define MFBD_GROUP_RESET_NBTN(GROUP) do{} while(0) +#define MFBD_GROUP_SCAN_NBTN(GROUP) do{} while(0) +#define MFBD_GROUP_SKIP_NBTN(GROUP, TIMES) do{} while(0) +#define MFBD_GROUP_RESET_NBTN(GROUP) do{} while(0) #endif #if MFBD_USE_MULTIFUCNTION_BUTTON @@ -507,6 +518,13 @@ extern void mfbd_mbtn_reset(const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd extern const int MFBD_SECTION_END(GROUP, mbtn); \ mfbd_mbtn_scan((const mfbd_group_t *)&(MFBD_GROUP_NAME(GROUP)), (const mfbd_mbtn_info_t *)&(MFBD_SECTION_START(GROUP, mbtn)), (const mfbd_mbtn_info_t *)&(MFBD_SECTION_END(GROUP, mbtn))); \ } while (0) +#define MFBD_GROUP_SKIP_MBTN(GROUP, TIMES) \ + do \ + { \ + extern const int MFBD_SECTION_START(GROUP, mbtn); \ + extern const int MFBD_SECTION_END(GROUP, mbtn); \ + mfbd_mbtn_skip((const mfbd_group_t *)&(MFBD_GROUP_NAME(GROUP)), (const mfbd_mbtn_info_t *)&(MFBD_SECTION_START(GROUP, mbtn)), (const mfbd_mbtn_info_t *)&(MFBD_SECTION_END(GROUP, mbtn)), TIMES); \ + } while (0) #define MFBD_GROUP_RESET_MBTN(GROUP) \ do \ { \ @@ -515,8 +533,9 @@ extern void mfbd_mbtn_reset(const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd mfbd_mbtn_reset((const mfbd_mbtn_info_t *)&(MFBD_SECTION_START(GROUP, mbtn)), (const mfbd_mbtn_info_t *)&(MFBD_SECTION_END(GROUP, mbtn))); \ } while (0) #else -#define MFBD_GROUP_SCAN_MBTN(GROUP) do{} while(0) -#define MFBD_GROUP_RESET_MBTN(GROUP) do{} while(0) +#define MFBD_GROUP_SCAN_MBTN(GROUP) do{} while(0) +#define MFBD_GROUP_SKIP_MBTN(GROUP, TIMES) do{} while(0) +#define MFBD_GROUP_RESET_MBTN(GROUP) do{} while(0) #endif #elif defined (__IAR_SYSTEMS_ICC__) /* IAR Compiler */ @@ -533,7 +552,7 @@ extern void mfbd_mbtn_reset(const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd mfbd_tbtn_reset((const mfbd_tbtn_info_t *)MFBD_SECTION_START(GROUP, nbtn), (const mfbd_tbtn_info_t *)MFBD_SECTION_END(GROUP, nbtn)); \ } while (0) #else -#define MFBD_GROUP_SCAN_TBTN(GROUP) do{} while(0) +#define MFBD_GROUP_SCAN_TBTN(GROUP) do{} while(0) #endif #if MFBD_USE_NORMAL_BUTTON #define MFBD_GROUP_SCAN_NBTN(GROUP) \ @@ -541,20 +560,31 @@ extern void mfbd_mbtn_reset(const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd { \ mfbd_nbtn_scan((const mfbd_group_t *)&(MFBD_GROUP_NAME(GROUP)), (const mfbd_nbtn_info_t *)MFBD_SECTION_START(GROUP, nbtn), (const mfbd_nbtn_info_t *)MFBD_SECTION_END(GROUP, nbtn)); \ } while (0) +#define MFBD_GROUP_SKIP_NBTN(GROUP, TIMES) \ + do \ + { \ + mfbd_nbtn_skip((const mfbd_group_t *)&(MFBD_GROUP_NAME(GROUP)), (const mfbd_nbtn_info_t *)MFBD_SECTION_START(GROUP, nbtn), (const mfbd_nbtn_info_t *)MFBD_SECTION_END(GROUP, nbtn), TIMES); \ + } while (0) #define MFBD_GROUP_RESET_NBTN(GROUP) \ do \ { \ mfbd_nbtn_reset((const mfbd_nbtn_info_t *)MFBD_SECTION_START(GROUP, nbtn), (const mfbd_nbtn_info_t *)MFBD_SECTION_END(GROUP, nbtn)); \ } while (0) #else -#define MFBD_GROUP_SCAN_NBTN(GROUP) do{} while(0) -#define MFBD_GROUP_RESET_NBTN(GROUP) do{} while(0) +#define MFBD_GROUP_SCAN_NBTN(GROUP) do{} while(0) +#define MFBD_GROUP_SKIP_NBTN(GROUP, TIMES) do{} while(0) +#define MFBD_GROUP_RESET_NBTN(GROUP) do{} while(0) #endif #if MFBD_USE_MULTIFUCNTION_BUTTON #define MFBD_GROUP_SCAN_MBTN(GROUP) \ do \ { \ - mfbd_mbtn_scan((const mfbd_group_t *)&(MFBD_GROUP_NAME(GROUP)), (const mfbd_mbtn_info_t *)MFBD_SECTION_START(GROUP, mbtn), (const mfbd_mbtn_info_t *)MFBD_SECTION_END(GROUP, mbtn)); \ + mfbd_mbtn_scan((const mfbd_group_t *)&(MFBD_GROUP_NAME(GROUP)), (const mfbd_mbtn_info_t *)MFBD_SECTION_START(GROUP, mbtn), (const mfbd_mbtn_info_t *)MFBD_SECTION_END(GROUP, mbtn)); \ + } while (0) +#define MFBD_GROUP_SKIP_MBTN(GROUP, TIMES) \ + do \ + { \ + mfbd_mbtn_skip((const mfbd_group_t *)&(MFBD_GROUP_NAME(GROUP)), (const mfbd_mbtn_info_t *)MFBD_SECTION_START(GROUP, mbtn), (const mfbd_mbtn_info_t *)MFBD_SECTION_END(GROUP, mbtn), TIMES); \ } while (0) #define MFBD_GROUP_RESET_MBTN(GROUP) \ do \ @@ -562,8 +592,9 @@ extern void mfbd_mbtn_reset(const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd mfbd_mbtn_reset((const mfbd_mbtn_info_t *)MFBD_SECTION_START(GROUP, mbtn), (const mfbd_mbtn_info_t *)MFBD_SECTION_END(GROUP, mbtn)); \ } while (0) #else -#define MFBD_GROUP_SCAN_MBTN(GROUP) do{} while(0) -#define MFBD_GROUP_RESET_MBTN(GROUP) do{} while(0) +#define MFBD_GROUP_SCAN_MBTN(GROUP) do{} while(0) +#define MFBD_GROUP_SKIP_MBTN(GROUP, TIMES) do{} while(0) +#define MFBD_GROUP_RESET_MBTN(GROUP) do{} while(0) #endif #elif defined (__GNUC__) /* GNU GCC Compiler */ @@ -584,8 +615,8 @@ extern void mfbd_mbtn_reset(const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd mfbd_tbtn_reset((const mfbd_tbtn_info_t *)&(MFBD_SECTION_START(GROUP, tbtn)), (const mfbd_tbtn_info_t *)&(MFBD_SECTION_END(GROUP, tbtn))); \ } while (0) #else -#define MFBD_GROUP_SCAN_TBTN(GROUP) do{} while(0) -#define MFBD_GROUP_RESET_TBTN(GROUP) do{} while(0) +#define MFBD_GROUP_SCAN_TBTN(GROUP) do{} while(0) +#define MFBD_GROUP_RESET_TBTN(GROUP) do{} while(0) #endif #if MFBD_USE_NORMAL_BUTTON @@ -594,7 +625,14 @@ extern void mfbd_mbtn_reset(const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd { \ extern const int MFBD_SECTION_START(GROUP, nbtn); \ extern const int MFBD_SECTION_END(GROUP, nbtn); \ - mfbd_nbtn_scan((const mfbd_group_t *)&(MFBD_GROUP_NAME(GROUP)), (const mfbd_nbtn_info_t *)&(MFBD_SECTION_START(GROUP, nbtn)), (const mfbd_nbtn_info_t *)&(MFBD_SECTION_END(GROUP, nbtn))); \ + mfbd_nbtn_scan((const mfbd_group_t *)&(MFBD_GROUP_NAME(GROUP)), (const mfbd_nbtn_info_t *)&(MFBD_SECTION_START(GROUP, nbtn)), (const mfbd_nbtn_info_t *)&(MFBD_SECTION_END(GROUP, nbtn))); \ + } while (0) +#define MFBD_GROUP_SKIP_NBTN(GROUP, TIMES) \ + do \ + { \ + extern const int MFBD_SECTION_START(GROUP, nbtn); \ + extern const int MFBD_SECTION_END(GROUP, nbtn); \ + mfbd_nbtn_skip((const mfbd_group_t *)&(MFBD_GROUP_NAME(GROUP)), (const mfbd_nbtn_info_t *)&(MFBD_SECTION_START(GROUP, nbtn)), (const mfbd_nbtn_info_t *)&(MFBD_SECTION_END(GROUP, nbtn)), TIMES); \ } while (0) #define MFBD_GROUP_RESET_NBTN(GROUP) \ do \ @@ -604,8 +642,9 @@ extern void mfbd_mbtn_reset(const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd mfbd_nbtn_reset((const mfbd_nbtn_info_t *)&(MFBD_SECTION_START(GROUP, nbtn)), (const mfbd_nbtn_info_t *)&(MFBD_SECTION_END(GROUP, nbtn))); \ } while (0) #else -#define MFBD_GROUP_SCAN_NBTN(GROUP) do{} while(0) -#define MFBD_GROUP_RESET_NBTN(GROUP) do{} while(0) +#define MFBD_GROUP_SCAN_NBTN(GROUP) do{} while(0) +#define MFBD_GROUP_SKIP_NBTN(GROUP, TIMES) do{} while(0) +#define MFBD_GROUP_RESET_NBTN(GROUP) do{} while(0) #endif #if MFBD_USE_MULTIFUCNTION_BUTTON @@ -616,6 +655,13 @@ extern void mfbd_mbtn_reset(const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd extern const int MFBD_SECTION_END(GROUP, mbtn); \ mfbd_mbtn_scan((const mfbd_group_t *)&(MFBD_GROUP_NAME(GROUP)), (const mfbd_mbtn_info_t *)&(MFBD_SECTION_START(GROUP, mbtn)), (const mfbd_mbtn_info_t *)&(MFBD_SECTION_END(GROUP, mbtn))); \ } while (0) +#define MFBD_GROUP_SKIP_MBTN(GROUP, TIMES) \ + do \ + { \ + extern const int MFBD_SECTION_START(GROUP, mbtn); \ + extern const int MFBD_SECTION_END(GROUP, mbtn); \ + mfbd_mbtn_skip((const mfbd_group_t *)&(MFBD_GROUP_NAME(GROUP)), (const mfbd_mbtn_info_t *)&(MFBD_SECTION_START(GROUP, mbtn)), (const mfbd_mbtn_info_t *)&(MFBD_SECTION_END(GROUP, mbtn)), TIMES); \ + } while (0) #define MFBD_GROUP_RESET_MBTN(GROUP) \ do \ { \ @@ -624,8 +670,9 @@ extern void mfbd_mbtn_reset(const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd mfbd_mbtn_reset((const mfbd_mbtn_info_t *)&(MFBD_SECTION_START(GROUP, mbtn)), (const mfbd_mbtn_info_t *)&(MFBD_SECTION_END(GROUP, mbtn))); \ } while (0) #else -#define MFBD_GROUP_SCAN_MBTN(GROUP) do{} while(0) -#define MFBD_GROUP_RESET_MBTN(GROUP) do{} while(0) +#define MFBD_GROUP_SCAN_MBTN(GROUP) do{} while(0) +#define MFBD_GROUP_SKIP_MBTN(GROUP, TIMES) do{} while(0) +#define MFBD_GROUP_RESET_MBTN(GROUP) do{} while(0) #endif #else @@ -673,6 +720,13 @@ extern void mfbd_mbtn_reset(const mfbd_mbtn_info_t *_pbtn_info_start, const mfbd MFBD_GROUP_SCAN_AFTER(GROUP); \ } while (0) +#define MFBD_GROUP_SKIP(GROUP, TIMES) \ + do \ + { \ + MFBD_GROUP_SKIP_NBTN(GROUP, TIMES); \ + MFBD_GROUP_SKIP_MBTN(GROUP, TIMES); \ + } while (0) + #define MFBD_GROUP_RESET(GROUP) \ do \ { \