idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
button.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/gpio>
23
24#include <chrono>
25#include <functional>
26
27namespace idfxx {
28
38class button {
39public:
44 enum class mode : int {
45 poll = 0,
46 interrupt = 1,
47 };
48
62 enum class event_type : int {
63 pressed = 0,
64 released = 1,
65 clicked = 2,
66 long_press = 3,
67 };
68
86 struct config {
90 bool enable_pull = true;
91 bool autorepeat = false;
92 std::chrono::microseconds dead_time{std::chrono::milliseconds{50}};
93 std::chrono::microseconds long_press_time{std::chrono::milliseconds{1000}};
94 std::chrono::microseconds autorepeat_timeout{std::chrono::milliseconds{500}};
95 std::chrono::microseconds autorepeat_interval{std::chrono::milliseconds{250}};
96 std::chrono::microseconds poll_interval{std::chrono::milliseconds{10}};
97 std::move_only_function<void(event_type)> callback;
98 };
99
100#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
109 [[nodiscard]] explicit button(config cfg);
110#endif
111
119 [[nodiscard]] static result<button> make(config cfg);
120
127
128 button(const button&) = delete;
129 button& operator=(const button&) = delete;
130 button(button&& other) noexcept;
131 button& operator=(button&& other) noexcept;
132
133private:
134 explicit button(void* ctx);
135
136 void _delete() noexcept;
137
138 void* _context = nullptr;
139};
140
// end of idfxx_button
142
143} // namespace idfxx
GPIO push-button with debounce, click, long press, and autorepeat support.
Definition button.hpp:38
button(config cfg)
Creates a button and begins monitoring.
button & operator=(const button &)=delete
mode
Button detection mode.
Definition button.hpp:44
@ interrupt
GPIO interrupt with debounce timer.
@ poll
Periodic timer polling (default)
button & operator=(button &&other) noexcept
event_type
Button event type.
Definition button.hpp:62
@ released
Button released.
@ long_press
Button held beyond long-press threshold.
@ pressed
Button pressed.
@ clicked
Short press completed (pressed then released)
static result< button > make(config cfg)
Creates a button and begins monitoring.
button(const button &)=delete
button(button &&other) noexcept
~button()
Destroys the button.
A GPIO pin.
Definition gpio.hpp:61
static constexpr gpio nc()
Returns a GPIO representing "not connected".
Definition gpio.hpp:254
level
GPIO output/input level.
Definition gpio.hpp:67
@ low
Logic low (0)
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120
Button configuration.
Definition button.hpp:86
bool enable_pull
Enable internal pull resistor.
Definition button.hpp:90
std::move_only_function< void(event_type)> callback
Event callback (required)
Definition button.hpp:97
std::chrono::microseconds long_press_time
Long press threshold.
Definition button.hpp:93
bool autorepeat
Enable autorepeat (disables long press)
Definition button.hpp:91
std::chrono::microseconds autorepeat_interval
Autorepeat repeat interval.
Definition button.hpp:95
std::chrono::microseconds poll_interval
Polling interval.
Definition button.hpp:96
gpio::level pressed_level
GPIO level when pressed.
Definition button.hpp:89
std::chrono::microseconds autorepeat_timeout
Autorepeat start delay.
Definition button.hpp:94
idfxx::gpio pin
GPIO pin (required)
Definition button.hpp:87
std::chrono::microseconds dead_time
Debounce delay.
Definition button.hpp:92