uTimerLib
https://github.com/Naguissa/uTimerLib
uTimerLib.h
Go to the documentation of this file.
1
35#ifndef _uTimerLib_
39 #define _uTimerLib_
40
41 #include "Arduino.h"
42
43 #if defined(ARDUINO_ARCH_ESP8266)
44 #include <Ticker.h> //Ticker Library
45 #endif
46
47 #if defined(ARDUINO_ARCH_ESP32)
48 #include "esp_timer.h"
49 #endif
50
51 // Operation modes
55 #define UTIMERLIB_TYPE_OFF 0
59 #define UTIMERLIB_TYPE_TIMEOUT 1
63 #define UTIMERLIB_TYPE_INTERVAL 2
64
65 #if defined(_VARIANT_ARDUINO_STM32_) || defined(ARDUINO_ARCH_STM32)
66 #include "HardwareTimer.h"
67
68 // ST's Arduino Core STM32, https://github.com/stm32duino/Arduino_Core_STM32
69 #ifdef BOARD_NAME
70 // Private member
71
72 // Roger Clark Arduino STM32, https://github.com/rogerclarkmelbourne/Arduino_STM32
73 #else
74 extern HardwareTimer Timer3;
75 #endif
76 #endif
77
78 class uTimerLib {
79 public:
80 uTimerLib();
81 void setInterval_us(void (*) (), unsigned long int);
82 void setInterval_s(void (*) (), unsigned long int);
83 void setTimeout_us(void (*) (), unsigned long int);
84 void setTimeout_s(void (*) (), unsigned long int);
85
91 void clearTimer();
92
93
94 #if defined(_VARIANT_ARDUINO_STM32_) || defined(ARDUINO_ARCH_STM32)
95 volatile static unsigned long int _overflows;
96 volatile static unsigned long int __overflows;
97 volatile static unsigned long int _remaining;
98 volatile static unsigned long int __remaining;
99 static void (*_cb)();
100 static void _interrupt();
101 volatile static unsigned char _type;
102 #else
109 void _interrupt();
110 #endif
111
112 #if defined(ARDUINO_ARCH_ESP32)
113 static void interrupt(void* arg);
114 #endif
115
116 #if defined(ARDUINO_ARCH_ESP8266)
117 #pragma message "ESP8266 can only reach a ms resolution so any us interrupt will be rounded to that"
118 static void interrupt();
119 #endif
120
121 #ifdef _SAMD21_
122 TcCount16* _TC = (TcCount16*) TC3;
123 #endif
124
125 #ifdef __SAMD51__
126 #pragma message "SAMD51 support is still experimental"
127 #endif
128
129 private:
130 static uTimerLib *_instance;
131
132 #if defined(_VARIANT_ARDUINO_STM32_) || defined(ARDUINO_ARCH_STM32)
133 bool _toInit = true;
134
135 // ST's Arduino Core STM32, https://github.com/stm32duino/Arduino_Core_STM32
136 #ifdef BOARD_NAME
137 HardwareTimer *Timer3 = new HardwareTimer(TIM3);
138 #endif
139 #else
140 volatile unsigned long int _overflows = 0;
141 volatile unsigned long int __overflows = 0;
142 #ifdef ARDUINO_ARCH_AVR
143 volatile unsigned char _remaining = 0;
144 volatile unsigned char __remaining = 0;
145 #else
146 volatile unsigned long int _remaining = 0;
147 volatile unsigned long int __remaining = 0;
148 #endif
149 void (*_cb)() = NULL;
150 volatile unsigned char _type = UTIMERLIB_TYPE_OFF;
151 #endif
152
153 void _loadRemaining();
154
155 void _attachInterrupt_us(unsigned long int);
156 void _attachInterrupt_s(unsigned long int);
157
158
159 #if defined(ARDUINO_ARCH_ESP32)
160 esp_timer_handle_t _timer;
161 #endif
162
163 #if defined(ARDUINO_ARCH_ESP8266)
164 Ticker _ticker;
165 #endif
166
167 };
168
172 extern uTimerLib TimerLib;
173
174#endif
175
Arduino tiny and cross-device compatible timer library.
Definition: uTimerLib.h:78
void setTimeout_us(void(*)(), unsigned long int)
Attaches a callback function to be executed once when us microseconds have passed.
Definition: uTimerLib.cpp:74
void setInterval_us(void(*)(), unsigned long int)
Attaches a callback function to be executed each us microseconds.
Definition: uTimerLib.cpp:55
void setInterval_s(void(*)(), unsigned long int)
Attaches a callback function to be executed each s seconds.
Definition: uTimerLib.cpp:93
uTimerLib()
Constructor.
Definition: uTimerLib.cpp:47
void clearTimer()
Loads last bit of time needed to precisely count until desired time (non complete loop)
void setTimeout_s(void(*)(), unsigned long int)
Attaches a callback function to be executed once when s seconds have passed.
Definition: uTimerLib.cpp:112
void _interrupt()
Internal intermediate function to control timer interrupts.
uTimerLib TimerLib
Declares TimerLib variable to access this library's methods.
#define UTIMERLIB_TYPE_OFF
Internal status.
Definition: uTimerLib.h:55