feat: 启用BLE蓝牙图片传输 + 移除温湿度传感器代码

1、app_main 中新增 ble_init() 调用,启动 BLE GATT Server(设备名: MY-BLE)
2、移除 temp 模块:CMakeLists.txt、main.c、pages.c 中的引用和 temp_test() 函数
3、精简 ble.c:移除 SENSOR_SERVICE(温湿度服务)、TEMP/HUMI 特征、READ_EVT 处理,仅保留图片传输服务
4、新增项目性能分析报告(N16R8 模组资源消耗详细分析)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Rdzleo 2026-02-12 16:19:11 +08:00
parent c36c2bfa08
commit 58fb9aab86
5 changed files with 205 additions and 136 deletions

View File

@ -4,7 +4,6 @@ idf_component_register(
"./gpio/gpio.c"
"./wifi/wifi.c"
"./lcd/lcd.c"
"./temp/temp.c"
"./fatfs/fatfs.c"
"./pages/pages.c"
"./ble/ble.c"
@ -32,7 +31,6 @@ idf_component_register(
"./gpio/include/"
"./wifi/include/"
"./lcd/include/"
"./temp/include/"
"./fatfs/include/"
"./pages/include/"
"./ble/include/"

View File

@ -5,31 +5,21 @@
#include "esp_bt.h"
#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
// #include "esp_bt_defs.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "esp_gatt_common_api.h"
#include "esp_mac.h"
#include "temp.h"
#include "fatfs.h"
#include "pages.h"
#define APP_ID_PLACEHOLDER 0
#define SENSOR_SERVICE_INSTID 0x0A
#define SENSOR_SERVICE_UUID 0x0A00
#define SENSOR_TEMP_UUID 0x0A01
#define SENSOR_HUMI_UUID 0x0A02
#define IMAGE_SERVICE_INSTID 0x0B
#define IMAGE_SERVICE_UUID 0x0B00
#define IMAGE_WRITE_UUID 0x0B01
#define IMAGE_EDIT_UUID 0x0B02
static uint16_t sensor_service_handle = 0;
static uint16_t temp_attr_handle = 0;
static uint16_t humi_attr_handle = 0;
static uint16_t image_service_handle = 0;
static uint16_t image_write_handle = 0;
static uint16_t image_edit_handle = 0;
@ -45,7 +35,7 @@ static uint16_t conn_id;
static char *filepath;
typedef struct
typedef struct
{
uint8_t type;
char filename[23];
@ -63,21 +53,9 @@ MegStatus SendStatus = {false,0};
uint8_t *img_data = 0;
FILE *file_img;
static uint8_t attr_value_temp[22] = {0};
static uint8_t attr_value_humi[22] = {0};
static uint8_t attr_value_write[512] = {0};
static uint8_t attr_value_edit[20] = {0};
static esp_attr_value_t char_val_temp = {
.attr_max_len = 4,
.attr_len = 4,
.attr_value = attr_value_temp
} ;
static esp_attr_value_t char_val_humi = {
.attr_max_len = 4,
.attr_len = 4,
.attr_value = attr_value_humi
} ;
static esp_attr_value_t char_val_image_write = {
.attr_max_len = 512,
.attr_len = 512,
@ -89,13 +67,6 @@ static esp_attr_value_t char_val_image_edit = {
.attr_value = attr_value_edit
} ;
static esp_attr_control_t control_temp = {
.auto_rsp = ESP_GATT_AUTO_RSP
};
static esp_attr_control_t control_humi = {
.auto_rsp = ESP_GATT_AUTO_RSP
};
static esp_attr_control_t control_image_write = {
.auto_rsp = ESP_GATT_AUTO_RSP
};
@ -103,25 +74,7 @@ static esp_attr_control_t control_image_edit = {
.auto_rsp = ESP_GATT_AUTO_RSP
};
//传感器服务
static esp_gatt_srvc_id_t server_id_sensor = {
.id = {
.uuid.uuid.uuid16 = SENSOR_SERVICE_UUID,
.inst_id = SENSOR_SERVICE_INSTID,
},
.is_primary = true,
};
static esp_bt_uuid_t sensor_temp_uuid = {
.len = ESP_UUID_LEN_16,
.uuid.uuid16 = SENSOR_TEMP_UUID,
};
static esp_bt_uuid_t sensor_humi_uuid = {
.len = ESP_UUID_LEN_16,
.uuid.uuid16 = SENSOR_HUMI_UUID,
};
//图片传输服务
// 图片传输服务
static esp_gatt_srvc_id_t server_id_image = {
.id.uuid.uuid.uuid16 = IMAGE_SERVICE_UUID,
.id.inst_id = IMAGE_SERVICE_INSTID,
@ -152,7 +105,6 @@ static uint8_t adv_raw_data[] = {
0x07, ESP_BLE_AD_TYPE_NAME_CMPL, 'M','Y','-','B','L','E',
0x02, ESP_BLE_AD_TYPE_TX_PWR, 0x09,
0x03, ESP_BLE_AD_TYPE_16SRV_CMPL, 0xB0, 0x00,
0x03, ESP_BLE_AD_TYPE_16SRV_CMPL, 0xA0, 0x00,
0x07, ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE,0x4C,0x44,0x64,0x7A,0x62,0x6A
};
@ -259,46 +211,24 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
break;
case ESP_GATTS_CREATE_EVT:
if (param->create.status == ESP_GATT_OK) {
// ESP_LOGI(CONN_TAG, "服务创建成功,句柄:0x%x,instid:0x%02x",param->create.service_handle,param->create.service_id.id.inst_id);
if (param->create.service_id.id.inst_id == (uint16_t)SENSOR_SERVICE_INSTID) {
sensor_service_handle = param->create.service_handle;
esp_ble_gatts_add_char(
sensor_service_handle,
&sensor_temp_uuid,
ESP_GATT_PERM_READ,
ESP_GATT_CHAR_PROP_BIT_READ,
&char_val_temp,
&control_temp
);
esp_ble_gatts_add_char(
sensor_service_handle,
&sensor_humi_uuid,
ESP_GATT_PERM_READ,
ESP_GATT_CHAR_PROP_BIT_READ,
&char_val_humi,
&control_humi
);
ESP_LOGI(CONN_TAG, "温湿度服务创建成功,句柄: %x", sensor_service_handle);
} else if (param->create.service_id.id.inst_id == (uint16_t)IMAGE_SERVICE_INSTID) {
image_service_handle = param->create.service_handle;
esp_ble_gatts_add_char(
image_service_handle,
&image_write_uuid,
ESP_GATT_PERM_WRITE,
ESP_GATT_CHAR_PROP_BIT_WRITE,
&char_val_image_write,
&control_image_write
);
esp_ble_gatts_add_char(
image_service_handle,
&image_edit_uuid,
ESP_GATT_PERM_WRITE,
ESP_GATT_CHAR_PROP_BIT_WRITE,
&char_val_image_edit,
&control_image_edit
);
ESP_LOGI(CONN_TAG, "图片传输服务创建成功,句柄: %x", image_service_handle);
}
image_service_handle = param->create.service_handle;
esp_ble_gatts_add_char(
image_service_handle,
&image_write_uuid,
ESP_GATT_PERM_WRITE,
ESP_GATT_CHAR_PROP_BIT_WRITE,
&char_val_image_write,
&control_image_write
);
esp_ble_gatts_add_char(
image_service_handle,
&image_edit_uuid,
ESP_GATT_PERM_WRITE,
ESP_GATT_CHAR_PROP_BIT_WRITE,
&char_val_image_edit,
&control_image_edit
);
ESP_LOGI(CONN_TAG, "图片传输服务创建成功,句柄: %x", image_service_handle);
} else {
ESP_LOGE(CONN_TAG, "服务创建失败,状态: %d", param->create.status);
}
@ -306,39 +236,18 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
case ESP_GATTS_ADD_CHAR_EVT:
if (param->add_char.status == ESP_GATT_OK) {
// ESP_LOGI(CONN_TAG, "特征创建成功,句柄:0x%x,UUID:0x%04x",param->add_char.attr_handle,param->add_char.char_uuid.uuid.uuid16);
if (param->add_char.char_uuid.uuid.uuid16 == (uint16_t)SENSOR_TEMP_UUID) {
temp_attr_handle = param->add_char.attr_handle;
ESP_LOGI(CONN_TAG, "TEMP特征创建成功,句柄: %d", temp_attr_handle);
} else if (param->add_char.char_uuid.uuid.uuid16 == (uint16_t)SENSOR_HUMI_UUID) {
humi_attr_handle = param->add_char.attr_handle;
ESP_LOGI(CONN_TAG, "HUMI特征创建成功,句柄: %d", humi_attr_handle);
esp_ble_gatts_start_service(sensor_service_handle);
} else if (param->add_char.char_uuid.uuid.uuid16 == (uint16_t)IMAGE_WRITE_UUID) {
if (param->add_char.char_uuid.uuid.uuid16 == (uint16_t)IMAGE_WRITE_UUID) {
image_write_handle = param->add_char.attr_handle;
ESP_LOGI(CONN_TAG, "图片写入特征创建成功,句柄: %d", image_write_handle);
}else if (param->add_char.char_uuid.uuid.uuid16 == (uint16_t)IMAGE_EDIT_UUID) {
} else if (param->add_char.char_uuid.uuid.uuid16 == (uint16_t)IMAGE_EDIT_UUID) {
image_edit_handle = param->add_char.attr_handle;
ESP_LOGI(CONN_TAG, "图片编辑特征创建成功,句柄: %d", image_edit_handle);
esp_ble_gatts_start_service(image_service_handle);
esp_ble_gatts_create_service(gatts_if,&server_id_sensor,10);
}
} else {
ESP_LOGE(CONN_TAG, "特征创建失败,状态: %d", param->add_char.status);
}
break;
case ESP_GATTS_READ_EVT:
uint16_t char_handle = param->read.handle;
if(char_handle == temp_attr_handle){
float *temp = malloc(4);
uint8_t *buffer = malloc(4);
get_temp(temp);
memcpy(buffer, temp, 4);;
esp_ble_gatts_set_attr_value(char_handle,4,buffer);
ESP_LOGI(CONN_TAG, "获取到温度读取事件,温度:%.2f,句柄: %d",*temp, char_handle);
free(temp);
}
break;
case ESP_GATTS_WRITE_EVT:
if(param->write.handle == image_write_handle){
uint8_t *value = param->write.value;
@ -392,14 +301,6 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
free(img_data);
free(filepath);
}
// printf("%s\n %x",imgName,type);
// printf("接收到数据:");
// for (size_t i = 0; i < param->write.len; i++)
// {
// printf("%x ",*(value+i));
// }
// printf("\n");
}
break;
case ESP_GATTS_CONNECT_EVT:
@ -422,4 +323,4 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
default:
break;
}
}
}

View File

@ -13,7 +13,6 @@
#include "gpio.h"
#include "wifi.h"
#include "lcd.h"
#include "temp.h"
#include "fatfs.h"
#include "pages.h"
#include "ble.h"
@ -274,11 +273,15 @@ void app_main(void)
button_on_boot_press(boot_btn_handler, NULL);
ESP_LOGI("MAIN", "12.1 BOOT按键回调已注册");
// 初始化BLE蓝牙用于APP端图片传输
ble_init();
ESP_LOGI("MAIN", "13. BLE蓝牙已初始化设备名: MY-BLE");
// 初始化休眠管理器依赖按键和UI必须最后初始化
sleep_mgr_init();
ESP_LOGI("MAIN", "13. 休眠管理器已初始化");
ESP_LOGI("MAIN", "14. 休眠管理器已初始化");
ESP_LOGI("MAIN", "系统初始化完成成功");// 系统初始化完成成功
ESP_LOGI("MAIN", "系统初始化完成");// 系统初始化完成
// =====================================================================

View File

@ -3,7 +3,6 @@
#include "driver/ledc.h"
#include "gpio.h"
#include "wifi.h"
#include "temp.h"
#include "jpeg_decoder.h"
#include "../ui/screens/ui_ScreenImg.h"
#include <inttypes.h>
@ -149,14 +148,6 @@ void app_wifi_display(){
}
// 测试获取温度和湿度
void temp_test(){
float temp,humi;// 定义温度和湿度变量
get_temp(&temp);// 获取温度
get_humi(&humi);// 获取湿度
ESP_LOGI("TEMP","temp:%.2f humi:%.2f",temp,humi);// 打印温度和湿度
}
// 从NVS中读取图片路径
esp_err_t nvs_read_img(void) {
nvs_handle_t nvs_handle; // NVS 句柄

176
项目性能分析报告.md Normal file
View File

@ -0,0 +1,176 @@
# ESP32-S3 LVGL 智能设备 - 项目性能分析报告
> 模组ESP32-S3-WROOM-1-N16R8
> 更新日期2026-02-12
> 编译环境ESP-IDF 5.4 + LVGL 8.3.11 + BLE Bluedroid
---
## 一、模组硬件规格
| 资源 | 规格 | 说明 |
|------|------|------|
| Flash | 16 MB (Quad SPI) | 存储固件 + SPIFFS 图片 |
| PSRAM | 8 MB (OPI) | 外部 RAM可供 BLE/LVGL 动态分配 |
| 内部 SRAM | 512 KB | IRAM + DIRAM + RTC运行时核心内存 |
| CPU | Xtensa LX7 双核 240MHz | 当前配置 40-160MHz (DFS) |
---
## 二、固件总体资源消耗
### 2.1 固件大小
| 项目 | 大小 | 占用率 | 说明 |
|------|------|--------|------|
| **固件总大小** | **1,359 KB** | — | program.bin |
| App 分区 | 4,096 KB | **33%** 已用67% 空闲 | 可容纳约 3 倍当前固件 |
| Bootloader | 22 KB | 68% 已用 | 32% 空闲 |
### 2.2 内存区域使用情况
| 内存区域 | 已用 | 总计 | 占用率 | 剩余 | 状态 |
|----------|------|------|--------|------|------|
| **DIRAM** | 180.6 KB | 333.8 KB | **54.11%** | 153.2 KB | 充裕 |
|  .text (代码) | 92.2 KB | — | 27.61% | — | |
|  .bss (未初始化) | 70.2 KB | — | 21.04% | — | |
|  .data (已初始化) | 18.2 KB | — | 5.45% | — | |
| **IRAM** | 16.0 KB | 16.0 KB | **99.99%** | 1 B | 已满 |
|  .text | 15.0 KB | — | 93.73% | — | |
|  .vectors | 1.0 KB | — | 6.27% | — | |
| **Flash Code** | 662.4 KB | — | — | — | 存储在 Flash |
| **Flash Data** | 580.0 KB | — | — | — | 存储在 Flash |
| **RTC FAST** | 112 B | 8.0 KB | 1.37% | 7.9 KB | 充裕 |
| **RTC SLOW** | 28 B | 8.0 KB | 0.34% | 8.0 KB | 充裕 |
### 2.3 Flash 分区表使用情况
| 分区 | 大小 | 用途 |
|------|------|------|
| bootloader | ~32 KB | 引导程序 |
| partition-table | 4 KB | 分区表 |
| nvs | 24 KB | NVS 配置存储 |
| app | 4 MB | 应用固件 (已用 33%) |
| spiffs | 2 MB | 图片资源存储 |
| **已分配** | **~6 MB** | |
| **Flash 剩余** | **~10 MB** | 可扩展 OTA、更大 SPIFFS 等 |
---
## 三、各组件资源消耗 (Top 15)
| 排名 | 组件 | 总大小 | Flash Code | Flash Data | DIRAM | 说明 |
|------|------|--------|-----------|-----------|-------|------|
| 1 | **libmain.a** | 348.5 KB | 14.3 KB | 332.9 KB | 1.3 KB | 应用代码 + UI 图片资源 |
| 2 | **liblvgl.a** | 322.9 KB | 169.1 KB | 88.5 KB | 65.2 KB | LVGL 图形库 |
| 3 | **libbt.a** | 193.7 KB | 171.7 KB | 21.3 KB | 0.7 KB | BLE 蓝牙协议栈 |
| 4 | libesp_app_format.a | 101.5 KB | 0.4 KB | 101.1 KB | 10 B | 应用格式信息 |
| 5 | **libbtdm_app.a** | 71.5 KB | 48.6 KB | 8.7 KB | 14.1 KB | BLE 控制器 |
| 6 | libc.a | 58.2 KB | 54.0 KB | 3.6 KB | 0.6 KB | C 标准库 |
| 7 | libesp_hw_support.a | 46.3 KB | 28.0 KB | 1.8 KB | 12.4 KB | 硬件支持层 |
| 8 | libphy.a | 34.1 KB | 26.0 KB | 0 | 8.2 KB | PHY 射频驱动 |
| 9 | libhal.a | 26.5 KB | 10.2 KB | 0.2 KB | 15.6 KB | HAL 硬件抽象层 |
| 10 | libspiffs.a | 22.7 KB | 22.3 KB | 0.4 KB | 12 B | SPIFFS 文件系统 |
| 11 | libfreertos.a | 19.9 KB | 0.9 KB | 1.2 KB | 16.9 KB | FreeRTOS RTOS |
| 12 | libesp_system.a | 15.5 KB | 9.2 KB | 0.7 KB | 1.3 KB | 系统核心 |
| 13 | libspi_flash.a | 14.6 KB | 1.2 KB | 0.4 KB | 12.5 KB | SPI Flash 驱动 |
| 14 | libnvs_flash.a | 14.2 KB | 13.9 KB | 0.2 KB | 28 B | NVS 存储 |
| 15 | libheap.a | 11.8 KB | 3.5 KB | 1.0 KB | 5.4 KB | 堆内存管理 |
---
## 四、BLE 蓝牙资源消耗专项
| 组件 | 大小 | 说明 |
|------|------|------|
| libbt.a | 193.7 KB | Bluedroid 协议栈 |
| libbtdm_app.a | 71.5 KB | BLE 控制器固件 |
| libcoexist.a | 4.9 KB | WiFi/BLE 共存 |
| libbtbb.a | 3.4 KB | BLE Baseband |
| libesp_phy.a | 3.1 KB | PHY (部分) |
| **BLE 合计** | **~276.6 KB** | 约占固件总大小 20% |
**BLE 内存优化配置**
- `CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST=y`BLE 动态分配优先使用 PSRAM减少内部 SRAM 压力
- BLE DIRAM 占用仅 ~15 KB得益于 PSRAM 分流)
---
## 五、LVGL 图形库资源消耗专项
| 项目 | 大小 | 说明 |
|------|------|------|
| LVGL 核心库 | 322.9 KB | 控件、渲染、动画等 |
| LVGL Port | 3.9 KB | ESP-IDF 适配层 |
| UI 图片资源 (Flash) | ~333 KB | 内嵌在 libmain.a 中的 PNG/图标 |
| LVGL DIRAM (.bss) | 65.1 KB | 帧缓冲、对象树等 |
| **LVGL 合计** | **~326.8 KB** | 约占固件总大小 24% |
---
## 六、功能模块清单
| 模块 | 状态 | 资源影响 |
|------|------|----------|
| LVGL UI (三界面) | 已启用 | Flash ~327 KB, DIRAM ~65 KB |
| BLE GATT Server (图片传输) | 已启用 | Flash ~277 KB, DIRAM ~15 KB |
| SPIFFS 文件系统 | 已启用 | Flash ~23 KB |
| JPEG 解码 | 已启用 | Flash ~6.3 KB |
| LCD ST77916 (QSPI) | 已启用 | Flash ~5.2 KB |
| CST816S 触摸 | 已启用 | Flash ~1.3 KB |
| PWM 背光 | 已启用 | Flash ~5.1 KB |
| 电池 ADC 监控 | 已启用 | < 1 KB |
| 按键驱动 | 已启用 | < 1 KB |
| 低功耗管理 (DFS + Light Sleep) | 已启用 | Flash ~2.8 KB |
| WiFi | 未启用 | 仅链接 58 B (stub) |
| ~~温湿度传感器~~ | **已移除** | — |
---
## 七、资源余量评估
### 7.1 关键资源余量
| 资源 | 当前占用 | 总量 | 剩余 | 余量评估 |
|------|----------|------|------|----------|
| **内部 SRAM (DIRAM)** | 180.6 KB (54.1%) | 333.8 KB | **153.2 KB** | 充裕,可支撑更多功能 |
| **IRAM** | 16.0 KB (99.99%) | 16.0 KB | **1 B** | 已满但正常ESP-IDF 自动管理溢出到 Flash |
| **App Flash** | 1,359 KB (33%) | 4,096 KB | **2,737 KB** | 非常充裕 |
| **总 Flash** | ~6 MB (分区) | 16 MB | **~10 MB** | 极其充裕 |
| **PSRAM** | 运行时动态分配 | 8 MB | ~7.5+ MB | 极其充裕 |
### 7.2 IRAM 满载说明
IRAM 99.99% 是正常现象:
- ESP-IDF 的链接脚本会自动将溢出的 IRAM 代码放到 Flash 中执行
- 只有标记为 `IRAM_ATTR` 的关键中断处理函数才必须在 IRAM
- 不影响正常运行,无需优化
### 7.3 运行时内存估算(满负荷)
| 项目 | 预估消耗 | 来源 |
|------|----------|------|
| FreeRTOS 任务栈 (5-6个任务) | ~30 KB | DIRAM 堆 |
| LVGL 帧缓冲 (双缓冲) | ~43 KB | PSRAM |
| LVGL 对象树/样式 | ~20 KB | DIRAM 堆 |
| BLE 连接管理 | ~10 KB | PSRAM (优先) |
| JPEG 解码临时缓冲 | ~15 KB | 堆 (临时) |
| SPIFFS 缓存 | ~4 KB | 堆 |
| **合计** | **~122 KB** | |
| **DIRAM 堆剩余** | **~153 KB** | 满足需求 |
---
## 八、总结
**N16R8 模组资源完全满足当前项目需求**
1. **Flash (16MB)**:固件仅用 1.36 MBSPIFFS 2 MB大量空间可扩展 OTA 或增加图片存储
2. **内部 SRAM (512KB)**DIRAM 占 54%,剩余 153 KB 足够运行时动态分配
3. **PSRAM (8MB)**BLE 动态内存优先使用 PSRAM有效降低内部 SRAM 压力,剩余 7+ MB
4. **BLE 影响可控**BLE 增加约 277 KB Flash + 15 KB DIRAM对 N16R8 影响微乎其微
**优化建议**(可选):
- 如需进一步减少 Flash 占用,可考虑压缩 UI 内嵌图片资源(当前 ~333 KB
- 如需启用 WiFi + BLE 共存DIRAM 仍有足够余量
- PSRAM 可用于存储更大的 JPEG 解码缓冲,提升图片切换速度