Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
FrameworkTask Class Referenceabstract

Base class for FreeRTOS-aware tasks in the framework. More...

#include <FrameworkTask.h>

+ Inheritance diagram for FrameworkTask:
+ Collaboration diagram for FrameworkTask:

Public Member Functions

 FrameworkTask (const char *name, uint16_t stackSize=1024, UBaseType_t priority=1)
 Constructor.
 
virtual ~FrameworkTask ()
 Destructor.
 
virtual void onEvent (const Event &event)
 Called when the task receives an event.
 
bool start ()
 Starts the task via FreeRTOS.
 
void suspend ()
 Suspends the task using vTaskSuspend().
 
void resume ()
 Resumes the task using vTaskResume().
 
TaskHandle_t getHandle () const
 Returns the FreeRTOS task handle.
 
const char * getName () const
 Returns the task name.
 
void notify (uint8_t index, uint32_t value=1)
 Sends a notification to this task using an index.
 
void notify (Notification n, uint32_t value=1)
 Sends a notification using a framework-defined enum.
 
void notifyFromISR (uint8_t index, uint32_t value=1, BaseType_t *pxHigherPriorityTaskWoken=nullptr)
 Sends a notification from an ISR (by index).
 
void notifyFromISR (Notification n, uint32_t value=1, BaseType_t *pxHigherPriorityTaskWoken=nullptr)
 Sends a notification from ISR using enum identifier.
 
bool waitFor (uint8_t index, TickType_t timeout=portMAX_DELAY)
 Waits for a notification (by index).
 
bool waitFor (Notification n, TickType_t timeout=portMAX_DELAY)
 Waits for a notification (by enum identifier).
 
Notification waitForAny (uint8_t index, uint32_t mask, TickType_t timeout=portMAX_DELAY)
 waits for any notification matching the given mask.
 

Protected Member Functions

virtual void run ()=0
 Main task loop.
 
uint32_t waitFor (TickType_t timeout=portMAX_DELAY)
 Wait for any notification (default index).
 
bool createQueue (size_t itemSize, size_t length)
 Creates an internal FreeRTOS queue.
 
bool sendToQueue (const void *item, TickType_t timeout=0)
 Sends an item to the internal queue.
 
bool receiveFromQueue (void *item, TickType_t timeout=portMAX_DELAY)
 Receives an item from the internal queue.
 

Protected Attributes

const char * _name
 
uint16_t _stackSize
 
UBaseType_t _priority
 
TaskHandle_t _handle = nullptr
 
QueueHandle_t _queue = nullptr
 

Static Private Member Functions

static void taskEntry (void *pvParams)
 

Detailed Description

Subclass this to define application or system-level tasks.

  • Override run() to define the main task loop.
  • Optionally override onEvent() to receive posted events.

Supports:

  • Indexed task notifications (including from ISR)
  • Optional message queue for passing data
  • Integration with the framework event system

Definition at line 41 of file FrameworkTask.h.

Constructor & Destructor Documentation

◆ FrameworkTask()

FrameworkTask::FrameworkTask ( const char *  name,
uint16_t  stackSize = 1024,
UBaseType_t  priority = 1 
)

Constructor.

Parameters
nameTask name (used by FreeRTOS).
stackSizeStack size in words.
priorityTask priority.
Parameters
nameTask name (used by FreeRTOS).
stackSizeStack size in words.
priorityTask priority.

Definition at line 18 of file FrameworkTask.cpp.

19 : _name(name), _stackSize(stackSize), _priority(priority) {}
UBaseType_t _priority
const char * _name
uint16_t _stackSize

◆ ~FrameworkTask()

FrameworkTask::~FrameworkTask ( )
virtual

Destructor.

Frees the task and its queue (if allocated).

Frees the task and its queue (if allocated).

Definition at line 22 of file FrameworkTask.cpp.

23{
24 if (_handle)
25 {
26 vTaskDelete(_handle);
27 }
28 if (_queue)
29 {
30 vQueueDelete(_queue);
31 }
32}
QueueHandle_t _queue
TaskHandle_t _handle

References _handle, and _queue.

Member Function Documentation

◆ createQueue()

bool FrameworkTask::createQueue ( size_t  itemSize,
size_t  length 
)
protected

Creates an internal FreeRTOS queue.

Parameters
itemSizeSize of each message item.
lengthNumber of slots in the queue.
Returns
true if queue was successfully created.
Parameters
itemSizeSize of each message item.
lengthNumber of slots in the queue.
Returns
true if queue was successfully created.

Definition at line 141 of file FrameworkTask.cpp.

