forked from sglvladi/Ticker_ESP32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicker.cpp
78 lines (60 loc) · 1.95 KB
/
Ticker.cpp
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
/*
Ticker.cpp - esp32 library that calls functions periodically
(similar to Ticker.h for esp8266)
Copyright (C) 2017 Lyudmil Vladimirov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Ticker.h"
Ticker::Ticker(int hw_timer_id)
{
if(hw_timer_id<0 || hw_timer_id>3){
Serial.println("[Ticker.h] ERROR - hw_timer_id must be in the range 0-3!");
return;
}
_hw_timer_id = hw_timer_id;
_hw_timer = timerBegin(_hw_timer_id, 80, true);
}
Ticker::~Ticker()
{
detach();
free(_hw_timer);
}
void Ticker::attach(float seconds, void func())
{
// Repeat the alarm (third parameter)
attach_us(seconds*1000000L, func);
}
void Ticker::once(float seconds, void func())
{
// Repeat the alarm (third parameter)
attach_us(seconds*1000000L, func, false);
}
void Ticker::once_us(uint32_t microseconds, void func())
{
// Repeat the alarm (third parameter)
attach_us(microseconds, func, false);
}
void Ticker::attach_us(uint32_t microseconds, void func(), bool repeat)
{
// Attach onTimer function to our timer.
timerAttachInterrupt(_hw_timer, func, true);
// Set alarm to fire per given interval
timerAlarmWrite(_hw_timer, microseconds, repeat);
timerStart(_hw_timer);
// Start an alarm
timerAlarmEnable(_hw_timer);
}
void Ticker::detach()
{
// Stop alarm
timerAlarmDisable(_hw_timer);
}