Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
FrameworkController.cpp
Go to the documentation of this file.
1
35#include "events/EventManager.h"
36#include "events/Event.h"
37#include "http/Router.h"
38
39FrameworkController::FrameworkController(const char *name, Router &sharedRouter, uint16_t stackSize, UBaseType_t priority)
40 : FrameworkTask(name, stackSize, priority),
41 router(sharedRouter) {}
42
45{
46 enableEventQueue(); // MUST be here to initialize queue before use
47 initRoutes(); // Call initRoutes() to set up routes
48 onStart(); // Call onStart() to initialize controller state
49 while (true)
50 {
51 waitAndDispatch(getPollIntervalTicks()); // Wait for notifications or timeout
52 poll(); // Call user logic
53 }
54}
55
58{
59 // can safely be overridden in derived classes
60 // Default implementation does nothing
61}
62
65{
66 // Default implementation does nothing
67 // Override in derived classes to set up routes
68}
69
72{
73 // Default: do nothing
74}
75
78{
79 // Default no-op
80}
81
84{
85 Event event;
86 if (getNextEvent(event, timeoutMs))
87 {
88 onEvent(event);
89 }
90}
91
93void FrameworkController::runEvery(uint32_t intervalMs, const std::function<void()> &fn, const char *id)
94{
95 TickType_t now = xTaskGetTickCount();
96 TickType_t &last = _timers[std::string(id)];
97
98 if ((now - last) >= pdMS_TO_TICKS(intervalMs))
99 {
100 fn();
101 last = now;
102 }
103}
104
Event pub/sub manager for embedded applications using FreeRTOS.
Defines the Event structure and related utilities for event messaging.
The FrameworkController class for event-driven control logic in embedded applications.
FreeRTOS-aware task abstraction with built-in notification and queue support.
HTTP routing with middleware and optional JWT-based authorization. Part of the PicoFramework HTTP ser...
void runEvery(uint32_t intervalMs, const std::function< void()> &fn, const char *id)
Run a function periodically with millisecond resolution.
virtual TickType_t getPollIntervalTicks()
Returns the polling interval in ticks used in run().
bool getNextEvent(Event &event, uint32_t timeoutMs)
Check if there are any pending events in the queue.
virtual void onStart()
Called once at task start before entering the main loop.
virtual void onEvent(const Event &event)
Called when an event is dispatched to this controller.
void enableEventQueue(size_t depth=8)
Enable the event queue for this controller.
void run() override final
Main task loop.
virtual void initRoutes()
Initialize routes for this controller.
FrameworkController(const char *name, Router &sharedRouter, uint16_t stackSize=1024, UBaseType_t priority=tskIDLE_PRIORITY+1)
Constructor.
virtual void poll()
Called during every loop iteration for non-blocking background logic.
void waitAndDispatch(uint32_t timeoutMs=portMAX_DELAY)
Waits for an event and dispatches it to onEvent() if applicable.
std::unordered_map< std::string, TickType_t > _timers
Stores last-execution timestamps per ID.
Base class for FreeRTOS-aware tasks in the framework.
The central router for handling HTTP requests and middleware.
Definition Router.h:60
Represents a framework event, optionally carrying payload data.
Definition Event.h:24