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