/ src / hal / components / time.comp
time.comp
  1  component time "Time on in Hours, Minutes, Seconds";
  2  
  3  description 
  4  """
  5  Time
  6  
  7  When either the time.N.start or time.N.pause bits goes true the cycle
  8  timer resets and starts to time until time.N.start AND time.N.pause go
  9  false. When the time.N.pause bit goes true timing is paused until
 10  time.N.pause goes false. If you connect time.N.start to
 11  halui.program.is-running and leave time.N.pause unconnected the timer
 12  will reset during a pause. See the example connections below for more
 13  information.
 14  
 15  Time returns the hours, minutes, and seconds that time.N.start is true.
 16  
 17  Sample pyVCP code to display the hours:minutes:seconds.
 18  
 19  <pyvcp>
 20    <hbox>
 21    <label>
 22      <text>"Cycle Time"</text>
 23      <font>("Helvetica",14)</font>
 24    </label>
 25    <u32> 
 26        <halpin>"time-hours"</halpin>
 27        <font>("Helvetica",14)</font>
 28        <format>"2d"</format>
 29    </u32>
 30    <label>
 31      <text>":"</text>
 32      <font>("Helvetica",14)</font>
 33    </label>
 34    <u32> 
 35        <halpin>"time-minutes"</halpin>
 36        <font>("Helvetica",14)</font>
 37        <format>"2d"</format>
 38    </u32>
 39    <label>
 40      <text>":"</text>
 41      <font>("Helvetica",14)</font>
 42    </label>
 43    <u32> 
 44        <halpin>"time-seconds"</halpin>
 45        <font>("Helvetica",14)</font>
 46        <format>"2d"</format>
 47    </u32>
 48    </hbox>
 49  </pyvcp>
 50  
 51  In your post-gui.hal file you might use one of the following to connect
 52  this timer:
 53   
 54   For a new config:
 55   
 56   loadrt time
 57   addf time.0 servo-thread
 58   net cycle-timer        time.0.start <= halui.program.is-running
 59   net cycle-timer-pause  time.0.pause <= halui.program.is-paused
 60   net cycle-seconds pyvcp.time-seconds <= time.0.seconds
 61   net cycle-minutes pyvcp.time-minutes <= time.0.minutes
 62   net cycle-hours pyvcp.time-hours <= time.0.hours
 63  
 64  
 65   Previous to this version if you wanted the timer to continue running
 66   during a pause instead of resetting, you had to use a HAL NOT component
 67   to invert the halui.program.is-idle pin and connect to time.N.start as
 68   shown below:
 69  
 70   loadrt time
 71   loadrt not
 72   addf time.0 servo-thread
 73   addf not.0 servo-thread
 74   net prog-running not.0.in <= halui.program.is-idle
 75   net cycle-timer time.0.start <= not.0.out
 76   net cycle-seconds pyvcp.time-seconds <= time.0.seconds
 77   net cycle-minutes pyvcp.time-minutes <= time.0.minutes
 78   net cycle-hours pyvcp.time-hours <= time.0.hours
 79   
 80   For those who have this setup already, you can simply add a net connecting
 81   time.N.pause to halui.program.is-paused:
 82  
 83   net cycle-timer-pause time.0.pause <= halui.program.is-paused
 84  
 85  
 86  """;
 87   
 88  author "John Thornton, itaib, Moses McKnight";
 89  
 90  license "GPL";
 91  
 92  // Input Pins
 93  pin in bit start "Timer On";
 94  pin in bit pause = 0 "Pause";
 95  
 96  // Output Pins
 97  pin out u32 seconds "Seconds";
 98  pin out u32 minutes "Minutes";
 99  pin out u32 hours "Hours";
100  
101  // Global Variables
102  variable double totalnsec;
103  variable int old_start;
104  
105  function _;
106  
107  ;;
108  
109  #include "rtapi_math.h"
110  
111  FUNCTION(_) {
112      rtapi_u32 totalseconds;
113      int running = start | pause;
114      if(running && !old_start) totalnsec = 0;
115  
116      if(start && !pause){
117      totalnsec = totalnsec + period;
118      totalseconds = totalnsec * 0.000000001;
119      seconds = totalseconds % 60;
120  	minutes = (totalseconds / 60) % 60;
121  	hours = (totalseconds / 3600);
122  	}
123  	old_start = running;
124  }
125