Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
GpioController.cpp
Go to the documentation of this file.
1#include "GpioController.h"
2#include "hardware/gpio.h"
3#include <cstdio>
4#include "http/JsonResponse.h"
5#include "PicoModel.h"
6
7using nlohmann::json;
8
10 : FrameworkController("GpioController", r, 1024, 1), pico(pico) {}
11
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}
29
30void GpioController::getState(HttpRequest& req, HttpResponse& res, const std::vector<std::string>& params) {
31 int pin = std::stoi(params[0]);
32 bool state = pico.getGpioState(pin);
33 res.json({{"pin", pin}, {"state", state ? 1 : 0}});
34}
35
36void GpioController::setState(HttpRequest& req, HttpResponse& res, const std::vector<std::string>& params) {
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}
42
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}
81
82
nlohmann::json json
Utility functions to send standard JSON responses using nlohmann::json.
Base class for event-driven control logic in embedded applications.
Router & router
Handles path-to-handler mapping - reference to shared Router instance.
GpioController(Router &r, PicoModel &pico)
PicoModel & pico
void initRoutes() override
Initialize routes for this controller.
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
const std::unordered_multimap< std::string, std::string > getQueryParams()
Get parsed query string parameters.
Represents an HTTP response object.
HttpResponse & json(const std::string &jsonString)
Send a JSON string/object with correct content type.
void setGpioState(int pin, bool state)
Definition PicoModel.cpp:59
bool getGpioState(int pin)
Definition PicoModel.cpp:53
The central router for handling HTTP requests and middleware.
Definition Router.h:60
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
void sendNoContent(HttpResponse &res)
Represents a match of a route against an incoming HTTP request.
Definition RouteTypes.h:18
std::vector< std::string > ordered
Definition RouteTypes.h:19