drivers/input: Implement the debounce in button_upper.c

use delay configurable watchdog to implement debounce

Signed-off-by: zhanghu5 <zhanghu5@xiaomi.com>
This commit is contained in:
zhanghu5 2023-03-15 14:46:57 +08:00 committed by Xiang Xiao
parent 26f996b927
commit 5890fde1ed
2 changed files with 25 additions and 3 deletions

View File

@ -544,6 +544,13 @@ config INPUT_BUTTONS
---help--- ---help---
Enable standard button upper half driver. Enable standard button upper half driver.
config INPUT_BUTTONS_DEBOUNCE_DELAY
int "Button Debounce Delay (millisecond)"
default 0
depends on INPUT_BUTTONS
---help---
Button Debounce Delay in ms
if INPUT_BUTTONS if INPUT_BUTTONS
config INPUT_BUTTONS_LOWER config INPUT_BUTTONS_LOWER

View File

@ -66,6 +66,10 @@ struct btn_upperhalf_s
*/ */
FAR struct btn_open_s *bu_open; FAR struct btn_open_s *bu_open;
#if CONFIG_INPUT_BUTTONS_DEBOUNCE_DELAY
struct wdog_s bu_wdog;
#endif
}; };
/* This structure describes the state of one open button driver instance */ /* This structure describes the state of one open button driver instance */
@ -106,7 +110,7 @@ static void btn_interrupt(FAR const struct btn_lowerhalf_s *lower,
/* Sampling */ /* Sampling */
static void btn_sample(FAR struct btn_upperhalf_s *priv); static void btn_sample(wdparm_t arg);
/* Character driver methods */ /* Character driver methods */
@ -214,15 +218,21 @@ static void btn_interrupt(FAR const struct btn_lowerhalf_s *lower,
/* Process the next sample */ /* Process the next sample */
btn_sample(priv); #if CONFIG_INPUT_BUTTONS_DEBOUNCE_DELAY
wd_start(&priv->bu_wdog, MSEC2TICK(CONFIG_INPUT_BUTTONS_DEBOUNCE_DELAY),
btn_sample, (wdparm_t)priv);
#else
btn_sample((wdparm_t)priv);
#endif
} }
/**************************************************************************** /****************************************************************************
* Name: btn_sample * Name: btn_sample
****************************************************************************/ ****************************************************************************/
static void btn_sample(FAR struct btn_upperhalf_s *priv) static void btn_sample(wdparm_t arg)
{ {
FAR struct btn_upperhalf_s *priv;
FAR const struct btn_lowerhalf_s *lower; FAR const struct btn_lowerhalf_s *lower;
FAR struct btn_open_s *opriv; FAR struct btn_open_s *opriv;
btn_buttonset_t sample; btn_buttonset_t sample;
@ -230,6 +240,7 @@ static void btn_sample(FAR struct btn_upperhalf_s *priv)
btn_buttonset_t press; btn_buttonset_t press;
btn_buttonset_t release; btn_buttonset_t release;
priv = (FAR struct btn_upperhalf_s *)arg;
DEBUGASSERT(priv && priv->bu_lower); DEBUGASSERT(priv && priv->bu_lower);
lower = priv->bu_lower; lower = priv->bu_lower;
@ -363,6 +374,10 @@ static int btn_close(FAR struct file *filep)
flags = enter_critical_section(); flags = enter_critical_section();
#if CONFIG_INPUT_BUTTONS_DEBOUNCE_DELAY
wd_cancel(&priv->bu_wdog);
#endif
/* Find the open structure in the list of open structures for the device */ /* Find the open structure in the list of open structures for the device */
for (prev = NULL, curr = priv->bu_open; for (prev = NULL, curr = priv->bu_open;