Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
FrameworkTask.h
Go to the documentation of this file.
1
19#ifndef FREERTOS_TASK_H
20#define FREERTOS_TASK_H
21#pragma once
22
23#include <FreeRTOS.h>
24#include <task.h>
25#include <queue.h>
26#include "events/Event.h"
27#include "events/Notification.h"
28
42{
43public:
51 FrameworkTask(const char *name, uint16_t stackSize = 1024, UBaseType_t priority = 1);
52
56 virtual ~FrameworkTask();
57
64 virtual void onEvent(const Event &event) {}
65
73 bool start();
74
78 void suspend();
79
83 void resume();
84
88 TaskHandle_t getHandle() const;
89
93 const char *getName() const { return _name; }
94
95 // ----------- Notification Support -----------
96
103 void notify(uint8_t index, uint32_t value = 1);
104
108 void notify(Notification n, uint32_t value = 1);
109
113 void notifyFromISR(uint8_t index, uint32_t value = 1, BaseType_t *pxHigherPriorityTaskWoken = nullptr);
114
118 void notifyFromISR(Notification n, uint32_t value = 1, BaseType_t *pxHigherPriorityTaskWoken = nullptr);
119
127 bool waitFor(uint8_t index, TickType_t timeout = portMAX_DELAY);
128
132 bool waitFor(Notification n, TickType_t timeout = portMAX_DELAY);
133
140 Notification waitForAny(uint8_t index, uint32_t mask, TickType_t timeout = portMAX_DELAY);
141
142
143protected:
147 virtual void run() = 0;
148
152 uint32_t waitFor(TickType_t timeout = portMAX_DELAY);
153
154 // ----------- Optional Queue Support -----------
155
163 bool createQueue(size_t itemSize, size_t length);
164
168 bool sendToQueue(const void *item, TickType_t timeout = 0);
169
173 bool receiveFromQueue(void *item, TickType_t timeout = portMAX_DELAY);
174
175protected:
176 const char *_name;
177 uint16_t _stackSize;
178 UBaseType_t _priority;
179 TaskHandle_t _handle = nullptr;
180 QueueHandle_t _queue = nullptr;
181
182private:
183 static void taskEntry(void *pvParams);
184};
185
186#endif // FREERTOS_TASK_H
Defines the Event structure and related utilities for event messaging.
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.
virtual void onEvent(const Event &event)
Called when the task receives an event.
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().
const char * getName() const
Returns the task name.
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.
TaskHandle_t _handle
bool sendToQueue(const void *item, TickType_t timeout=0)
Sends an item to the internal queue.
Represents a framework event, optionally carrying payload data.
Definition Event.h:24
A tagged union representing either a system or user-defined notification.