Arduino DLNA Server
Loading...
Searching...
No Matches
TaskRTOS.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef ESP32
4#include "freertos/FreeRTOS.h"
5#include "freertos/task.h"
6#elif defined(__linux__)
7#else
8#include "FreeRTOS.h"
9#include "task.h"
10#endif
11#include <functional>
12
13#include "basic/Logger.h"
14
15namespace tiny_dlna {
16
23class TaskRTOS {
24 public:
26 TaskRTOS(const char* name, int stackSize, int priority = 1, int core = -1) {
27 create(name, stackSize, priority, core);
28 }
29
31 create("TaskRTOS", 4096, 1, -1);
32 };
33
35
37 void remove() {
38 if (xHandle != nullptr) {
39 suspend();
40 vTaskDelete(xHandle);
41 xHandle = nullptr;
42 }
43 }
44
45 bool begin(std::function<void()> process) {
46 DlnaLogger.log(DlnaLogLevel::Info, "staring task");
47 loop_code = process;
48 resume();
49 return true;
50 }
51
53 void end() { suspend(); }
54
55 void suspend() {
56 if (xHandle == nullptr) {
57 DlnaLogger.log(DlnaLogLevel::Warning,
58 "TaskRTOS::suspend() called but task not created");
59 return;
60 }
61 vTaskSuspend(xHandle);
62 }
63
64 void resume() {
65 if (xHandle == nullptr) {
66 DlnaLogger.log(DlnaLogLevel::Warning,
67 "TaskRTOS::resume() called but task not created");
68 return;
69 }
70
71 vTaskResume(xHandle);
72 }
73
74 TaskHandle_t& getTaskHandle() { return xHandle; }
75
76 void setReference(void* r) { ref = r; }
77
78 void* getReference() { return ref; }
79
80#ifdef ESP32
81 int getCoreID() { return xPortGetCoreID(); }
82#endif
83
84 protected:
85 TaskHandle_t xHandle = nullptr;
86 std::function<void()> loop_code = nop;
87 void* ref;
88
90 bool create(const char* name, int stackSize, int priority = 1,
91 int core = -1) {
92 if (xHandle != 0) return false;
93#ifdef ESP32
94 if (core >= 0)
95 xTaskCreatePinnedToCore(task_loop, name, stackSize, this, priority,
96 &xHandle, core);
97 else
98 xTaskCreate(task_loop, name, stackSize, this, priority, &xHandle);
99#else
100 xTaskCreate(task_loop, name, stackSize, this, priority, &xHandle);
101#endif
102 delay(50);
103 suspend();
104 return xHandle != nullptr;
105 }
106
107 static void nop() { delay(100); }
108
109 static void task_loop(void* arg) {
110 TaskRTOS* self = (TaskRTOS*)arg;
111 while (true) {
112 self->loop_code();
113 }
114 }
115};
116
118
119} // namespace tiny_dlna
FreeRTOS task.
Definition: TaskRTOS.h:23
TaskHandle_t & getTaskHandle()
Definition: TaskRTOS.h:74
TaskRTOS()
Definition: TaskRTOS.h:30
TaskHandle_t xHandle
Definition: TaskRTOS.h:85
void remove()
deletes the FreeRTOS task
Definition: TaskRTOS.h:37
std::function< void()> loop_code
Definition: TaskRTOS.h:86
~TaskRTOS()
Definition: TaskRTOS.h:34
bool begin(std::function< void()> process)
Definition: TaskRTOS.h:45
TaskRTOS(const char *name, int stackSize, int priority=1, int core=-1)
Defines and creates a FreeRTOS task.
Definition: TaskRTOS.h:26
void suspend()
Definition: TaskRTOS.h:55
void * getReference()
Definition: TaskRTOS.h:78
void * ref
Definition: TaskRTOS.h:87
void setReference(void *r)
Definition: TaskRTOS.h:76
void resume()
Definition: TaskRTOS.h:64
void end()
suspends the task
Definition: TaskRTOS.h:53
static void task_loop(void *arg)
Definition: TaskRTOS.h:109
bool create(const char *name, int stackSize, int priority=1, int core=-1)
If you used the empty constructor, you need to call create!
Definition: TaskRTOS.h:90
static void nop()
Definition: TaskRTOS.h:107
Definition: Allocator.h:13