From bb2f7c65a5fb5a6c6bd98f005498db77c48fd72f Mon Sep 17 00:00:00 2001 From: david mueller Date: Sat, 19 Sep 2026 09:41:08 +0100 Subject: [PATCH] 2nist comps: Fix glitch caused by loose pulse_width counter Both 'momentary2nist' and 'toggle2nist' suffer from a loose pulse_length counter that leads to 'on'-,'off'-signals getting cut short because the counter is not reset properly and counts when idling. The counter is now reset when the pins are changed and no longer counts when the component is idle. Also changes two misleading comments as the input pin change is debounced after the conditions are met. --- src/hal/components/momentary2nist.comp | 29 ++++++++++++-------------- src/hal/components/toggle2nist.comp | 17 ++++++++------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/hal/components/momentary2nist.comp b/src/hal/components/momentary2nist.comp index 420c114e481..ab032d39378 100644 --- a/src/hal/components/momentary2nist.comp +++ b/src/hal/components/momentary2nist.comp @@ -35,7 +35,7 @@ pin out bool on "turn device on"; pin out bool off "turn device off"; variable unsigned debounce_cntr; variable unsigned pulse_length; -variable bool state; +variable unsigned state; option period no; function _; @@ -52,34 +52,31 @@ FUNCTION(_) { debounce_val = 2; // set a sane value } - if (in_val && state == 0 ) { // input has changed from debounced 0 -> 1 + if (in_val && state == 0 ) { + // input has changed from 0 -> 1 debounce_cntr++; if ( debounce_cntr >= debounce_val ) { - if (!ison_val) { // turn ON if it's off - on_set(1); - off_set(0); - } else { // turn OFF if it's on - on_set(0); - off_set(1); - } + on_set(!ison_val); + off_set(ison_val); + pulse_length = 0; state = 1; debounce_cntr = 0; } - } else if (!in_val && state == 1) { // input has changed from debounced 1 -> 0 + } else if (!in_val && state == 1) { + // input has changed from 1 -> 0 debounce_cntr++; if ( debounce_cntr >= debounce_val ) { state = 0; debounce_cntr = 0; } - } else if ((!ison_val && off) || (ison_val && on) || (pulse_length > max_pulse_length)) { - // reset outputs once device has switched or maximum pulse length is reached - on_set(0); - off_set(0); + } else if ( (!ison_val != off) && (ison_val != on) && (pulse_length < max_pulse_length) ) { + // Waiting for the 'ison' pin to confirm change of state debounce_cntr = 0; - pulse_length = 0; + pulse_length ++; } else { + on_set(0); + off_set(0); debounce_cntr = 0; - pulse_length ++; } } diff --git a/src/hal/components/toggle2nist.comp b/src/hal/components/toggle2nist.comp index 9c164425f21..12a97956746 100644 --- a/src/hal/components/toggle2nist.comp +++ b/src/hal/components/toggle2nist.comp @@ -53,34 +53,35 @@ FUNCTION(_) { debounce_val = 2; // set a sane value } - if (in_val && state == 0 ) { /* input has changed from debounced 0 -> 1 */ + if (in_val && state == 0) { /* input has changed from 0 -> 1 */ debounce_cntr++; if ( debounce_cntr >= debounce_val ) { if (!ison_val) { /* turn ON if it's off */ on_set(1); off_set(0); + pulse_length = 0; } state = 1; debounce_cntr = 0; } - } else if (!in_val && state == 1) { /* input has changed from debounced 1 -> 0 */ + } else if (!in_val && state == 1) { /* input has changed from 1 -> 0 */ debounce_cntr++; if ( debounce_cntr >= debounce_val ) { if (ison_val) { /* turn OFF if it's on */ on_set(0); off_set(1); + pulse_length = 0; } state = 0; debounce_cntr = 0; } - } else if ((!ison_val && off) || (ison_val && on) || (pulse_length > max_pulse_length)) { - // reset outputs once device has switched or maximum pulse length is reached - on_set(0); - off_set(0); + } else if ( (!ison_val != off) && (ison_val != on) && (pulse_length < max_pulse_length) ) { + // Waiting for the 'ison' pin to confirm change of state debounce_cntr = 0; - pulse_length = 0; + pulse_length ++; } else { + on_set(0); + off_set(0); debounce_cntr = 0; - pulse_length ++; } }