#include "button.h" #include "iot_button.h" #include "button_gpio.h" #include "esp_log.h" static const char *TAG = "BTN"; // 回调存储 typedef struct { btn_event_cb_t cb; void *usr_data; } btn_cb_t; static btn_cb_t boot_cb = {0}; static btn_cb_t key2_cb = {0}; // iot_button句柄 static button_handle_t boot_btn_handle = NULL; static button_handle_t key2_btn_handle = NULL; // BOOT按键回调 static void boot_click_cb(void *arg, void *usr_data) { ESP_LOGI(TAG, "BOOT单击"); if (boot_cb.cb) boot_cb.cb(PIN_BTN_BOOT, BTN_EVT_CLICK, boot_cb.usr_data); } static void boot_double_click_cb(void *arg, void *usr_data) { ESP_LOGI(TAG, "BOOT双击"); if (boot_cb.cb) boot_cb.cb(PIN_BTN_BOOT, BTN_EVT_DOUBLE_CLICK, boot_cb.usr_data); } static void boot_long_press_cb(void *arg, void *usr_data) { ESP_LOGI(TAG, "BOOT长按"); if (boot_cb.cb) boot_cb.cb(PIN_BTN_BOOT, BTN_EVT_LONG_PRESS, boot_cb.usr_data); } // KEY2按键回调 static void key2_click_cb(void *arg, void *usr_data) { ESP_LOGI(TAG, "KEY2单击"); if (key2_cb.cb) key2_cb.cb(PIN_BTN_KEY2, BTN_EVT_CLICK, key2_cb.usr_data); } static void key2_double_click_cb(void *arg, void *usr_data) { ESP_LOGI(TAG, "KEY2双击"); if (key2_cb.cb) key2_cb.cb(PIN_BTN_KEY2, BTN_EVT_DOUBLE_CLICK, key2_cb.usr_data); } static void key2_long_press_cb(void *arg, void *usr_data) { ESP_LOGI(TAG, "KEY2长按"); if (key2_cb.cb) key2_cb.cb(PIN_BTN_KEY2, BTN_EVT_LONG_PRESS, key2_cb.usr_data); } esp_err_t button_init(void) { esp_err_t ret; // 通用按键配置 button_config_t btn_cfg = { .long_press_time = 2000, // 长按2秒 .short_press_time = 0, // 使用默认180ms(双击检测窗口,非去抖) }; // BOOT按键GPIO配置(GPIO9,低电平有效) button_gpio_config_t boot_gpio_cfg = { .gpio_num = PIN_BTN_BOOT, .active_level = 0, }; ret = iot_button_new_gpio_device(&btn_cfg, &boot_gpio_cfg, &boot_btn_handle); if (ret != ESP_OK) { ESP_LOGE(TAG, "BOOT按键创建失败: %s", esp_err_to_name(ret)); return ret; } // KEY2按键GPIO配置(GPIO8,低电平有效) button_gpio_config_t key2_gpio_cfg = { .gpio_num = PIN_BTN_KEY2, .active_level = 0, }; ret = iot_button_new_gpio_device(&btn_cfg, &key2_gpio_cfg, &key2_btn_handle); if (ret != ESP_OK) { ESP_LOGE(TAG, "KEY2按键创建失败: %s", esp_err_to_name(ret)); return ret; } // 注册BOOT按键事件(第3参数event_args传NULL使用默认参数) iot_button_register_cb(boot_btn_handle, BUTTON_SINGLE_CLICK, NULL, boot_click_cb, NULL); iot_button_register_cb(boot_btn_handle, BUTTON_DOUBLE_CLICK, NULL, boot_double_click_cb, NULL); iot_button_register_cb(boot_btn_handle, BUTTON_LONG_PRESS_START, NULL, boot_long_press_cb, NULL); // 注册KEY2按键事件 iot_button_register_cb(key2_btn_handle, BUTTON_SINGLE_CLICK, NULL, key2_click_cb, NULL); iot_button_register_cb(key2_btn_handle, BUTTON_DOUBLE_CLICK, NULL, key2_double_click_cb, NULL); iot_button_register_cb(key2_btn_handle, BUTTON_LONG_PRESS_START, NULL, key2_long_press_cb, NULL); ESP_LOGI(TAG, "按键初始化完成 (iot_button, BOOT=GPIO%d, KEY2=GPIO%d)", PIN_BTN_BOOT, PIN_BTN_KEY2); return ESP_OK; } void button_on_boot_event(btn_event_cb_t cb, void *usr_data) { boot_cb.cb = cb; boot_cb.usr_data = usr_data; } void button_on_key2_event(btn_event_cb_t cb, void *usr_data) { key2_cb.cb = cb; key2_cb.usr_data = usr_data; }