Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
FrameworkTask.cpp
Go to the documentation of this file.
1
16
18FrameworkTask::FrameworkTask(const char *name, uint16_t stackSize, UBaseType_t priority)
19 : _name(name), _stackSize(stackSize), _priority(priority) {}
20
23{
24 if (_handle)
25 {
26 vTaskDelete(_handle);
27 }
28 if (_queue)
29 {
30 vQueueDelete(_queue);
31 }
32}
33
36{
37 return xTaskCreate(taskEntry, _name, _stackSize, this, _priority, &_handle) == pdPASS;
38}
39
41void FrameworkTask::taskEntry(void *pvParams)
42{
43 static_cast<FrameworkTask *>(pvParams)->run();
44 vTaskDelete(nullptr); // Clean up after run ends
45}
46
49{
50 if (_handle)
51 vTaskSuspend(_handle);
52}
53
56{
57 if (_handle)
58 vTaskResume(_handle);
59}
60
62TaskHandle_t FrameworkTask::getHandle() const
63{
64 return _handle;
65}
66
68void FrameworkTask::notify(uint8_t index, uint32_t value)
69{
70 xTaskNotifyIndexed(_handle, index, value, eSetValueWithOverwrite);
71}
72
74void FrameworkTask::notify(Notification n, uint32_t value)
75{
76 notify(n.code(), value);
77}
78
80void FrameworkTask::notifyFromISR(uint8_t index, uint32_t value, BaseType_t *pxHigherPriorityTaskWoken)
81{
82 xTaskNotifyIndexedFromISR(_handle, index, value, eSetValueWithOverwrite, pxHigherPriorityTaskWoken);
83}
84
86void FrameworkTask::notifyFromISR(Notification n, uint32_t value, BaseType_t *pxHigherPriorityTaskWoken)
87{
88 notifyFromISR(n.code(), value, pxHigherPriorityTaskWoken);
89}
90
92bool FrameworkTask::waitFor(uint8_t index, TickType_t timeout)
93{
94 uint32_t value;
95 return xTaskNotifyWaitIndexed(index, 0, UINT32_MAX, &value, timeout) == pdTRUE;
96}
97
99bool FrameworkTask::waitFor(Notification n, TickType_t timeout)
100{
101 return waitFor(n.code(), timeout);
102}
103
105uint32_t FrameworkTask::waitFor(TickType_t timeout)
106{
107 uint32_t value = 0;
108 xTaskNotifyWaitIndexed(0, 0, UINT32_MAX, &value, timeout);
109 return value;
110}
111
113Notification FrameworkTask::waitForAny(uint8_t index, uint32_t mask, TickType_t timeout)
114{
115 uint32_t value = 0;
116 if (xTaskNotifyWaitIndexed(index, 0, mask, &value, timeout) == pdTRUE && (value & mask))
117 {
118 Notification n;
120
121 // Find the *first* matching bit set — basic scan
122 for (uint8_t i = 0; i < 32; ++i)
123 {
124 if (value & (1u << i))
125 {
126 n.system = static_cast<SystemNotification>(i);
127 break;
128 }
129 }
130
131 return n;
132 }
133
134 Notification n;
137 return n;
138}
139
141bool FrameworkTask::createQueue(size_t itemSize, size_t length)
142{
143 _queue = xQueueCreate(length, itemSize);
144 return _queue != nullptr;
145}
146
148bool FrameworkTask::sendToQueue(const void *item, TickType_t timeout)
149{
150 return xQueueSend(_queue, item, timeout) == pdTRUE;
151}
152
154bool FrameworkTask::receiveFromQueue(void *item, TickType_t timeout)
155{
156 return xQueueReceive(_queue, item, timeout) == pdTRUE;
157}
FreeRTOS-aware task abstraction with built-in notification and queue support.
SystemNotification
System-defined notification types reserved by the framework.
Base class for FreeRTOS-aware tasks in the framework.
void notifyFromISR(uint8_t index, uint32_t value=1, BaseType_t *pxHigherPriorityTaskWoken=nullptr)
Sends a notification from an ISR (by index).
virtual ~FrameworkTask()
Destructor.
TaskHandle_t getHandle() const
Returns the FreeRTOS task handle.
void resume()
Resumes the task using vTaskResume().
void notify(uint8_t index, uint32_t value=1)
Sends a notification to this task using an index.
QueueHandle_t _queue
void suspend()
Suspends the task using vTaskSuspend().
bool waitFor(uint8_t index, TickType_t timeout=portMAX_DELAY)
Waits for a notification (by index).
UBaseType_t _priority
const char * _name
uint16_t _stackSize
bool createQueue(size_t itemSize, size_t length)
Creates an internal FreeRTOS queue.
static void taskEntry(void *pvParams)
Notification waitForAny(uint8_t index, uint32_t mask, TickType_t timeout=portMAX_DELAY)
waits for any notification matching the given mask.
bool start()
Starts the task via FreeRTOS.
virtual void run()=0
Main task loop.
bool receiveFromQueue(void *item, TickType_t timeout=portMAX_DELAY)
Receives an item from the internal queue.
FrameworkTask(const char *name, uint16_t stackSize=1024, UBaseType_t priority=1)
Constructor.
TaskHandle_t _handle
bool sendToQueue(const void *item, TickType_t timeout=0)
Sends an item to the internal queue.
A tagged union representing either a system or user-defined notification.
SystemNotification system
NotificationKind kind
uint8_t code() const
Get the notification code.