1. 加入event mask处理逻辑,屏蔽不必要的事件上报

This commit is contained in:
wenbo13579
2024-03-20 16:39:57 +08:00
parent 9be172aced
commit b4bbe3e5b2
4 changed files with 62 additions and 20 deletions

View File

@@ -110,6 +110,7 @@ LIBDIRS := $(LIB)
FIXPATH = $(subst /,\,$1) FIXPATH = $(subst /,\,$1)
RM := del /q /s RM := del /q /s
MD := mkdir MD := mkdir
MD_CHECK := $(Q)if not exist "$@"
else else
MAIN := $(TARGET) MAIN := $(TARGET)
ECHO=echo ECHO=echo
@@ -119,6 +120,7 @@ LIBDIRS := $(shell find $(LIB) -type d)
FIXPATH = $1 FIXPATH = $1
RM = rm -rf RM = rm -rf
MD := mkdir -p MD := mkdir -p
MD_CHECK :=
endif endif
# define any directories containing header files other than /usr/include # define any directories containing header files other than /usr/include
@@ -167,14 +169,14 @@ all: main
# mk path for object. # mk path for object.
$(OBJ_MD): $(OBJ_MD):
$(Q)if not exist "$@" $(Q)$(MD) $(call FIXPATH, $@) $(MD_CHECK) $(Q)$(MD) $(call FIXPATH, $@)
# mk output path. # mk output path.
$(OUTPUT_PATH): $(OUTPUT_PATH):
$(Q)if not exist "$@" $(Q)$(MD) $(call FIXPATH, $@) $(MD_CHECK) $(Q)$(MD) $(call FIXPATH, $@)
$(OBJDIR): $(OBJDIR):
$(Q)if not exist "$@" $(Q)$(MD) $(call FIXPATH, $@) $(MD_CHECK) $(Q)$(MD) $(call FIXPATH, $@)
$(OUTPUT_MAIN): $(OBJECTS) $(OUTPUT_MAIN): $(OBJECTS)
@$(ECHO) Linking : "$@" @$(ECHO) Linking : "$@"

View File

@@ -1,8 +1,8 @@
#include <string.h> #include <string.h>
#include "ebtn.h" #include "ebtn.h"
#define EBTN_FLAG_ONPRESS_SENT ((uint16_t)0x0001) /*!< Flag indicates that on-press event has been sent */ #define EBTN_FLAG_ONPRESS_SENT ((uint8_t)0x01) /*!< 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_IN_PROCESS ((uint8_t)0x02) /*!< Flag indicates that button in process */
/* Default button group instance */ /* Default button group instance */
static ebtn_t ebtn_default; 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)) 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; 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 */ /* Start with new on-press */
btn->flags |= EBTN_FLAG_ONPRESS_SENT; 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 */ 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_last_time += btn->param->time_keepalive_period;
++btn->keepalive_cnt; ++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. // 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)) 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; 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 */ /* Handle on-release event */
btn->flags &= ~EBTN_FLAG_ONPRESS_SENT; 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 */ /* Check time validity for click event */
if (ebtn_timer_sub(mstime, btn->time_change) >= btn->param->time_click_pressed_min && 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. // positive, send event to user.
if ((btn->click_cnt > 0) && (ebtn_timer_sub(mstime, btn->time_change) < btn->param->time_click_pressed_min)) 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 * 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. // maximum number of consecutive clicks has been reached.
if ((btn->click_cnt > 0) && (btn->click_cnt == btn->param->max_consecutive)) 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; 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) 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; btn->click_cnt = 0;
} }
} }

View File

@@ -41,6 +41,13 @@ typedef enum
EBTN_EVT_KEEPALIVE, /*!< Keep alive event - sent periodically when button is active */ EBTN_EVT_KEEPALIVE, /*!< Keep alive event - sent periodically when button is active */
} ebtn_evt_t; } 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. * @brief Returns the difference between two absolute times: time1-time2.
* @param[in] time1: Absolute time expressed in internal time units. * @param[in] time1: Absolute time expressed in internal time units.
@@ -178,16 +185,23 @@ typedef struct ebtn_btn_param
.max_consecutive = _max_consecutive \ .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) \ #define EBTN_BUTTON_DYN_INIT(_key_id, _param) \
{ \ { \
.next = NULL, .btn = EBTN_BUTTON_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) \ #define EBTN_BUTTON_COMBO_INIT(_key_id, _param) \
{ \ { \
.comb_key = {0}, .btn = EBTN_BUTTON_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 typedef struct ebtn_btn
{ {
uint16_t key_id; /*!< User defined custom argument for callback function purpose */ uint16_t key_id; /*!< User defined custom argument for callback function purpose */
uint16_t flags; /*!< Private button flags management */ 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 ebtn_time_t time_change; /*!< Time in ms when button state got changed last time after valid
debounce */ debounce */
ebtn_time_t time_state_change; /*!< Time in ms when button state got changed last time */ 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 uint16_t keepalive_cnt; /*!< Number of keep alive events sent after successful on-press
detection. Value is reset after on-release */ detection. Value is reset after on-release */
uint16_t click_cnt; /*!< Number of consecutive clicks detected, respecting maximum timeout uint16_t click_cnt; /*!< Number of consecutive clicks detected, respecting maximum timeout
between clicks */ between clicks */
const ebtn_btn_param_t *param; const ebtn_btn_param_t *param;
} ebtn_btn_t; } ebtn_btn_t;

4
main.c
View File

@@ -8,7 +8,7 @@ extern int example_user(void);
int main(void) int main(void)
{ {
example_test(); // example_test();
// example_user(); example_user();
return 0; return 0;
} }