DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
gpio_map.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file gpio_map.h
6 * @brief Browser GPIO pin-mapper / diagnostics (DETWS_ENABLE_GPIO_MAP).
7 *
8 * Exposes a compile-time table of GPIO pins (number, label, configured direction,
9 * live level) as JSON so a browser diag panel can show the pin map and toggle
10 * outputs. The live read (digitalRead) and write (pinMode / digitalWrite) use the
11 * Arduino API on ESP32; the JSON serializer and the control-POST parser are pure
12 * and host-tested. No allocation: the pin table is caller-owned and the JSON is
13 * written into a caller buffer.
14 *
15 * @author Douglas Quigg (dstroy0)
16 * @date 2026
17 */
18
19#ifndef DETERMINISTICESPASYNCWEBSERVER_GPIO_MAP_H
20#define DETERMINISTICESPASYNCWEBSERVER_GPIO_MAP_H
21
22#include "ServerConfig.h"
23#include <stddef.h>
24#include <stdint.h>
25
26#if DETWS_ENABLE_GPIO_MAP
27
28class DetWebServer;
29
30/** @brief Configured direction of a mapped pin (how the panel renders / drives it). */
31enum class DetwsGpioDir : uint8_t
32{
33 DETWS_GPIO_IN = 0, ///< read-only input.
34 DETWS_GPIO_IN_PULLUP, ///< input with internal pull-up.
35 DETWS_GPIO_IN_PULLDOWN, ///< input with internal pull-down.
36 DETWS_GPIO_OUT, ///< output (drivable from the panel).
37};
38
39/** @brief One mapped GPIO pin. */
40struct DetwsGpioPin
41{
42 uint8_t pin; ///< GPIO number.
43 const char *label; ///< human label (null-terminated, caller-owned).
44 DetwsGpioDir dir; ///< pin direction.
45 uint8_t level; ///< live level (0 / 1); filled by detws_gpio_read.
46};
47
48// ---------------------------------------------------------------------------
49// Host-testable core
50// ---------------------------------------------------------------------------
51
52/** @brief Short name for a direction ("in", "in_pullup", "in_pulldown", "out"). */
53const char *detws_gpio_dir_name(DetwsGpioDir dir);
54
55/**
56 * @brief Serialize a pin array as JSON `{"pins":[...]}` into @p out.
57 * @return characters written, or 0 if @p cap is too small (fail-closed).
58 */
59int detws_gpio_json(const DetwsGpioPin *pins, uint8_t count, char *out, size_t cap);
60
61/**
62 * @brief Parse a control body of the form `pin=<n>&level=<0|1>` (form-encoded).
63 * @return true if both fields parsed into @p pin / @p level.
64 */
65bool detws_gpio_parse_set(const char *body, size_t len, uint8_t *pin, uint8_t *level);
66
67/** @brief True if @p pin is a drivable output in the table (guards a control POST). */
68bool detws_gpio_is_output(const DetwsGpioPin *pins, uint8_t count, uint8_t pin);
69
70// ---------------------------------------------------------------------------
71// ESP32 integration (no-ops on host builds)
72// ---------------------------------------------------------------------------
73
74/** @brief Apply pinMode() for every entry per its direction (call once at setup). */
75void detws_gpio_begin_pins(const DetwsGpioPin *pins, uint8_t count);
76
77/** @brief Refresh each pin's live @c level via digitalRead (no-op on host). */
78void detws_gpio_read(DetwsGpioPin *pins, uint8_t count);
79
80/** @brief Drive an output @p pin to @p level via digitalWrite (no-op on host). */
81void detws_gpio_write(uint8_t pin, uint8_t level);
82
83/**
84 * @brief Serve the GPIO map at @p path: GET returns the JSON, POST drives an
85 * output (body `pin=<n>&level=<0|1>`, only pins marked DetwsGpioDir::DETWS_GPIO_OUT).
86 * The pin table is caller-owned and must outlive the server.
87 */
88void detws_gpio_map_begin(DetWebServer &server, const char *path, DetwsGpioPin *pins, uint8_t count);
89
90#endif // DETWS_ENABLE_GPIO_MAP
91#endif // DETERMINISTICESPASYNCWEBSERVER_GPIO_MAP_H
User-facing configuration for DeterministicESPAsyncWebServer.
Single-port HTTP server with deterministic, zero-allocation execution.
Definition dwserver.h:348