kitchen_timer

class kitchen_timer

Counts a simulation time (not a wall clock time) and triggers an event once the timer expires.  This class is not thread-safe.

Summary
kitchen_timerCounts a simulation time (not a wall clock time) and triggers an event once the timer expires.
Events
ringTriggers when the specified delay elapsed.
Functions
newCreates a new timer.
set_delaySets the delay for the timer to ring.
add_delayAdds the specified delay.
set_random_delaySet the random delay for the timer to ring between the specified range.
startStarts the timer.
stopStops the timer.
pausePauses the timer.
resumeResumes the timer, if the timer was paused.
resetResets the timer.
get_elapsedReturns the elapsed time since the timer was last started or resumed.
get_remainingReturns the remaining time to ring.
is_stoppedReturns 1 if the timer is currently stopped.
is_runningReturns 1 if the timer is currently running.
is_pausedReturns 1 if the timer is currently paused.
get_stateReturns the current state of the timer.

Events

ring

Triggers when the specified delay elapsed.

Functions

new

function new(time delay =  0)

Creates a new timer.

Argumment

delayoptional The delay for the timer to ring.  The default is 0.

set_delay

function void set_delay(time delay)

Sets the delay for the timer to ring.  This function resets the timer.

Argumment

delayThe delay for the timer to ring.

Example

kitchen_timer kt = new();
kt.set_delay( 100 );
kt.start();
@kt.ring;
assert( kt.get_elapsed() == 100 );

add_delay

function void add_delay(time delay)

Adds the specified delay.  If the timer is currently running, the timer is paused, added the delay, then resumed.

Argument

delayThe additional delay for the timer to ring.

set_random_delay

function time set_random_delay(time delay1,
time delay2)

Set the random delay for the timer to ring between the specified range.  This function resets the timer.

Arguments

delay1The delay boundary 1.
delay2The delay boundary 2.  The delay is randomized between delay1 and delay2, inclusive.

Returns

The randomized delay value.

Example

kitchen_timer kt = new();
time random_delay = kt.set_random_delay( 100, 200 );
kt.start();
@kt.ring;
assert( kt.get_elapsed() == random_delay );

start

function void start()

Starts the timer.  The ring event is triggered when the remaining time becomes 0.  The timer can be stopped by calling stop or be paused by calling pause.  If the timer is already started, no action is taken.

Example

kitchen_timer kt = new();
kt.set_delay( 100 );
kt.start();
@kt.ring;
assert( kt.get_elapsed() == 100 );

stop

function void stop()

Stops the timer.  The elapsed time is stamped if the timer was running.

Example

kitchen_timer kt = new();
kt.set_delay( 100 );
kt.start();
#50 kt.stop();
assert( kt.get_elapsed() == 50 );

pause

function void pause()

Pauses the timer.  The elapsed time is stamped if the timer was running.  The timer can be resumed by calling resume.  If the timer was not running, no action is taken.

Example

kitchen_timer kt = new();
kt.set_delay( 100 );
kt.start();
#50 kt.pause();
assert( kt.get_elapsed() == 50 );

resume

function void resume()

Resumes the timer, if the timer was paused.  Otherwise, no action is taken.

Example

kitchen_timer kt = new();
kt.set_delay( 100 );
kt.start();
#30 kt.pause();
assert( kt.get_elapsed() == 30 );
kt.resume();
@kt.ring;
assert( kt.get_elapsed() == 70 );

reset

function void reset()

Resets the timer.

get_elapsed

function time get_elapsed()

Returns the elapsed time since the timer was last started or resumed.  If the timer is paused or stopped, returns the elapsed time to the last paused or stopped time.

Returns

The elapsed time.

Example

kitchen_timer kt = new();
kt.set_delay( 100 );
kt.start();
@kt.ring;
assert( kt.get_elapsed() == 100 );

get_remaining

function time get_remaining()

Returns the remaining time to ring.  If the timer is paused or stopped, returns the remaining time from the last paused or stopped time.

Returns

The remaining time.

Example

kitchen_timer kt = new();
kt.set_delay( 100 );
kt.start();
#30;
assert( kt.get_remaining() == 70 );
@kt.ring;
assert( kt.get_remaining() == 0 );

is_stopped

function bit is_stopped()

Returns 1 if the timer is currently stopped.

Returns

If the timer is currently stopped, returns 1.  Otherwise, returns 0.

Example

kitchen_timer kt = new();
kt.set_delay( 100 );
kt.start();
#30;
assert( kt.is_stopped() == 0 );
@kt.ring;
assert( kt.is_stopped() == 1 );

is_running

function bit is_running()

Returns 1 if the timer is currently running.

Returns

If the timer is currently running, returns 1.  Otherwise, returns 0.

Example

kitchen_timer kt = new();
kt.set_delay( 100 );
kt.start();
#30;
assert( kt.is_running() == 1 );
@kt.ring;
assert( kt.is_running() == 0 );

is_paused

function bit is_paused()

Returns 1 if the timer is currently paused.

Returns

If the timer is currently paused, returns 1.  Otherwise, returns 0.

Example

kitchen_timer kt = new();
kt.set_delay( 100 );
kt.start();
#50 kt.pause();
assert( kt.is_paused() == 1 );
kt.resume();
#10;
assert( kt.is_paused() == 0 );

get_state

function state_e get_state()

Returns the current state of the timer.

Returns

The current state of the timer.

Example

kitchen_timer kt = new();
kt.set_delay( 100 );
kt.start();
#50 kt.pause();
assert( kt.get_state() == kitchen_timer::PAUSED );
kt.resume();
#10;
assert( kt.get_state() == kitchen_timer::RUNNING );
class kitchen_timer
Counts a simulation time (not a wall clock time) and triggers an event once the timer expires.
function new(time delay =  0)
Creates a new timer.
function void set_delay(time delay)
Sets the delay for the timer to ring.
function void add_delay(time delay)
Adds the specified delay.
function time set_random_delay(time delay1,
time delay2)
Set the random delay for the timer to ring between the specified range.
function void start()
Starts the timer.
function void stop()
Stops the timer.
function void pause()
Pauses the timer.
function void resume()
Resumes the timer, if the timer was paused.
function void reset()
Resets the timer.
function time get_elapsed()
Returns the elapsed time since the timer was last started or resumed.
function time get_remaining()
Returns the remaining time to ring.
function bit is_stopped()
Returns 1 if the timer is currently stopped.
function bit is_running()
Returns 1 if the timer is currently running.
function bit is_paused()
Returns 1 if the timer is currently paused.
function state_e get_state()
Returns the current state of the timer.
Triggers when the specified delay elapsed.