-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
22 changed files
with
2,343 additions
and
1,826 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Task.h | ||
* | ||
* Created on: Mar 4, 2017 | ||
* Author: kolban | ||
*/ | ||
|
||
#ifndef COMPONENTS_CPP_UTILS_TASK_H_ | ||
#define COMPONENTS_CPP_UTILS_TASK_H_ | ||
#include <freertos/FreeRTOS.h> | ||
#include <freertos/task.h> | ||
#include <string> | ||
/** | ||
* @brief Encapsulate a runnable task. | ||
* | ||
* This class is designed to be subclassed with the method: | ||
* | ||
* @code{.cpp} | ||
* void run(void *data) { ... } | ||
* @endcode | ||
* | ||
* For example: | ||
* | ||
* @code{.cpp} | ||
* class CurlTestTask : public Task { | ||
* void run(void *data) { | ||
* // Do something | ||
* } | ||
* }; | ||
* @endcode | ||
* | ||
* implemented. | ||
*/ | ||
class Task { | ||
public: | ||
Task(std::string taskName = "Task", uint16_t stackSize = 10000, uint8_t priority = 5); | ||
virtual ~Task(); | ||
void setStackSize(uint16_t stackSize); | ||
void setPriority(uint8_t priority); | ||
void setName(std::string name); | ||
void setCore(BaseType_t coreId); | ||
void start(void* taskData = nullptr); | ||
void stop(); | ||
/** | ||
* @brief Body of the task to execute. | ||
* | ||
* This function must be implemented in the subclass that represents the actual task to run. | ||
* When a task is started by calling start(), this is the code that is executed in the | ||
* newly created task. | ||
* | ||
* @param [in] data The data passed in to the newly started task. | ||
*/ | ||
virtual void run(void* data) = 0; // Make run pure virtual | ||
static void delay(int ms); | ||
|
||
private: | ||
xTaskHandle m_handle; | ||
void* m_taskData; | ||
static void runTask(void* data); | ||
std::string m_taskName; | ||
uint16_t m_stackSize; | ||
uint8_t m_priority; | ||
BaseType_t m_coreId; | ||
|
||
}; | ||
|
||
#endif /* COMPONENTS_CPP_UTILS_TASK_H_ */ |
Oops, something went wrong.