142{
143 _queue = xQueueCreate(length, itemSize);
144 return _queue != nullptr;
145}

References _queue.

◆ getHandle()

TaskHandle_t FrameworkTask::getHandle ( ) const

Returns the FreeRTOS task handle.

Definition at line 62 of file FrameworkTask.cpp.

63{
64 return _handle;
65}

References _handle.

◆ getName()

const char * FrameworkTask::getName ( ) const
inline

Definition at line 93 of file FrameworkTask.h.

93{ return _name; }

References _name.

Referenced by FrameworkController::getName().

+ Here is the caller graph for this function:

◆ notify() [1/2]

void FrameworkTask::notify ( Notification  n,
uint32_t  value = 1 
)

Sends a notification to this task using an index.

Parameters
indexNotification slot index (0–7).
valueValue to send (default is 1).

Definition at line 74 of file FrameworkTask.cpp.

75{
76 notify(n.code(), value);
77}
void notify(uint8_t index, uint32_t value=1)
Sends a notification to this task using an index.
uint8_t code() const
Get the notification code.

References Notification::code(), and notify().

+ Here is the call graph for this function:

◆ notify() [2/2]

void FrameworkTask::notify ( uint8_t  index,
uint32_t  value = 1 
)

Sends a notification to this task using an index.

Parameters
indexNotification slot index (0–7).
valueValue to send (default is 1).
Parameters
indexNotification slot index (0–7).
valueValue to send (default is 1).

Definition at line 68 of file FrameworkTask.cpp.

69{
70 xTaskNotifyIndexed(_handle, index, value, eSetValueWithOverwrite);
71}

References _handle.

Referenced by notify().

+ Here is the caller graph for this function:

◆ notifyFromISR() [1/2]

void FrameworkTask::notifyFromISR ( Notification  n,
uint32_t  value = 1,
BaseType_t *  pxHigherPriorityTaskWoken = nullptr 
)

Sends a notification from an ISR (by index).

Definition at line 86 of file FrameworkTask.cpp.

87{
88 notifyFromISR(n.code(), value, pxHigherPriorityTaskWoken);
89}
void notifyFromISR(uint8_t index, uint32_t value=1, BaseType_t *pxHigherPriorityTaskWoken=nullptr)
Sends a notification from an ISR (by index).

References Notification::code(), and notifyFromISR().

+ Here is the call graph for this function:

◆ notifyFromISR() [2/2]

void FrameworkTask::notifyFromISR ( uint8_t  index,
uint32_t  value = 1,
BaseType_t *  pxHigherPriorityTaskWoken = nullptr 
)

Sends a notification from an ISR (by index).

Definition at line 80 of file FrameworkTask.cpp.

81{
82 xTaskNotifyIndexedFromISR(_handle, index, value, eSetValueWithOverwrite, pxHigherPriorityTaskWoken);
83}

References _handle.

Referenced by notifyFromISR().

+ Here is the caller graph for this function:

◆ onEvent()

virtual void FrameworkTask::onEvent ( const Event event)
inlinevirtual

Override to react to framework events. This is called by the event manager. Default implementation does nothing.

Reimplemented in App, FrameworkController, and FrameworkManager.

Definition at line 64 of file FrameworkTask.h.

64{}

◆ receiveFromQueue()

bool FrameworkTask::receiveFromQueue ( void *  item,
TickType_t  timeout = portMAX_DELAY 
)
protected

Receives an item from the internal queue.

Definition at line 154 of file FrameworkTask.cpp.

155{
156 return xQueueReceive(_queue, item, timeout) == pdTRUE;
157}

References _queue.

◆ resume()

void FrameworkTask::resume ( )

Resumes the task using vTaskResume().

Definition at line 55 of file FrameworkTask.cpp.

56{
57 if (_handle)
58 vTaskResume(_handle);
59}

References _handle.

◆ run()

virtual void FrameworkTask::run ( )
protectedpure virtual

Must be implemented by subclasses.

Implemented in FrameworkController.

Referenced by taskEntry().

+ Here is the caller graph for this function:

◆ sendToQueue()

bool FrameworkTask::sendToQueue ( const void *  item,
TickType_t  timeout = 0 
)
protected

Sends an item to the internal queue.

Definition at line 148 of file FrameworkTask.cpp.

149{
150 return xQueueSend(_queue, item, timeout) == pdTRUE;
151}

References _queue.

◆ start()

bool FrameworkTask::start ( )

Starts the task via FreeRTOS.

Internally calls xTaskCreate(). The task runs run() when scheduled.

Returns
true if task was successfully created.

