2024-11-05 20:15:00 +08:00
|
|
|
#include "button.h"
|
|
|
|
|
|
2025-05-07 04:55:51 +08:00
|
|
|
#include <button_gpio.h>
|
2024-10-03 06:39:22 +08:00
|
|
|
#include <esp_log.h>
|
|
|
|
|
|
2025-05-07 04:55:51 +08:00
|
|
|
#define TAG "Button"
|
|
|
|
|
|
2025-05-07 15:09:43 +08:00
|
|
|
#if CONFIG_SOC_ADC_SUPPORTED
|
|
|
|
|
AdcButton::AdcButton(const button_adc_config_t& adc_config) : Button(nullptr) {
|
|
|
|
|
button_config_t btn_config = {
|
|
|
|
|
.long_press_time = 2000,
|
2025-05-07 16:42:23 +08:00
|
|
|
.short_press_time = 0,
|
2025-05-07 15:09:43 +08:00
|
|
|
};
|
|
|
|
|
ESP_ERROR_CHECK(iot_button_new_adc_device(&btn_config, &adc_config, &button_handle_));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-05-07 04:55:51 +08:00
|
|
|
Button::Button(button_handle_t button_handle) : button_handle_(button_handle) {
|
2025-03-07 21:53:29 +08:00
|
|
|
}
|
2024-10-03 06:39:22 +08:00
|
|
|
|
2025-07-19 22:45:22 +08:00
|
|
|
Button::Button(gpio_num_t gpio_num, bool active_high, uint16_t long_press_time, uint16_t short_press_time, bool enable_power_save) : gpio_num_(gpio_num) {
|
2024-10-29 00:22:29 +08:00
|
|
|
if (gpio_num == GPIO_NUM_NC) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-10-03 06:39:22 +08:00
|
|
|
button_config_t button_config = {
|
2025-05-07 04:55:51 +08:00
|
|
|
.long_press_time = long_press_time,
|
|
|
|
|
.short_press_time = short_press_time
|
2024-10-03 06:39:22 +08:00
|
|
|
};
|
2025-05-07 04:55:51 +08:00
|
|
|
button_gpio_config_t gpio_config = {
|
|
|
|
|
.gpio_num = gpio_num,
|
|
|
|
|
.active_level = static_cast<uint8_t>(active_high ? 1 : 0),
|
2025-07-19 22:45:22 +08:00
|
|
|
.enable_power_save = enable_power_save,
|
2025-05-07 04:55:51 +08:00
|
|
|
.disable_pull = false
|
|
|
|
|
};
|
|
|
|
|
ESP_ERROR_CHECK(iot_button_new_gpio_device(&button_config, &gpio_config, &button_handle_));
|
2024-10-03 06:39:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Button::~Button() {
|
|
|
|
|
if (button_handle_ != NULL) {
|
|
|
|
|
iot_button_delete(button_handle_);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-23 16:14:24 +08:00
|
|
|
void Button::OnPressDown(std::function<void()> callback) {
|
2024-10-29 00:22:29 +08:00
|
|
|
if (button_handle_ == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-11-23 16:14:24 +08:00
|
|
|
on_press_down_ = callback;
|
2025-05-07 04:55:51 +08:00
|
|
|
iot_button_register_cb(button_handle_, BUTTON_PRESS_DOWN, nullptr, [](void* handle, void* usr_data) {
|
2024-10-03 06:39:22 +08:00
|
|
|
Button* button = static_cast<Button*>(usr_data);
|
2024-11-23 16:14:24 +08:00
|
|
|
if (button->on_press_down_) {
|
|
|
|
|
button->on_press_down_();
|
|
|
|
|
}
|
|
|
|
|
}, this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Button::OnPressUp(std::function<void()> callback) {
|
|
|
|
|
if (button_handle_ == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
on_press_up_ = callback;
|
2025-05-07 04:55:51 +08:00
|
|
|
iot_button_register_cb(button_handle_, BUTTON_PRESS_UP, nullptr, [](void* handle, void* usr_data) {
|
2024-11-23 16:14:24 +08:00
|
|
|
Button* button = static_cast<Button*>(usr_data);
|
|
|
|
|
if (button->on_press_up_) {
|
|
|
|
|
button->on_press_up_();
|
2024-10-03 06:39:22 +08:00
|
|
|
}
|
|
|
|
|
}, this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Button::OnLongPress(std::function<void()> callback) {
|
2024-10-29 00:22:29 +08:00
|
|
|
if (button_handle_ == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-10-03 06:39:22 +08:00
|
|
|
on_long_press_ = callback;
|
2025-05-07 04:55:51 +08:00
|
|
|
iot_button_register_cb(button_handle_, BUTTON_LONG_PRESS_START, nullptr, [](void* handle, void* usr_data) {
|
2024-10-03 06:39:22 +08:00
|
|
|
Button* button = static_cast<Button*>(usr_data);
|
|
|
|
|
if (button->on_long_press_) {
|
|
|
|
|
button->on_long_press_();
|
|
|
|
|
}
|
|
|
|
|
}, this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Button::OnClick(std::function<void()> callback) {
|
2024-10-29 00:22:29 +08:00
|
|
|
if (button_handle_ == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-10-03 06:39:22 +08:00
|
|
|
on_click_ = callback;
|
2025-05-07 04:55:51 +08:00
|
|
|
iot_button_register_cb(button_handle_, BUTTON_SINGLE_CLICK, nullptr, [](void* handle, void* usr_data) {
|
2024-10-03 06:39:22 +08:00
|
|
|
Button* button = static_cast<Button*>(usr_data);
|
|
|
|
|
if (button->on_click_) {
|
|
|
|
|
button->on_click_();
|
|
|
|
|
}
|
|
|
|
|
}, this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Button::OnDoubleClick(std::function<void()> callback) {
|
2024-10-29 00:22:29 +08:00
|
|
|
if (button_handle_ == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-10-03 06:39:22 +08:00
|
|
|
on_double_click_ = callback;
|
2025-05-07 04:55:51 +08:00
|
|
|
iot_button_register_cb(button_handle_, BUTTON_DOUBLE_CLICK, nullptr, [](void* handle, void* usr_data) {
|
2024-10-03 06:39:22 +08:00
|
|
|
Button* button = static_cast<Button*>(usr_data);
|
|
|
|
|
if (button->on_double_click_) {
|
|
|
|
|
button->on_double_click_();
|
|
|
|
|
}
|
|
|
|
|
}, this);
|
|
|
|
|
}
|
2025-05-07 04:55:51 +08:00
|
|
|
|
|
|
|
|
void Button::OnMultipleClick(std::function<void()> callback, uint8_t click_count) {
|
|
|
|
|
if (button_handle_ == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
on_multiple_click_ = callback;
|
|
|
|
|
button_event_args_t event_args = {
|
|
|
|
|
.multiple_clicks = {
|
|
|
|
|
.clicks = click_count
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
iot_button_register_cb(button_handle_, BUTTON_MULTIPLE_CLICK, &event_args, [](void* handle, void* usr_data) {
|
|
|
|
|
Button* button = static_cast<Button*>(usr_data);
|
|
|
|
|
if (button->on_multiple_click_) {
|
|
|
|
|
button->on_multiple_click_();
|
|
|
|
|
}
|
|
|
|
|
}, this);
|
|
|
|
|
}
|