测试完毕可以正常运行的电源管理组件及消息队列处理任务

This commit is contained in:
ipason 2025-11-28 06:35:07 +00:00
parent e0cdcc1542
commit 55c4531938
15 changed files with 373 additions and 192 deletions

View File

@ -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

View File

@ -1,6 +1,6 @@
{
"name": "FluxDocker28",
"image": "my-esp-idf:latest",
"image": "my-esp-idf-new:latest",
"customizations": {
"vscode": {
"settings": {

View File

@ -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
}
}

View File

@ -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"
}
}

View File

@ -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}
)

View File

@ -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 <stdio.h>
#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*/

View File

@ -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}
)

View File

@ -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;
}
}
}
}

View File

@ -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按钮IDUI任务
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

View File

@ -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}
)

View File

@ -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 <stdio.h>
#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;
}
}
}
}

View File

@ -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 <stdio.h>
#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*/

View File

@ -6,7 +6,8 @@ set(MAIN_REQUIRES
lvgl
esp_driver_ledc
esp_driver_gpio
FluxButton
user_power_task
user_common
)
#

View File

@ -1,11 +1,41 @@
#include <stdio.h>
#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);
}
}

View File

@ -1,35 +1,47 @@
/**
* @file main.h
* @brief
* @details FreeRTOSESP32外设驱动LVGL图形库等组件的头文件
* C/C++
* @author
* @date 2025-11-28
*/
#ifndef __MAIN_H__
#define __MAIN_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#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
#include "user_power_task.h"
#include "user_common.h"
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__