-
Notifications
You must be signed in to change notification settings - Fork 14
/
esp_timer_cxx.cpp
52 lines (39 loc) · 1.08 KB
/
esp_timer_cxx.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
/*
* SPDX-FileCopyrightText: 2020 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifdef __cpp_exceptions
#include <functional>
#include "esp_timer_cxx.hpp"
#include "esp_exception.hpp"
using namespace std;
namespace idf {
namespace esp_timer {
ESPTimer::ESPTimer(function<void()> timeout_cb, const string &timer_name)
: timeout_cb(timeout_cb), name(timer_name)
{
if (timeout_cb == nullptr) {
throw ESPException(ESP_ERR_INVALID_ARG);
}
esp_timer_create_args_t timer_args = {};
timer_args.callback = esp_timer_cb;
timer_args.arg = this;
timer_args.dispatch_method = ESP_TIMER_TASK;
timer_args.name = name.c_str();
CHECK_THROW(esp_timer_create(&timer_args, &timer_handle));
}
ESPTimer::~ESPTimer()
{
// Ignore potential ESP_ERR_INVALID_STATE here to not throw exception.
esp_timer_stop(timer_handle);
esp_timer_delete(timer_handle);
}
void ESPTimer::esp_timer_cb(void *arg)
{
ESPTimer *timer = static_cast<ESPTimer*>(arg);
timer->timeout_cb();
}
} // esp_timer
} // idf
#endif // __cpp_exceptions