11#include " button.h"
22
3+ #include < button_gpio.h>
34#include < esp_log.h>
45
5- static const char * TAG = " Button" ;
6- #if CONFIG_SOC_ADC_SUPPORTED
7- Button::Button (const button_adc_config_t & adc_cfg) {
8- button_config_t button_config = {
9- .type = BUTTON_TYPE_ADC,
10- .long_press_time = 1000 ,
11- .short_press_time = 50 ,
12- .adc_button_config = adc_cfg
13- };
14- button_handle_ = iot_button_create (&button_config);
15- if (button_handle_ == NULL ) {
16- ESP_LOGE (TAG, " Failed to create button handle" );
17- return ;
18- }
6+ #define TAG " Button"
7+
8+ Button::Button (button_handle_t button_handle) : button_handle_(button_handle) {
199}
20- #endif
2110
22- Button::Button (gpio_num_t gpio_num, bool active_high) : gpio_num_(gpio_num) {
11+ Button::Button (gpio_num_t gpio_num, bool active_high, uint16_t long_press_time, uint16_t short_press_time ) : gpio_num_(gpio_num) {
2312 if (gpio_num == GPIO_NUM_NC) {
2413 return ;
2514 }
2615 button_config_t button_config = {
27- .type = BUTTON_TYPE_GPIO,
28- .long_press_time = 1000 ,
29- .short_press_time = 50 ,
30- .gpio_button_config = {
31- .gpio_num = gpio_num,
32- .active_level = static_cast <uint8_t >(active_high ? 1 : 0 )
33- }
16+ .long_press_time = long_press_time,
17+ .short_press_time = short_press_time
3418 };
35- button_handle_ = iot_button_create (&button_config);
36- if (button_handle_ == NULL ) {
37- ESP_LOGE (TAG, " Failed to create button handle" );
38- return ;
39- }
19+ button_gpio_config_t gpio_config = {
20+ .gpio_num = gpio_num,
21+ .active_level = static_cast <uint8_t >(active_high ? 1 : 0 ),
22+ .enable_power_save = false ,
23+ .disable_pull = false
24+ };
25+ ESP_ERROR_CHECK (iot_button_new_gpio_device (&button_config, &gpio_config, &button_handle_));
4026}
4127
4228Button::~Button () {
@@ -50,7 +36,7 @@ void Button::OnPressDown(std::function<void()> callback) {
5036 return ;
5137 }
5238 on_press_down_ = callback;
53- iot_button_register_cb (button_handle_, BUTTON_PRESS_DOWN, [](void * handle, void * usr_data) {
39+ iot_button_register_cb (button_handle_, BUTTON_PRESS_DOWN, nullptr , [](void * handle, void * usr_data) {
5440 Button* button = static_cast <Button*>(usr_data);
5541 if (button->on_press_down_ ) {
5642 button->on_press_down_ ();
@@ -63,7 +49,7 @@ void Button::OnPressUp(std::function<void()> callback) {
6349 return ;
6450 }
6551 on_press_up_ = callback;
66- iot_button_register_cb (button_handle_, BUTTON_PRESS_UP, [](void * handle, void * usr_data) {
52+ iot_button_register_cb (button_handle_, BUTTON_PRESS_UP, nullptr , [](void * handle, void * usr_data) {
6753 Button* button = static_cast <Button*>(usr_data);
6854 if (button->on_press_up_ ) {
6955 button->on_press_up_ ();
@@ -76,7 +62,7 @@ void Button::OnLongPress(std::function<void()> callback) {
7662 return ;
7763 }
7864 on_long_press_ = callback;
79- iot_button_register_cb (button_handle_, BUTTON_LONG_PRESS_START, [](void * handle, void * usr_data) {
65+ iot_button_register_cb (button_handle_, BUTTON_LONG_PRESS_START, nullptr , [](void * handle, void * usr_data) {
8066 Button* button = static_cast <Button*>(usr_data);
8167 if (button->on_long_press_ ) {
8268 button->on_long_press_ ();
@@ -89,7 +75,7 @@ void Button::OnClick(std::function<void()> callback) {
8975 return ;
9076 }
9177 on_click_ = callback;
92- iot_button_register_cb (button_handle_, BUTTON_SINGLE_CLICK, [](void * handle, void * usr_data) {
78+ iot_button_register_cb (button_handle_, BUTTON_SINGLE_CLICK, nullptr , [](void * handle, void * usr_data) {
9379 Button* button = static_cast <Button*>(usr_data);
9480 if (button->on_click_ ) {
9581 button->on_click_ ();
@@ -102,10 +88,28 @@ void Button::OnDoubleClick(std::function<void()> callback) {
10288 return ;
10389 }
10490 on_double_click_ = callback;
105- iot_button_register_cb (button_handle_, BUTTON_DOUBLE_CLICK, [](void * handle, void * usr_data) {
91+ iot_button_register_cb (button_handle_, BUTTON_DOUBLE_CLICK, nullptr , [](void * handle, void * usr_data) {
10692 Button* button = static_cast <Button*>(usr_data);
10793 if (button->on_double_click_ ) {
10894 button->on_double_click_ ();
10995 }
11096 }, this );
11197}
98+
99+ void Button::OnMultipleClick (std::function<void ()> callback, uint8_t click_count) {
100+ if (button_handle_ == nullptr ) {
101+ return ;
102+ }
103+ on_multiple_click_ = callback;
104+ button_event_args_t event_args = {
105+ .multiple_clicks = {
106+ .clicks = click_count
107+ }
108+ };
109+ iot_button_register_cb (button_handle_, BUTTON_MULTIPLE_CLICK, &event_args, [](void * handle, void * usr_data) {
110+ Button* button = static_cast <Button*>(usr_data);
111+ if (button->on_multiple_click_ ) {
112+ button->on_multiple_click_ ();
113+ }
114+ }, this );
115+ }
0 commit comments