arduino-audio-tools
Loading...
Searching...
No Matches
Task.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <functional>
6#include <zephyr/kernel.h>
7
8namespace audio_tools {
9
11
20 public:
22 TaskZephyr(const char *name, int stackSize, int priority = 1, int core = -1) {
23 create(name, stackSize, priority, core);
24 }
25
26 TaskZephyr() = default;
28
30 bool create(const char *name, int stackSize, int priority = 1,
31 int core = -1) {
32 (void)core;
33
34 if (xHandle != nullptr) return false;
35
36 stack_size = stackSize > 0 ? stackSize : 2048;
38 if (p_stack == nullptr) {
39 LOGE("Task stack allocation failed");
40 return false;
41 }
42
44 nullptr, nullptr, priority, 0, K_FOREVER);
45 if (xHandle == nullptr) {
47 p_stack = nullptr;
48 return false;
49 }
50
51 if (name != nullptr) {
53 }
54
55 is_started = false;
56 return true;
57 }
58
60 void remove() {
61 if (xHandle != nullptr) {
63 xHandle = nullptr;
64 is_started = false;
65 }
66 if (p_stack != nullptr) {
68 p_stack = nullptr;
69 }
70 }
71
72 bool begin(std::function<void()> process) {
73 LOGI("starting task");
74 loop_code = process;
75 resume();
76 return true;
77 }
78
80 void end() { suspend(); }
81
82 void suspend() {
83 if (xHandle != nullptr && is_started) {
85 }
86 }
87
88 void resume() {
89 if (xHandle == nullptr) return;
90
91 if (!is_started) {
93 is_started = true;
94 } else {
96 }
97 }
98
100
101 void setReference(void *r) { ref = r; }
102
103 void *getReference() { return ref; }
104
105 protected:
108 std::function<void()> loop_code = nop;
109 void *ref = nullptr;
110 bool is_started = false;
111 size_t stack_size = 0;
113
114 static void nop() { delay(100); }
115
116 static void task_loop(void *arg, void *p2, void *p3) {
117 (void)p2;
118 (void)p3;
119
120 TaskZephyr *self = (TaskZephyr *)arg;
121 while (true) {
122 self->loop_code();
123 }
124 }
125};
126
129
130} // namespace audio_tools
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGE(...)
Definition AudioLoggerIDF.h:30
FreeRTOS task.
Definition Task.h:25
Zephyr thread based task.
Definition Task.h:19
TaskHandle_t xHandle
Definition Task.h:107
void * getReference()
Definition Task.h:103
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:30
void * ref
Definition Task.h:109
TaskHandle_t & getTaskHandle()
Definition Task.h:99
void remove()
deletes the task
Definition Task.h:60
void suspend()
Definition Task.h:82
void resume()
Definition Task.h:88
bool is_started
Definition Task.h:110
k_thread_stack_t * p_stack
Definition Task.h:112
~TaskZephyr()
Definition Task.h:27
static void nop()
Definition Task.h:114
TaskZephyr(const char *name, int stackSize, int priority=1, int core=-1)
Defines and creates a task.
Definition Task.h:22
static void task_loop(void *arg, void *p2, void *p3)
Definition Task.h:116
size_t stack_size
Definition Task.h:111
void end()
suspends the task
Definition Task.h:80
std::function< void()> loop_code
Definition Task.h:108
bool begin(std::function< void()> process)
Definition Task.h:72
struct k_thread thread_data
Definition Task.h:106
void setReference(void *r)
Definition Task.h:101
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