stamp.core
Class Timer

java.lang.Object
  |
  +--stamp.core.VirtualPeripheral
        |
        +--stamp.core.Timer

public class Timer
extends VirtualPeripheral

A general purpose 32 bit timer. There is only a single instantiation of the timer itself, but there can be multiple Timer objects which use the timer value. The first Timer created starts the timer VP. Subsequent instantiations use the same VP. Caution should be taken that stopping the timer VP does not affect other Timer objects.

The timer is a 32 bit counter which is incremented every 8.68us. Therefore:

The timer will overflow every 10.36 hours.

A timer would typically be used in code like the following:

 Timer t = new Timer();

 t.mark();

 while (true) {
   // Perform some action.

   if ( t.timeout(100) ) { // Check for a 100ms timeout.
     t.mark();
     // Perform a periodic action.
   }
 }

 


Constructor Summary
Timer()
          Creates a Timer object.
 
Method Summary
 void mark()
          Remembers the current timer value.
static void start(Timer vp)
          Starts the timer VP.
static void stop()
          Stops the timer VP.
 int tickHi()
          Retrieves the high 16 bits of the timer value.
 int tickLo()
          Retrieves the low 16 bits of the timer value.
 boolean timeout(int timeMS)
          Checks whether timeMS milliseconds have elapsed since the last call to mark().
 boolean timeout(int hi, int lo)
          Compares the current timer value with the value remembered by the last call to the mark() method.
 boolean timeoutSec(int timeS)
          Checks whether timeS seconds have elapsed since the last call to mark().
 
Methods inherited from class java.lang.Object
equals
 

Constructor Detail

Timer

public Timer()
Creates a Timer object. The first instantiation of this class starts the timer VP. Subsequent instantiations use the same VP.
Method Detail

start

public static void start(Timer vp)
Starts the timer VP. The constructor will start the Timer when it is first created. If it is later stopped with stop() then start() will start it again with the same set of parameters.

It is an error to call start if the Timer is already running.


stop

public static void stop()
Stops the timer VP.

tickHi

public int tickHi()
Retrieves the high 16 bits of the timer value.
Returns:
the high 16 bits of the timer value.

tickLo

public int tickLo()
Retrieves the low 16 bits of the timer value.

tickLo() should always be called before tickHi().

Returns:
the low 16 bits of the timer value.

mark

public void mark()
Remembers the current timer value. Call this method to mark the start of a timeout period.

timeout

public boolean timeout(int hi,
                       int lo)
Compares the current timer value with the value remembered by the last call to the mark() method. If the difference is greater than or equal to the parameter then returns true.
Returns:
true if the timeout period has been exceeded.

timeout

public boolean timeout(int timeMS)
Checks whether timeMS milliseconds have elapsed since the last call to mark().
Returns:
true if the timeout period has been exceeded.

timeoutSec

public boolean timeoutSec(int timeS)
Checks whether timeS seconds have elapsed since the last call to mark(). The maximum timeout is 935 seconds.
Returns:
true if the timeout period has been exceeded.