Uniot Core
0.8.1
Loading...
Searching...
No Matches
ESP32Task.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2020 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
23
24#pragma once
25
26#if defined(ESP32)
27
28#include <esp_timer.h>
29
30namespace uniot {
41class ESP32Task {
42 public:
46 using TaskCallback = void (*)(void);
47
51 using TaskArgCallback = void (*)(void *);
52
56 template <typename T>
57 using TaskTypeCallback = void (*)(volatile T);
58
63 : mpTimer(nullptr) {}
64
68 virtual ~ESP32Task() {
69 detach();
70 }
71
79 void attach(uint32_t ms, bool repeat, TaskCallback callback) {
80 attach_arg(ms, repeat, reinterpret_cast<TaskArgCallback>(callback), nullptr);
81 }
82
91 template <typename T>
92 void attach(uint32_t ms, bool repeat, TaskTypeCallback<volatile T> callback, volatile T arg) {
93 attach_arg(ms, repeat, reinterpret_cast<TaskArgCallback>(callback), reinterpret_cast<volatile void *>(arg));
94 }
95
99 void detach() {
100 if (mpTimer) {
101 esp_timer_stop(mpTimer);
102 esp_timer_delete(mpTimer);
103 mpTimer = nullptr;
104 }
105 }
106
113 bool isAttached() {
114 return mpTimer != nullptr;
115 }
116
117 private:
118 esp_timer_handle_t mpTimer;
119
128 void attach_arg(uint32_t ms, bool repeat, TaskArgCallback callback, volatile void *arg) {
129 esp_timer_create_args_t timerConfig;
130 timerConfig.callback = reinterpret_cast<esp_timer_cb_t>(callback);
131 timerConfig.arg = const_cast<void*>(arg);
132 timerConfig.dispatch_method = ESP_TIMER_TASK;
133 timerConfig.name = "Task";
134
135 if (mpTimer) {
136 esp_timer_stop(mpTimer);
137 esp_timer_delete(mpTimer);
138 }
139
140 esp_timer_create(&timerConfig, &mpTimer);
141
142 if (repeat) {
143 esp_timer_start_periodic(mpTimer, ms * 1000);
144 } else {
145 esp_timer_start_once(mpTimer, ms * 1000);
146 }
147 }
148};
149
150
151} // namespace uniot
152
153#endif // ESP32
void(*)(volatile T) TaskTypeCallback
Templated callback function with typed argument.
Definition ESP32Task.h:57
void detach()
Stop and detach the timer.
Definition ESP32Task.h:99
virtual ~ESP32Task()
Destructor that ensures the timer is detached.
Definition ESP32Task.h:68
void attach(uint32_t ms, bool repeat, TaskCallback callback)
Attach a simple callback to run periodically.
Definition ESP32Task.h:79
void(*)(void) TaskCallback
Callback function with no arguments.
Definition ESP32Task.h:46
ESP32Task()
Constructor.
Definition ESP32Task.h:62
bool isAttached()
Check if the timer is attached.
Definition ESP32Task.h:113
void attach(uint32_t ms, bool repeat, TaskTypeCallback< volatile T > callback, volatile T arg)
Attach a typed callback with argument to run periodically.
Definition ESP32Task.h:92
void(*)(void *) TaskArgCallback
Callback function with void pointer argument.
Definition ESP32Task.h:51
Contains all classes and functions related to the Uniot Core.