diff --git a/components/FluxButton/CMakeLists.txt b/components/FluxButton/CMakeLists.txt new file mode 100644 index 0000000..2f4a57a --- /dev/null +++ b/components/FluxButton/CMakeLists.txt @@ -0,0 +1,20 @@ +# 定义源文件变量 +set(FLUX_BUTTON_SRCS "flux_button.c") + +# 定义依赖组件变量 +set(FLUX_BUTTON_REQUIRES + espressif__button + esp_driver_gpio + lvgl + freertos +) + +# 定义包含目录变量 +set(FLUX_BUTTON_INCLUDE_DIRS ".") + +# 注册组件 +idf_component_register( + SRCS ${FLUX_BUTTON_SRCS} + REQUIRES ${FLUX_BUTTON_REQUIRES} + INCLUDE_DIRS ${FLUX_BUTTON_INCLUDE_DIRS} +) \ No newline at end of file diff --git a/components/FluxButton/flux_button.c b/components/FluxButton/flux_button.c new file mode 100644 index 0000000..463d8d5 --- /dev/null +++ b/components/FluxButton/flux_button.c @@ -0,0 +1,123 @@ +/** + * @file FluxButton.c + * @brief 控制板按键源文件 + * + * 实现按键初始化及响应功能 + * + * @author wang xiang en + * @date 2025-04-18 + * @version 版本号 + * @copyright 版权声明((C)2025, YUWELL MEDTECH Co.ltd) + */ +#include +#include "flux_button.h" +#include "driver/gpio.h" + + +/* 日志打印标签 */ +static const char *S_BUTTON_TAG = "tag_flux_button"; + +/* 屏幕状态记录事件组 */ +EventGroupHandle_t g_lcd_state_event_group; + +/** + * @brief 按键事件处理函数 + * + * 将按键触发事件绑定到此函数中,按键事件发生后执行该函数。 + * + * @param[in] arg not used + * @param[in] data 按键事件枚举值 + */ +static void button_event_cb(void *arg, void *data) +{ + /* 按键事件出发后发送日志信息到屏幕 */ + ESP_LOGI(S_BUTTON_TAG, "Button event %d", (int)data); + + switch ((int)data) + { + /* 电源按键按下事件 */ + case BUTTON_CMD_POWER_ON: + POWER_ON; + break; + case BUTTON_CMD_POWER_OFF: + POWER_OFF; + break; + case BUTTON_CMD_LCD_BACKLIGHT_CHANGE: + #if USE_LCD_BACKLIGHT_CLOSE + /* 单击电源键事件 */ + ESP_LOGI(S_BUTTON_TAG, "LCD backlight change"); + /* Toggle backlight based on current backlight state */ + if (xEventGroupGetBits(g_lcd_state_event_group) & LCD_STATE_BIT) // If the current backlight state is on, turn it off + { + LCD_BACKLIGHT_CLOSE; + xEventGroupClearBits(g_lcd_state_event_group, LCD_STATE_BIT); + }else{ + LCD_BACKLIGHT_OPEN; + xEventGroupSetBits(g_lcd_state_event_group, LCD_STATE_BIT); + } + #endif + break; + default: + break; + } +} + +/** + * @brief 单个按键初始化函数 + * + * 根据按键编号,初始化对应的按键IO,并设置按键回调函数为button_event_cb + * + * @param[in] button_num 按键编号 + */ +static void single_button_init(uint32_t button_num) +{ + /* 根据引脚是否为电源键判断活动状态电平值 */ + uint8_t activeLevel = BUTTON_POWERIN_ACTIVE_LEVEL; + + /* 按键配置结构体初始化 */ + button_config_t btn_cfg = { + .type = BUTTON_TYPE_GPIO, + .gpio_button_config = { + .gpio_num = button_num, + .active_level = activeLevel, + }, + }; + button_handle_t btn = iot_button_create(&btn_cfg); + assert(btn); + + /* 注册按键事件处理函数 */ + esp_err_t err = ESP_OK; + + /* Turn off by double-clicking the power button */ + err |= iot_button_register_cb(btn, BUTTON_DOUBLE_CLICK, button_event_cb, (void *)BUTTON_CMD_POWER_OFF); + /* Turn on by long-pressing the power button */ + err |= iot_button_register_cb(btn, BUTTON_LONG_PRESS_HOLD, button_event_cb, (void *)BUTTON_CMD_POWER_ON); + /* Single click on the power button to toggle the display backlight state */ + err |= iot_button_register_cb(btn, BUTTON_SINGLE_CLICK, button_event_cb, (void *)BUTTON_CMD_LCD_BACKLIGHT_CHANGE); + + ESP_ERROR_CHECK(err); +} + +/** + * @brief 初始化所有按键 + * + * 配置按键、电源管理IO引脚状态 + */ +void flux_button_init(void) +{ + /* 初始化电源按键 */ + single_button_init(BUTTON_PowerIn_IO_NUM); + + /* 初始化按键事件组 */ + g_lcd_state_event_group = xEventGroupCreate(); + + /* Set the default state to backlight on */ + xEventGroupSetBits(g_lcd_state_event_group, LCD_STATE_BIT); + + /* 初始化电源控制引脚 */ + gpio_reset_pin(BUTTON_PowerOut_IO_NUM); + gpio_set_direction(BUTTON_PowerOut_IO_NUM, GPIO_MODE_OUTPUT); + + +} + diff --git a/components/FluxButton/flux_button.h b/components/FluxButton/flux_button.h new file mode 100644 index 0000000..2214ea1 --- /dev/null +++ b/components/FluxButton/flux_button.h @@ -0,0 +1,91 @@ +/** + * @file FluxButton.h + * @brief 控制板按键头文件 + * + * 用于声明按键IO编号,按键回调函数等 + * + * @author wang xiang en + * @date 2025-04-18 + * @version 版本号 + * @copyright 版权声明((C)2025, YUWELL MEDTECH Co.ltd) + */ +#ifndef _FLUX_BUTTON_H +#define _FLUX_BUTTON_H + +#ifdef __cplusplus +extern "C" { +#endif + + +/* Include external button component header files */ +#include "iot_button.h" +#include "esp_log.h" + +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" + + +/* 定义电源输入IO编号 */ +#define BUTTON_PowerIn_IO_NUM GPIO_NUM_10 +#define BUTTON_PowerOut_IO_NUM GPIO_NUM_11 + +/* 定义电源输出电平 */ +#define POWER_OFF_OUT_LEVEL (0) +#define POWER_ON_OUT_LEVEL (1) + +/* 定义按键电平 */ +#define BUTTON_ACTIVE_LEVEL (0) +#define BUTTON_POWERIN_ACTIVE_LEVEL (1) + +/* 定义LCD状态位 */ +#define LCD_STATE_BIT BIT0 + +/* 定义按键支持休眠 */ +#define CONFIG_GPIO_BUTTON_SUPPORT_POWER_SAVE (0) + +#define USE_LCD_BACKLIGHT_CLOSE (0) + +/** + * @brief 按键命令枚举 + * + * 定义按键按下事件的枚举值 + */ +enum button_cmd_t +{ + BUTTON_CMD_POWER_ON, + BUTTON_CMD_POWER_OFF, + BUTTON_CMD_LCD_BACKLIGHT_CHANGE, +}; + +/** + * @brief 关闭电源 + * + * 用于快捷关闭电源,关闭LDO芯片电源输出 + */ +#define POWER_OFF gpio_set_level(BUTTON_PowerOut_IO_NUM, POWER_OFF_OUT_LEVEL); + +/** + * @brief 开启电源 + * + * 用于快捷开启电源,使能LDO芯片电源输出 + */ +#define POWER_ON gpio_set_level(BUTTON_PowerOut_IO_NUM, POWER_ON_OUT_LEVEL); + + +/** + * @brief 按键初始化函数 + * + * 按键功能初始化 + * + */ +void flux_button_init(void); + + +#ifdef __cplusplus +} /* extern "C" */ +#endif + + +#endif /*_FLUX_BUTTON_H*/ \ No newline at end of file diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 63df83b..e1ab0a6 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -6,6 +6,7 @@ set(MAIN_REQUIRES lvgl esp_driver_ledc esp_driver_gpio + FluxButton ) # 定义包含目录变量 diff --git a/main/main.h b/main/main.h index 3e85d22..8c00a5f 100644 --- a/main/main.h +++ b/main/main.h @@ -17,7 +17,7 @@ #include "driver/ledc.h" #include "lvgl.h" - +#include "flux_button.h" // /* Include UI initialization header file */ // #include "FluxDisplayPort.h"