// 按键驱动模块(基于iot_button组件) // 支持BOOT和KEY2两个按键的单击/双击/长按事件 #include "button.h" #include "iot_button.h" #include "button_gpio.h" #include "esp_log.h" static const char *TAG = "BTN"; // 按键句柄 static button_handle_t boot_handle = NULL; static button_handle_t key2_handle = NULL; esp_err_t button_init(void) { // BOOT按键配置 button_config_t boot_cfg = { .long_press_time = 1200, // 长按阈值1.2秒 // 长按阈值2秒 .short_press_time = 300, // 双击检测窗口300ms // 双击窗口180ms }; button_gpio_config_t boot_gpio_cfg = { .gpio_num = PIN_BTN_BOOT, .active_level = 0, // 低电平有效 }; esp_err_t ret = iot_button_new_gpio_device(&boot_cfg, &boot_gpio_cfg, &boot_handle); if (ret != ESP_OK) { ESP_LOGE(TAG, "BOOT按键创建失败: %s", esp_err_to_name(ret)); return ret; } // KEY2按键配置 button_config_t key2_cfg = { .long_press_time = 1200, // 长按阈值1.2秒 .short_press_time = 300, // 双击检测窗口300ms }; button_gpio_config_t key2_gpio_cfg = { .gpio_num = PIN_BTN_KEY2, .active_level = 0, }; ret = iot_button_new_gpio_device(&key2_cfg, &key2_gpio_cfg, &key2_handle); if (ret != ESP_OK) { ESP_LOGE(TAG, "KEY2按键创建失败: %s", esp_err_to_name(ret)); return ret; } ESP_LOGI(TAG, "按键初始化完成 (BOOT=GPIO%d, KEY2=GPIO%d)", PIN_BTN_BOOT, PIN_BTN_KEY2); return ESP_OK; } // === BOOT按键回调注册 === void button_on_boot_click(btn_event_cb_t cb, void *usr_data) { iot_button_register_cb(boot_handle, BUTTON_SINGLE_CLICK, NULL, (button_cb_t)cb, usr_data); } void button_on_boot_double_click(btn_event_cb_t cb, void *usr_data) { iot_button_register_cb(boot_handle, BUTTON_DOUBLE_CLICK, NULL, (button_cb_t)cb, usr_data); } void button_on_boot_long_press(btn_event_cb_t cb, void *usr_data) { iot_button_register_cb(boot_handle, BUTTON_LONG_PRESS_START, NULL, (button_cb_t)cb, usr_data); } // === KEY2按键回调注册 === void button_on_key2_click(btn_event_cb_t cb, void *usr_data) { iot_button_register_cb(key2_handle, BUTTON_SINGLE_CLICK, NULL, (button_cb_t)cb, usr_data); } void button_on_key2_double_click(btn_event_cb_t cb, void *usr_data) { iot_button_register_cb(key2_handle, BUTTON_DOUBLE_CLICK, NULL, (button_cb_t)cb, usr_data); } void button_on_key2_long_press(btn_event_cb_t cb, void *usr_data) { iot_button_register_cb(key2_handle, BUTTON_LONG_PRESS_START, NULL, (button_cb_t)cb, usr_data); }