Internally calls xTaskCreate(). The task runs run() when scheduled.

Returns
true if task was successfully created.

Definition at line 35 of file FrameworkTask.cpp.

36{
37 return xTaskCreate(taskEntry, _name, _stackSize, this, _priority, &_handle) == pdPASS;
38}
static void taskEntry(void *pvParams)

References _handle, _name, _priority, _stackSize, and taskEntry().

Referenced by FrameworkApp::FrameworkApp(), App::onStart(), and FrameworkApp::start().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ suspend()

void FrameworkTask::suspend ( )

Suspends the task using vTaskSuspend().

Definition at line 48 of file FrameworkTask.cpp.

49{
50 if (_handle)
51 vTaskSuspend(_handle);
52}

References _handle.

◆ taskEntry()

void FrameworkTask::taskEntry ( void *  pvParams)
staticprivate

Definition at line 41 of file FrameworkTask.cpp.

42{
43 static_cast<FrameworkTask *>(pvParams)->run();
44 vTaskDelete(nullptr); // Clean up after run ends
45}
Base class for FreeRTOS-aware tasks in the framework.
virtual void run()=0
Main task loop.

References run().

Referenced by start().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ waitFor() [1/3]

bool FrameworkTask::waitFor ( Notification  n,
TickType_t  timeout = portMAX_DELAY 
)

Waits for a notification (by index).

Parameters
indexNotification index to wait for.
timeoutTimeout in ticks.
Returns
true if notified, false on timeout.

Definition at line 99 of file FrameworkTask.cpp.

100{
101 return waitFor(n.code(), timeout);
102}
bool waitFor(uint8_t index, TickType_t timeout=portMAX_DELAY)
Waits for a notification (by index).

References Notification::code(), and waitFor().

+ Here is the call graph for this function:

◆ waitFor() [2/3]

uint32_t FrameworkTask::waitFor ( TickType_t  timeout = portMAX_DELAY)
protected

Waits for a notification (by index).

Parameters
indexNotification index to wait for.
timeoutTimeout in ticks.
Returns
true if notified, false on timeout.

Definition at line 105 of file FrameworkTask.cpp.

106{
107 uint32_t value = 0;
108 xTaskNotifyWaitIndexed(0, 0, UINT32_MAX, &value, timeout);
109 return value;
110}

◆ waitFor() [3/3]

bool FrameworkTask::waitFor ( uint8_t  index,
TickType_t  timeout = portMAX_DELAY 
)

Waits for a notification (by index).

Parameters
indexNotification index to wait for.
timeoutTimeout in ticks.
Returns
true if notified, false on timeout.
Parameters
indexNotification index to wait for.
timeoutTimeout in ticks.
Returns
true if notified, false on timeout.

Definition at line 92 of file FrameworkTask.cpp.

93{
94 uint32_t value;
95 return xTaskNotifyWaitIndexed(index, 0, UINT32_MAX, &value, timeout) == pdTRUE;
96}

Referenced by waitFor().

+ Here is the caller graph for this function:

◆ waitForAny()

Notification FrameworkTask::waitForAny ( uint8_t  index,
uint32_t  mask,
TickType_t  timeout = portMAX_DELAY 
)

This is useful for waiting on multiple notification types.

Parameters
indexNotification index to wait for.

Definition at line 113 of file FrameworkTask.cpp.

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}
SystemNotification
System-defined notification types reserved by the framework.
A tagged union representing either a system or user-defined notification.
SystemNotification system
NotificationKind kind

References Notification::kind, System, Notification::system, and WaitForTimeout.

Member Data Documentation

◆ _handle

TaskHandle_t FrameworkTask::_handle = nullptr
protected

Definition at line 179 of file FrameworkTask.h.

Referenced by getHandle(), notify(), notifyFromISR(), resume(), start(), suspend(), and ~FrameworkTask().

◆ _name

const char* FrameworkTask::_name
protected

Definition at line 176 of file FrameworkTask.h.

Referenced by getName(), and start().

◆ _priority

UBaseType_t FrameworkTask::_priority
protected

Definition at line 178 of file FrameworkTask.h.

Referenced by start().

◆ _queue

QueueHandle_t FrameworkTask::_queue = nullptr
protected

Definition at line 180 of file FrameworkTask.h.

Referenced by createQueue(), receiveFromQueue(), sendToQueue(), and ~FrameworkTask().

◆ _stackSize

uint16_t FrameworkTask::_stackSize
protected

Definition at line 177 of file FrameworkTask.h.

Referenced by start().


The documentation for this class was generated from the following files: