TTimer (Arduino Library)

TTimer is not one and only – there quite a few examples around – however I made one myself because I needed one that is simple to use. First about the capabilites… TTimer can be set to a certain amount of milli seconds, periodically polled and when this time passed it calls a function of type: void function(). If nothing else is specified it starts over, but you can restrict the  class to repeat this a certain amount of time.

To instantiate TTime the following is required at minimum:

TTimer timer = TTimer(time, callback)

where call back is a function of the type void (*callback)());

The class can provides functions to read the remaining time and the state of the timer. The class returns one of the following states:

  • STOP (the timer has been disabled)
  • RUN  (the timer is activ and counting)
  • HALT  (the timer ended)
  • CLEAR  (the timer is deleted respectivly does not exist)

TTimer to be polled through timer.run(). However when your application requires synchronized timing you can also poll and pass the actual  with timer.run(time).

TTimer.h

#ifndef _TTIMER_H_
#define _TTIMER_H_

class TTimer {
 public:
 enum Mode_t {STOP, RUN, HALT, CLEAR};

 private:
 unsigned char _signal;
 unsigned long _lastTime;
 unsigned long _trig;
 unsigned long _time;
 int _cycl;
 Mode_t _mode;
 void (*_callback)();

public:

 TTimer();

 // Constructor with setup - limited cycles/repetitions
 TTimer(unsigned long time, int rept, void (*callback)());

 // Constructor with setup - unlimited cycles/repetitions
 TTimer(unsigned long time, void (*callback)());

 // Init timer with limited cycles/repetitions
 void init(unsigned long time, int rept, void (*callback)());

 // Init timer with unlimited cycles/repetitions
 void init(unsigned long time, void (*callback)());

 // Set time
 void setTime(unsigned long time);

 // Set cycles/repetitions
 unsigned long getTime() { return _time; };

 // Set cycles/repetitions
 void setCycles(int cycles);

 // Clear the timer
 void clear();

 // Reset a running timer
 void reset();

 // Enable timer
 void enable();

 // Disable but not clear timer
 void disable();

 // Process timer
 int run();

 // Process timer at a given time - returns signal !=0 means timer expired
 int run(unsigned long now);

 // Returns current timer mode
 Mode_t getMode();
};

TTimer.cpp

#include <Arduino.h>
#include <limits.h>
#include <TTimer.h>

// ---------------------------------------------------------------------------
TTimer::TTimer() {
 clear();
}

// ---------------------------------------------------------------------------
TTimer::TTimer(unsigned long time, void (*callback)()) {
 init(time, callback);
}

// ---------------------------------------------------------------------------
TTimer::TTimer(unsigned long time, int cycl, void (*callback)()) {
 init(time, cycl, callback);
}

// ---------------------------------------------------------------------------
void TTimer::init(unsigned long time, int cycl, void (*callback)()) {
 clear();
 _callback = callback;
 _time = time;
 _cycl = cycl;
 _trig = millis();
 _mode = RUN;
}

// ---------------------------------------------------------------------------
void TTimer::init(unsigned long time, void (*callback)()) {
 init(time, -1, callback);
}

// ---------------------------------------------------------------------------
void TTimer::reset() {
 if (_mode == RUN) _trig = millis();
}

// ---------------------------------------------------------------------------
void TTimer::clear(){
 _callback = NULL;
 _mode = CLEAR;
}

// ---------------------------------------------------------------------------
void TTimer::enable() {
 if (_mode != CLEAR) {
 _trig = millis();
 _mode = RUN;
 }
}

// ---------------------------------------------------------------------------
void TTimer::disable() {
 if (_mode != CLEAR) _mode = STOP;
}

// ---------------------------------------------------------------------------
void TTimer::setTime(unsigned long time) {
 _time = time;
}

// ---------------------------------------------------------------------------
void TTimer::setCycles(int cycles) {
 _cycl = cycles;
}

// ---------------------------------------------------------------------------
TTimer::Mode_t TTimer::getMode() {
 return _mode;
}

// ---------------------------------------------------------------------------
int TTimer::run() {
 return run(millis());
}

// ---------------------------------------------------------------------------
int TTimer::run(unsigned long now) {

 _signal = 0;

// If current timer is inactive proceed to the next
 if (_mode != RUN) return _signal;

// Determine the elapsed time since trigger
 long elapsed = now - _trig;

 // if timer is not expired proceed stop further processing
 if (( (unsigned long)(elapsed)) < _time) return _signal;

 // the timer expired, set a flag
 _signal = 1;

 // trigger call back function if defined
 if (_callback != NULL) _callback();

 // Restart timer if required
 if ( _cycl < 0 ) {
 _trig = now;
 }
 // Repeat timer
 else if ( _cycl > 0 ) {
 _cycl--;
 _trig = now;
 }
 // Halt timer
 else if ( _cycl == 0 ) {
 _mode = HALT;
 }

 return _signal;
}

TTimer as library including an example can be found here for download

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s