From 83afcad3c4daaf4e9ec0385cd5a3f207129450a7 Mon Sep 17 00:00:00 2001 From: David Mueller Date: Wed, 27 Nov 2024 09:56:36 +0100 Subject: [PATCH] toggle2nist: Fix issues with race conditions and add input button debounce --- src/hal/components/toggle2nist.comp | 69 ++++++++++++++++------------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/src/hal/components/toggle2nist.comp b/src/hal/components/toggle2nist.comp index 427948f298a..74f5cf5be8d 100644 --- a/src/hal/components/toggle2nist.comp +++ b/src/hal/components/toggle2nist.comp @@ -4,28 +4,29 @@ description """ Toggle2nist can be used with a momentary push button to control a device that has separate on and off inputs -and an is-on output. +and an is-on output. +A debounce delay in cycles can be set. (default = 2) \[bu] On a rising edge on pin \\fIin\\fR when \\fIis-on\\fR is low: It sets \\fIon\\fR until \\fIis-on\\fR becomes high. \[bu] On a rising edge on pin \\fIin\\fR when \\fIis-on\\fR is high: It sets \\fIoff\\fR until \\fIis-on\\fR becomes low. -       ┐     ┌─────xxxxxxxxxxxx┐           ┌─────xxxxxxxxxxxx┐ + ┐ ┌─────xxxxxxxxxxxx┐ ┌─────xxxxxxxxxxxx┐ .br -in   : └─────┘     xxxxxxxxxxxx└───────────┘     xxxxxxxxxxxx└───── +in : └─────┘ xxxxxxxxxxxx└───────────┘ xxxxxxxxxxxx└───── .br -       ┐     ┌───────────┐ + ┐ ┌───────────┐ .br -on   : └─────┘           └───────────────────────────────────────── +on : └─────┘ └───────────────────────────────────────── .br -       ┐                                   ┌───────────┐ + ┐ ┌───────────┐ .br -off  : └───────────────────────────────────┘           └─────────── +off : └───────────────────────────────────┘ └─────────── .br -       ┐                 ┌─────────────────────────────┐ + ┐ ┌─────────────────────────────┐ .br -is-on: └─────────────────┘                             └─────────── +is-on: └─────────────────┘ └─────────── """; @@ -33,32 +34,38 @@ pin in bit in; pin in bit is_on; pin out bit on; pin out bit off; -variable int old_in; -variable int to_state=0; +param rw u32 debounce = 2 "debounce delay in cycles"; +variable int debounce_cntr; +variable int state; + function _ nofp; license "GPL"; -author "Anders Wallin"; +author "David Mueller, Based on work by Anders Wallin and John Kasunich"; ;; FUNCTION(_) { -if (in!=old_in) /* a toggle has occurred */ { - if (is_on) { /* turn OFF if it's on */ - on=0; - off=1; - to_state=0; - } - else if (!is_on) { /* turn ON if it's off */ - on=1; - off=0; - to_state=1; + + if (( debounce < 1 ) || ( debounce > 10000 )) { + debounce = 2; /* set a sane value */ } -} -else { -/* reset pins when we see the desired state */ - if (to_state==is_on) { - on=0; - off=0; + + if (in && state == 0 ) { /* input has changed from debounced 0 -> 1 */ + debounce_cntr++; + if ( debounce_cntr >= debounce ) { + if (!is_on) { /* turn ON if it's off */ + on=1; + off=0; + } else { /* turn OFF if it's on */ + on=0; + off=1; + } + state = 1; + debounce_cntr = 0; + } + } else if (!in && state == 1) { /* input has changed from debounced 1 -> 0 */ + debounce_cntr++; + if ( debounce_cntr >= debounce ) { + state = 0; + debounce_cntr = 0; + } } } -old_in=in; -} -