Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
FrameworkController.h
Go to the documentation of this file.
1
32#pragma once
33
34#include "FrameworkTask.h"
35#include <unordered_map>
36#include <functional>
37#include <string>
38#include <FreeRTOS.h>
39#include <task.h>
40#include "events/Event.h"
41#include "http/Router.h"
42
54{
55public:
72 FrameworkController(const char* name, Router& sharedRouter, uint16_t stackSize = 1024, UBaseType_t priority = tskIDLE_PRIORITY + 1);
73
82 void run() override final;
83
89 const char *getName() const { return FrameworkTask::getName(); }
90
91protected:
97 virtual void onStart();
98
105 virtual void initRoutes();
106
112 virtual void onEvent(const Event &event);
113
114
119 virtual TickType_t getPollIntervalTicks() {
120 return pdMS_TO_TICKS(100); // Default: 100ms
121 }
122
128 virtual void poll();
129
137 void runEvery(uint32_t intervalMs, const std::function<void()> &fn, const char *id);
138
140
141public:
142
143 // --- Event queue management ---
152 void enableEventQueue(size_t depth = 8) {
153 if (!eventQueue_) {
154 eventQueue_ = xQueueCreate(depth, sizeof(Event));
155 }
156 }
163 QueueHandle_t getEventQueue() const {
164 return eventQueue_;
165 }
166
172 bool getNextEvent(Event& event, uint32_t timeoutMs) {
173 return eventQueue_ && xQueueReceive(eventQueue_, &event, pdMS_TO_TICKS(timeoutMs)) == pdTRUE;
174 }
175
176
177private:
178 std::unordered_map<std::string, TickType_t> _timers;
179
180 QueueHandle_t eventQueue_ = nullptr; // FreeRTOS queue for pending events
181
189 void waitAndDispatch(uint32_t timeoutMs = portMAX_DELAY);
190};
191
192// --- Timing convenience macro ---
193
199#define STRINGIFY_DETAIL(x) #x
200#define STRINGIFY(x) STRINGIFY_DETAIL(x)
201#define RUN_EVERY(ms, fn) runEvery(ms, fn, __FUNCTION__ ":" STRINGIFY(__LINE__))
Defines the Event structure and related utilities for event messaging.
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...
Base class for event-driven control logic in embedded applications.
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().
Router & router
Handles path-to-handler mapping - reference to shared Router instance.
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.
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.
void run() override final
Main task loop.
virtual void initRoutes()
Initialize routes for this controller.
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.
const char * getName() const
Returns the task name.
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