Timer
2.1
|
#include <Timer.h>
Public Member Functions | |
Timer (TimerAdapter *adapter=0, bool isRecurring=false, unsigned int timeMillis=0) | |
virtual | ~Timer () |
void | attachAdapter (TimerAdapter *adapter) |
TimerAdapter * | adapter () |
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 | |
Timer * | next () |
void | setNext (Timer *timer) |
Private Member Functions | |
void | internalTick () |
void | startInterval () |
Timer & | operator= (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 |
TimerAdapter * | m_adapter |
Timer * | m_next |
Friends | |
class | TimerContext |
Universal Timer.
Features:
Integration:
(shown on the basis of a simple application toggling the Arduino board's built-in LED)
#include "Timer.h"
const unsigned int BLINK_TIME_MILLIS = 200;
class BlinkTimerAdapter : public TimerAdapter { public: void timeExpired() { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); } };
//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); }
// The loop function is called in an endless loop void loop() { yield(); }
Timer::Timer | ( | TimerAdapter * | adapter = 0 , |
bool | isRecurring = false , |
||
unsigned int | timeMillis = 0 |
||
) |
Timer constructor.
adapter | TimerAdapter, is able to emit a timer expired event to any specific listener, default: 0 (no event will be sent) |
isRecurring | Operation mode, true: recurring, false: non-recurring, default: false |
timeMillis | Timer interval/timeout time [ms], >0: timer starts automatically after creation, others: timer stopped after creation, default: 0 |
|
virtual |
Timer destructor. Will detach itself from TimerContext.
|
private |
TimerAdapter * Timer::adapter | ( | ) |
Timer Adapter accessor method.
void Timer::attachAdapter | ( | TimerAdapter * | adapter | ) |
Attach specific TimerAdapter, acts as dependency injection.
adapter | Specific 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.
|
private |
Internal tick method, evaluates the expired state.
bool Timer::isRunning | ( | ) |
Indicates whether the timer is currently 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.
|
protected |
|
protected |
|
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.
timeMillis | Time out or interval time to be set for the timer [ms]; 0 will cancel the timer, |
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.
|
friend |
|
static |
Constant for isRecurring parameter of the constructor (
|
static |
Constant for isRecurring parameter of the constructor (
|
private |
|
private |
Timer expiration flag.
|
private |
|
private |
Timer mode flag, true: timer will automatically restart after expiration.
|
private |
|
private |
|
private |
interval time measurement base, updated every internalTick(), called either by tick() or by isTimerExpired()
|
private |