This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
MillisMicrosTimer.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Arduino.h>
5 
7 
9 using timefunction = unsigned long (*)();
10 
13 
21 template <timefunction time = micros>
22 class Timer {
23  public:
29  Timer(unsigned long interval) : interval(interval) {
30 #ifdef ARDUINO
31  begin();
32 #endif
33  }
35  void begin() { previous = time() - interval; }
37  explicit operator bool() {
38  auto now = time();
39  if (now - previous >= interval) {
40  previous += interval;
41  return true;
42  }
43  return false;
44  }
45 
46  private:
47  const unsigned long interval;
48  unsigned long previous = 0;
49 };
50 
52 
Timer::Timer
Timer(unsigned long interval)
Constructor.
Definition: MillisMicrosTimer.hpp:29
Timer::interval
const unsigned long interval
Definition: MillisMicrosTimer.hpp:47
Timer::begin
void begin()
Initialize the timer.
Definition: MillisMicrosTimer.hpp:35
Timer
A class for easily managing timed events.
Definition: MillisMicrosTimer.hpp:22
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: NamespaceSettings.hpp:9
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: NamespaceSettings.hpp:10
NamespaceSettings.hpp
Timer::previous
unsigned long previous
Definition: MillisMicrosTimer.hpp:48
timefunction
unsigned long(*)() timefunction
A function type that returns a time value.
Definition: MillisMicrosTimer.hpp:9