/
esp_timer_cxx.hpp
137 lines (115 loc) · 3.68 KB
/
esp_timer_cxx.hpp
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
/*
* SPDX-FileCopyrightText: 2020 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#ifdef __cpp_exceptions
#include <chrono>
#include <functional>
#include <string>
#include "esp_exception.hpp"
#include "esp_timer.h"
namespace idf {
namespace esp_timer {
/**
* @brief Get time since boot
* @return time since \c esp_timer_init() was called (this normally happens early during application startup).
*/
static inline std::chrono::microseconds get_time()
{
return std::chrono::microseconds(esp_timer_get_time());
}
/**
* @brief Get the timestamp when the next timeout is expected to occur
* @return Timestamp of the nearest timer event.
* The timebase is the same as for the values returned by \c get_time().
*/
static inline std::chrono::microseconds get_next_alarm()
{
return std::chrono::microseconds(esp_timer_get_next_alarm());
}
/**
* @brief
* A timer using the esp_timer component which can be started either as one-shot timer or periodically.
*/
class ESPTimer {
public:
/**
* @param timeout_cb The timeout callback.
* @param timer_name The name of the timer (optional). This is for debugging using \c esp_timer_dump().
*/
ESPTimer(std::function<void()> timeout_cb, const std::string &timer_name = "ESPTimer");
/**
* Stop the timer if necessary and delete it.
*/
~ESPTimer();
/**
* Default copy constructor is deleted since one instance of esp_timer_handle_t must not be shared.
*/
ESPTimer(const ESPTimer&) = delete;
/**
* Default copy assignment is deleted since one instance of esp_timer_handle_t must not be shared.
*/
ESPTimer &operator=(const ESPTimer&) = delete;
/**
* @brief Start one-shot timer
*
* Timer should not be running (started) when this function is called.
*
* @param timeout timer timeout, in microseconds relative to the current moment.
*
* @throws ESPException with error ESP_ERR_INVALID_STATE if the timer is already running.
*/
inline void start(std::chrono::microseconds timeout)
{
CHECK_THROW(esp_timer_start_once(timer_handle, timeout.count()));
}
/**
* @brief Start periodic timer
*
* Timer should not be running when this function is called. This function will
* start a timer which will trigger every 'period' microseconds.
*
* Timer should not be running (started) when this function is called.
*
* @param timeout timer timeout, in microseconds relative to the current moment.
*
* @throws ESPException with error ESP_ERR_INVALID_STATE if the timer is already running.
*/
inline void start_periodic(std::chrono::microseconds period)
{
CHECK_THROW(esp_timer_start_periodic(timer_handle, period.count()));
}
/**
* @brief Stop the previously started timer.
*
* This function stops the timer previously started using \c start() or \c start_periodic().
*
* @throws ESPException with error ESP_ERR_INVALID_STATE if the timer has not been started yet.
*/
inline void stop()
{
CHECK_THROW(esp_timer_stop(timer_handle));
}
private:
/**
* Internal callback to hook into esp_timer component.
*/
static void esp_timer_cb(void *arg);
/**
* Timer instance of the underlying esp_event component.
*/
esp_timer_handle_t timer_handle;
/**
* Callback which will be called once the timer triggers.
*/
std::function<void()> timeout_cb;
/**
* Name of the timer, will be passed to the underlying timer framework and is used for debugging.
*/
const std::string name;
};
} // esp_timer
} // idf
#endif // __cpp_exceptions