|
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 | level : int { low = 0 , high = 1 } |
| GPIO output/input level. More... | |
| 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 &) const noexcept=default |
| Compares two GPIO pins for equality. | |
| 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 | 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. | |
| void | 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 (enum level level) |
| Sets the output level. | |
| enum level | get_level () const |
| Reads the current input level. | |
| void | toggle_level () |
| Toggles the output 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. | |
| 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. | |
| void | 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. | |
| 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. | |
| result< void > | try_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. | |
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 (idfxx::intr_levels levels=intr_level_lowmed, idfxx::flags< intr_flag > flags={}) |
| Installs the GPIO ISR service for per-gpio interrupt handlers. | |
| static void | install_isr_service (idfxx::flags< intr_flag > flags) |
| Installs the GPIO ISR service with default priority levels. | |
| static result< void > | try_install_isr_service (idfxx::intr_levels levels=intr_level_lowmed, idfxx::flags< intr_flag > flags={}) |
| Installs the GPIO ISR service for per-gpio interrupt handlers. | |
| static result< void > | try_install_isr_service (idfxx::flags< intr_flag > flags) |
| Installs the GPIO ISR service with default priority levels. | |
| 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 |
|
strong |
|
strong |
|
strong |
|
inlineconstexpr |
|
explicit |
Constructs a validated GPIO pin.
| num | GPIO pin number. |
| std::system_error | if the pin number is invalid. |
|
default |
Disables hold for all digital GPIOs during deep sleep.
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.
Gets the current drive capability.
| std::system_error | on failure. |
Definition at line 417 of file gpio.hpp.
References try_get_drive_capability(), and idfxx::unwrap().
Reads the current input level.
|
inline |
Disables gpio hold function.
| std::system_error | on failure. |
Definition at line 643 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 634 of file gpio.hpp.
References try_hold_enable(), and idfxx::unwrap().
|
inlineconstexpr |
| void idfxx::gpio::input_enable | ( | ) |
Enables input on this gpio.
|
inlinestatic |
Installs the GPIO ISR service with default priority levels.
Uses low/medium priority levels (1-3). Equivalent to calling install_isr_service(intr_level_lowmed, flags).
| flags | Behavioral flags (e.g., intr_flag::iram). |
| std::system_error | on failure. |
Definition at line 457 of file gpio.hpp.
References try_install_isr_service(), and idfxx::unwrap().
|
inlinestatic |
Installs the GPIO ISR service for per-gpio interrupt handlers.
Must be called before adding handlers with isr_handler_add().
| levels | Interrupt priority levels to accept. |
| flags | Behavioral flags (e.g., intr_flag::iram). |
| std::system_error | on failure. |
| void idfxx::gpio::intr_disable | ( | ) |
Disables interrupts for this gpio.
| void idfxx::gpio::intr_enable | ( | ) |
Enables interrupts for this gpio.
|
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 intr_flag::iram was passed to install_isr_service().
intr_flag::iram was passed to install_isr_service(), as std::move_only_function may allocate memory outside of IRAM. Registration will fail with not_supported. Use the raw function pointer overload instead.| handler | Function to call on interrupt. |
| std::system_error | on failure, including not_supported if the ISR service was installed with intr_flag::iram. |
Definition at line 507 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 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 523 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 529 of file gpio.hpp.
References try_isr_handler_remove(), and idfxx::unwrap().
| void idfxx::gpio::isr_handler_remove_all | ( | ) |
Removes all ISR handlers for this gpio.
Creates a validated GPIO pin.
| num | GPIO pin number. |
Compares two GPIO pins for equality.
|
inline |
Disables the internal pull-down resistor.
| std::system_error | on failure. |
Definition at line 332 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 326 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 320 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 314 of file gpio.hpp.
References try_pullup_enable(), and idfxx::unwrap().
| void idfxx::gpio::reset | ( | ) |
Resets the gpio to default state.
Selects GPIO function, enables pullup, and disables input and output.
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().
Sets the gpio drive capability.
| std::system_error | on failure. |
Definition at line 411 of file gpio.hpp.
References try_set_drive_capability(), and idfxx::unwrap().
Sets the interrupt trigger type.
Sets the output level.
| level | The level to set (gpio::level::low or gpio::level::high). |
Sets the pull resistor mode.
| std::system_error | on failure. |
Definition at line 308 of file gpio.hpp.
References try_set_pull_mode(), and idfxx::unwrap().
| void idfxx::gpio::sleep_sel_disable | ( | ) |
Disables SLP_SEL to change GPIO status automatically in light sleep.
| void idfxx::gpio::sleep_sel_enable | ( | ) |
Enables SLP_SEL to change GPIO status automatically in light sleep.
Sets GPIO direction at sleep.
| std::system_error | on failure. |
Definition at line 694 of file gpio.hpp.
References try_sleep_set_direction(), and idfxx::unwrap().
Sets pull resistor mode at sleep.
| void idfxx::gpio::toggle_level | ( | ) |
Toggles the output level.
Reads the current level and sets the output to the opposite level.
Gets the current drive capability.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If called on a gpio that is not output-capable. |
Referenced by get_drive_capability().
Disables gpio hold function.
| invalid_state | If called on gpio::nc(). |
| not_supported | If the GPIO doesn't support hold. |
Referenced by hold_disable().
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. |
Referenced by hold_enable().
|
inlinestatic |
Installs the GPIO ISR service with default priority levels.
Uses low/medium priority levels (1-3). Equivalent to calling try_install_isr_service(intr_level_lowmed, flags).
| flags | Behavioral flags (e.g., intr_flag::iram). |
Definition at line 477 of file gpio.hpp.
References idfxx::intr_level_lowmed, and try_install_isr_service().
|
static |
Installs the GPIO ISR service for per-gpio interrupt handlers.
Must be called before adding handlers with isr_handler_add().
| levels | Interrupt priority levels to accept. |
| flags | Behavioral flags (e.g., intr_flag::iram). |
Referenced by install_isr_service(), and try_install_isr_service().
| 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 intr_flag::iram was passed to install_isr_service().
intr_flag::iram was passed to install_isr_service(), as std::move_only_function may allocate memory outside of IRAM. Registration will fail with not_supported. Use the raw function pointer overload instead.| handler | Function to call on interrupt. |
| not_supported | If the ISR service was installed with 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 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().
Disables the internal pull-down resistor.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If the GPIO doesn't support pull resistors. |
Referenced by pulldown_disable().
Enables the internal pull-down resistor.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If the GPIO doesn't support pull resistors. |
Referenced by pulldown_enable().
Disables the internal pull-up resistor.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If the GPIO doesn't support pull resistors. |
Referenced by pullup_disable().
Enables the internal pull-up resistor.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If the GPIO doesn't support pull resistors. |
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. |
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. |
Referenced by set_drive_capability().
Sets the pull resistor mode.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If the GPIO doesn't support pull resistors. |
Referenced by set_pull_mode().
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. |
Referenced by sleep_set_direction().
Disables GPIO wake-up.
| invalid_state | If called on gpio::nc(). |
| invalid_arg | If the GPIO is not an RTC GPIO. |
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. |
Referenced by wakeup_enable().
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 604 of file gpio.hpp.
References try_wakeup_disable(), and idfxx::unwrap().
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 598 of file gpio.hpp.
References try_wakeup_enable(), and idfxx::unwrap().