Skip to content

Commit 4992bb1

Browse files
committed
hal: split 'toggle2nist.comp' for toggle and momentary inputs
Creates different components for handling toggle buttons and momentary buttons with component names that actually reflect the functionality. - Restores the legacy functionality (as in 2.9) of 'toggle2nist' with added input debounce. - Fixes the toggle2nist documentation so it matches actual (legacy) behavior. - Adds a new 'momentary2nist' component that does what the old 'toggle2nist' docs described.
1 parent 1a52a4a commit 4992bb1

2 files changed

Lines changed: 87 additions & 9 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
component momentary2nist "momentary button to nist logic";
2+
3+
description
4+
"""
5+
Monemtary2nist can be used with a momentary push button
6+
to control a device that has separate on and off inputs
7+
and an is-on output.
8+
A debounce delay in cycles can be set for 'in'. (default = 2)
9+
10+
* On a rising edge on pin *in* when *is-on* is low: It sets *on* until *is-on* becomes high.
11+
* On a rising edge on pin *in* when *is-on* is high: It sets *off* until *is-on* becomes low.
12+
13+
....
14+
       ┐     ┌─────xxxxxxxxxxxx┐           ┌─────xxxxxxxxxxxx┐
15+
in   : └─────┘     xxxxxxxxxxxx└───────────┘     xxxxxxxxxxxx└─────
16+
17+
       ┐     ┌───────────┐
18+
on   : └─────┘           └─────────────────────────────────────────
19+
20+
       ┐                                   ┌───────────┐
21+
off  : └───────────────────────────────────┘           └───────────
22+
23+
       ┐                 ┌─────────────────────────────┐
24+
is-on: └─────────────────┘                             └───────────
25+
....
26+
27+
""";
28+
29+
pin in bit in "momentary button in";
30+
pin in bit is_on "current state of device";
31+
pin in unsigned debounce = 2 "debounce delay for 'in'-pin in cycles";
32+
pin out bit on "turn device on";
33+
pin out bit off "turn device off";
34+
variable int debounce_cntr;
35+
variable unsigned debounce_set;
36+
variable int state;
37+
38+
option period no;
39+
function _;
40+
license "GPL";
41+
author "David Mueller";
42+
;;
43+
FUNCTION(_) {
44+
45+
if (( debounce < 1 ) || ( debounce > 10000 )) {
46+
debounce_set = 2; /* set a sane value */
47+
} else {
48+
debounce_set = debounce;
49+
}
50+
51+
if (in && state == 0 ) { /* input has changed from debounced 0 -> 1 */
52+
debounce_cntr++;
53+
if ( debounce_cntr >= (int)debounce_set ) {
54+
if (!is_on) { /* turn ON if it's off */
55+
on=1;
56+
off=0;
57+
} else { /* turn OFF if it's on */
58+
on=0;
59+
off=1;
60+
}
61+
state = 1;
62+
debounce_cntr = 0;
63+
}
64+
} else if (!in && state == 1) { /* input has changed from debounced 1 -> 0 */
65+
debounce_cntr++;
66+
if ( debounce_cntr >= (int)debounce_set ) {
67+
state = 0;
68+
debounce_cntr = 0;
69+
}
70+
} else if ((!is_on && off) || (is_on && on)) { /* reset outputs once device has switched*/
71+
on = 0;
72+
off = 0;
73+
debounce_cntr = 0;
74+
} else {
75+
debounce_cntr = 0;
76+
}
77+
}

src/hal/components/toggle2nist.comp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ component toggle2nist "toggle button to nist logic";
22

33
description
44
"""
5-
Toggle2nist can be used with a momentary push button
5+
Toggle2nist can be used with a latching push button
66
to control a device that has separate on and off inputs
77
and an is-on output.
88
A debounce delay in cycles can be set for 'in'. (default = 2)
99

1010
* On a rising edge on pin *in* when *is-on* is low: It sets *on* until *is-on* becomes high.
11-
* On a rising edge on pin *in* when *is-on* is high: It sets *off* until *is-on* becomes low.
11+
* On a falling edge on pin *in* when *is-on* is high: It sets *off* until *is-on* becomes low.
1212

1313
....
14-
       ┐     ┌─────xxxxxxxxxxxx┐           ┌─────xxxxxxxxxxxx
15-
in   : └─────┘     xxxxxxxxxxxx└───────────┘     xxxxxxxxxxxx└─────
14+
       ┐     ┌─────────────────────────────
15+
in   : └─────┘                             └───────────────────────
1616

1717
       ┐     ┌───────────┐
1818
on   : └─────┘           └─────────────────────────────────────────
@@ -26,7 +26,7 @@ is-on: └─────────────────┘       
2626

2727
""";
2828

29-
pin in bit in "momentary button in";
29+
pin in bit in "toggle button in";
3030
pin in bit is_on "current state of device";
3131
pin in unsigned debounce = 2 "debounce delay for 'in'-pin in cycles";
3232
pin out bit on "turn device on";
@@ -38,7 +38,7 @@ variable int state;
3838
option period no;
3939
function _;
4040
license "GPL";
41-
author "Anders Wallin, David Mueller";
41+
author "Anders Wallin";
4242
;;
4343
FUNCTION(_) {
4444

@@ -54,16 +54,17 @@ FUNCTION(_) {
5454
if (!is_on) { /* turn ON if it's off */
5555
on=1;
5656
off=0;
57-
} else { /* turn OFF if it's on */
58-
on=0;
59-
off=1;
6057
}
6158
state = 1;
6259
debounce_cntr = 0;
6360
}
6461
} else if (!in && state == 1) { /* input has changed from debounced 1 -> 0 */
6562
debounce_cntr++;
6663
if ( debounce_cntr >= (int)debounce_set ) {
64+
if (is_on) { /* turn OFF if it's on */
65+
on=0;
66+
off=1;
67+
}
6768
state = 0;
6869
debounce_cntr = 0;
6970
}

0 commit comments

Comments
 (0)