Files
xiaozhi-esp32/main/Button.cc
2024-10-03 06:39:22 +08:00

68 lines
2.0 KiB
C++

#include "Button.h"
#include <esp_log.h>
static const char* TAG = "Button";
Button::Button(gpio_num_t gpio_num) : gpio_num_(gpio_num) {
button_config_t button_config = {
.type = BUTTON_TYPE_GPIO,
.long_press_time = 3000,
.short_press_time = 100,
.gpio_button_config = {
.gpio_num = gpio_num,
.active_level = 0
}
};
button_handle_ = iot_button_create(&button_config);
if (button_handle_ == NULL) {
ESP_LOGE(TAG, "Failed to create button handle");
return;
}
}
Button::~Button() {
if (button_handle_ != NULL) {
iot_button_delete(button_handle_);
}
}
void Button::OnPress(std::function<void()> callback) {
on_press_ = callback;
iot_button_register_cb(button_handle_, BUTTON_PRESS_DOWN, [](void* handle, void* usr_data) {
Button* button = static_cast<Button*>(usr_data);
if (button->on_press_) {
button->on_press_();
}
}, this);
}
void Button::OnLongPress(std::function<void()> callback) {
on_long_press_ = callback;
iot_button_register_cb(button_handle_, BUTTON_LONG_PRESS_START, [](void* handle, void* usr_data) {
Button* button = static_cast<Button*>(usr_data);
if (button->on_long_press_) {
button->on_long_press_();
}
}, this);
}
void Button::OnClick(std::function<void()> callback) {
on_click_ = callback;
iot_button_register_cb(button_handle_, BUTTON_SINGLE_CLICK, [](void* handle, void* usr_data) {
Button* button = static_cast<Button*>(usr_data);
if (button->on_click_) {
button->on_click_();
}
}, this);
}
void Button::OnDoubleClick(std::function<void()> callback) {
on_double_click_ = callback;
iot_button_register_cb(button_handle_, BUTTON_DOUBLE_CLICK, [](void* handle, void* usr_data) {
Button* button = static_cast<Button*>(usr_data);
if (button->on_double_click_) {
button->on_double_click_();
}
}, this);
}