Uniot Core
0.8.1
Loading...
Searching...
No Matches
ESP8266Task.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
19#pragma once
20
21#if defined(ESP8266)
22
23#include <stddef.h>
24#include <stdint.h>
25
26extern "C" {
27#include "c_types.h"
28#include "eagle_soc.h"
29#include "ets_sys.h"
30#include "osapi.h"
31}
32
33namespace uniot {
34extern "C" {
35typedef struct _ETSTIMER_ ETSTimer;
36}
37
49 public:
53 using TaskCallback = void (*)(void);
54
58 using TaskArgCallback = void (*)(void *);
59
63 template <typename T>
64 using TaskTypeCallback = void (*)(volatile T);
65
70 : mpTimer(nullptr) {}
71
75 virtual ~ESP8266Task() {
76 detach();
77 }
78
86 void attach(uint32_t ms, bool repeat, TaskCallback callback) {
87 attach_arg(ms, repeat, reinterpret_cast<TaskArgCallback>(callback), 0);
88 }
89
98 template <typename T>
99 void attach(uint32_t ms, bool repeat, TaskTypeCallback<volatile T> callback, volatile T arg) {
100 static_assert(sizeof(volatile T) <= sizeof(uint32_t), "sizeof arg must be <= sizeof(uint32_t), i.e 4 bytes");
101 attach_arg(ms, repeat, reinterpret_cast<TaskArgCallback>(callback), reinterpret_cast<volatile void *>(arg));
102 }
103
107 void detach() {
108 if (mpTimer) {
109 os_timer_disarm(mpTimer);
110 delete mpTimer;
111 mpTimer = nullptr;
112 }
113 }
114
121 bool isAttached() {
122 return mpTimer != nullptr;
123 }
124
125 private:
126 ETSTimer *mpTimer;
127
136 void attach_arg(uint32_t ms, bool repeat, TaskArgCallback callback, volatile void *arg) {
137 if (mpTimer) {
138 os_timer_disarm(mpTimer);
139 } else {
140 mpTimer = new ETSTimer;
141 }
142
143 os_timer_setfn(mpTimer, reinterpret_cast<ETSTimerFunc *>(callback), const_cast<void*>(arg));
144 os_timer_arm(mpTimer, ms, repeat);
145 }
146};
147
148} // namespace uniot
149
150#endif // defined(ESP8266)
151
ESP8266Task()
Constructor.
Definition ESP8266Task.h:69
virtual ~ESP8266Task()
Destructor that ensures the timer is detached.
Definition ESP8266Task.h:75
void attach(uint32_t ms, bool repeat, TaskTypeCallback< volatile T > callback, volatile T arg)
Attach a typed callback with argument to run periodically.
Definition ESP8266Task.h:99
void(*)(void *) TaskArgCallback
Callback function with void pointer argument.
Definition ESP8266Task.h:58
void attach(uint32_t ms, bool repeat, TaskCallback callback)
Attach a simple callback to run periodically.
Definition ESP8266Task.h:86
void detach()
Stop and detach the timer.
Definition ESP8266Task.h:107
void(*)(volatile T) TaskTypeCallback
Templated callback function with typed argument.
Definition ESP8266Task.h:64
void(*)(void) TaskCallback
Callback function with no arguments.
Definition ESP8266Task.h:53
bool isAttached()
Check if the timer is attached.
Definition ESP8266Task.h:121
Contains all classes and functions related to the Uniot Core.
struct _ETSTIMER_ ETSTimer
Definition ESP8266Task.h:35