forked from Naguissa/uTimerLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuTimerLib.cpp
More file actions
148 lines (126 loc) · 6.4 KB
/
Copy pathuTimerLib.cpp
File metadata and controls
148 lines (126 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* \class uTimerLib
* \brief Arduino tiny and cross-device compatible timer library.
*
* Timers used by each microcontroller:
* * Atmel ATtiny: Timer1 (2nd timer) - https://github.com/damellis/attiny and https://github.com/SpenceKonde/Disgispark AVRCore (25, 45 and 85)
* * DisgiSpark AVR: Timer0 (1st timer) - https://github.com/digistump/DigistumpArduino
* * Atmel AVR 32U4: Timer3 (4rd timer)
* * Atmel AVR other: Timer2 (3rd timer)
* * STM32: Timer3 (3rd timer)
* * SAM (Due): TC3 (Timer1, channel 0)
* * ESP8266: OS Timer, one slot of seven available (Software timer provided by Arduino because ESP8266 has only two hardware timers and one is needed by it normal operation)
* * ESP32: OS Timer, one slot of software timer.
* * SAMD21: Timer 4, CC0 (TC3). See http://ww1.microchip.com/downloads/en/DeviceDoc/40001882A.pdf
* * SAMD51: Timer 2 (TC1), 16 bits mode (See http://ww1.microchip.com/downloads/en/DeviceDoc/60001507C.pdf
*
* You have public TimerLib variable with following methods:
* * TimerLib.setInterval_us(callback_function, microseconds);* : callback_function will be called each microseconds.
* * TimerLib.setInterval_s(callback_function, seconds);* : callback_function will be called each seconds.
* * TimerLib.setTimeout_us(callback_function, microseconds);* : callback_function will be called once when microseconds have passed.
* * TimerLib.setTimeout_s(callback_function, seconds);* : callback_function will be called once when seconds have passed.
* * TimerLib.clearTimer();* : will clear any timed function if exists.
*
* @file hardware/uTimerLib.ATTINY.cpp
* @copyright Naguissa
* @author Naguissa
* @see <a href="https://github.com/Naguissa/uTimerLib">https://github.com/Naguissa/uTimerLib</a>
* @see <a href="https://www.foroelectro.net/librerias-arduino-ide-f29/utimerlib-libreria-arduino-para-eventos-temporizad-t191.html">https://www.foroelectro.net/librerias-arduino-ide-f29/utimerlib-libreria-arduino-para-eventos-temporizad-t191.html</a>
* @see <a href="mailto:[email protected]">[email protected]</a>
* @version 1.6.4
*/
// # if !defined(_uTimerLib_cpp_) && defined(_uTimerLib_IMP_)
#ifndef _uTimerLib_cpp_
#define _uTimerLib_cpp_
#include "uTimerLib.h"
// extern uTimerLib TimerLib;
#if defined(_VARIANT_ARDUINO_STM32_) || defined(ARDUINO_ARCH_STM32)
uTimerLib *uTimerLib::_instance = NULL;
#endif
/**
* \brief Constructor
*/
uTimerLib::uTimerLib() {
#ifdef _VARIANT_ARDUINO_STM32_
_instance = this;
clearTimer();
#endif
}
/**
* \brief Attaches a callback function to be executed each us microseconds
*
* @param cb Callback function to be called
* @param us Interval in microseconds
*/
void uTimerLib::setInterval_us(void (* cb)(), unsigned long int us) {
clearTimer();
_cb = cb;
_type = UTIMERLIB_TYPE_INTERVAL;
_attachInterrupt_us(us);
}
/**
* \brief Attaches a callback function to be executed once when us microseconds have passed
*
* @param cb Callback function to be called
* @param us Timeout in microseconds
*/
void uTimerLib::setTimeout_us(void (* cb)(), unsigned long int us) {
clearTimer();
_cb = cb;
_type = UTIMERLIB_TYPE_TIMEOUT;
_attachInterrupt_us(us);
}
/**
* \brief Attaches a callback function to be executed each s seconds
*
* @param cb Callback function to be called
* @param s Interval in seconds
*/
void uTimerLib::setInterval_s(void (* cb)(), unsigned long int s) {
clearTimer();
_cb = cb;
_type = UTIMERLIB_TYPE_INTERVAL;
_attachInterrupt_s(s);
}
/**
* \brief Attaches a callback function to be executed once when s seconds have passed
*
* @param cb Callback function to be called
* @param s Timeout in seconds
*/
void uTimerLib::setTimeout_s(void (* cb)(), unsigned long int s) {
clearTimer();
_cb = cb;
_type = UTIMERLIB_TYPE_TIMEOUT;
_attachInterrupt_s(s);
}
#define UTIMERLIB_HW_COMPILE
// Now load each hardware variation support:
#if (defined(__AVR_ATmega32U4__) || defined(ARDUINO_ARCH_AVR)) && !defined(ARDUINO_attiny) && !defined(ARDUINO_AVR_ATTINYX4) && !defined(ARDUINO_AVR_ATTINYX5) && !defined(ARDUINO_AVR_ATTINYX7) && !defined(ARDUINO_AVR_ATTINYX8) && !defined(ARDUINO_AVR_ATTINYX61) && !defined(ARDUINO_AVR_ATTINY43) && !defined(ARDUINO_AVR_ATTINY828) && !defined(ARDUINO_AVR_ATTINY1634) && !defined(ARDUINO_AVR_ATTINYX313) && !defined(ARDUINO_AVR_DIGISPARK)
#include "hardware/uTimerLib.AVR.cpp"
#endif
#if defined(ARDUINO_ARCH_AVR) && (defined(ARDUINO_attiny) || defined(ARDUINO_AVR_ATTINYX4) || defined(ARDUINO_AVR_ATTINYX5) || defined(ARDUINO_AVR_ATTINYX7) || defined(ARDUINO_AVR_ATTINYX8) || defined(ARDUINO_AVR_ATTINYX61) || defined(ARDUINO_AVR_ATTINY43) || defined(ARDUINO_AVR_ATTINY828) || defined(ARDUINO_AVR_ATTINY1634) || defined(ARDUINO_AVR_ATTINYX313))
#include "hardware/uTimerLib.ATTINY.cpp"
#endif
#if defined(ARDUINO_ARCH_AVR) && defined(ARDUINO_AVR_DIGISPARK)
#include "hardware/uTimerLib.DIGISPARK_AVR.cpp"
#endif
#if defined(_VARIANT_ARDUINO_STM32_) || defined(ARDUINO_ARCH_STM32)
#include "hardware/uTimerLib.STM32.cpp"
#endif
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
#include "hardware/uTimerLib.ESP.cpp"
#endif
#ifdef ARDUINO_ARCH_SAM
#include "hardware/uTimerLib.SAM.cpp"
#endif
#ifdef _SAMD21_
#include "hardware/uTimerLib.SAMD21.cpp"
#endif
#ifdef __SAMD51__
#include "hardware/uTimerLib.SAMD51.cpp"
#endif
#if !defined(__AVR_ATmega32U4__) && !defined(ARDUINO_ARCH_AVR) && !defined(_VARIANT_ARDUINO_STM32_) && !defined(ARDUINO_ARCH_STM32) && !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) && !defined(ARDUINO_ARCH_SAM) && !defined(_SAMD21_) && !defined(__SAMD51__) && !defined(ARDUINO_attiny) && !defined(ARDUINO_AVR_ATTINYX5) && !defined(ARDUINO_AVR_DIGISPARK)
#include "hardware/uTimerLib.UNSUPPORTED.cpp"
#endif
#endif