-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTasko.h
More file actions
218 lines (183 loc) · 5.26 KB
/
Tasko.h
File metadata and controls
218 lines (183 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Developer: Sreeraj
// GitHub: https://github.com/s-r-e-e-r-a-j
#ifndef TASKO_H
#define TASKO_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef ARDUINO
#include <Arduino.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#else
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#endif
// Maximum tasks allowed
#ifndef TASKO_MAX_TASKS
#define TASKO_MAX_TASKS 16
#endif
typedef void (*TaskoCallback)(void*);
typedef void (*TaskoHook)(int id);
typedef struct {
TaskHandle_t handle;
TimerHandle_t timer;
TaskoCallback callback;
void* arg;
TaskoHook onStart;
TaskoHook onStop;
uint32_t intervalMs;
uint8_t priority;
uint8_t core;
bool repeating;
bool active;
bool pendingRemove;
bool used;
} TaskoTask;
static TaskoTask taskList[TASKO_MAX_TASKS];
static uint8_t taskCount = 0;
static bool taskoDebug = false;
// Enable debug logging
static inline void TaskoEnableDebug(bool enable) { taskoDebug = enable; }
static inline void TaskoLog(const char* msg, int id) {
if (taskoDebug) {
#ifdef ARDUINO
if (id >= 0) Serial.printf("[Tasko] %s (id=%d)\n", msg, id);
else Serial.printf("[Tasko] %s\n", msg);
#else
if (id >= 0) printf("[Tasko] %s (id=%d)\n", msg, id);
else printf("[Tasko] %s\n", msg);
#endif
}
}
// One-time task wrapper
static void TaskoOneTimeWrapper(void* param) {
int idx = (int)(intptr_t)param;
if (idx < 0 || idx >= TASKO_MAX_TASKS) return;
TaskoTask* t = &taskList[idx];
if (!t->used) return;
if (t->onStart) t->onStart(idx);
if (t->callback) t->callback(t->arg);
if (t->onStop) t->onStop(idx);
t->active = false;
t->used = false;
taskCount--;
vTaskDelete(NULL); // safe deletion
}
// Timer callback wrapper
static void TaskoTimerCallback(TimerHandle_t xTimer) {
int idx = (int)(intptr_t)pvTimerGetTimerID(xTimer);
if (idx < 0 || idx >= TASKO_MAX_TASKS) return;
TaskoTask* t = &taskList[idx];
if (!t->used || !t->active) return;
if (t->pendingRemove) {
if (t->timer != NULL) {
xTimerDelete(t->timer, 0);
t->timer = NULL;
}
t->used = false;
t->pendingRemove = false;
taskCount--;
TaskoLog("Removed pending repeating task", idx);
return;
}
if (t->onStart) t->onStart(idx);
if (t->callback) t->callback(t->arg);
if (t->onStop) t->onStop(idx);
}
// Add task
static int TaskoAdd(TaskoCallback func, void* arg, uint32_t intervalMs, bool repeat,
uint8_t priority, uint8_t core,
TaskoHook startHook, TaskoHook stopHook,
size_t stackSize) {
if (taskCount >= TASKO_MAX_TASKS) return -1;
int id = -1;
for (int i = 0; i < TASKO_MAX_TASKS; i++) {
if (!taskList[i].used) { id = i; break; }
}
if (id == -1) return -1;
TaskoTask* t = &taskList[id];
t->callback = func;
t->arg = arg;
t->intervalMs = intervalMs;
t->repeating = repeat;
t->priority = priority;
t->core = core;
t->active = true;
t->onStart = startHook;
t->onStop = stopHook;
t->pendingRemove = false;
t->used = true;
if (repeat) {
t->timer = xTimerCreate("TaskoTimer", pdMS_TO_TICKS(intervalMs), pdTRUE,
(void*)(intptr_t)id, TaskoTimerCallback);
if (t->timer) xTimerStart(t->timer, 0);
t->handle = NULL;
TaskoLog("Added repeating task", id);
} else {
TaskHandle_t tmpHandle = NULL;
xTaskCreatePinnedToCore(TaskoOneTimeWrapper, "TaskoOnce",
stackSize, (void*)(intptr_t)id,
priority, &tmpHandle, core);
t->handle = tmpHandle;
TaskoLog("Added one-time task", id);
}
taskCount++;
return id;
}
// Remove task safely
static void TaskoRemove(int id) {
if (id < 0 || id >= TASKO_MAX_TASKS) return;
TaskoTask* t = &taskList[id];
if (!t->used) return;
t->active = false;
if (t->handle == xTaskGetCurrentTaskHandle()) {
t->pendingRemove = true;
TaskoLog("Pending removal of running task", id);
return;
}
if (t->timer != NULL) {
xTimerDelete(t->timer, 0);
t->timer = NULL;
}
if (t->handle != NULL) {
vTaskDelete(t->handle);
t->handle = NULL;
}
t->used = false;
taskCount--;
TaskoLog("Removed task immediately", id);
}
// Clear all tasks
static void TaskoClearAll() {
for (int i = 0; i < TASKO_MAX_TASKS; i++) {
if (taskList[i].used) TaskoRemove(i);
}
}
// Pause / Resume
static void TaskoPause(int id) {
if (id < 0 || id >= TASKO_MAX_TASKS) return;
TaskoTask* t = &taskList[id];
if (!t->used) return;
t->active = false;
if (t->timer) xTimerStop(t->timer, 0);
TaskoLog("Paused task", id);
}
static void TaskoResume(int id) {
if (id < 0 || id >= TASKO_MAX_TASKS) return;
TaskoTask* t = &taskList[id];
if (!t->used) return;
t->active = true;
if (t->timer) xTimerStart(t->timer, 0);
TaskoLog("Resumed task", id);
}
#ifdef __cplusplus
}
#endif
#endif // TASKO_H