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>
12
13namespace audio_tools {
14
21class Task {
22 public:
24 Task(const char* name, int stackSize, int priority = 1, int core = -1) {
25 create(name, stackSize, priority, core);
26 }
27
28 Task() = default;
29 ~Task() { remove(); }
30
32 bool create(const char* name, int stackSize, int priority = 1,
33 int core = -1) {
34 if (xHandle != 0) return false;
35#ifdef ESP32
36 if (core >= 0)
38 &xHandle, core);
39 else
41#else
43#endif
44 suspend();
45 return true;
46 }
47
49 void remove() {
50 if (xHandle != nullptr) {
51 suspend();
53 xHandle = nullptr;
54 }
55 }
56
57 bool begin(std::function<void()> process) {
58 LOGI("staring task");
59 loop_code = process;
60 resume();
61 return true;
62 }
63
65 void end() { suspend(); }
66
68
70
72 return xHandle;
73 }
74
75 void setReference(void *r){
76 ref = r;
77 }
78
79 void *getReference(){
80 return ref;
81 }
82
83#ifdef ESP32
84 int getCoreID() {
85 return xPortGetCoreID();
86 }
87#endif
88
89 protected:
91 std::function<void()> loop_code = nop;
92 void *ref;
93
94 static void nop() { delay(100); }
95
96 static void task_loop(void* arg) {
97 Task* self = (Task*)arg;
98 while (true) {
99 self->loop_code();
100 }
101 }
102};
103
104} // namespace audio_tools
105
#define LOGI(...)
Definition AudioLoggerIDF.h:28
FreeRTOS task.
Definition Task.h:21
TaskHandle_t xHandle
Definition Task.h:90
void * getReference()
Definition Task.h:79
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 Task.h:32
void * ref
Definition Task.h:92
~Task()
Definition Task.h:29
TaskHandle_t & getTaskHandle()
Definition Task.h:71
void remove()
deletes the FreeRTOS task
Definition Task.h:49
void suspend()
Definition Task.h:67
void resume()
Definition Task.h:69
static void nop()
Definition Task.h:94
int getCoreID()
Definition Task.h:84
Task(const char *name, int stackSize, int priority=1, int core=-1)
Defines and creates a FreeRTOS task.
Definition Task.h:24
static void task_loop(void *arg)
Definition Task.h:96
void end()
suspends the task
Definition Task.h:65
std::function< void()> loop_code
Definition Task.h:91
bool begin(std::function< void()> process)
Definition Task.h:57
void setReference(void *r)
Definition Task.h:75
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
void delay(unsigned long ms)
Definition Time.h:23
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512