|
idfxx 1.0.0
Modern C++23 components for ESP-IDF
|
A GPIO pin. More...
Classes | |
| struct | config |
| Configuration parameters for idfxx::gpio_config. More... | |
| class | isr_handle |
| Handle to a registered ISR handler. More... | |
| class | unique_isr_handle |
| RAII handle for ISR registration that removes the handler on destruction. More... | |
Public Types | |
| enum class | mode : int { disable = GPIO_MODE_DISABLE , input = GPIO_MODE_INPUT , output = GPIO_MODE_OUTPUT , output_od = GPIO_MODE_OUTPUT_OD , input_output_od = GPIO_MODE_INPUT_OUTPUT_OD , input_output = GPIO_MODE_INPUT_OUTPUT } |
| GPIO direction mode. More... | |
| enum class | pull_mode : int { pullup = GPIO_PULLUP_ONLY , pulldown = GPIO_PULLDOWN_ONLY , pullup_pulldown = GPIO_PULLUP_PULLDOWN , floating = GPIO_FLOATING } |
| Pull resistor configuration. More... | |
| enum class | drive_cap : int { cap_0 = GPIO_DRIVE_CAP_0 , cap_1 = GPIO_DRIVE_CAP_1 , cap_2 = GPIO_DRIVE_CAP_2 , cap_default = GPIO_DRIVE_CAP_DEFAULT , cap_3 = GPIO_DRIVE_CAP_3 } |
| Pin drive capability (output strength). More... | |
| enum class | intr_type : int { disable = GPIO_INTR_DISABLE , posedge = GPIO_INTR_POSEDGE , negedge = GPIO_INTR_NEGEDGE , anyedge = GPIO_INTR_ANYEDGE , low_level = GPIO_INTR_LOW_LEVEL , high_level = GPIO_INTR_HIGH_LEVEL } |
| Interrupt trigger type. More... | |
| enum class | hys_ctrl_mode : int { soft_disable = GPIO_HYS_CTRL_SOFT_DISABLE , soft_enable = GPIO_HYS_CTRL_SOFT_ENABLE } |
| Hysteresis control mode. More... | |
Public Member Functions | |
| constexpr | gpio () |
| Constructs a GPIO representing "not connected". | |
| gpio (int num) | |
| Constructs a validated GPIO pin. | |
| gpio (const gpio &)=default | |
| gpio (gpio &&)=default | |
| gpio & | operator= (const gpio &)=default |
| gpio & | operator= (gpio &&)=default |
| constexpr bool | operator== (const gpio &other) const =default |
| constexpr bool | is_connected () const |
| Returns true if this is a valid GPIO pin. | |
| constexpr bool | is_output_capable () const |
| Returns true if this gpio supports output mode. | |
| constexpr bool | is_digital_io_pin_capable () const |
| Returns true if this gpio is a valid digital I/O pin. | |
| constexpr int | num () const |
| Returns the underlying GPIO pin number. | |
| constexpr gpio_num_t | idf_num () const |
| Returns the underlying ESP-IDF GPIO number. | |
| void | reset () |
| Resets the gpio to default state. | |
| void | set_direction (enum mode mode) |
| Sets the GPIO direction mode. | |
| void | input_enable () |
| Enables input on this gpio. | |
| void | set_pull_mode (enum pull_mode mode) |
| Sets the pull resistor mode. | |
| void | pullup_enable () |
| Enables the internal pull-up resistor. | |
| void | pullup_disable () |
| Disables the internal pull-up resistor. | |
| void | pulldown_enable () |
| Enables the internal pull-down resistor. | |
| void | pulldown_disable () |
| Disables the internal pull-down resistor. | |
| result< void > | try_set_direction (enum mode mode) |
| Sets the GPIO direction mode. | |
| result< void > | try_input_enable () |
| Enables input on this gpio. | |
| result< void > | try_set_pull_mode (enum pull_mode mode) |
| Sets the pull resistor mode. | |
| result< void > | try_pullup_enable () |
| Enables the internal pull-up resistor. | |
| result< void > | try_pullup_disable () |
| Disables the internal pull-up resistor. | |
| result< void > | try_pulldown_enable () |
| Enables the internal pull-down resistor. | |
| result< void > | try_pulldown_disable () |
| Disables the internal pull-down resistor. | |
| void | set_level (bool level) |
| Sets the output level. | |
| bool | get_level () const |
| Reads the current input level. | |
| void | set_drive_capability (enum drive_cap strength) |
| Sets the gpio drive capability. | |
| enum drive_cap | get_drive_capability () const |
| Gets the current drive capability. | |
| result< void > | try_set_drive_capability (enum drive_cap strength) |
| Sets the gpio drive capability. | |
| result< drive_cap > | try_get_drive_capability () const |
| Gets the current drive capability. | |
| isr_handle | isr_handler_add (std::move_only_function< void() const > handler) |
| Adds an ISR handler for this gpio. | |
| isr_handle | isr_handler_add (void(*fn)(void *), void *arg) |
| Adds an ISR handler for this gpio. | |
| void | isr_handler_remove (isr_handle handle) |
| Removes a specific ISR handler. | |
| void | isr_handler_remove_all () |
| Removes all ISR handlers for this gpio. | |
| result< isr_handle > | try_isr_handler_add (std::move_only_function< void() const > handler) |
| Adds an ISR handler for this gpio. | |
| result< isr_handle > | try_isr_handler_add (void(*fn)(void *), void *arg) |
| Adds an ISR handler for this gpio. | |
| result< void > | try_isr_handler_remove (isr_handle handle) |
| Removes a specific ISR handler. | |
| result< void > | try_isr_handler_remove_all () |
| Removes all ISR handlers for this gpio. | |
| void | set_intr_type (enum intr_type intr_type) |
| Sets the interrupt trigger type. | |
| void | intr_enable () |
| Enables interrupts for this gpio. | |
| void | intr_disable () |
| Disables interrupts for this gpio. | |
| result< void > | try_set_intr_type (enum intr_type intr_type) |
| Sets the interrupt trigger type. | |
| result< void > | try_intr_enable () |
| Enables interrupts for this gpio. | |
| result< void > | try_intr_disable () |
| Disables interrupts for this gpio. | |
| void | wakeup_enable (enum intr_type intr_type) |
| Enables GPIO wake-up from light sleep. | |
| void | wakeup_disable () |
| Disables GPIO wake-up. | |
| result< void > | try_wakeup_enable (enum intr_type intr_type) |
| Enables GPIO wake-up from light sleep. | |
| result< void > | try_wakeup_disable () |
| Disables GPIO wake-up. | |
| void | hold_enable () |
| Enables gpio hold function. | |
| void | hold_disable () |
| Disables gpio hold function. | |
| result< void > | try_hold_enable () |
| Enables gpio hold function. | |
| result< void > | try_hold_disable () |
| Disables gpio hold function. | |
| void | sleep_sel_enable () |
| Enables SLP_SEL to change GPIO status automatically in light sleep. | |
| void | sleep_sel_disable () |
| Disables SLP_SEL to change GPIO status automatically in light sleep. | |
| void | sleep_set_direction (enum mode mode) |
| Sets GPIO direction at sleep. | |
| void | sleep_set_pull_mode (enum pull_mode pull) |
| Sets pull resistor mode at sleep. | |
| result< void > | try_sleep_sel_enable () |
| Enables SLP_SEL to change GPIO status automatically in light sleep. | |
| result< void > | try_sleep_sel_disable () |
| Disables SLP_SEL to change GPIO status automatically in light sleep. | |
| result< void > | try_sleep_set_direction (enum mode mode) |
| Sets GPIO direction at sleep. | |
| result< void > | try_sleep_set_pull_mode (enum pull_mode pull) |
| Sets pull resistor mode at sleep. | |
Static Public Member Functions | |
| static result< gpio > | make (int num) |
| Creates a validated GPIO pin. | |
| static constexpr gpio | nc () |
| Returns a GPIO representing "not connected". | |
| static constexpr gpio | max () |
| Returns a GPIO for the highest valid pin number. | |
| static void | install_isr_service (flags< intr_flag > intr_alloc_flags={}) |
| Installs the GPIO ISR service for per-gpio interrupt handlers. | |
| static result< void > | try_install_isr_service (flags< intr_flag > intr_flags={}) |
| Installs the GPIO ISR service for per-gpio interrupt handlers. | |
| static void | uninstall_isr_service () |
| Uninstalls the GPIO ISR service, freeing related resources. | |
| static void | deep_sleep_hold_enable () |
| Enables hold for all digital GPIOs during deep sleep. | |
| static void | deep_sleep_hold_disable () |
| Disables hold for all digital GPIOs during deep sleep. | |
Friends | |
| template<int N> | |
| struct | gpio_constant |
A GPIO pin.
Lightweight, non-owning GPIO pin identifier. Provides type-safe configuration and supports both exception-throwing and result-returning error handling.
Construction is validated: only valid GPIO numbers (or nc) are allowed. Use gpio::make() for result-based construction, or the throwing constructor when exceptions are enabled.
For interrupt handling, first install the ISR service, then add handlers:
|
strong |
|
strong |
|
strong |
Interrupt trigger type.
|
strong |
GPIO direction mode.
|
strong |
|
inlineconstexpr |
|
explicit |
Constructs a validated GPIO pin.
| num | GPIO pin number. |
| std::system_error | if the pin number is invalid. |
|
default |
|
default |
|
inlinestatic |
|
inlinestatic |
Enables hold for all digital GPIOs during deep sleep.
The hold only takes effect during deep sleep. In active mode, GPIO state can still be changed. Each gpio must also have hold_enable() called for this to work.
|
inline |
Gets the current drive capability.
| std::system_error | on failure. |
Definition at line 427 of file gpio.hpp.
References try_get_drive_capability(), and idfxx::unwrap().
|
inline |
Reads the current input level.
|
inline |
Disables gpio hold function.
| std::system_error | on failure. |
Definition at line 671 of file gpio.hpp.
References try_hold_disable(), and idfxx::unwrap().
|
inline |
Enables gpio hold function.
When enabled, the gpio's state is latched and will not change when the internal signal or IO MUX/GPIO configuration is modified. Use this to retain GPIO state during sleep or reset. Only applicable to output-capable GPIOs.
| std::system_error | on failure. |
Definition at line 662 of file gpio.hpp.
References try_hold_enable(), and idfxx::unwrap().
|
inlineconstexpr |
|
inline |
Enables input on this gpio.
| std::system_error | on failure. |
Definition at line 307 of file gpio.hpp.
References try_input_enable(), and idfxx::unwrap().
|
inlinestatic |
Installs the GPIO ISR service for per-gpio interrupt handlers.
Must be called before adding handlers with isr_handler_add().
| intr_alloc_flags | Flags for interrupt allocation (ESP_INTR_FLAG_*). |
| std::system_error | on failure. |
|
inline |
Disables interrupts for this gpio.
| std::system_error | on failure. |
Definition at line 593 of file gpio.hpp.
References try_intr_disable(), and idfxx::unwrap().
|
inline |
Enables interrupts for this gpio.
| std::system_error | on failure. |
Definition at line 587 of file gpio.hpp.
References try_intr_enable(), and idfxx::unwrap().
|
inlineconstexpr |
Returns true if this is a valid GPIO pin.
Definition at line 262 of file gpio.hpp.
Referenced by reset(), idfxx::to_string(), and try_get_drive_capability().
|
inlineconstexpr |
|
inlineconstexpr |
|
inline |
Adds an ISR handler for this gpio.
The handler will be called from an ISR context. ISR handlers do not need IRAM_ATTR unless ESP_INTR_FLAG_IRAM was passed to install_isr_service().
| handler | Function to call on interrupt. |
| std::system_error | on failure, including not_supported if the ISR service was installed with ESP_INTR_FLAG_IRAM. |
Definition at line 501 of file gpio.hpp.
References try_isr_handler_add(), and idfxx::unwrap().
|
inline |
Adds an ISR handler for this gpio.
The handler will be called from an ISR context. ISR handlers do not need IRAM_ATTR unless ESP_INTR_FLAG_IRAM was passed to install_isr_service().
| fn | Function to call on interrupt. |
| arg | Argument passed to the handler. |
| std::system_error | on failure. |
Definition at line 517 of file gpio.hpp.
References try_isr_handler_add(), and idfxx::unwrap().
|
inline |
Removes a specific ISR handler.
| std::system_error | on failure. |
Definition at line 523 of file gpio.hpp.
References try_isr_handler_remove(), and idfxx::unwrap().
|
inline |
Removes all ISR handlers for this gpio.
| std::system_error | on failure. |
Definition at line 529 of file gpio.hpp.
References try_isr_handler_remove_all(), and idfxx::unwrap().
Creates a validated GPIO pin.
| num | GPIO pin number. |
|
inlinestaticconstexpr |
|
inlinestaticconstexpr |
|
inlineconstexpr |
Returns the underlying GPIO pin number.
Definition at line 273 of file gpio.hpp.
Referenced by idfxx::to_string().
|
constexprdefault |
|
inline |
Disables the internal pull-down resistor.
| std::system_error | on failure. |
Definition at line 339 of file gpio.hpp.
References try_pulldown_disable(), and idfxx::unwrap().
|
inline |
Enables the internal pull-down resistor.
| std::system_error | on failure. |
Definition at line 333 of file gpio.hpp.
References try_pulldown_enable(), and idfxx::unwrap().
|
inline |
Disables the internal pull-up resistor.
| std::system_error | on failure. |
Definition at line 327 of file gpio.hpp.
References try_pullup_disable(), and idfxx::unwrap().
|
inline |
Enables the internal pull-up resistor.
| std::system_error | on failure. |
Definition at line 321 of file gpio.hpp.
References try_pullup_enable(), and idfxx::unwrap().
|
inline |
Resets the gpio to default state.
Selects GPIO function, enables pullup, and disables input and output.
Definition at line 285 of file gpio.hpp.
References is_connected().
|
inline |
Sets the GPIO direction mode.
| std::system_error | on failure. |
Definition at line 300 of file gpio.hpp.
References try_set_direction(), and idfxx::unwrap().
|
inline |
Sets the gpio drive capability.
| std::system_error | on failure. |
Definition at line 421 of file gpio.hpp.
References try_set_drive_capability(), and idfxx::unwrap().
|
inline |
Sets the interrupt trigger type.
| std::system_error | on failure. |
Definition at line 581 of file gpio.hpp.
References try_set_intr_type(), and idfxx::unwrap().
|
inline |
|
inline |
Sets the pull resistor mode.
| std::system_error | on failure. |
Definition at line 315 of file gpio.hpp.
References try_set_pull_mode(), and idfxx::unwrap().
|
inline |
Disables SLP_SEL to change GPIO status automatically in light sleep.
| std::system_error | on failure. |
Definition at line 718 of file gpio.hpp.
References try_sleep_sel_disable(), and idfxx::unwrap().
|
inline |
Enables SLP_SEL to change GPIO status automatically in light sleep.
| std::system_error | on failure. |
Definition at line 712 of file gpio.hpp.
References try_sleep_sel_enable(), and idfxx::unwrap().
|
inline |
Sets GPIO direction at sleep.
| std::system_error | on failure. |
Definition at line 724 of file gpio.hpp.
References try_sleep_set_direction(), and idfxx::unwrap().
|
inline |
Sets pull resistor mode at sleep.
| std::system_error | on failure. |
Definition at line 732 of file gpio.hpp.
References try_sleep_set_pull_mode(), and idfxx::unwrap().
Gets the current drive capability.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If called on a gpio that is not output-capable. |
Definition at line 442 of file gpio.hpp.
References idfxx::error(), idfxx::invalid_state, is_connected(), and idfxx::wrap().
Referenced by get_drive_capability().
|
inline |
Disables gpio hold function.
| invalid_state | If called on gpio::nc(). |
| not_supported | If the GPIO doesn't support hold. |
Definition at line 694 of file gpio.hpp.
Referenced by hold_disable().
|
inline |
Enables gpio hold function.
When enabled, the gpio's state is latched and will not change when the internal signal or IO MUX/GPIO configuration is modified. Use this to retain GPIO state during sleep or reset. Only applicable to output-capable GPIOs.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If the GPIO doesn't support hold. |
Definition at line 685 of file gpio.hpp.
Referenced by hold_enable().
|
inline |
Enables input on this gpio.
| invalid_state | If called on gpio::nc(). |
Definition at line 355 of file gpio.hpp.
Referenced by input_enable().
|
static |
Installs the GPIO ISR service for per-gpio interrupt handlers.
Must be called before adding handlers with isr_handler_add().
| intr_flags | Flags for interrupt allocation (ESP_INTR_FLAG_*). |
|
inline |
Disables interrupts for this gpio.
| invalid_state | If called on gpio::nc(). |
Definition at line 614 of file gpio.hpp.
Referenced by intr_disable().
|
inline |
Enables interrupts for this gpio.
| invalid_state | If called on gpio::nc(). |
Definition at line 608 of file gpio.hpp.
Referenced by intr_enable().
| result< isr_handle > idfxx::gpio::try_isr_handler_add | ( | std::move_only_function< void() const > | handler | ) |
Adds an ISR handler for this gpio.
The handler will be called from an ISR context. ISR handlers do not need IRAM_ATTR unless ESP_INTR_FLAG_IRAM was passed to install_isr_service().
| handler | Function to call on interrupt. |
| not_supported | If the ISR service was installed with ESP_INTR_FLAG_IRAM. |
Referenced by isr_handler_add(), and isr_handler_add().
| result< isr_handle > idfxx::gpio::try_isr_handler_add | ( | void(*)(void *) | fn, |
| void * | arg | ||
| ) |
Adds an ISR handler for this gpio.
The handler will be called from an ISR context. ISR handlers do not need IRAM_ATTR unless ESP_INTR_FLAG_IRAM was passed to install_isr_service().
| fn | Function to call on interrupt. |
| arg | Argument passed to the handler. |
| result< void > idfxx::gpio::try_isr_handler_remove | ( | isr_handle | handle | ) |
Removes a specific ISR handler.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If the handle is not valid for this gpio. |
Referenced by isr_handler_remove().
| result< void > idfxx::gpio::try_isr_handler_remove_all | ( | ) |
Removes all ISR handlers for this gpio.
| invalid_state | If called on gpio::nc(). |
Referenced by isr_handler_remove_all().
|
inline |
Disables the internal pull-down resistor.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If the GPIO doesn't support pull resistors. |
Definition at line 397 of file gpio.hpp.
Referenced by pulldown_disable().
|
inline |
Enables the internal pull-down resistor.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If the GPIO doesn't support pull resistors. |
Definition at line 389 of file gpio.hpp.
Referenced by pulldown_enable().
|
inline |
Disables the internal pull-up resistor.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If the GPIO doesn't support pull resistors. |
Definition at line 381 of file gpio.hpp.
Referenced by pullup_disable().
|
inline |
Enables the internal pull-up resistor.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If the GPIO doesn't support pull resistors. |
Definition at line 373 of file gpio.hpp.
Referenced by pullup_enable().
Sets the GPIO direction mode.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If the gpio is input-only and an output mode is requested. |
Definition at line 347 of file gpio.hpp.
Referenced by set_direction().
Sets the gpio drive capability.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If called on a gpio that is not output-capable. |
Definition at line 434 of file gpio.hpp.
Referenced by set_drive_capability().
Sets the interrupt trigger type.
| invalid_state | If called on gpio::nc(). |
Definition at line 600 of file gpio.hpp.
Referenced by set_intr_type().
Sets the pull resistor mode.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If the GPIO doesn't support pull resistors. |
Definition at line 365 of file gpio.hpp.
Referenced by set_pull_mode().
|
inline |
Disables SLP_SEL to change GPIO status automatically in light sleep.
Definition at line 743 of file gpio.hpp.
Referenced by sleep_sel_disable().
|
inline |
Enables SLP_SEL to change GPIO status automatically in light sleep.
Definition at line 738 of file gpio.hpp.
Referenced by sleep_sel_enable().
Sets GPIO direction at sleep.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If the gpio is input-only and an output mode is requested. |
Definition at line 749 of file gpio.hpp.
Referenced by sleep_set_direction().
Sets pull resistor mode at sleep.
| invalid_state | If called on gpio::nc(). |
Definition at line 759 of file gpio.hpp.
Referenced by sleep_set_pull_mode().
|
inline |
Disables GPIO wake-up.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If the GPIO is not an RTC GPIO. |
Definition at line 646 of file gpio.hpp.
Referenced by wakeup_disable().
Enables GPIO wake-up from light sleep.
| intr_type | Only low_level or high_level can be used. |
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If intr_type is not low_level or high_level. |
Definition at line 638 of file gpio.hpp.
Referenced by wakeup_enable().
|
static |
Uninstalls the GPIO ISR service, freeing related resources.
All registered ISR handlers are removed and internal state is reset.
|
inline |
Disables GPIO wake-up.
| std::system_error | on failure. |
Definition at line 630 of file gpio.hpp.
References try_wakeup_disable(), and idfxx::unwrap().
|
inline |
Enables GPIO wake-up from light sleep.
| intr_type | Only low_level or high_level can be used. |
| std::system_error | on failure. |
Definition at line 624 of file gpio.hpp.
References try_wakeup_enable(), and idfxx::unwrap().