From b4bbe3e5b206434bd5429efc4bcda4b7c744b078 Mon Sep 17 00:00:00 2001 From: wenbo13579 Date: Wed, 20 Mar 2024 16:39:57 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=8A=A0=E5=85=A5event=20mask=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C=E5=B1=8F=E8=94=BD=E4=B8=8D?= =?UTF-8?q?=E5=BF=85=E8=A6=81=E7=9A=84=E4=BA=8B=E4=BB=B6=E4=B8=8A=E6=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 8 +++++--- ebtn/ebtn.c | 44 ++++++++++++++++++++++++++++++++++---------- ebtn/ebtn.h | 26 +++++++++++++++++++++----- main.c | 4 ++-- 4 files changed, 62 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 40b5787..ca1b454 100644 --- a/Makefile +++ b/Makefile @@ -110,6 +110,7 @@ LIBDIRS := $(LIB) FIXPATH = $(subst /,\,$1) RM := del /q /s MD := mkdir +MD_CHECK := $(Q)if not exist "$@" else MAIN := $(TARGET) ECHO=echo @@ -119,6 +120,7 @@ LIBDIRS := $(shell find $(LIB) -type d) FIXPATH = $1 RM = rm -rf MD := mkdir -p +MD_CHECK := endif # define any directories containing header files other than /usr/include @@ -167,14 +169,14 @@ all: main # mk path for object. $(OBJ_MD): - $(Q)if not exist "$@" $(Q)$(MD) $(call FIXPATH, $@) + $(MD_CHECK) $(Q)$(MD) $(call FIXPATH, $@) # mk output path. $(OUTPUT_PATH): - $(Q)if not exist "$@" $(Q)$(MD) $(call FIXPATH, $@) + $(MD_CHECK) $(Q)$(MD) $(call FIXPATH, $@) $(OBJDIR): - $(Q)if not exist "$@" $(Q)$(MD) $(call FIXPATH, $@) + $(MD_CHECK) $(Q)$(MD) $(call FIXPATH, $@) $(OUTPUT_MAIN): $(OBJECTS) @$(ECHO) Linking : "$@" diff --git a/ebtn/ebtn.c b/ebtn/ebtn.c index 988e8b9..d40cabb 100644 --- a/ebtn/ebtn.c +++ b/ebtn/ebtn.c @@ -1,8 +1,8 @@ #include #include "ebtn.h" -#define EBTN_FLAG_ONPRESS_SENT ((uint16_t)0x0001) /*!< Flag indicates that on-press event has been sent */ -#define EBTN_FLAG_IN_PROCESS ((uint16_t)0x0002) /*!< Flag indicates that button in process */ +#define EBTN_FLAG_ONPRESS_SENT ((uint8_t)0x01) /*!< Flag indicates that on-press event has been sent */ +#define EBTN_FLAG_IN_PROCESS ((uint8_t)0x02) /*!< Flag indicates that button in process */ /* Default button group instance */ static ebtn_t ebtn_default; @@ -58,7 +58,10 @@ static void prv_process_btn(ebtn_btn_t *btn, uint8_t old_state, uint8_t new_stat */ if ((btn->click_cnt > 0) && (ebtn_timer_sub(mstime, btn->click_last_time) >= btn->param->time_click_multi_max)) { - ebtobj->evt_fn(btn, EBTN_EVT_ONCLICK); + if (btn->event_mask & EBTN_EVT_MASK_ONCLICK) + { + ebtobj->evt_fn(btn, EBTN_EVT_ONCLICK); + } btn->click_cnt = 0; } @@ -68,7 +71,10 @@ static void prv_process_btn(ebtn_btn_t *btn, uint8_t old_state, uint8_t new_stat /* Start with new on-press */ btn->flags |= EBTN_FLAG_ONPRESS_SENT; - ebtobj->evt_fn(btn, EBTN_EVT_ONPRESS); + if (btn->event_mask & EBTN_EVT_MASK_ONPRESS) + { + ebtobj->evt_fn(btn, EBTN_EVT_ONPRESS); + } btn->time_change = mstime; /* Button state has now changed */ } @@ -85,13 +91,19 @@ static void prv_process_btn(ebtn_btn_t *btn, uint8_t old_state, uint8_t new_stat { btn->keepalive_last_time += btn->param->time_keepalive_period; ++btn->keepalive_cnt; - ebtobj->evt_fn(btn, EBTN_EVT_KEEPALIVE); + if (btn->event_mask & EBTN_EVT_MASK_KEEPALIVE) + { + ebtobj->evt_fn(btn, EBTN_EVT_KEEPALIVE); + } } // Scene1: multi click end with a long press, need send onclick event. if ((btn->click_cnt > 0) && (ebtn_timer_sub(mstime, btn->time_change) > btn->param->time_click_pressed_max)) { - ebtobj->evt_fn(btn, EBTN_EVT_ONCLICK); + if (btn->event_mask & EBTN_EVT_MASK_ONCLICK) + { + ebtobj->evt_fn(btn, EBTN_EVT_ONCLICK); + } btn->click_cnt = 0; } @@ -117,7 +129,10 @@ static void prv_process_btn(ebtn_btn_t *btn, uint8_t old_state, uint8_t new_stat { /* Handle on-release event */ btn->flags &= ~EBTN_FLAG_ONPRESS_SENT; - ebtobj->evt_fn(btn, EBTN_EVT_ONRELEASE); + if (btn->event_mask & EBTN_EVT_MASK_ONRELEASE) + { + ebtobj->evt_fn(btn, EBTN_EVT_ONRELEASE); + } /* Check time validity for click event */ if (ebtn_timer_sub(mstime, btn->time_change) >= btn->param->time_click_pressed_min && @@ -133,7 +148,10 @@ static void prv_process_btn(ebtn_btn_t *btn, uint8_t old_state, uint8_t new_stat // positive, send event to user. if ((btn->click_cnt > 0) && (ebtn_timer_sub(mstime, btn->time_change) < btn->param->time_click_pressed_min)) { - ebtobj->evt_fn(btn, EBTN_EVT_ONCLICK); + if (btn->event_mask & EBTN_EVT_MASK_ONCLICK) + { + ebtobj->evt_fn(btn, EBTN_EVT_ONCLICK); + } } /* * There was an on-release event, but timing @@ -148,7 +166,10 @@ static void prv_process_btn(ebtn_btn_t *btn, uint8_t old_state, uint8_t new_stat // maximum number of consecutive clicks has been reached. if ((btn->click_cnt > 0) && (btn->click_cnt == btn->param->max_consecutive)) { - ebtobj->evt_fn(btn, EBTN_EVT_ONCLICK); + if (btn->event_mask & EBTN_EVT_MASK_ONCLICK) + { + ebtobj->evt_fn(btn, EBTN_EVT_ONCLICK); + } btn->click_cnt = 0; } @@ -169,7 +190,10 @@ static void prv_process_btn(ebtn_btn_t *btn, uint8_t old_state, uint8_t new_stat { if (ebtn_timer_sub(mstime, btn->click_last_time) >= btn->param->time_click_multi_max) { - ebtobj->evt_fn(btn, EBTN_EVT_ONCLICK); + if (btn->event_mask & EBTN_EVT_MASK_ONCLICK) + { + ebtobj->evt_fn(btn, EBTN_EVT_ONCLICK); + } btn->click_cnt = 0; } } diff --git a/ebtn/ebtn.h b/ebtn/ebtn.h index 72f343e..0d457b9 100644 --- a/ebtn/ebtn.h +++ b/ebtn/ebtn.h @@ -41,6 +41,13 @@ typedef enum EBTN_EVT_KEEPALIVE, /*!< Keep alive event - sent periodically when button is active */ } ebtn_evt_t; +#define EBTN_EVT_MASK_ONPRESS (1 << EBTN_EVT_ONPRESS) +#define EBTN_EVT_MASK_ONRELEASE (1 << EBTN_EVT_ONRELEASE) +#define EBTN_EVT_MASK_ONCLICK (1 << EBTN_EVT_ONCLICK) +#define EBTN_EVT_MASK_KEEPALIVE (1 << EBTN_EVT_KEEPALIVE) + +#define EBTN_EVT_MASK_ALL (EBTN_EVT_MASK_ONPRESS | EBTN_EVT_MASK_ONRELEASE | EBTN_EVT_MASK_ONCLICK | EBTN_EVT_MASK_KEEPALIVE) + /** * @brief Returns the difference between two absolute times: time1-time2. * @param[in] time1: Absolute time expressed in internal time units. @@ -178,16 +185,23 @@ typedef struct ebtn_btn_param .max_consecutive = _max_consecutive \ } -#define EBTN_BUTTON_INIT(_key_id, _param) \ +#define EBTN_BUTTON_INIT_RAW(_key_id, _param, _mask) \ { \ - .key_id = _key_id, .param = _param, \ + .key_id = _key_id, .param = _param, .event_mask = _mask, \ } +#define EBTN_BUTTON_INIT(_key_id, _param) EBTN_BUTTON_INIT_RAW(_key_id, _param, EBTN_EVT_MASK_ALL) + #define EBTN_BUTTON_DYN_INIT(_key_id, _param) \ { \ .next = NULL, .btn = EBTN_BUTTON_INIT(_key_id, _param), \ } +#define EBTN_BUTTON_COMBO_INIT_RAW(_key_id, _param, _mask) \ + { \ + .comb_key = {0}, .btn = EBTN_BUTTON_INIT_RAW(_key_id, _param, _mask), \ + } + #define EBTN_BUTTON_COMBO_INIT(_key_id, _param) \ { \ .comb_key = {0}, .btn = EBTN_BUTTON_INIT(_key_id, _param), \ @@ -205,8 +219,10 @@ typedef struct ebtn_btn_param */ typedef struct ebtn_btn { - uint16_t key_id; /*!< User defined custom argument for callback function purpose */ - uint16_t flags; /*!< Private button flags management */ + uint16_t key_id; /*!< User defined custom argument for callback function purpose */ + uint8_t flags; /*!< Private button flags management */ + uint8_t event_mask; /*!< Private button event mask management */ + ebtn_time_t time_change; /*!< Time in ms when button state got changed last time after valid debounce */ ebtn_time_t time_state_change; /*!< Time in ms when button state got changed last time */ @@ -218,7 +234,7 @@ typedef struct ebtn_btn uint16_t keepalive_cnt; /*!< Number of keep alive events sent after successful on-press detection. Value is reset after on-release */ uint16_t click_cnt; /*!< Number of consecutive clicks detected, respecting maximum timeout - between clicks */ + between clicks */ const ebtn_btn_param_t *param; } ebtn_btn_t; diff --git a/main.c b/main.c index 245f29c..66f8156 100644 --- a/main.c +++ b/main.c @@ -8,7 +8,7 @@ extern int example_user(void); int main(void) { - example_test(); - // example_user(); + // example_test(); + example_user(); return 0; }