Timer  2.1
 All Classes Files Functions Variables Friends
Timer Class Reference

#include <Timer.h>

Public Member Functions

 Timer (TimerAdapter *adapter=0, bool isRecurring=false, unsigned int timeMillis=0)
 
virtual ~Timer ()
 
void attachAdapter (TimerAdapter *adapter)
 
TimerAdapteradapter ()
 
void startTimer (unsigned int timeMillis)
 
void startTimer ()
 
void cancelTimer ()
 
bool isTimerExpired ()
 
bool isRunning ()
 
void tick ()
 

Static Public Attributes

static const bool IS_NON_RECURRING = false
 
static const bool IS_RECURRING = true
 

Protected Member Functions

Timernext ()
 
void setNext (Timer *timer)
 

Private Member Functions

void internalTick ()
 
void startInterval ()
 
Timeroperator= (const Timer &src)
 
 Timer (const Timer &src)
 

Private Attributes

bool m_isRecurring
 
bool m_isExpiredFlag
 Timer mode flag, true: timer will automatically restart after expiration. More...
 
unsigned long m_currentTimeMillis
 Timer expiration flag. More...
 
unsigned long m_triggerTimeMillis
 interval time measurement base, updated every internalTick(), called either by tick() or by isTimerExpired() More...
 
unsigned long m_triggerTimeMillisUpperLimit
 
unsigned long m_delayMillis
 
TimerAdapterm_adapter
 
Timerm_next
 

Friends

class TimerContext
 

Detailed Description

Universal Timer.

Features:

  • intended use: encapsulate recurring and non-recurring timed actions with a non-busy wait approach for time spans in a range of 10 to thousands of milliseconds, such as:
    • debouncing push-button and switch signals
    • blink some LEDs
    • schedule some sequences where time accuracy is not crucial
    • implements Arduino yield() function in order to keep the timers scheduling ongoing even while applications and drivers use the Arduino delay() function
  • configurable to be either
    • recurring (timer automatically restarts after the interval) or
    • non-recurring (timer stops after timeout period is over)
  • timer interval/timeout time configurable ([ms])
  • automatically attaches to TimerContext's linked list of Timer objects. As long as the TimerContext::handleTick() will be called (use global functions yield() or scheduleTimers() to do so), this will periodically update the timers' states and thus perform the timers' expire evaluations
  • based on millis() function (number of milliseconds since the Arduino board began running the current program), handles unsigned long int overflows correctly (occurring around every 50 hours)

Integration:

(shown on the basis of a simple application toggling the Arduino board's built-in LED)

  • Include
    #include "Timer.h"
    
  • Timer interval constant definition
    const unsigned int BLINK_TIME_MILLIS = 200;
    
  • specific TimerAdapter implementation, periodically toggling the built-in LED
    class BlinkTimerAdapter : public TimerAdapter
    {
    public:
      void timeExpired()
      {
        digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
      }
    };
    
  • Setup: set LED pin to output; create recurring Timer, inject specific TimerAdapter
    //The setup function is called once at startup of the sketch
    void setup()
    {
      pinMode(LED_BUILTIN, OUTPUT);
      new Timer(new BlinkTimerAdapter(), Timer::IS_RECURRING, BLINK_TIME_MILLIS);
    }
    
  • Loop: call yield(), the Arduino scheduler function
    // The loop function is called in an endless loop
    void loop()
    {
      yield();
    }
    

Constructor & Destructor Documentation

Timer::Timer ( TimerAdapter adapter = 0,
bool  isRecurring = false,
unsigned int  timeMillis = 0 
)

Timer constructor.

Parameters
adapterTimerAdapter, is able to emit a timer expired event to any specific listener, default: 0 (no event will be sent)
isRecurringOperation mode, true: recurring, false: non-recurring, default: false
timeMillisTimer interval/timeout time [ms], >0: timer starts automatically after creation, others: timer stopped after creation, default: 0
Timer::~Timer ( )
virtual

Timer destructor. Will detach itself from TimerContext.

Timer::Timer ( const Timer src)
private

Member Function Documentation

TimerAdapter * Timer::adapter ( )

Timer Adapter accessor method.

Returns
TimerAdapter object pointer or 0 if no adapter is attached.
void Timer::attachAdapter ( TimerAdapter adapter)

Attach specific TimerAdapter, acts as dependency injection.

See Also
TimerAdapter interface.
Parameters
adapterSpecific TimerAdapter
void Timer::cancelTimer ( )

Cancel the timer and stop. No time expired event will be sent out after the specified time would have been elapsed. Subsequent isTimerExpired() queries will return false.

void Timer::internalTick ( )
private

Internal tick method, evaluates the expired state.

bool Timer::isRunning ( )

Indicates whether the timer is currently running.

Returns
true if timer is running.
bool Timer::isTimerExpired ( )

Poll method to get the timer expire status, recalculates whether the timer has expired before. This method could be used in a pure polling mode, where tick() has not to get called (by the TimerContext::handleTick() method), but also a mixed operation in combination with calling tick() periodically is possible. Subsequent isTimerExpired() queries will return false after the first one returned true, as long as the time did not expire again in case of a recurring timer.

Returns
true if the timer has expired.
Timer * Timer::next ( )
protected

Get next Timer object pointer out of the linked list containing timers.

Returns
Timer object pointer or 0 if current object is the trailing list element.
Timer& Timer::operator= ( const Timer src)
private
void Timer::setNext ( Timer timer)
protected

Set next Timer object of the linked list containing timers.

Parameters
timerTimer object pointer to be set as the next element of the list.
void Timer::startInterval ( )
private

Starts time interval measurement, calculates the expiration trigger time. Manages to avoid Arduino millis() overflow issues occurring around every 50 hours.

void Timer::startTimer ( unsigned int  timeMillis)

Start or restart the timer with a specific time out or interval time.

Parameters
timeMillisTime out or interval time to be set for the timer [ms]; 0 will cancel the timer,
See Also
cancelTimer().
void Timer::startTimer ( )

Start or restart the timer. If the timer has been canceled before, this will have no effect - in order to start the timer again, the startTimer(timeMillis) method with specific time value parameter has to be used instead.

void Timer::tick ( )

Kick the Timer. Recalculates whether the timer has expired.

Friends And Related Function Documentation

friend class TimerContext
friend

Member Data Documentation

const bool Timer::IS_NON_RECURRING = false
static

Constant for isRecurring parameter of the constructor (

See Also
Timer()), to create a one shot timer.
const bool Timer::IS_RECURRING = true
static

Constant for isRecurring parameter of the constructor (

See Also
Timer()), to create a recurring timer.
TimerAdapter* Timer::m_adapter
private
unsigned long Timer::m_currentTimeMillis
private

Timer expiration flag.

unsigned long Timer::m_delayMillis
private
bool Timer::m_isExpiredFlag
private

Timer mode flag, true: timer will automatically restart after expiration.

bool Timer::m_isRecurring
private
Timer* Timer::m_next
private
unsigned long Timer::m_triggerTimeMillis
private

interval time measurement base, updated every internalTick(), called either by tick() or by isTimerExpired()

unsigned long Timer::m_triggerTimeMillisUpperLimit
private

The documentation for this class was generated from the following files: