Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
Timer.h
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/anakod/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  ****/
7 
11 #ifndef _SMING_CORE_Timer_H_
12 #define _SMING_CORE_Timer_H_
13 
14 
15 #include "../SmingCore/Interrupts.h"
16 #include "../SmingCore/Delegate.h"
17 #include "../Wiring/WiringFrameworkDependencies.h"
18 
19 
20 // According to documentation maximum value of interval for ms
21 // timer after doing system_timer_reinit is 268435ms.
22 // If we do some testing we find that higher values works,
23 // the actual limit seems to be about twice as high
24 // but we use the documented value anyway to be on the safe side.
25 #define MAX_OS_TIMER_INTERVAL_US 268435000
26 
28 
29 class Timer
30 {
31 public:
36  Timer();
37  ~Timer();
38 
39  // It return value for Method Chaining (return "this" reference)
40  // http://en.wikipedia.org/wiki/Method_chaining
41  // We provide both versions: Delegate and classic c-style callback function for performance reason (for high-frequency timers)
42  // Usually only Delegate needed
43 
49  Timer& IRAM_ATTR initializeMs(uint32_t milliseconds, InterruptCallback callback = NULL); // Init in Milliseconds.
50 
56  Timer& IRAM_ATTR initializeUs(uint32_t microseconds, InterruptCallback callback = NULL); // Init in Microseconds.
57 
63  Timer& IRAM_ATTR initializeMs(uint32_t milliseconds, TimerDelegate delegateFunction = NULL); // Init in Milliseconds.
64 
70  Timer& IRAM_ATTR initializeUs(uint32_t microseconds, TimerDelegate delegateFunction = NULL); // Init in Microseconds.
71 
75  void IRAM_ATTR start(bool repeating = true);
76 
80  void __forceinline IRAM_ATTR startOnce() { start(false); }
81 
85  void IRAM_ATTR stop();
86 
90  void IRAM_ATTR restart();
91 
95  bool isStarted();
96 
100  uint64_t getIntervalUs();
101 
105  uint32_t getIntervalMs();
106 
110  void IRAM_ATTR setIntervalUs(uint64_t microseconds = 1000000);
111 
115  void IRAM_ATTR setIntervalMs(uint32_t milliseconds = 1000000);
116 
121  void IRAM_ATTR setCallback(InterruptCallback interrupt = NULL);
122 
127  void IRAM_ATTR setCallback(TimerDelegate delegateFunction);
128 
131 protected:
132  static void IRAM_ATTR processing(void *arg);
133 
134 
135 private:
136  os_timer_t timer;
137  uint64_t interval = 0;
138  InterruptCallback callback = nullptr;
139  TimerDelegate delegate_func = nullptr;
140  bool repeating = false;
141  bool started = false;
142 
143  // Because of the limitation in Espressif SDK a workaround
144  // was added to allow for longer timer intervals.
145  uint16_t long_intvl_cntr = 0;
146  uint16_t long_intvl_cntr_lim = 0;
147 };
148 
149 #endif /* _SMING_CORE_Timer_H_ */
Timer &IRAM_ATTR initializeMs(uint32_t milliseconds, InterruptCallback callback=NULL)
Initialise millisecond timer.
void IRAM_ATTR start(bool repeating=true)
Start timer running.
void IRAM_ATTR setIntervalUs(uint64_t microseconds=1000000)
Set timer interval.
uint64_t getIntervalUs()
Get timer interval.
void __forceinline IRAM_ATTR startOnce()
Start one-shot timer running.
Definition: Timer.h:80
void IRAM_ATTR restart()
Restarts timer.
Delegate< void()> TimerDelegate
Delegate callback type for timer trigger.
Definition: HardwareTimer.h:26
Timer()
Timer class.
uint32_t getIntervalMs()
Get timer interval.
Timer &IRAM_ATTR initializeUs(uint32_t microseconds, InterruptCallback callback=NULL)
Initialise microsecond timer.
void IRAM_ATTR stop()
Stop timer.
void IRAM_ATTR setIntervalMs(uint32_t milliseconds=1000000)
Set timer interval.
void IRAM_ATTR setCallback(InterruptCallback interrupt=NULL)
Set timer trigger function.
Definition: Timer.h:29
bool isStarted()
Check if timer is started.