TDuino
Convenient functionality for Arduino
Public Member Functions
TTimer Class Reference

Used for timing a callback method. More...

#include <TTimer.h>

Public Member Functions

 TTimer (void(*callback)(byte), byte numTimers=1)
 Constructs a timer instance. More...
 
virtual ~TTimer ()
 Destructor for the TTimer instance. More...
 
int firstActive ()
 Get first active timer slot. More...
 
int firstInactive ()
 Get first inactive timer slot. More...
 
unsigned int getCounter (byte index=0)
 Get the counter for a timer slot. More...
 
unsigned long getInterval (byte index=0)
 Get the interval for a timer slot. More...
 
byte getSize ()
 Get number of timer slots. More...
 
bool isActive (byte index=0)
 Check if a timer slot is active. More...
 
void restart (byte index=0)
 Restarts a timer slot. More...
 
void restartAll ()
 Restarts all timer slot. More...
 
void resume (byte index=0)
 Resumes a timer slot. More...
 
void resumeAll ()
 Resumes all timer slots. More...
 
void set (byte index, unsigned long interval, unsigned int repetitions)
 Set a timer slot. More...
 
void set (unsigned long interval, unsigned int repetitions)
 
void stop (byte index=0)
 Stop a timer slot. More...
 
void stopAll ()
 Stop all timer slot. More...
 
virtual void loop ()
 The timer's loop phase. More...
 
- Public Member Functions inherited from TBase
 TBase ()
 Constructor for class TBase. More...
 
virtual ~TBase ()
 Destructor for class TBase. More...
 

Additional Inherited Members

- Protected Member Functions inherited from TBase
virtual void defaults ()
 Method used to reset all class internal variables. More...
 
- Protected Attributes inherited from TBase
unsigned long loopMillis
 The value of millis() to be used within loop()
 

Detailed Description

Used for timing a callback method.

TTimer is used to perform timed operations using a callback method. It is possible to have up to 255 timed operations (if memory allows it) using one TTimer instance. All operations use the same callback method and passes the index of the timer operation to the callback.

void timerCallback(byte timerIndex)
{
// "timerIndex" is the index of the timer slot.
if (timerIndex == 0)
{
//Do something every 500 milliseconds
}
else if (timerIndex == 1)
{
//Do something every 1000 milliseconds
}
}
TTimer timer(timerCallback, 2);
void setup()
{
timer.set(0, 500, 0);
timer.set(1, 1000, 0);
}
void loop()
{
timer.loop();
}

Constructor & Destructor Documentation

◆ TTimer()

TTimer::TTimer ( void(*)(byte)  callback,
byte  numTimers = 1 
)

Constructs a timer instance.

Parameters
callbackThe callback used by this instance.
numTimersThe number of timer slots to create.

Constructs an instance of the TTimer with a predefined number of timer slots and a callback to be invoked whenever a timer slot is triggered.

numTimers must be in the range 1..255, memory usage (in bytes) is: (13 * numTimers) + 2.

Static allocation of object memory

◆ ~TTimer()

TTimer::~TTimer ( )
virtual

Destructor for the TTimer instance.

The destructor releases dynamic memory (if any) used by the TTimer instance.

Member Function Documentation

◆ firstActive()

int TTimer::firstActive ( )

Get first active timer slot.

Get the index of the first active timer slot.

Returns
The index of the first active timer slot or -1 if all are inactive.

◆ firstInactive()

int TTimer::firstInactive ( )

Get first inactive timer slot.

Get the index of the first inactive timer slot.

Returns
The index of the first inactive timer slot or -1 if all are active.

◆ getCounter()

unsigned int TTimer::getCounter ( byte  index = 0)

Get the counter for a timer slot.

Parameters
indexThe index of a timer slot.

The counter for a timer slot is incremented for each trigger. You can use the counter value in order to perform different tasks on different counts. Beware of the counters roll-over at 65535 (0xFFFF).

Returns
The counter for the timer slot.

◆ getInterval()

unsigned long TTimer::getInterval ( byte  index = 0)

Get the interval for a timer slot.

Parameters
indexThe index of a timer slot.

Get the interval of the timer slot at the specified index.

Returns
The interval used by the slot.

◆ getSize()

byte TTimer::getSize ( )

Get number of timer slots.

Returns
The number of timer slots.

Used to detect the number of timer slots used by this instance of TTimer.

◆ isActive()

bool TTimer::isActive ( byte  index = 0)

Check if a timer slot is active.

Parameters
indexThe index of the timer slot to check.

Check wheter or not a timer slot is active. It will return true if the counter for the slot has not reached the maximum repetitions and if the timer slot has not been stopped.

See also
stop()

◆ loop()

void TTimer::loop ( )
virtual

The timer's loop phase.

Must be called to ensure that the timer slots are working as intended.

Reimplemented from TBase.

◆ restart()

void TTimer::restart ( byte  index = 0)

Restarts a timer slot.

Parameters
indexThe index of the timer slot to restart.

Used to restart a timer slot with the values previosly defined with set().

See also
set() stop()

◆ restartAll()

void TTimer::restartAll ( )

Restarts all timer slot.

Used to restart all timer slots.

See also
retart()

◆ resume()

void TTimer::resume ( byte  index = 0)

Resumes a timer slot.

Parameters
indexThe index of the timer slot to resume.

Used to resume a timer slot. Unlike restart(), resume() does not reset the values in the timer slot but resumes from the point where the slot was stopped.

See also
restart() stop()

◆ resumeAll()

void TTimer::resumeAll ( )

Resumes all timer slots.

Used to resume all timer slots.

See also
resume()

◆ set() [1/2]

TTimer::set ( byte  index,
unsigned long  interval,
unsigned int  repetitions 
)

Set a timer slot.

Parameters
indexThe index of the slot to set.
intervalThe interval in which the slot triggers.
repetitionsMaximum number of repetitions to trigger.

Used to set the timer slot at the given index to be triggered with the given interval and the number of repetitions. When the maximum number of repetitions has been triggered, the slot is deactivated. If repetitions is set to 0 (the default), the slot will be triggered indefinately. The first trigger will happen after interval has elapsed.

◆ set() [2/2]

TTimer::set ( unsigned long  interval,
unsigned int  repetitions 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Set the first timer slot.

◆ stop()

void TTimer::stop ( byte  index = 0)

Stop a timer slot.

Parameters
indexThe index of the slot to stop.

If you want to stop a timer slot you must call this method with the index of the slot to stop. If index is not defined, the first slot is stopped.

See also
resume() restart()

◆ stopAll()

void TTimer::stopAll ( )

Stop all timer slot.

Used to stop all timer slots.

See also
stop()