This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 5890fde1ed drivers/input: Implement the debounce in button_upper.c
5890fde1ed is described below

commit 5890fde1eddfd7bd9d3588799fba024841da2f0f
Author: zhanghu5 <[email protected]>
AuthorDate: Wed Mar 15 14:46:57 2023 +0800

    drivers/input: Implement the debounce in button_upper.c
    
    use delay configurable watchdog to implement debounce
    
    Signed-off-by: zhanghu5 <[email protected]>
---
 drivers/input/Kconfig        |  7 +++++++
 drivers/input/button_upper.c | 21 ++++++++++++++++++---
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/drivers/input/Kconfig b/drivers/input/Kconfig
index 9b6421f62f..33fd33ec6b 100644
--- a/drivers/input/Kconfig
+++ b/drivers/input/Kconfig
@@ -544,6 +544,13 @@ config INPUT_BUTTONS
        ---help---
                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
 
 config INPUT_BUTTONS_LOWER
diff --git a/drivers/input/button_upper.c b/drivers/input/button_upper.c
index 76a5a7109b..c847ed8ea5 100644
--- a/drivers/input/button_upper.c
+++ b/drivers/input/button_upper.c
@@ -66,6 +66,10 @@ struct btn_upperhalf_s
    */
 
   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 */
@@ -106,7 +110,7 @@ static void    btn_interrupt(FAR const struct 
btn_lowerhalf_s *lower,
 
 /* Sampling */
 
-static void    btn_sample(FAR struct btn_upperhalf_s *priv);
+static void    btn_sample(wdparm_t arg);
 
 /* Character driver methods */
 
@@ -214,15 +218,21 @@ static void btn_interrupt(FAR const struct 
btn_lowerhalf_s *lower,
 
   /* 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
  ****************************************************************************/
 
-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 struct btn_open_s *opriv;
   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 release;
 
+  priv = (FAR struct btn_upperhalf_s *)arg;
   DEBUGASSERT(priv && priv->bu_lower);
   lower = priv->bu_lower;
 
@@ -363,6 +374,10 @@ static int btn_close(FAR struct file *filep)
 
   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 */
 
   for (prev = NULL, curr = priv->bu_open;

Reply via email to