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