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:
43 Task(const char* name, int stackSizeWords, int priority = 1, int core = -1)
44 : task_name(name),
45 task_stack_size(stackSizeWords),
46 task_priority(priority),
47 task_core(core) {}
48
49 Task() = default;
50 ~Task() { remove(); }
51
54 bool create(const char* name, int stackSizeWords, int priority = 1,
55 int core = -1) {
56 if (xHandle != 0) return false;
57 // configMAX_PRIORITIES varies a lot between platforms (e.g.
58 // RP2040/arduino-pico only goes up to 7); clamp to avoid undefined
59 // behavior when a caller-supplied priority is out of range
60 if (priority >= configMAX_PRIORITIES) {
61 priority = configMAX_PRIORITIES - 1;
62 LOGW("clamped task priority to %d ", priority);
63 }
64#ifdef ESP32
65 if (core >= 0)
66 xTaskCreatePinnedToCore(task_loop, name, stackSizeWords, this, priority,
67 &xHandle, core);
68 else
69 xTaskCreate(task_loop, name, stackSizeWords, this, priority, &xHandle);
70 suspend();
71#else
72 xTaskCreate(task_loop, name, stackSizeWords, this, priority, &xHandle);
73 suspend();
74#if defined(configUSE_CORE_AFFINITY) && (configUSE_CORE_AFFINITY == 1) && \
75 (configNUMBER_OF_CORES > 1)
76 // e.g. RP2040 (Earle Philhower core): pin the task to a core after
77 // creation via the FreeRTOS SMP core-affinity API
78 if (core >= 0) vTaskCoreAffinitySet(xHandle, (1 << core));
79#endif
80#endif
81 return true;
82 }
83
85 void remove() {
86 if (xHandle != nullptr) {
87 suspend();
88 vTaskDelete(xHandle);
89 xHandle = nullptr;
90 task_suspended = false;
91 }
92 }
93
94 bool begin(std::function<void()> process) {
95 LOGI("staring task");
96 if (xHandle == nullptr && task_name != nullptr) {
98 }
99 loop_code = process;
100 resume();
101 return true;
102 }
103
105 void end() { suspend(); }
106
107 void suspend() {
108 vTaskSuspend(xHandle);
109 task_suspended = true;
110 }
111
112 void resume() {
113 vTaskResume(xHandle);
114 task_suspended = false;
115 }
116
118 bool isSuspended() { return xHandle != nullptr && task_suspended; }
119
121 bool isEnded() { return xHandle == nullptr; }
122
124 bool isRunning() { return xHandle != nullptr && !task_suspended; }
125
127 return xHandle;
128 }
129
130 void setReference(void *r){
131 ref = r;
132 }
133
135 return ref;
136 }
137
138#ifdef ESP32
139 int getCoreID() {
140 return xPortGetCoreID();
141 }
142#endif
143
144 protected:
146 std::function<void()> loop_code = nop;
147 void *ref;
148 const char* task_name = nullptr;
151 int task_core = -1;
152 bool task_suspended = false;
153
154 static void nop() { delay(100); }
155
156 static void task_loop(void* arg) {
157 Task* self = (Task*)arg;
158 while (true) {
159 self->loop_code();
160 }
161 }
162};
163
164} // namespace audio_tools
165
#define LOGW(...)
Definition AudioLoggerIDF.h:29
#define LOGI(...)
Definition AudioLoggerIDF.h:28
Interface for a task that can be started and stopped.
Definition ITask.h:10
FreeRTOS task.
Definition Task.h:26
TaskHandle_t xHandle
Definition Task.h:145
void * getReference()
Definition Task.h:134
void * ref
Definition Task.h:147
~Task()
Definition Task.h:50
TaskHandle_t & getTaskHandle()
Definition Task.h:126
void remove()
deletes the FreeRTOS task
Definition Task.h:85
int task_stack_size
Definition Task.h:149
int task_priority
Definition Task.h:150
int task_core
Definition Task.h:151
void suspend()
Definition Task.h:107
bool isRunning()
true if the task was created and is not suspended
Definition Task.h:124
void resume()
Definition Task.h:112
bool isSuspended()
true if the task was created and is currently suspended
Definition Task.h:118
bool isEnded()
true if the task was never created or has been removed
Definition Task.h:121
static void nop()
Definition Task.h:154
bool create(const char *name, int stackSizeWords, int priority=1, int core=-1)
Definition Task.h:54
int getCoreID()
Definition Task.h:139
bool task_suspended
Definition Task.h:152
Task(const char *name, int stackSizeWords, int priority=1, int core=-1)
Definition Task.h:43
static void task_loop(void *arg)
Definition Task.h:156
void end()
suspends the task
Definition Task.h:105
std::function< void()> loop_code
Definition Task.h:146
bool begin(std::function< void()> process)
Definition Task.h:94
const char * task_name
Definition Task.h:148
void setReference(void *r)
Definition Task.h:130
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