-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadEsp32.cpp
160 lines (133 loc) · 4.21 KB
/
ThreadEsp32.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// Created by Xx220xX on 19/07/2019.
//
/***
* https://www.freertos.org/a00021.html
* link for all function used here
*
***/
#include "ThreadEsp32.h"
#ifndef THREAD_ESP32_DEBUG
#define THREAD_DEBUG_PRINT(x)
#define THREAD_DEBUG_PRINTLN(x)
#else
#define THREAD_DEBUG_PRINT(x) Serial.print(x);
#define THREAD_DEBUG_PRINTLN(x) Serial.println(x);
#endif
int last_id = 0;
Thread::Thread( ) {
THREAD_DEBUG_PRINTLN("criando tarefa vazia agr")
this->ready = false;
}
Thread::Thread(const char *name,void (*function)(void *), uint32_t stack_size, uint32_t priority, uint8_t nucleo) {
THREAD_DEBUG_PRINTLN("criando tarefa agr")
this->pFunction = function;
this->stack_size = stack_size;
this->nucleo = nucleo;
this->priority = priority;
this->ready = true;
if (name == NULL)
snprintf(this->name, 14, "%s", "THREAD id: ", last_id);
snprintf(this->name, 14, "%s", name, last_id);
snprintf(this->name,17,"%s %d",this->name,last_id);
last_id++;
THREAD_DEBUG_PRINT("Criado uma nova tarefa: ")
THREAD_DEBUG_PRINTLN(this->name)
}
void Run(void *pThread) {
((Thread *) pThread)->run();
}
bool Thread::start(void *params) {
if (!this->ready || this->started) {
return false;
}
void *pThread = this;
this->params = params;
bool pdPass;
if(this->nucleo == 0 ||this->nucleo == 1){
pdPass = xTaskCreatePinnedToCore(Run,
this->name,
this->stack_size,
pThread,
this->priority,
&this->handle,
this->nucleo);
}else{
pdPass = xTaskCreate( Run,
this->name,
this->stack_size,
pThread,
this->priority,
&this->handle);
}
delay(1000);// tempo para iniciar e nao dar watchdog error
this->started = true;
return pdPass;
}
void Thread::stop() {
THREAD_DEBUG_PRINTLN("tentativa de finalizar Tarefa: ")
if (this->started) {
this->stoped = true;
vTaskDelete(this->handle);
this->handle= 0;
THREAD_DEBUG_PRINTLN("SUCCESS\n")
return ;
}
THREAD_DEBUG_PRINTLN("FALHA\n")
}
void Thread::sleep(uint32_t ms) {
ms = ms / portTICK_PERIOD_MS;
vTaskDelay(ms);
}
uint32_t Thread::getPriority() {
return uxTaskPriorityGet(this->handle);
}
bool Thread::isRuning() {
return this->handle != NULL && !paused;
}
Thread Thread::getCurrent() {
THREAD_DEBUG_PRINTLN("Get Current Thread:")
Thread currentThread;
currentThread.ready = true;
currentThread.started = true;
currentThread.priority = uxTaskPriorityGet(NULL);
snprintf(currentThread.name, 16, "%s", pcTaskGetTaskName(NULL));
currentThread.nucleo = xPortGetCoreID();
currentThread.handle = xTaskGetCurrentTaskHandle();
THREAD_DEBUG_PRINT("name: ")
THREAD_DEBUG_PRINTLN(currentThread.name)
THREAD_DEBUG_PRINT("Priority: ")
THREAD_DEBUG_PRINTLN(currentThread.priority)
THREAD_DEBUG_PRINT("nucleo: ")
THREAD_DEBUG_PRINTLN(currentThread.nucleo)
return currentThread;
}
void Thread::pause() {
THREAD_DEBUG_PRINT(this->name)
THREAD_DEBUG_PRINTLN(": solicitou uma pausa :")
if (ready && started && !paused) {
paused = true;
vTaskSuspend(handle);
THREAD_DEBUG_PRINTLN("Concedido\n")
return;
}
THREAD_DEBUG_PRINTLN("negado\n")
}
void Thread::resume() {
if (ready && started && paused) {
paused = false;
vTaskResume(handle);
}
}
void Thread::run() {
this->pFunction(this->params);
THREAD_DEBUG_PRINTLN("executou com sucesso agr vai parar :D")
this->stoped = true;
this->handle= NULL;
vTaskDelete(NULL);
}
const char *Thread::getName() {
return this->name;
}
#undef THREAD_DEBUG_PRINT(x)
#undef THREAD_DEBUG_PRINTLN(x)