Firmware  0.4.1
Loading...
Searching...
No Matches
task.hpp
Go to the documentation of this file.
1// Copyright (C) 2025 Vincent Hamp
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
21
22#include <esp_task.h>
23#include <array>
24
25#pragma once
26
27#define CONCAT_DETAIL(A, B) A##B
28#define CONCAT(A, B) CONCAT_DETAIL(A, B)
29
36#define COMMON_TASK_MEMBERS(NAME, PRIORITY, CORE_ID, TIMEOUT) \
37 static constexpr char const* name{NAME}; \
38 static constexpr UBaseType_t priority{PRIORITY}; \
39 static constexpr BaseType_t core_id{CORE_ID}; \
40 static constexpr TickType_t timeout{TIMEOUT}; \
41 static inline StaticTask_t tcb{}; \
42 static inline TaskFunction_t function{NULL}; \
43 static inline TaskHandle_t handle{NULL}; \
44 static void create(TaskFunction_t f = function) { \
45 handle = xTaskCreateStaticPinnedToCore(function = f, \
46 name, \
47 size(stack), \
48 NULL, \
49 priority, \
50 data(stack), \
51 &tcb, \
52 core_id); \
53 } \
54 static void destroy() { \
55 vTaskDelete(handle); \
56 handle = NULL; \
57 } \
58 static void resume() { vTaskResume(handle); } \
59 static void suspend() { vTaskSuspend(handle); }
60
69#define TASK(OBJECT, NAME, STACK_SIZE, PRIORITY, CORE_ID, TIMEOUT) \
70 struct CONCAT(Task, __LINE__) { \
71 static inline std::array<StackType_t, STACK_SIZE> stack{}; \
72 COMMON_TASK_MEMBERS(NAME, PRIORITY, CORE_ID, TIMEOUT) \
73 } OBJECT
74
82#define SHARED_TASK(OBJECT, NAME, PRIORITY, CORE_ID, TIMEOUT) \
83 struct CONCAT(SharedTask, __LINE__) { \
84 COMMON_TASK_MEMBERS(NAME, PRIORITY, CORE_ID, TIMEOUT) \
85 } OBJECT