Control Surface  1.1.1
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 <Arduino.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  explicit operator bool() {
44  auto now = time();
45  if (now - previous >= interval) {
46  previous += interval;
47  return true;
48  }
49  return false;
50  }
51 
53  unsigned long getInterval() const { return interval; }
55  void setInterval(unsigned long interval) { this->interval = interval; }
56 
57  private:
58  unsigned long interval;
59  unsigned long previous = 0;
60 };
61 
63 
65 
AH::Timer::Timer
Timer(unsigned long interval)
Constructor.
Definition: MillisMicrosTimer.hpp:35
AH::Timer::interval
unsigned long interval
Definition: MillisMicrosTimer.hpp:58
Warnings.hpp
AH::Timer
A class for easily managing timed events.
Definition: MillisMicrosTimer.hpp:28
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:17
AH::Timer::begin
void begin()
Initialize the timer.
Definition: MillisMicrosTimer.hpp:41
AH::Timer::setInterval
void setInterval(unsigned long interval)
Set the interval of the timer.
Definition: MillisMicrosTimer.hpp:55
AH::Timer::getInterval
unsigned long getInterval() const
Get the interval of the timer.
Definition: MillisMicrosTimer.hpp:53
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:18
AH_DIAGNOSTIC_WERROR
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:16
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