8000 Added methods to Task class and moved a debug logging line. · kebot-embedded/esp32-snippets@1891195 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1891195

Browse files
Added methods to Task class and moved a debug logging line.
- Added a method "setPriority" that enables one to set the task priority. - Added a method "setName" that enables one to set the name of the Task outside the constructor. - Moved the task exit ESP_LOG call in front of the stop() call. It would otherwise never be called.
1 parent e2bf94a commit 1891195

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

cpp_utils/Task.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ static char tag[] = "Task";
2424
* @param [in] stackSize The size of the stack.
2525
* @return N/A.
2626
*/
27-
Task::Task(std::string taskName, uint16_t stackSize) {
27+
Task::Task(std::string taskName, uint16_t stackSize, uint8_t priority) {
2828
m_taskName = taskName;
2929
m_stackSize = stackSize;
30+
m_priority = priority;
3031
m_taskData = nullptr;
3132
m_handle = nullptr;
3233
} // Task
@@ -55,8 +56,8 @@ void Task::runTask(void* pTaskInstance) {
5556
Task* pTask = (Task*)pTaskInstance;
5657
ESP_LOGD(tag, ">> runTask: taskName=%s", pTask->m_taskName.c_str());
5758
pTask->run(pTask->m_taskData);
58-
pTask->stop();
5959
ESP_LOGD(tag, "<< runTask: taskName=%s", pTask->m_taskName.c_str());
60+
pTask->stop();
6061
} // runTask
6162

6263
/**
@@ -70,7 +71,7 @@ void Task::start(void* taskData) {
7071
ESP_LOGW(tag, "Task::start - There might be a task already running!");
7172
}
7273
m_taskData = taskData;
73-
::xTaskCreate(&runTask, m_taskName.c_str(), m_stackSize, this, 5, &m_handle);
74+
::xTaskCreate(&runTask, m_taskName.c_str(), m_stackSize, this, m_priority, &m_handle);
7475
} // start
7576

7677

@@ -97,3 +98,23 @@ void Task::stop() {
9798
void Task::setStackSize(uint16_t stackSize) {
9899
m_stackSize = stackSize;
99100
} // setStackSize
101+
102+
/**
103+
* @brief Set the priority of the task.
104+
*
105+
* @param [in] priority The priority for the task.
106+
* @return N/A.
107+
*/
108+
void Task::setPriority(uint8_t priority) {
109+
m_priority = priority;
110+
} // setPriority
111+
112+
/**
113+
* @brief Set the name of the task.
114+
*
115+
* @param [in] name The name for the task.
116+
* @return N/A.
117+
*/
118+
void Task::setName(std::string name) {
119+
m_taskName = name;
120+
} // setName

cpp_utils/Task.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
*/
3434
class Task {
3535
public:
36-
Task(std::string taskName="Task", uint16_t stackSize=10000);
36+
Task(std::string taskName="Task", uint16_t stackSize=10000, uint8_t priority=5);
3737
virtual ~Task();
3838
void setStackSize(uint16_t stackSize);
39+
void setPriority(uint8_t priority);
40+
void setName(std::string name);
3941
void start(void* taskData=nullptr);
4042
void stop();
4143
/**
@@ -56,6 +58,7 @@ class Task {
5658
static void runTask(void *data);
5759
std::string m_taskName;
5860
uint16_t m_stackSize;
61+
uint8_t m_priority;
5962
};
6063

6164
#endif /* COMPONENTS_CPP_UTILS_TASK_H_ */

0 commit comments

Comments
 (0)
0