wellhub_reloaded/main/TaskFactory.h

41 lines
1015 B
C++

/// © MiroZ 2024
#ifndef __TASK_FACTORY_H__
#define __TASK_FACTORY_H__
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/semphr.h>
#include <stdint.h>
#include <esp_err.h>
#include <functional>
typedef std::function<void()> TASK_FUNCTION;
// no need for mutex here
#define _USE_MUTEX
class TaskFactory
{
protected:
#ifdef USE_MUTEX
SemaphoreHandle_t m_xMutex = NULL;
#endif
TASK_FUNCTION m_task_function = nullptr;
protected:
static void taskStarter(void * ptr) { (static_cast<TaskFactory*>(ptr)->run()); }
void run(){ m_task_function(); }
public:
TaskHandle_t createTask(TASK_FUNCTION task_function, const char *name, uint32_t stack_size, UBaseType_t priority, BaseType_t core);
static uint32_t getStackHighWaterMark(TaskHandle_t task = NULL){ return (uint32_t)uxTaskGetStackHighWaterMark(task); }
static void deleteTask(TaskHandle_t task = NULL){ vTaskDelete(task); }
TaskFactory();
~TaskFactory();
};
extern TaskFactory TASK_FACTORY;
#endif /* __TASK_FACTORY_H__ */