Control Surface  1.2.0
MIDI Control Surface library for Arduino
MillisMicrosTimer.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
5 
7 #include <AH/Arduino-Wrapper.h> // millis, micros
9 
10 #include <AH/Settings/NamespaceSettings.hpp>
11 
13 
15 using timefunction = unsigned long (*)();
16 
19 
27 template <timefunction time = micros>
28 class 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) {
48  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 
AH::Timer::Timer
Timer(unsigned long interval)
Constructor.
Definition: MillisMicrosTimer.hpp:35
AH::Timer::interval
unsigned long interval
Definition: MillisMicrosTimer.hpp:60
Warnings.hpp
AH::Timer
A class for easily managing timed events.
Definition: MillisMicrosTimer.hpp:28
AH::Timer::beginNextPeriod
void beginNextPeriod()
Initialize or reset the timer. The timer will fire after one period.
Definition: MillisMicrosTimer.hpp:43
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:36
AH::Timer::begin
void begin()
Initialize or reset the timer. The timer will fire immediately.
Definition: MillisMicrosTimer.hpp:41
AH::Timer::setInterval
void setInterval(unsigned long interval)
Set the interval of the timer.
Definition: MillisMicrosTimer.hpp:57
AH::Timer::getInterval
unsigned long getInterval() const
Get the interval of the timer.
Definition: MillisMicrosTimer.hpp:55
AH::timefunction
unsigned long(*)() timefunction
A function type that returns a time value.
Definition: MillisMicrosTimer.hpp:15
AH_DIAGNOSTIC_EXTERNAL_HEADER
#define AH_DIAGNOSTIC_EXTERNAL_HEADER()
Definition: Warnings.hpp:37
AH_DIAGNOSTIC_WERROR
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:35
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:9
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:10