component toggle2nist "toggle button to nist logic";
description
"""
toggle2nist can be used with a momentary push button connected to a
toggle component to control a device that has separate on and off inputs
and has an is-on output.
If in changes states via the toggle output
If is-on is true then on is false and off is true.
If is-on is false the on true and off is false.
""";
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;
function _ nofp;
license "GPL";
;;
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;
}
}
else {
/* reset pins when we see the desired state */
if (to_state==is_on) {
on=0;
off=0;
}
}
old_in=in;
}