/ src / hal / components / toggle2nist.comp
toggle2nist.comp
 1  component toggle2nist "toggle button to nist logic";
 2  
 3  description
 4  """
 5  toggle2nist can be used with a momentary push button connected to a
 6  toggle component to control a device that has separate on and off inputs
 7  and has an is-on output. 
 8  If in changes states via the toggle output
 9    If is-on is true then on is false and off is true. 
10    If is-on is false the on true and off is false.
11  """;
12  
13  pin in  bit in;
14  pin in  bit is_on;
15  pin out bit on;
16  pin out bit off;
17  variable int old_in;
18  variable int to_state=0;
19  function _ nofp;
20  license "GPL";
21  ;;
22  FUNCTION(_)  {
23  if (in!=old_in) /* a toggle has occurred */ {
24      if (is_on) {   /* turn OFF if it's on */
25          on=0;
26          off=1;
27          to_state=0;
28      }
29      else if (!is_on) { /* turn ON if it's off */
30          on=1;
31          off=0;
32          to_state=1;
33      }
34  }
35  else {
36  /* reset pins when we see the desired state */
37      if (to_state==is_on) {
38          on=0;
39          off=0;
40      }
41  }
42  old_in=in;
43  }
44