Base class for event-driven control logic in embedded applications. More...
#include <FrameworkController.h>
Public Member Functions | |
FrameworkController (const char *name, Router &sharedRouter, uint16_t stackSize=1024, UBaseType_t priority=tskIDLE_PRIORITY+1) | |
Constructor. | |
void | run () override final |
Main task loop. | |
const char * | getName () const |
Get the name of this controller. | |
void | enableEventQueue (size_t depth=8) |
Enable the event queue for this controller. | |
QueueHandle_t | getEventQueue () const |
Get the event queue for this controller. | |
bool | getNextEvent (Event &event, uint32_t timeoutMs) |
Check if there are any pending events in the queue. | |
![]() | |
FrameworkTask (const char *name, uint16_t stackSize=1024, UBaseType_t priority=1) | |
Constructor. | |
virtual | ~FrameworkTask () |
Destructor. | |
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 | onStart () |
Called once at task start before entering the main loop. | |
virtual void | initRoutes () |
Initialize routes for this controller. | |
virtual void | onEvent (const Event &event) |
Called when an event is dispatched to this controller. | |
virtual TickType_t | getPollIntervalTicks () |
Returns the polling interval in ticks used in run(). | |
virtual void | poll () |
Called during every loop iteration for non-blocking background logic. | |
void | runEvery (uint32_t intervalMs, const std::function< void()> &fn, const char *id) |
Run a function periodically with millisecond resolution. | |
![]() | |
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 | |
Router & | router |
Handles path-to-handler mapping - reference to shared Router instance. | |
![]() | |
const char * | _name |
uint16_t | _stackSize |
UBaseType_t | _priority |
TaskHandle_t | _handle = nullptr |
QueueHandle_t | _queue = nullptr |
Private Member Functions | |
void | waitAndDispatch (uint32_t timeoutMs=portMAX_DELAY) |
Waits for an event and dispatches it to onEvent() if applicable. | |
Private Attributes | |
std::unordered_map< std::string, TickType_t > | _timers |
Stores last-execution timestamps per ID. | |
QueueHandle_t | eventQueue_ = nullptr |
FrameworkController builds on FrameworkTask to offer structured hooks:
onStart()
– called once at task startonEvent()
– called when an event is receivedpoll()
– called periodically for background workIt also includes a utility runEvery()
to call a function at regular intervals.
Definition at line 53 of file FrameworkController.h.
FrameworkController::FrameworkController | ( | const char * | name, |
Router & | sharedRouter, | ||
uint16_t | stackSize = 1024 , |
||
UBaseType_t | priority = tskIDLE_PRIORITY + 1 |
||
) |
name | Task name. |
stackSize | Stack size in words. |
priority | Task priority (default is one above idle). |
Constructor for framework controllers.
name | Task name. |
sharedRouter | Reference to the shared Router instance. |
stackSize | Task stack size (in words). |
priority | Task priority. |
Definition at line 39 of file FrameworkController.cpp.
|
inline |
Creates a FreeRTOS queue to hold events if it doesn't already exist. The queue depth can be specified, defaulting to 8.
depth | Maximum number of events in the queue. |
Definition at line 152 of file FrameworkController.h.
References eventQueue_.
Referenced by run().
|
inline |
Returns the FreeRTOS queue handle used to hold events. This queue is used to post events from other tasks or ISRs.
Definition at line 163 of file FrameworkController.h.
References eventQueue_.
|
inline |
Definition at line 89 of file FrameworkController.h.
References FrameworkTask::getName().
|
inline |
controller | Pointer to the controller to check. |
Definition at line 172 of file FrameworkController.h.
References eventQueue_.
Referenced by waitAndDispatch().
|
inlineprotectedvirtual |
Override this in subclasses if different polling frequency is needed.
Definition at line 119 of file FrameworkController.h.
Referenced by run().
|
protectedvirtual |
@bcopydoc FrameworkController::initRoutes
Override to define HTTP routes or other event handlers. This is called once at task start.
Reimplemented in FrameworkApp, App, DashboardController, and GpioController.
Definition at line 64 of file FrameworkController.cpp.
Referenced by run().
|
protectedvirtual |
Called when an event is dispatched to this controller.
Override to implement your event-driven logic.
Override to implement your event-driven logic.
Reimplemented from FrameworkTask.
Reimplemented in App, and FrameworkManager.
Definition at line 71 of file FrameworkController.cpp.
Referenced by waitAndDispatch().
|
protectedvirtual |
Called once at task start before entering the main loop.
Override this to initialize controller state.
Override this to initialize controller state.
Reimplemented in FrameworkManager, FrameworkApp, and App.
Definition at line 57 of file FrameworkController.cpp.
Referenced by FrameworkApp::onStart(), and run().
|
protectedvirtual |
Called during every loop iteration for non-blocking background logic.
Runs after waitAndDispatch() — useful for polling sensors or internal FSMs.
Runs after waitAndDispatch() — useful for polling sensors or internal FSMs.
Reimplemented in FrameworkManager, and App.
Definition at line 77 of file FrameworkController.cpp.
Referenced by run().
|
finaloverridevirtual |
Main task loop.
Calls onStart()
once, then enters a loop that:
onEvent()
poll()
to perform non-blocking logicCalls onStart()
once, then enters a loop that:
onEvent()
poll()
to perform non-blocking logicImplements FrameworkTask.
Definition at line 44 of file FrameworkController.cpp.
References enableEventQueue(), getPollIntervalTicks(), initRoutes(), onStart(), poll(), and waitAndDispatch().
|
protected |
Run a function periodically with millisecond resolution.
intervalMs | Time interval in milliseconds. |
fn | Function to run. |
id | Unique ID for this timed function (used to track last execution). |
intervalMs | Time interval in milliseconds. |
fn | Function to run. |
id | Unique ID for this timed function (used to track last execution). |
Definition at line 93 of file FrameworkController.cpp.
References _timers.
Referenced by App::poll().
|
private |
Waits for an event and dispatches it to onEvent()
if applicable.
If the event is targeted at this controller (or has no target), it will be passed to onEvent()
.
timeoutMs | Maximum time to wait for an event in milliseconds. |
If the event is targeted at this controller (or has no target), it will be passed to onEvent()
.
timeoutMs | Maximum time to wait for an event in milliseconds. |
Definition at line 83 of file FrameworkController.cpp.
References getNextEvent(), and onEvent().
Referenced by run().
|
private |
Definition at line 178 of file FrameworkController.h.
Referenced by runEvery().
|
private |
Definition at line 180 of file FrameworkController.h.
Referenced by enableEventQueue(), getEventQueue(), and getNextEvent().
|
protected |
Definition at line 139 of file FrameworkController.h.
Referenced by DashboardController::initRoutes(), and GpioController::initRoutes().