Button Component
Source Path: example\button\src
Button Example
The button library uses pin devices to detect various button actions. It utilizes one timer to handle debouncing and another timer to detect long press actions.
It supports the following actions:
Press: The button is pressed.
Release: The button is released.
Long Press: The button is pressed and held for a duration specified by
BUTTON_ADV_ACTION_CHECK_DELAY
.Click: The button is pressed and released without triggering a long press action.
Users can register handlers for each button to be notified when any action occurs. The button activity state is configurable.
Configuration
The maximum number of buttons supported and the threshold for detecting long press actions are configured by the following macros:
#define BUTTON_MAX_NUM 2
#define BUTTON_ADV_ACTION_CHECK_DELAY 3000
These can be configured under SiFli Middleware -> Enable button library
. You can enter the compilation interface and type menuconfig
to access it.
Main Code Section
static void button_event_handler(int32_t pin, button_action_t action)
{
printf("button:%d,%d
", pin, action);
}
button_cfg_t cfg;
#if defined(BSP_USING_BOARD_EM_LB525XXX)
cfg.pin = 34;
#elif defined (BSP_USING_BOARD_EM_LB587XXX)
cfg.pin = 152;
#endif
cfg.active_state = BUTTON_ACTIVE_HIGH;
cfg.mode = PIN_MODE_INPUT;
cfg.button_handler = button_event_handler;
int32_t id = button_init(&cfg);
RT_ASSERT(id >= 0);
RT_ASSERT(SF_EOK == button_enable(id));
#if defined(BSP_USING_BOARD_EM_LB525XXX)
cfg.pin = 11;
#elif defined (BSP_USING_BOARD_EM_LB587XXX)
cfg.pin = 150;
#endif
cfg.active_state = BUTTON_ACTIVE_HIGH;
cfg.mode = PIN_MODE_INPUT;
cfg.button_handler = button_event_handler;
id = button_init(&cfg);
RT_ASSERT(id >= 0);
RT_ASSERT(SF_EOK == button_enable(id));