-
Notifications
You must be signed in to change notification settings - Fork 1
/
Task.cpp
130 lines (113 loc) · 2.77 KB
/
Task.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
/*
* Task.cpp
*
* Created on: Mar 4, 2017
* Author: kolban
*/
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <string>
#include "Task.h"
#include "sdkconfig.h"
static const char* LOG_TAG = "Task";
/**
* @brief Create an instance of the task class.
*
* @param [in] taskName The name of the task to create.
* @param [in] stackSize The size of the stack.
* @return N/A.
*/
Task::Task(std::string taskName, uint16_t stackSize, uint8_t priority) {
m_taskName = taskName;
m_stackSize = stackSize;
m_priority = priority;
m_taskData = nullptr;
m_handle = nullptr;
m_coreId = tskNO_AFFINITY;
} // Task
Task::~Task() {
} // ~Task
/**
* @brief Suspend the task for the specified milliseconds.
*
* @param [in] ms The delay time in milliseconds.
* @return N/A.
*/
/* static */ void Task::delay(int ms) {
::vTaskDelay(ms / portTICK_PERIOD_MS);
} // delay
/**
* Static class member that actually runs the target task.
*
* The code here will run on the task thread.
* @param [in] pTaskInstance The task to run.
*/
void Task::runTask(void* pTaskInstance) {
Task* pTask = (Task*) pTaskInstance;
ESP_LOGD(LOG_TAG, ">> runTask: taskName=%s", pTask->m_taskName.c_str());
pTask->run(pTask->m_taskData);
ESP_LOGD(LOG_TAG, "<< runTask: taskName=%s", pTask->m_taskName.c_str());
pTask->stop();
} // runTask
/**
* @brief Start an instance of the task.
*
* @param [in] taskData Data to be passed into the task.
* @return N/A.
*/
void Task::start(void* taskData) {
if (m_handle != nullptr) {
ESP_LOGW(LOG_TAG, "Task::start - There might be a task already running!");
}
m_taskData = taskData;
::xTaskCreatePinnedToCore(&runTask, m_taskName.c_str(), m_stackSize, this, m_priority, &m_handle, m_coreId);
} // start
/**
* @brief Stop the task.
*
* @return N/A.
*/
void Task::stop() {
if (m_handle == nullptr) return;
xTaskHandle temp = m_handle;
m_handle = nullptr;
::vTaskDelete(temp);
} // stop
/**
* @brief Set the stack size of the task.
*
* @param [in] stackSize The size of the stack for the task.
* @return N/A.
*/
void Task::setStackSize(uint16_t stackSize) {
m_stackSize = stackSize;
} // setStackSize
/**
* @brief Set the priority of the task.
*
* @param [in] priority The priority for the task.
* @return N/A.
*/
void Task::setPriority(uint8_t priority) {
m_priority = priority;
} // setPriority
/**
* @brief Set the name of the task.
*
* @param [in] name The name for the task.
* @return N/A.
*/
void Task::setName(std::string name) {
m_taskName = name;
} // setName
/**
* @brief Set the core number the task has to be executed on.
* If the core number is not set, tskNO_AFFINITY will be used
*
* @param [in] coreId The id of the core.
* @return N/A.
*/
void Task::setCore(BaseType_t coreId) {
m_coreId = coreId;
}