idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
gpio.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Chris Leishman
3
4#pragma once
5
21#include <idfxx/error>
22#include <idfxx/flags>
23#include <idfxx/intr_alloc>
24
25#include <driver/gpio.h>
26#include <functional>
27#include <string>
28
29namespace idfxx {
30
32template<int N>
33struct gpio_constant;
58class gpio {
59 template<int N>
60 friend struct gpio_constant;
61
62public:
64 enum class mode : int {
65 disable = GPIO_MODE_DISABLE,
66 input = GPIO_MODE_INPUT,
67 output = GPIO_MODE_OUTPUT,
68 output_od = GPIO_MODE_OUTPUT_OD,
69 input_output_od = GPIO_MODE_INPUT_OUTPUT_OD,
70 input_output = GPIO_MODE_INPUT_OUTPUT,
71 };
72
74 enum class pull_mode : int {
75 pullup = GPIO_PULLUP_ONLY,
76 pulldown = GPIO_PULLDOWN_ONLY,
77 pullup_pulldown = GPIO_PULLUP_PULLDOWN,
78 floating = GPIO_FLOATING,
79 };
80
82 enum class drive_cap : int {
83 cap_0 = GPIO_DRIVE_CAP_0,
84 cap_1 = GPIO_DRIVE_CAP_1,
85 cap_2 = GPIO_DRIVE_CAP_2,
86 cap_default = GPIO_DRIVE_CAP_DEFAULT,
87 cap_3 = GPIO_DRIVE_CAP_3,
88 };
89
91 enum class intr_type : int {
92 disable = GPIO_INTR_DISABLE,
93 posedge = GPIO_INTR_POSEDGE,
94 negedge = GPIO_INTR_NEGEDGE,
95 anyedge = GPIO_INTR_ANYEDGE,
96 low_level = GPIO_INTR_LOW_LEVEL,
97 high_level = GPIO_INTR_HIGH_LEVEL,
98 };
99
100#if SOC_GPIO_SUPPORT_PIN_HYS_FILTER
102 enum class hys_ctrl_mode : int {
103#if SOC_GPIO_SUPPORT_PIN_HYS_CTRL_BY_EFUSE
104 efuse = GPIO_HYS_CTRL_EFUSE,
105#endif
106 soft_disable = GPIO_HYS_CTRL_SOFT_DISABLE,
107 soft_enable = GPIO_HYS_CTRL_SOFT_ENABLE,
108 };
109#endif
110
111 class unique_isr_handle;
112
123 friend class gpio;
124 friend class unique_isr_handle;
125 constexpr isr_handle(gpio_num_t num, uint32_t id)
126 : _num(num)
127 , _id(id) {}
128 gpio_num_t _num;
129 uint32_t _id;
130 };
131
142 public:
148 explicit unique_isr_handle() noexcept
149 : _num(GPIO_NUM_NC)
150 , _id(0) {}
151
156 explicit unique_isr_handle(isr_handle handle) noexcept
157 : _num(handle._num)
158 , _id(handle._id) {}
159
165 : _num(other._num)
166 , _id(other._id) {
167 other._num = GPIO_NUM_NC;
168 }
169
176 if (this != &other) {
177 if (_num != GPIO_NUM_NC) {
178 gpio{_num}.try_isr_handler_remove(isr_handle{_num, _id});
179 }
180 _num = other._num;
181 _id = other._id;
182 other._num = GPIO_NUM_NC;
183 }
184 return *this;
185 }
186
189
194 if (_num != GPIO_NUM_NC) {
195 // Removing the ISR handler will not fail, so we can ignore the result
196 gpio{_num}.try_isr_handler_remove(isr_handle{_num, _id});
197 }
198 }
199
204 [[nodiscard]] isr_handle release() noexcept {
205 auto handle = isr_handle{_num, _id};
206 _num = GPIO_NUM_NC;
207 return handle;
208 }
209
210 private:
211 gpio_num_t _num;
212 uint32_t _id;
213 };
214
224
225public:
227 constexpr gpio()
228 : _num(GPIO_NUM_NC) {}
229
235 [[nodiscard]] static result<gpio> make(int num);
236
237#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
244 [[nodiscard]] explicit gpio(int num);
245#endif
246
248 [[nodiscard]] static constexpr gpio nc() { return gpio{GPIO_NUM_NC}; }
249
251 [[nodiscard]] static constexpr gpio max() { return gpio{static_cast<gpio_num_t>(GPIO_NUM_MAX - 1)}; }
252
253 // Copyable and movable
254 gpio(const gpio&) = default;
255 gpio(gpio&&) = default;
256 gpio& operator=(const gpio&) = default;
257 gpio& operator=(gpio&&) = default;
258
259 constexpr bool operator==(const gpio& other) const = default;
260
262 [[nodiscard]] constexpr bool is_connected() const {
263 return _num != GPIO_NUM_NC && _num >= 0 && _num < GPIO_NUM_MAX && GPIO_IS_VALID_GPIO(_num);
264 }
265
267 [[nodiscard]] constexpr bool is_output_capable() const { return GPIO_IS_VALID_OUTPUT_GPIO(_num); }
268
270 [[nodiscard]] constexpr bool is_digital_io_pin_capable() const { return GPIO_IS_VALID_DIGITAL_IO_PAD(_num); }
271
273 [[nodiscard]] constexpr int num() const { return static_cast<int>(_num); }
274
276 [[nodiscard]] constexpr gpio_num_t idf_num() const { return _num; }
277
285 void reset() {
286 if (!is_connected()) {
287 return;
288 }
289 gpio_reset_pin(_num);
290 }
291
292 // Direction and pull configuration
293#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
340#endif
348 return guarded([&] { return gpio_set_direction(_num, static_cast<gpio_mode_t>(mode)); });
349 }
356 return guarded([&] { return gpio_input_enable(_num); });
357 }
366 return guarded([&] { return gpio_set_pull_mode(_num, static_cast<gpio_pull_mode_t>(mode)); });
367 }
374 return guarded([&] { return gpio_pullup_en(_num); });
375 }
382 return guarded([&] { return gpio_pullup_dis(_num); });
383 }
390 return guarded([&] { return gpio_pulldown_en(_num); });
391 }
398 return guarded([&] { return gpio_pulldown_dis(_num); });
399 }
400
401 // Level (input/output)
406 void set_level(bool level) { gpio_set_level(_num, level ? 1 : 0); }
412 [[nodiscard]] bool get_level() const { return gpio_get_level(_num) != 0; }
413
414 // Drive capability
415#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
427 [[nodiscard]] enum drive_cap get_drive_capability() const { return unwrap(try_get_drive_capability()); }
428#endif
435 return guarded([&] { return gpio_set_drive_capability(_num, static_cast<gpio_drive_cap_t>(strength)); });
436 }
443 if (!is_connected()) {
445 }
446 gpio_drive_cap_t cap;
447 auto err = gpio_get_drive_capability(_num, &cap);
448 return wrap(err).transform([cap]() { return static_cast<drive_cap>(cap); });
449 }
450
451 // ISR service (must be installed before adding handlers)
452#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
462 static void install_isr_service(flags<intr_flag> intr_alloc_flags = {}) {
463 unwrap(try_install_isr_service(intr_alloc_flags));
464 }
465#endif
473 [[nodiscard]] static result<void> try_install_isr_service(flags<intr_flag> intr_flags = {});
480
481 // ISR handlers
482#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
501 isr_handle isr_handler_add(std::move_only_function<void() const> handler) {
502 return unwrap(try_isr_handler_add(std::move(handler)));
503 }
517 isr_handle isr_handler_add(void (*fn)(void*), void* arg) { return unwrap(try_isr_handler_add(fn, arg)); }
530#endif
547 result<isr_handle> try_isr_handler_add(std::move_only_function<void() const> handler);
560 result<isr_handle> try_isr_handler_add(void (*fn)(void*), void* arg);
573
574 // Interrupt configuration
575#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
594#endif
601 return guarded(gpio_set_intr_type, _num, static_cast<gpio_int_type_t>(intr_type));
602 }
608 result<void> try_intr_enable() { return guarded(gpio_intr_enable, _num); }
614 result<void> try_intr_disable() { return guarded(gpio_intr_disable, _num); }
615
616 // Wakeup configuration
617#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
631#endif
639 return guarded(gpio_wakeup_enable, _num, static_cast<gpio_int_type_t>(intr_type));
640 }
646 result<void> try_wakeup_disable() { return guarded(gpio_wakeup_disable, _num); }
647
648 // Hold configuration (maintains state during sleep/reset)
649#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
672#endif
685 result<void> try_hold_enable() { return guarded(gpio_hold_en, _num); }
694 result<void> try_hold_disable() { return guarded(gpio_hold_dis, _num); }
701 static void deep_sleep_hold_enable() { gpio_deep_sleep_hold_en(); }
703 static void deep_sleep_hold_disable() { gpio_deep_sleep_hold_dis(); }
704
705 // Sleep mode configuration
706#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
733#endif
738 result<void> try_sleep_sel_enable() { return guarded(gpio_sleep_sel_en, _num); }
743 result<void> try_sleep_sel_disable() { return guarded(gpio_sleep_sel_dis, _num); }
750 return guarded(gpio_sleep_set_direction, _num, static_cast<gpio_mode_t>(mode));
751 }
760 return guarded(gpio_sleep_set_pull_mode, _num, static_cast<gpio_pull_mode_t>(pull));
761 }
762
763private:
764 constexpr gpio(gpio_num_t num)
765 : _num(num) {}
766
767 template<typename F, typename... Args>
768 result<void> guarded(F&& f, Args&&... args) const {
769 if (!is_connected()) {
771 }
772 return wrap(std::invoke(std::forward<F>(f), std::forward<Args>(args)...));
773 }
774
775 gpio_num_t _num;
776};
777
787[[nodiscard]] result<void> try_configure_gpios(const gpio::config& cfg, std::vector<gpio> gpios);
797template<typename... Gpios>
798[[nodiscard]] result<void> try_configure_gpios(const gpio::config& cfg, Gpios&&... gpios) {
799 return try_configure_gpios(cfg, std::vector<gpio>{std::forward<Gpios>(gpios)...});
800}
801#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
811inline void configure_gpios(const gpio::config& cfg, std::vector<gpio> gpios) {
812 unwrap(try_configure_gpios(cfg, std::move(gpios)));
813}
823template<typename... Gpios>
824void configure_gpios(const gpio::config& cfg, Gpios&&... gpios) {
825 unwrap(try_configure_gpios(cfg, std::vector<gpio>{std::forward<Gpios>(gpios)...}));
826}
827#endif
828
830/* Validates at compile time that the GPIO number is valid.
831 * Used internally to create the predefined GPIO constants.
832 */
833template<int N>
834struct gpio_constant {
835 static_assert(N == GPIO_NUM_NC || (SOC_GPIO_VALID_GPIO_MASK & (1ULL << N)), "Invalid GPIO number");
836 static constexpr gpio value{static_cast<gpio_num_t>(N)};
837};
849inline constexpr gpio gpio_nc = gpio_constant<GPIO_NUM_NC>::value;
850#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 0))
851inline constexpr gpio gpio_0 = gpio_constant<0>::value;
852#endif
853#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 1))
854inline constexpr gpio gpio_1 = gpio_constant<1>::value;
855#endif
856#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 2))
857inline constexpr gpio gpio_2 = gpio_constant<2>::value;
858#endif
859#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 3))
860inline constexpr gpio gpio_3 = gpio_constant<3>::value;
861#endif
862#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 4))
863inline constexpr gpio gpio_4 = gpio_constant<4>::value;
864#endif
865#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 5))
866inline constexpr gpio gpio_5 = gpio_constant<5>::value;
867#endif
868#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 6))
869inline constexpr gpio gpio_6 = gpio_constant<6>::value;
870#endif
871#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 7))
872inline constexpr gpio gpio_7 = gpio_constant<7>::value;
873#endif
874#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 8))
875inline constexpr gpio gpio_8 = gpio_constant<8>::value;
876#endif
877#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 9))
878inline constexpr gpio gpio_9 = gpio_constant<9>::value;
879#endif
880#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 10))
881inline constexpr gpio gpio_10 = gpio_constant<10>::value;
882#endif
883#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 11))
884inline constexpr gpio gpio_11 = gpio_constant<11>::value;
885#endif
886#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 12))
887inline constexpr gpio gpio_12 = gpio_constant<12>::value;
888#endif
889#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 13))
890inline constexpr gpio gpio_13 = gpio_constant<13>::value;
891#endif
892#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 14))
893inline constexpr gpio gpio_14 = gpio_constant<14>::value;
894#endif
895#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 15))
896inline constexpr gpio gpio_15 = gpio_constant<15>::value;
897#endif
898#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 16))
899inline constexpr gpio gpio_16 = gpio_constant<16>::value;
900#endif
901#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 17))
902inline constexpr gpio gpio_17 = gpio_constant<17>::value;
903#endif
904#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 18))
905inline constexpr gpio gpio_18 = gpio_constant<18>::value;
906#endif
907#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 19))
908inline constexpr gpio gpio_19 = gpio_constant<19>::value;
909#endif
910#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 20))
911inline constexpr gpio gpio_20 = gpio_constant<20>::value;
912#endif
913#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 21))
914inline constexpr gpio gpio_21 = gpio_constant<21>::value;
915#endif
916#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 22))
917inline constexpr gpio gpio_22 = gpio_constant<22>::value;
918#endif
919#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 23))
920inline constexpr gpio gpio_23 = gpio_constant<23>::value;
921#endif
922#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 24))
923inline constexpr gpio gpio_24 = gpio_constant<24>::value;
924#endif
925#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 25))
926inline constexpr gpio gpio_25 = gpio_constant<25>::value;
927#endif
928#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 26))
929inline constexpr gpio gpio_26 = gpio_constant<26>::value;
930#endif
931#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 27))
932inline constexpr gpio gpio_27 = gpio_constant<27>::value;
933#endif
934#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 28))
935inline constexpr gpio gpio_28 = gpio_constant<28>::value;
936#endif
937#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 29))
938inline constexpr gpio gpio_29 = gpio_constant<29>::value;
939#endif
940#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 30))
941inline constexpr gpio gpio_30 = gpio_constant<30>::value;
942#endif
943#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 31))
944inline constexpr gpio gpio_31 = gpio_constant<31>::value;
945#endif
946#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 32))
947inline constexpr gpio gpio_32 = gpio_constant<32>::value;
948#endif
949#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 33))
950inline constexpr gpio gpio_33 = gpio_constant<33>::value;
951#endif
952#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 34))
953inline constexpr gpio gpio_34 = gpio_constant<34>::value;
954#endif
955#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 35))
956inline constexpr gpio gpio_35 = gpio_constant<35>::value;
957#endif
958#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 36))
959inline constexpr gpio gpio_36 = gpio_constant<36>::value;
960#endif
961#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 37))
962inline constexpr gpio gpio_37 = gpio_constant<37>::value;
963#endif
964#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 38))
965inline constexpr gpio gpio_38 = gpio_constant<38>::value;
966#endif
967#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 39))
968inline constexpr gpio gpio_39 = gpio_constant<39>::value;
969#endif
970#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 40))
971inline constexpr gpio gpio_40 = gpio_constant<40>::value;
972#endif
973#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 41))
974inline constexpr gpio gpio_41 = gpio_constant<41>::value;
975#endif
976#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 42))
977inline constexpr gpio gpio_42 = gpio_constant<42>::value;
978#endif
979#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 43))
980inline constexpr gpio gpio_43 = gpio_constant<43>::value;
981#endif
982#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 44))
983inline constexpr gpio gpio_44 = gpio_constant<44>::value;
984#endif
985#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 45))
986inline constexpr gpio gpio_45 = gpio_constant<45>::value;
987#endif
988#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 46))
989inline constexpr gpio gpio_46 = gpio_constant<46>::value;
990#endif
991#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 47))
992inline constexpr gpio gpio_47 = gpio_constant<47>::value;
993#endif
994#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 48))
995inline constexpr gpio gpio_48 = gpio_constant<48>::value;
996#endif
997
// end of GPIO Constants
999
1007[[nodiscard]] inline std::string to_string(gpio g) {
1008 if (!g.is_connected()) {
1009 return "GPIO_NC";
1010 }
1011 return "GPIO_" + std::to_string(g.num());
1012}
1013
// end of idfxx_gpio
1015
1016} // namespace idfxx
1017
1018#include "sdkconfig.h"
1019#ifdef CONFIG_IDFXX_STD_FORMAT
1021#include <algorithm>
1022#include <format>
1023namespace std {
1024template<>
1025struct formatter<idfxx::gpio> {
1026 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
1027
1028 template<typename FormatContext>
1029 auto format(idfxx::gpio g, FormatContext& ctx) const {
1030 auto s = to_string(g);
1031 return std::copy(s.begin(), s.end(), ctx.out());
1032 }
1033};
1034} // namespace std
1036#endif // CONFIG_IDFXX_STD_FORMAT
Type-safe set of flags from a scoped enum.
Definition flags.hpp:88
Handle to a registered ISR handler.
Definition gpio.hpp:122
RAII handle for ISR registration that removes the handler on destruction.
Definition gpio.hpp:141
unique_isr_handle & operator=(unique_isr_handle &&other) noexcept
Move assignment operator.
Definition gpio.hpp:175
unique_isr_handle() noexcept
Constructs an empty unique_isr_handle.
Definition gpio.hpp:148
unique_isr_handle(unique_isr_handle &&other) noexcept
Move constructor.
Definition gpio.hpp:164
~unique_isr_handle()
Destructor.
Definition gpio.hpp:193
unique_isr_handle & operator=(const unique_isr_handle &)=delete
unique_isr_handle(const unique_isr_handle &)=delete
unique_isr_handle(isr_handle handle) noexcept
Constructs from an isr_handle, taking ownership.
Definition gpio.hpp:156
isr_handle release() noexcept
Releases ownership of the handle without removing the ISR.
Definition gpio.hpp:204
A GPIO pin.
Definition gpio.hpp:58
static result< gpio > make(int num)
Creates a validated GPIO pin.
static void uninstall_isr_service()
Uninstalls the GPIO ISR service, freeing related resources.
result< void > try_set_intr_type(enum intr_type intr_type)
Sets the interrupt trigger type.
Definition gpio.hpp:600
void set_drive_capability(enum drive_cap strength)
Sets the gpio drive capability.
Definition gpio.hpp:421
result< void > try_pulldown_disable()
Disables the internal pull-down resistor.
Definition gpio.hpp:397
static result< void > try_install_isr_service(flags< intr_flag > intr_flags={})
Installs the GPIO ISR service for per-gpio interrupt handlers.
void intr_enable()
Enables interrupts for this gpio.
Definition gpio.hpp:587
result< void > try_sleep_set_direction(enum mode mode)
Sets GPIO direction at sleep.
Definition gpio.hpp:749
result< void > try_wakeup_enable(enum intr_type intr_type)
Enables GPIO wake-up from light sleep.
Definition gpio.hpp:638
result< void > try_set_pull_mode(enum pull_mode mode)
Sets the pull resistor mode.
Definition gpio.hpp:365
result< void > try_wakeup_disable()
Disables GPIO wake-up.
Definition gpio.hpp:646
result< void > try_sleep_sel_enable()
Enables SLP_SEL to change GPIO status automatically in light sleep.
Definition gpio.hpp:738
void pulldown_enable()
Enables the internal pull-down resistor.
Definition gpio.hpp:333
constexpr bool is_digital_io_pin_capable() const
Returns true if this gpio is a valid digital I/O pin.
Definition gpio.hpp:270
void input_enable()
Enables input on this gpio.
Definition gpio.hpp:307
static void deep_sleep_hold_disable()
Disables hold for all digital GPIOs during deep sleep.
Definition gpio.hpp:703
void sleep_set_direction(enum mode mode)
Sets GPIO direction at sleep.
Definition gpio.hpp:724
void set_level(bool level)
Sets the output level.
Definition gpio.hpp:406
constexpr int num() const
Returns the underlying GPIO pin number.
Definition gpio.hpp:273
void set_intr_type(enum intr_type intr_type)
Sets the interrupt trigger type.
Definition gpio.hpp:581
result< drive_cap > try_get_drive_capability() const
Gets the current drive capability.
Definition gpio.hpp:442
result< void > try_set_direction(enum mode mode)
Sets the GPIO direction mode.
Definition gpio.hpp:347
void sleep_sel_disable()
Disables SLP_SEL to change GPIO status automatically in light sleep.
Definition gpio.hpp:718
result< void > try_pullup_enable()
Enables the internal pull-up resistor.
Definition gpio.hpp:373
gpio & operator=(const gpio &)=default
void wakeup_disable()
Disables GPIO wake-up.
Definition gpio.hpp:630
result< void > try_intr_disable()
Disables interrupts for this gpio.
Definition gpio.hpp:614
friend struct gpio_constant
Definition gpio.hpp:60
result< void > try_isr_handler_remove(isr_handle handle)
Removes a specific ISR handler.
static void install_isr_service(flags< intr_flag > intr_alloc_flags={})
Installs the GPIO ISR service for per-gpio interrupt handlers.
Definition gpio.hpp:462
result< void > try_set_drive_capability(enum drive_cap strength)
Sets the gpio drive capability.
Definition gpio.hpp:434
void set_pull_mode(enum pull_mode mode)
Sets the pull resistor mode.
Definition gpio.hpp:315
void pulldown_disable()
Disables the internal pull-down resistor.
Definition gpio.hpp:339
static constexpr gpio max()
Returns a GPIO for the highest valid pin number.
Definition gpio.hpp:251
gpio & operator=(gpio &&)=default
void sleep_set_pull_mode(enum pull_mode pull)
Sets pull resistor mode at sleep.
Definition gpio.hpp:732
result< void > try_input_enable()
Enables input on this gpio.
Definition gpio.hpp:355
static constexpr gpio nc()
Returns a GPIO representing "not connected".
Definition gpio.hpp:248
result< void > try_hold_disable()
Disables gpio hold function.
Definition gpio.hpp:694
void reset()
Resets the gpio to default state.
Definition gpio.hpp:285
mode
GPIO direction mode.
Definition gpio.hpp:64
@ disable
GPIO mode : disable input and output.
@ output
GPIO mode : output only mode.
@ input
GPIO mode : input only.
@ input_output_od
GPIO mode : output and input with open-drain mode.
@ input_output
GPIO mode : output and input mode.
@ output_od
GPIO mode : output only with open-drain mode.
isr_handle isr_handler_add(std::move_only_function< void() const > handler)
Adds an ISR handler for this gpio.
Definition gpio.hpp:501
constexpr gpio_num_t idf_num() const
Returns the underlying ESP-IDF GPIO number.
Definition gpio.hpp:276
gpio(const gpio &)=default
constexpr gpio()
Constructs a GPIO representing "not connected".
Definition gpio.hpp:227
pull_mode
Pull resistor configuration.
Definition gpio.hpp:74
@ floating
Pin floating.
@ pullup_pulldown
Pin pull up + pull down.
@ pullup
Pin pull up.
@ pulldown
Pin pull down.
constexpr bool operator==(const gpio &other) const =default
result< void > try_hold_enable()
Enables gpio hold function.
Definition gpio.hpp:685
void intr_disable()
Disables interrupts for this gpio.
Definition gpio.hpp:593
void hold_enable()
Enables gpio hold function.
Definition gpio.hpp:662
gpio(int num)
Constructs a validated GPIO pin.
result< isr_handle > try_isr_handler_add(std::move_only_function< void() const > handler)
Adds an ISR handler for this gpio.
void isr_handler_remove_all()
Removes all ISR handlers for this gpio.
Definition gpio.hpp:529
result< void > try_sleep_set_pull_mode(enum pull_mode pull)
Sets pull resistor mode at sleep.
Definition gpio.hpp:759
constexpr bool is_output_capable() const
Returns true if this gpio supports output mode.
Definition gpio.hpp:267
isr_handle isr_handler_add(void(*fn)(void *), void *arg)
Adds an ISR handler for this gpio.
Definition gpio.hpp:517
gpio(gpio &&)=default
drive_cap
Pin drive capability (output strength).
Definition gpio.hpp:82
@ cap_0
Pin drive capability: weak.
@ cap_default
Pin drive capability: medium.
@ cap_1
Pin drive capability: stronger.
@ cap_2
Pin drive capability: medium.
@ cap_3
Pin drive capability: strongest.
constexpr bool is_connected() const
Returns true if this is a valid GPIO pin.
Definition gpio.hpp:262
void hold_disable()
Disables gpio hold function.
Definition gpio.hpp:671
result< void > try_pullup_disable()
Disables the internal pull-up resistor.
Definition gpio.hpp:381
result< isr_handle > try_isr_handler_add(void(*fn)(void *), void *arg)
Adds an ISR handler for this gpio.
result< void > try_intr_enable()
Enables interrupts for this gpio.
Definition gpio.hpp:608
void set_direction(enum mode mode)
Sets the GPIO direction mode.
Definition gpio.hpp:300
void wakeup_enable(enum intr_type intr_type)
Enables GPIO wake-up from light sleep.
Definition gpio.hpp:624
result< void > try_pulldown_enable()
Enables the internal pull-down resistor.
Definition gpio.hpp:389
bool get_level() const
Reads the current input level.
Definition gpio.hpp:412
void sleep_sel_enable()
Enables SLP_SEL to change GPIO status automatically in light sleep.
Definition gpio.hpp:712
result< void > try_isr_handler_remove_all()
Removes all ISR handlers for this gpio.
enum drive_cap get_drive_capability() const
Gets the current drive capability.
Definition gpio.hpp:427
result< void > try_sleep_sel_disable()
Disables SLP_SEL to change GPIO status automatically in light sleep.
Definition gpio.hpp:743
hys_ctrl_mode
Hysteresis control mode.
Definition gpio.hpp:102
@ soft_enable
Pin input hysteresis enable by software.
@ soft_disable
Pin input hysteresis disable by software.
void pullup_enable()
Enables the internal pull-up resistor.
Definition gpio.hpp:321
void pullup_disable()
Disables the internal pull-up resistor.
Definition gpio.hpp:327
intr_type
Interrupt trigger type.
Definition gpio.hpp:91
@ disable
Disable GPIO interrupt.
@ low_level
GPIO interrupt type : input low level trigger.
@ high_level
GPIO interrupt type : input high level trigger.
@ anyedge
GPIO interrupt type : both rising and falling edge.
@ negedge
GPIO interrupt type : falling edge.
@ posedge
GPIO interrupt type : rising edge.
static void deep_sleep_hold_enable()
Enables hold for all digital GPIOs during deep sleep.
Definition gpio.hpp:701
void isr_handler_remove(isr_handle handle)
Removes a specific ISR handler.
Definition gpio.hpp:523
std::string to_string(core_id c)
Returns a string representation of a CPU core identifier.
Definition cpu.hpp:52
constexpr gpio gpio_36
Definition gpio.hpp:959
constexpr gpio gpio_11
Definition gpio.hpp:884
void configure_gpios(const gpio::config &cfg, std::vector< gpio > gpios)
Configures multiple GPIOs with the same settings.
Definition gpio.hpp:811
constexpr gpio gpio_33
Definition gpio.hpp:950
constexpr gpio gpio_47
Definition gpio.hpp:992
constexpr gpio gpio_23
Definition gpio.hpp:920
constexpr gpio gpio_8
Definition gpio.hpp:875
constexpr gpio gpio_20
Definition gpio.hpp:911
constexpr gpio gpio_38
Definition gpio.hpp:965
constexpr gpio gpio_39
Definition gpio.hpp:968
constexpr gpio gpio_46
Definition gpio.hpp:989
constexpr gpio gpio_3
Definition gpio.hpp:860
constexpr gpio gpio_16
Definition gpio.hpp:899
constexpr gpio gpio_35
Definition gpio.hpp:956
constexpr gpio gpio_0
Definition gpio.hpp:851
constexpr gpio gpio_4
Definition gpio.hpp:863
constexpr gpio gpio_7
Definition gpio.hpp:872
constexpr gpio gpio_12
Definition gpio.hpp:887
constexpr gpio gpio_13
Definition gpio.hpp:890
constexpr gpio gpio_17
Definition gpio.hpp:902
constexpr gpio gpio_2
Definition gpio.hpp:857
constexpr gpio gpio_29
Definition gpio.hpp:938
constexpr gpio gpio_14
Definition gpio.hpp:893
result< void > try_configure_gpios(const gpio::config &cfg, std::vector< gpio > gpios)
Configures multiple GPIOs with the same settings.
constexpr gpio gpio_21
Definition gpio.hpp:914
constexpr gpio gpio_34
Definition gpio.hpp:953
constexpr gpio gpio_44
Definition gpio.hpp:983
constexpr gpio gpio_18
Definition gpio.hpp:905
constexpr gpio gpio_31
Definition gpio.hpp:944
constexpr gpio gpio_9
Definition gpio.hpp:878
constexpr gpio gpio_41
Definition gpio.hpp:974
constexpr gpio gpio_26
Definition gpio.hpp:929
constexpr gpio gpio_43
Definition gpio.hpp:980
constexpr gpio gpio_25
Definition gpio.hpp:926
constexpr gpio gpio_nc
Definition gpio.hpp:849
constexpr gpio gpio_1
Definition gpio.hpp:854
constexpr gpio gpio_10
Definition gpio.hpp:881
constexpr gpio gpio_30
Definition gpio.hpp:941
constexpr gpio gpio_48
Definition gpio.hpp:995
constexpr gpio gpio_6
Definition gpio.hpp:869
constexpr gpio gpio_22
Definition gpio.hpp:917
constexpr gpio gpio_32
Definition gpio.hpp:947
constexpr gpio gpio_19
Definition gpio.hpp:908
constexpr gpio gpio_5
Definition gpio.hpp:866
constexpr gpio gpio_40
Definition gpio.hpp:971
constexpr gpio gpio_28
Definition gpio.hpp:935
constexpr gpio gpio_24
Definition gpio.hpp:923
constexpr gpio gpio_27
Definition gpio.hpp:932
constexpr gpio gpio_37
Definition gpio.hpp:962
constexpr gpio gpio_45
Definition gpio.hpp:986
constexpr gpio gpio_42
Definition gpio.hpp:977
constexpr gpio gpio_15
Definition gpio.hpp:896
constexpr std::unexpected< std::error_code > error(E e) noexcept
Creates an unexpected error from an error code enum.
Definition error.hpp:142
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
Definition error.hpp:237
@ invalid_state
Invalid state.
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:118
result< void > wrap(esp_err_t e)
Wraps an esp_err_t into a result<void>.
Definition error.hpp:216
Configuration parameters for idfxx::gpio_config.
Definition gpio.hpp:216