Arduino Helpers master
Utility library for Arduino
MillisMicrosTimer.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <AH/Arduino-Wrapper.h> // millis, micros
6
8
11
13using timefunction = unsigned long (*)();
14
22template <timefunction time = micros>
23class Timer {
24 public:
30 Timer(unsigned long interval) : interval(interval) {
31#ifdef ARDUINO
32 begin();
33#endif
34 }
36 void begin() { previous = time() - interval; }
38 void beginNextPeriod() { previous = time(); }
40 explicit operator bool() {
41 auto now = time();
42 if (now - previous >= interval) {
44 return true;
45 }
46 return false;
47 }
48
50 unsigned long getInterval() const { return interval; }
52 void setInterval(unsigned long interval) { this->interval = interval; }
53
54 private:
55 unsigned long interval;
56 unsigned long previous = 0;
57};
58
60
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
A class for easily managing timed events.
unsigned long getInterval() const
Get the interval of the timer.
Timer(unsigned long interval)
Constructor.
void setInterval(unsigned long interval)
Set the interval of the timer.
void beginNextPeriod()
Initialize or reset the timer. The timer will fire after one period.
unsigned long interval
void begin()
Initialize or reset the timer. The timer will fire immediately.
unsigned long previous
unsigned long(*)() timefunction
A function type that returns a time value.