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

@fileGpioController.cpp More...

#include <GpioController.h>

+ Inheritance diagram for GpioController:
+ Collaboration diagram for GpioController:

Public Member Functions

 GpioController (Router &r, PicoModel &pico)
 
void initRoutes () override
 Initialize routes for this controller.
 
- Public Member Functions inherited from FrameworkController
 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.
 
- Public Member Functions inherited from FrameworkTask
 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.
 

Private Member Functions

void getState (HttpRequest &req, HttpResponse &res, const std::vector< std::string > &params)
 
void setState (HttpRequest &req, HttpResponse &res, const std::vector< std::string > &params)
 
void handleGetMultipleGpios (HttpRequest &req, HttpResponse &res)
 

Private Attributes

PicoModelpico
 

Additional Inherited Members

- Protected Member Functions inherited from FrameworkController
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.
 
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.
 
- Protected Member Functions inherited from FrameworkTask
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 inherited from FrameworkController
Routerrouter
 Handles path-to-handler mapping - reference to shared Router instance.
 
- Protected Attributes inherited from FrameworkTask
const char * _name
 
uint16_t _stackSize
 
UBaseType_t _priority
 
TaskHandle_t _handle = nullptr
 
QueueHandle_t _queue = nullptr
 

Detailed Description

GPIO controller for handling GPIO state retrieval and setting. This controller provides HTTP endpoints to get and set GPIO states.

Version
0.1
Date
2025-04-14
License:\n MIT License

Definition at line 24 of file GpioController.h.

Constructor & Destructor Documentation

◆ GpioController()

GpioController::GpioController ( Router r,
PicoModel pico 
)

Definition at line 9 of file GpioController.cpp.

10 : FrameworkController("GpioController", r, 1024, 1), pico(pico) {}
Base class for event-driven control logic in embedded applications.
PicoModel & pico

Member Function Documentation

◆ getState()

void GpioController::getState ( HttpRequest req,
HttpResponse res,
const std::vector< std::string > &  params 
)
private

Definition at line 30 of file GpioController.cpp.

30 {
31 int pin = std::stoi(params[0]);
32 bool state = pico.getGpioState(pin);
33 res.json({{"pin", pin}, {"state", state ? 1 : 0}});
34}
HttpResponse & json(const std::string &jsonString)
Send a JSON string/object with correct content type.
bool getGpioState(int pin)
Definition PicoModel.cpp:53

References PicoModel::getGpioState(), HttpResponse::json(), and pico.

Referenced by initRoutes().

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

◆ handleGetMultipleGpios()

void GpioController::handleGetMultipleGpios ( HttpRequest req,
HttpResponse res 
)
private

Definition at line 43 of file GpioController.cpp.

44{
45 auto queryParams = req.getQueryParams();
46 if (queryParams.empty())
47 {
49 return;
50 }
51
52 json response = json::array();
53 bool foundPinParam = false;
54
55 for (const auto& param : queryParams)
56 {
57 if (param.first == "pin")
58 {
59 foundPinParam = true;
60 int pin = atoi(param.second.c_str());
61
62 int state = pico.getGpioState(pin);
63
64 json pinState = {
65 {"pin", pin},
66 {"state", state}
67 };
68
69 response.push_back(pinState);
70 }
71 }
72
73 if (!foundPinParam)
74 {
76 return;
77 }
78
79 res.json(response);
80}
nlohmann::json json
const std::unordered_multimap< std::string, std::string > getQueryParams()
Get parsed query string parameters.
void sendNoContent(HttpResponse &res)

References PicoModel::getGpioState(), HttpRequest::getQueryParams(), HttpResponse::json(), pico, and JsonResponse::sendNoContent().

Referenced by initRoutes().

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

◆ initRoutes()

void GpioController::initRoutes ( )
overridevirtual

@bcopydoc FrameworkController::initRoutes

Override to define HTTP routes or other event handlers. This is called once at task start.

Reimplemented from FrameworkController.

Definition at line 12 of file GpioController.cpp.

12 {
13
14 printf("[GpioController] Initializing GPIO routes...\n");
15
16 router.addRoute("GET", "/api/v1/gpio/{pin}", [this](HttpRequest& req, HttpResponse& res, const RouteMatch& match) {
17 getState(req, res, match.ordered);
18 });
19
20 router.addRoute("GET", "/api/v1/gpios", [this](HttpRequest& req, HttpResponse& res, const RouteMatch& match) {
21 handleGetMultipleGpios(req, res);
22 });
23
24
25 router.addRoute("POST", "/api/v1/gpio/{pin}/{value}", [this](HttpRequest& req, HttpResponse& res, const RouteMatch& match) {
26 setState(req, res, match.ordered);
27 });
28}
Router & router
Handles path-to-handler mapping - reference to shared Router instance.
void handleGetMultipleGpios(HttpRequest &req, HttpResponse &res)
void getState(HttpRequest &req, HttpResponse &res, const std::vector< std::string > &params)
void setState(HttpRequest &req, HttpResponse &res, const std::vector< std::string > &params)
Forward declaration for potential routing needs.
Definition HttpRequest.h:32
Represents an HTTP response object.
void addRoute(const std::string &method, const std::string &path, RouteHandler handler, std::vector< Middleware > middleware={})
Register a route with optional middleware.
Definition Router.cpp:144
Represents a match of a route against an incoming HTTP request.
Definition RouteTypes.h:18
std::vector< std::string > ordered
Definition RouteTypes.h:19

References Router::addRoute(), getState(), handleGetMultipleGpios(), RouteMatch::ordered, FrameworkController::router, and setState().

+ Here is the call graph for this function:

◆ setState()

void GpioController::setState ( HttpRequest req,
HttpResponse res,
const std::vector< std::string > &  params 
)
private

Definition at line 36 of file GpioController.cpp.

36 {
37 int pin = std::stoi(params[0]);
38 int value = std::stoi(params[1]);
39 pico.setGpioState(pin, value != 0); // Convert to boolean
40 res.json({{"pin", pin}, {"state", value}});
41}
void setGpioState(int pin, bool state)
Definition PicoModel.cpp:59

References HttpResponse::json(), pico, and PicoModel::setGpioState().

Referenced by initRoutes().

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

Member Data Documentation

◆ pico

PicoModel& GpioController::pico
private

Definition at line 33 of file GpioController.h.

Referenced by getState(), handleGetMultipleGpios(), and setState().


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