arduino-audio-tools
Loading...
Searching...
No Matches
Task.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>
13#include "AudioLogger.h"
14
15namespace audio_tools {
16
26class Task : public ITask {
27 public:
44 Task(const char* name, int stackSizeWords, int priority = 1, int core = -1)
45 : task_name(name),
46 task_stack_size(stackSizeWords),
47 task_priority(priority),
48 task_core(core) {}
49
50 Task() = default;
51 ~Task() { remove(); }
52
56 bool create(const char* name, int stackSizeWords, int priority = 1,
57 int core = -1) {
58 if (xHandle != 0) return false;
59 // configMAX_PRIORITIES varies a lot between platforms (e.g.
60 // RP2040/arduino-pico only goes up to 7); clamp to avoid undefined
61 // behavior when a caller-supplied priority is out of range
62 if (priority >= configMAX_PRIORITIES) {
63 priority = configMAX_PRIORITIES - 1;
64 LOGW("clamped task priority to %d ", priority);
65 }
66 BaseType_t rc;
67#ifdef ESP32
68 if (core >= 0)
69 rc = xTaskCreatePinnedToCore(task_loop, name, stackSizeWords, this,
70 priority, &xHandle, core);
71 else
72 rc = xTaskCreate(task_loop, name, stackSizeWords, this, priority,
73 &xHandle);
74 // without a valid handle suspend() would suspend the calling task
75 if (rc != pdPASS || xHandle == nullptr) {
76 LOGE("could not create task %s", name);
77 xHandle = nullptr;
78 return false;
79 }
80 suspend();
81#else
82 rc = xTaskCreate(task_loop, name, stackSizeWords, this, priority,
83 &xHandle);
84 if (rc != pdPASS || xHandle == nullptr) {
85 LOGE("could not create task %s", name);
86 xHandle = nullptr;
87 return false;
88 }
89 suspend();
90#if defined(configUSE_CORE_AFFINITY) && (configUSE_CORE_AFFINITY == 1) && \
91 (configNUMBER_OF_CORES > 1)
92 // e.g. RP2040 (Earle Philhower core): pin the task to a core after
93 // creation via the FreeRTOS SMP core-affinity API
94 if (core >= 0) vTaskCoreAffinitySet(xHandle, (1 << core));
95#endif
96#endif
97 return true;
98 }
99
101 void remove() {
102 if (xHandle != nullptr) {
103 suspend();
104 vTaskDelete(xHandle);
105 xHandle = nullptr;
106 task_suspended = false;
107 }
108 }
109
110 bool begin(std::function<void()> process) {
111 LOGI("starting task");
112 if (xHandle == nullptr && task_name != nullptr) {
114 return false;
115 }
116 if (xHandle == nullptr) {
117 LOGE("task not created: call create() or use the named constructor");
118 return false;
119 }
120 loop_code = process;
121 resume();
122 return true;
123 }
124
126 void end() { suspend(); }
127
130 void suspend() {
131 if (xHandle == nullptr) return;
132 vTaskSuspend(xHandle);
133 task_suspended = true;
134 }
135
136 void resume() {
137 if (xHandle == nullptr) return;
138 vTaskResume(xHandle);
139 task_suspended = false;
140 }
141
143 bool isSuspended() { return xHandle != nullptr && task_suspended; }
144
146 bool isEnded() { return xHandle == nullptr; }
147
149 bool isRunning() { return xHandle != nullptr && !task_suspended; }
150
152 return xHandle;
153 }
154
155 void setReference(void *r){
156 ref = r;
157 }
158
160 return ref;
161 }
162
163#ifdef ESP32
164 int getCoreID() {
165 return xPortGetCoreID();
166 }
167#endif
168
169 protected:
171 std::function<void()> loop_code = nop;
172 void *ref = nullptr;
173 const char* task_name = nullptr;
176 int task_core = -1;
177 bool task_suspended = false;
178
179 static void nop() { delay(100); }
180
181 static void task_loop(void* arg) {
182 Task* self = (Task*)arg;
183 while (true) {
184 self->loop_code();
185 }
186 }
187};
188
189} // namespace audio_tools
190
int BaseType_t
Definition BufferBlocking.h:12
#define LOGW(...)
#define LOGI(...)
#define LOGE(...)
Interface for a task that can be started and stopped.
Definition ITask.h:10
TaskHandle_t xHandle
Definition Task.h:170
void * getReference()
Definition Task.h:159
void * ref
Definition Task.h:172
~Task()
Definition Task.h:51
TaskHandle_t & getTaskHandle()
Definition Task.h:151
void remove()
deletes the FreeRTOS task
Definition Task.h:101
int task_stack_size
Definition Task.h:174
int task_priority
Definition Task.h:175
int task_core
Definition Task.h:176
void suspend()
Definition Task.h:130
bool isRunning()
true if the task was created and is not suspended
Definition Task.h:149
void resume()
Definition Task.h:136
bool isSuspended()
true if the task was created and is currently suspended
Definition Task.h:143
bool isEnded()
true if the task was never created or has been removed
Definition Task.h:146
static void nop()
Definition Task.h:179
bool create(const char *name, int stackSizeWords, int priority=1, int core=-1)
Definition Task.h:56
int getCoreID()
Definition Task.h:164
bool task_suspended
Definition Task.h:177
Task(const char *name, int stackSizeWords, int priority=1, int core=-1)
Definition Task.h:44
static void task_loop(void *arg)
Definition Task.h:181
void end()
suspends the task
Definition Task.h:126
std::function< void()> loop_code
Definition Task.h:171
bool begin(std::function< void()> process)
Definition Task.h:110
const char * task_name
Definition Task.h:173
void setReference(void *r)
Definition Task.h:155
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
k_tid_t TaskHandle_t
Definition Task.h:10
void delay(uint32_t ms)
Definition Arduino.h:259