Arduino Filters master
Filter library for Arduino
MillisMicrosTimer.hpp
Go to the documentation of this file.
1#pragma once
2
4AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
5
7#include <AH/Arduino-Wrapper.h> // millis, micros
9
10#include <AH/Settings/NamespaceSettings.hpp>
11
13
16
18using timefunction = unsigned long (*)();
19
27template <timefunction time = micros>
28class Timer {
29 public:
35 Timer(unsigned long interval) : interval(interval) {
36#ifdef ARDUINO
37 begin();
38#endif
39 }
41 void begin() { previous = time() - interval; }
43 void beginNextPeriod() { previous = time(); }
45 explicit operator bool() {
46 auto now = time();
47 if (now - previous >= interval) {
49 return true;
50 }
51 return false;
52 }
53
55 unsigned long getInterval() const { return interval; }
57 void setInterval(unsigned long interval) { this->interval = interval; }
58
59 private:
60 unsigned long interval;
61 unsigned long previous = 0;
62};
63
65
67
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
#define AH_DIAGNOSTIC_EXTERNAL_HEADER()
Definition: Warnings.hpp:37
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:36
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:35
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.