diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 49d72eb..121e65c 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,5 +1,5 @@ ARG DOCKER_TAG=latest -FROM my-esp-idf:${DOCKER_TAG} +FROM my-esp-idf-new:${DOCKER_TAG} ENV LC_ALL=C.UTF-8 ENV LANG=C.UTF-8 diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 43f8a8b..83d61c9 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,6 +1,6 @@ { "name": "FluxDocker28", - "image": "my-esp-idf:latest", + "image": "my-esp-idf-new:latest", "customizations": { "vscode": { "settings": { diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 3e51c0d..e2c2816 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -2,23 +2,24 @@ "configurations": [ { "name": "ESP-IDF", - "compilerPath": "${config:idf.toolsPathWin}\\tools\\xtensa-esp-elf\\esp-13.2.0_20240530\\xtensa-esp-elf\\bin\\xtensa-esp32s3-elf-gcc.exe", - "compileCommands": "${config:idf.buildPath}/compile_commands.json", + "compilerPath": "/opt/esp/tools/xtensa-esp-elf/esp-15.2.0_20250929/xtensa-esp-elf/bin/xtensa-esp32s3-elf-gcc", + "compileCommands": [ + "${config:idf.buildPath}/compile_commands.json" + ], "includePath": [ "${config:idf.espIdfPath}/components/**", - "${config:idf.espIdfPathWin}/components/**", - "${workspaceFolder}/**" + "${workspaceFolder}/**", + "${workspaceFolder}/components/user_common" ], "browse": { "path": [ - "${config:idf.espIdfPath}/components", - "${config:idf.espIdfPathWin}/components", - "${workspaceFolder}", - "${workspaceFolder}/main" + "${config:idf.espIdfPath}/components/**", + "${workspaceFolder}/**", + "${workspaceFolder}" ], "limitSymbolsToIncludedHeaders": true } } ], "version": 4 -} +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index f5e2c8e..118631b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -14,7 +14,8 @@ "esp_log.h": "c", "sdkconfig.h": "c", "gpio.h": "c", - "gpio_hal.h": "c" + "gpio_hal.h": "c", + "freertos.h": "c" } } \ No newline at end of file diff --git a/components/FluxButton/CMakeLists.txt b/components/FluxButton/CMakeLists.txt deleted file mode 100644 index 2f4a57a..0000000 --- a/components/FluxButton/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ -# 定义源文件变量 -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.h b/components/FluxButton/flux_button.h deleted file mode 100644 index 2214ea1..0000000 --- a/components/FluxButton/flux_button.h +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @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/components/user_common/CMakeLists.txt b/components/user_common/CMakeLists.txt new file mode 100644 index 0000000..6e10e47 --- /dev/null +++ b/components/user_common/CMakeLists.txt @@ -0,0 +1,17 @@ +# 定义源文件变量 +set(USER_COMMON_SRCS "user_common.c") + +# 定义依赖组件变量 +set(USER_COMMON_REQUIRES + freertos +) + +# 定义包含目录变量 +set(USER_COMMON_INCLUDE_DIRS ".") + +# 注册组件 +idf_component_register( + SRCS ${USER_COMMON_SRCS} + REQUIRES ${USER_COMMON_REQUIRES} + INCLUDE_DIRS ${USER_COMMON_INCLUDE_DIRS} +) \ No newline at end of file diff --git a/components/user_common/user_common.c b/components/user_common/user_common.c new file mode 100644 index 0000000..ee07f05 --- /dev/null +++ b/components/user_common/user_common.c @@ -0,0 +1,52 @@ + + +#include "user_common.h" + + +// 全局通用队列(所有任务共用) +QueueHandle_t xGlobalMsgQueue; + +// 电源相关队列 +QueueHandle_t xPowerTaskQueue; + + + + +// 消息分发函数用于进行消息分发 +void msg_dispatch_task(void* arg) +{ + GeneralMsg_t received_msg; + + // 创建用于任务间通讯的消息队列 + xGlobalMsgQueue = xQueueCreate( + 32, // 队列长度(可存储32条消息) + sizeof(GeneralMsg_t) // 每条消息大小 + ); + configASSERT(xGlobalMsgQueue != NULL); + + xPowerTaskQueue = xQueueCreate( + 32, // 队列长度(可存储32条消息) + sizeof(GeneralMsg_t) // 每条消息大小 + ); + configASSERT(xPowerTaskQueue != NULL); + + while (1) + { + if(xQueueReceive(xGlobalMsgQueue, &received_msg, portMAX_DELAY)) //若没有消息接收则将任务设置为阻塞态 + { + switch (received_msg.type) + { + case MSG_TYPE_POWER_OFF: + // 将消息下发到电源处理任务中 + xQueueSend(xPowerTaskQueue, &received_msg, 0); + break; + + default: + break; + } + + } + } + + +} \ No newline at end of file diff --git a/components/user_common/user_common.h b/components/user_common/user_common.h new file mode 100644 index 0000000..a4e76be --- /dev/null +++ b/components/user_common/user_common.h @@ -0,0 +1,42 @@ +#ifndef COMMON_MSG_H +#define COMMON_MSG_H + +#include "freertos/FreeRTOS.h" +#include "freertos/queue.h" +#include "esp_mac.h" + + + +// 1. 声明消息类型枚举(全局可见) +typedef enum +{ + MSG_TYPE_POWER_BRIGHT, // 电源管理-亮度调节 + MSG_TYPE_POWER_OFF, // 电源管理-开关机 + // 扩展其他消息类型... +} MsgType_t; + + +// 2. 声明数据联合体(全局可见) +typedef union +{ + int brightness; // 亮度值(电源管理) + bool power_state; // 开关机状态(true=开机,false=关机) + int button_id; // UI按钮ID(UI任务) + float sensor_value; // 传感器数值(传感器任务) + // 扩展其他数据类型... +} MsgData_t; + +// 3. 声明通用消息结构体(全局可见) +typedef struct { + MsgType_t type; // 消息类型(区分业务) + uint32_t src_task; // 消息来源(可选,标记发送任务) + MsgData_t data; // 消息数据(联合体按需使用) +} GeneralMsg_t; + +// 4. 声明全局队列句柄(extern 让其他组件可见) +extern QueueHandle_t xGlobalMsgQueue; +extern QueueHandle_t xPowerTaskQueue; + +void msg_dispatch_task(void* arg); + +#endif // COMMON_MSG_H \ No newline at end of file diff --git a/components/user_power_task/CMakeLists.txt b/components/user_power_task/CMakeLists.txt new file mode 100644 index 0000000..d78a281 --- /dev/null +++ b/components/user_power_task/CMakeLists.txt @@ -0,0 +1,22 @@ +# 定义源文件变量 +set(USER_POWER_SRCS "user_power_task.c") + +# 定义依赖组件变量 +set(USER_POWER_REQUIRES + espressif__button + esp_driver_gpio + esp_driver_ledc + lvgl + freertos + user_common +) + +# 定义包含目录变量 +set(USER_POWER_INCLUDE_DIRS ".") + +# 注册组件 +idf_component_register( + SRCS ${USER_POWER_SRCS} + REQUIRES ${USER_POWER_REQUIRES} + INCLUDE_DIRS ${USER_POWER_INCLUDE_DIRS} +) \ No newline at end of file diff --git a/components/FluxButton/flux_button.c b/components/user_power_task/user_power_task.c similarity index 51% rename from components/FluxButton/flux_button.c rename to components/user_power_task/user_power_task.c index 463d8d5..b8ca7ad 100644 --- a/components/FluxButton/flux_button.c +++ b/components/user_power_task/user_power_task.c @@ -5,20 +5,20 @@ * 实现按键初始化及响应功能 * * @author wang xiang en - * @date 2025-04-18 + * @date 2025-11-28 * @version 版本号 * @copyright 版权声明((C)2025, YUWELL MEDTECH Co.ltd) */ #include -#include "flux_button.h" +#include "user_power_task.h" #include "driver/gpio.h" -/* 日志打印标签 */ -static const char *S_BUTTON_TAG = "tag_flux_button"; -/* 屏幕状态记录事件组 */ -EventGroupHandle_t g_lcd_state_event_group; +/* 日志打印标签 */ +static const char *S_BUTTON_TAG = "tag_user_button"; + + /** * @brief 按键事件处理函数 @@ -40,22 +40,7 @@ static void button_event_cb(void *arg, void *data) 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 + POWER_OFF; break; default: break; @@ -71,7 +56,7 @@ static void button_event_cb(void *arg, void *data) */ static void single_button_init(uint32_t button_num) { - /* 根据引脚是否为电源键判断活动状态电平值 */ + uint8_t activeLevel = BUTTON_POWERIN_ACTIVE_LEVEL; /* 按键配置结构体初始化 */ @@ -92,32 +77,67 @@ static void single_button_init(uint32_t button_num) 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) + + +// 电源管理任务,初始化按键之后接收外部消息并处理 +void power_manager_task(void* arg) { - /* 初始化电源按键 */ - single_button_init(BUTTON_PowerIn_IO_NUM); + GeneralMsg_t received_msg; - /* 初始化按键事件组 */ - g_lcd_state_event_group = xEventGroupCreate(); + gpio_reset_pin(BUTTON_POWER_OUT_IO_NUM); + gpio_set_direction(BUTTON_POWER_OUT_IO_NUM, GPIO_MODE_OUTPUT); // 将控制按键设置为输出 - /* 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); + /* 初始化顶部的电源按键 */ + single_button_init(BUTTON_POWER_IN_IO_NUM); -} + // 初始化背光调节LEDC + // Prepare and then apply the LEDC PWM timer configuration + ledc_timer_config_t ledc_timer = { + .speed_mode = LEDC_MODE, + .duty_resolution = LEDC_DUTY_RES, + .timer_num = LEDC_TIMER, + .freq_hz = LEDC_FREQUENCY, // Set output frequency at 4 kHz + .clk_cfg = LEDC_AUTO_CLK + }; + ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer)); + // Prepare and then apply the LEDC PWM channel configuration + ledc_channel_config_t ledc_channel = { + .speed_mode = LEDC_MODE, + .channel = LEDC_CHANNEL, + .timer_sel = LEDC_TIMER, + .intr_type = LEDC_INTR_DISABLE, + .gpio_num = LEDC_OUTPUT_IO, + .duty = 0, // Set duty to 0% + .hpoint = 0 + }; + ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel)); + + ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 7222)); + ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0)); + + + while (1) + { + + if(xQueueReceive(xPowerTaskQueue, &received_msg, portMAX_DELAY)) //若没有消息接收则将任务设置为阻塞态 + { + switch (received_msg.type) + { + case MSG_TYPE_POWER_OFF: + POWER_OFF; + break; + + default: + break; + } + + } + } + +} \ No newline at end of file diff --git a/components/user_power_task/user_power_task.h b/components/user_power_task/user_power_task.h new file mode 100644 index 0000000..ccfa25d --- /dev/null +++ b/components/user_power_task/user_power_task.h @@ -0,0 +1,94 @@ +/** + * @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" +#include "esp_mac.h" +#include "user_common.h" +#include "driver/ledc.h" + +/* 定义电源输入IO编号 */ +#define BUTTON_POWER_IN_IO_NUM GPIO_NUM_10 +#define BUTTON_POWER_OUT_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) +#define USE_LCD_BACKLIGHT_CLOSE (0) + + +/* 定义背光相关参数 */ +#define LEDC_TIMER LEDC_TIMER_0 +#define LEDC_MODE LEDC_LOW_SPEED_MODE +#define LEDC_OUTPUT_IO GPIO_NUM_3 // Define the output GPIO +#define LEDC_CHANNEL LEDC_CHANNEL_0 +#define LEDC_DUTY_RES LEDC_TIMER_13_BIT // Set duty resolution to 13 bits +#define LEDC_DUTY (4096) // Set duty to 50%. (2 ** 13) * 50% = 4096 +#define LEDC_FREQUENCY (4000) // Frequency in Hertz. Set frequency at 4 kHz + +#define DEFAULT_LIGHT (4096) + + +/** + * @brief 按键命令枚举 + * + * 定义按键按下事件的枚举值 + */ +enum button_cmd_t +{ + BUTTON_CMD_POWER_ON, + BUTTON_CMD_POWER_OFF, +}; + +/** + * @brief 关闭电源 + * + * 用于快捷关闭电源,关闭LDO芯片电源输出 + */ +#define POWER_OFF gpio_set_level(BUTTON_POWER_OUT_IO_NUM, POWER_OFF_OUT_LEVEL); + +/** + * @brief 开启电源 + * + * 用于快捷开启电源,使能LDO芯片电源输出 + */ +#define POWER_ON gpio_set_level(BUTTON_POWER_OUT_IO_NUM, POWER_ON_OUT_LEVEL); + + +// 上电后初始化电源管理任务 包括电源使能及采样gpio 亮度调节等 +void power_manager_task(void* arg); + + + +#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 e1ab0a6..525c6ba 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -6,7 +6,8 @@ set(MAIN_REQUIRES lvgl esp_driver_ledc esp_driver_gpio - FluxButton + user_power_task + user_common ) # 定义包含目录变量 diff --git a/main/main.c b/main/main.c index e9eb04a..e685d54 100644 --- a/main/main.c +++ b/main/main.c @@ -1,11 +1,41 @@ #include #include "main.h" -#include "driver/gpio.h" -#include "hal/gpio_hal.h" + + void app_main(void) { + // 创建用于进行消息分发的任务 + xTaskCreate( + msg_dispatch_task, // 任务函数 + "msg_dispatch_task", // 任务名称 + 4096, // 任务栈大小 + NULL, // 任务参数 + 1, // 任务优先级 + NULL // 任务句柄 + ); + + + // 创建用于进行电源管理的任务:电源开关按键控制、屏幕背光调节、电池电量检测等 + xTaskCreate( + power_manager_task, // 任务函数 + "power_task", // 任务名称 + 4096, // 任务栈大小 + NULL, // 任务参数 + 1, // 任务优先级 + NULL // 任务句柄 + ); + + + + + while (1) + { + vTaskDelay(1000); + } + + } \ No newline at end of file diff --git a/main/main.h b/main/main.h index 8c00a5f..9602768 100644 --- a/main/main.h +++ b/main/main.h @@ -1,35 +1,47 @@ +/** + * @file main.h + * @brief 主程序头文件,包含项目所需的各类依赖和声明 + * @details 该头文件整合了FreeRTOS、ESP32外设驱动、LVGL图形库等组件的头文件, + * 为项目提供统一的依赖引入入口,并支持C/C++编译环境 + * @author 王大金 + * @date 2025-11-28 + */ + #ifndef __MAIN_H__ #define __MAIN_H__ - +#ifdef __cplusplus +extern "C" { +#endif #include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/semphr.h" -#include "esp_timer.h" -#include "esp_err.h" -#include "esp_log.h" -#include "esp_lcd_panel_io.h" -#include "esp_lcd_panel_vendor.h" -#include "esp_lcd_panel_ops.h" -#include "driver/gpio.h" -#include "driver/ledc.h" -#include "lvgl.h" -#include "flux_button.h" +#include "freertos/FreeRTOS.h" ///< FreeRTOS核心头文件 +#include "freertos/task.h" ///< 任务管理相关API +#include "freertos/semphr.h" ///< 信号量/互斥锁等同步机制 -// /* Include UI initialization header file */ -// #include "FluxDisplayPort.h" +#include "esp_timer.h" ///< 高精度定时器驱动 +#include "esp_err.h" ///< ESP错误码定义 +#include "esp_log.h" ///< 日志输出功能 +#include "driver/gpio.h" ///< GPIO外设驱动 +#include "driver/ledc.h" ///< LEDC PWM驱动 -// /* Perform UI decoration */ -// #include "ui.h" +#include "esp_lcd_panel_io.h" ///< LCD面板IO接口 +#include "esp_lcd_panel_vendor.h" ///< 厂商特定LCD面板驱动 +#include "esp_lcd_panel_ops.h" ///< LCD面板操作接口 -// /* Serial communication task runs in real time */ -// #include "FluxUart.h" - -// /* Include key control header file */ -// #include "Flux_Button.h" +#include "driver/gpio.h" +#include "hal/gpio_hal.h" -#endif \ No newline at end of file +#include "user_power_task.h" +#include "user_common.h" + + + + +#ifdef __cplusplus +} +#endif + +#endif // __MAIN_H__ \ No newline at end of file