Control Surface  1.1.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 <Arduino.h> // millis, micros
9 
10 #include <AH/Settings/NamespaceSettings.hpp>
11 
13 
14 /// A function type that returns a time value.
15 using timefunction = unsigned long (*)();
16 
17 /// @addtogroup AH_Timing
18 /// @{
19 
20 /**
21  * @brief A class for easily managing timed events. A wrapper for "Blink
22  * Without Delay".
23  *
24  * @tparam time
25  * The time function to use.
26  */
27 template <timefunction time = micros>
28 class Timer {
29  public:
30  /**
31  * @brief Constructor.
32  * @param interval
33  * The interval between two events.
34  */
35  Timer(unsigned long interval) : interval(interval) {
36 #ifdef ARDUINO
37  begin();
38 #endif
39  }
40  /// Initialize the timer.
41  void begin() { previous = time() - interval; }
42  /// Update the timer and return true if the event should fire.
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 
52  private:
53  const unsigned long interval;
54  unsigned long previous = 0;
55 };
56 
57 /// @}
58 
60 
AH::Timer::interval
const unsigned long interval
Definition: MillisMicrosTimer.hpp:53
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::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::Timer::Timer
Timer(unsigned long interval)
Constructor.
Definition: MillisMicrosTimer.hpp:35
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
AH::Timer::begin
void begin()
Initialize the timer.
Definition: MillisMicrosTimer.hpp:41