Dzbj_ESP32-S3_Key/main/ui/battery_ui.c
Rdzleo 75586b3744 ESP32-S3按键版电子吧唧功能完整实现
1、按键驱动重构:从GPIO中断改为iot_button组件,支持BOOT/KEY2的单击/双击/长按6种事件;
2、新增按键导航管理器(key_nav):9种上下文状态机,统一分发按键事件到对应界面业务逻辑;
3、BLE模块改造:广播默认关闭由按键触发,新增设备间图片传输(GATT Client),支持扫描配对→MTU协商→分包发送;
4、新增6个UI界面:配对(Peiwang)、更新(Update)、发送等待(ImageShar)、接收等待(ImageReception)、发送中(Sharing)、接收中(Receiving);
5、新增电池指示器组件(battery_ui):支持多界面真实电量显示,3秒渐隐效果;
6、Home界面重构:移除手势事件,改为airhub背景图+电池指示器;
7、Img界面重构:移除触摸事件,新增删除二次确认边框机制;
8、禁用ScreenSet/ScreenChar界面(保留文件),禁用触摸初始化(保留代码可恢复);
9、sleep_mgr简化:移除ScreenSet亮度UI依赖,按键唤醒由key_nav统一处理;
10、新增9张图片资源(airhub背景、配对、传输状态、电池图标等);

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-26 17:35:05 +08:00

201 lines
7.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 电池指示器模块
// 屏幕顶部药丸容器 + 电池图标LVGL基元绘制
// 仅 Home/Img 界面使用每次进入界面或切图时显示3秒后渐隐消失
#include "ui.h"
#include "battery_ui.h"
// === 刘海容器参数(小药丸形,悬浮) ===
#define NOTCH_W 50 // 容器宽度
#define NOTCH_H 20 // 容器高度
#define NOTCH_Y 8 // 距屏幕顶部间距
#define NOTCH_RADIUS 10 // 圆角=高度/2形成药丸形
#define NOTCH_BG_COLOR 0x000000 // 纯黑
#define NOTCH_BG_OPA 255 // 完全不透明
// === 电池图标参数 ===
#define BAT_W 24 // 电池主体宽度
#define BAT_H 12 // 电池主体高度
#define BAT_BORDER 2 // 边框宽度
#define BAT_RADIUS 3 // 主体圆角
#define BAT_OUTLINE_CLR 0xBBBBBB // 边框颜色
#define BAT_CAP_W 3 // 电池帽宽度
#define BAT_CAP_H 6 // 电池帽高度
#define BAT_CAP_RADIUS 1 // 电池帽圆角
#define BAT_PAD 1 // 填充条与边框间距
// === 填充条参数 ===
#define FILL_MAX_W (BAT_W - 2 * BAT_BORDER - 2 * BAT_PAD) // 18px
#define FILL_H (BAT_H - 2 * BAT_BORDER - 2 * BAT_PAD) // 6px
#define FILL_RADIUS 1
// === 颜色 ===
#define BAT_COLOR_GREEN 0x4CAF50 // 正常电量绿色
#define BAT_COLOR_RED 0xF44336 // 低电量红色
#define BAT_LOW_THRESH 20 // 低电量阈值
// === 显示时间和动画参数 ===
#define SHOW_DURATION_MS 2000 // 显示持续时间2秒
#define FADE_DURATION_MS 500 // 渐隐动画时间500ms
// === 容器跟踪最多2个界面Home + Img ===
#define MAX_SLOTS 2
typedef struct {
lv_obj_t *notch; // 容器对象
lv_obj_t *fill; // 填充条对象
int fill_x; // 填充条X坐标
int fill_y; // 填充条Y坐标
} bat_slot_t;
static bat_slot_t slots[MAX_SLOTS];
static int slot_count = 0;
static lv_timer_t *hide_timer = NULL; // 3秒后触发渐隐的定时器
static int cached_level = 100; // 缓存最新电量
// === 渐隐动画回调 ===
static void fade_anim_cb(void *obj, int32_t value) {
lv_obj_set_style_opa((lv_obj_t *)obj, (lv_opa_t)value, 0);
}
// 开始渐隐动画
static void start_fade_out(lv_obj_t *notch) {
lv_anim_t a;
lv_anim_init(&a);
lv_anim_set_var(&a, notch);
lv_anim_set_values(&a, LV_OPA_COVER, LV_OPA_TRANSP);
lv_anim_set_time(&a, FADE_DURATION_MS);
lv_anim_set_exec_cb(&a, fade_anim_cb);
lv_anim_start(&a);
}
// 定时器回调3秒后对所有活跃的 notch 执行渐隐
static void hide_timer_cb(lv_timer_t *timer) {
for (int i = 0; i < slot_count; i++) {
if (lv_obj_is_valid(slots[i].notch)) {
start_fade_out(slots[i].notch);
}
}
// 单次触发,删除定时器
lv_timer_del(timer);
hide_timer = NULL;
}
void battery_ui_add_to_screen(lv_obj_t *screen, int level) {
if (level < 0) level = 0;
if (level > 100) level = 100;
cached_level = level;
if (slot_count >= MAX_SLOTS) return;
// === 1. 药丸容器(初始透明/隐藏) ===
lv_obj_t *notch = lv_obj_create(screen);
lv_obj_remove_style_all(notch);
lv_obj_set_size(notch, NOTCH_W, NOTCH_H);
lv_obj_set_align(notch, LV_ALIGN_TOP_MID);
lv_obj_set_y(notch, NOTCH_Y);
lv_obj_set_style_bg_color(notch, lv_color_hex(NOTCH_BG_COLOR), 0);
lv_obj_set_style_bg_opa(notch, NOTCH_BG_OPA, 0);
lv_obj_set_style_radius(notch, NOTCH_RADIUS, 0);
lv_obj_set_style_border_width(notch, 0, 0);
lv_obj_clear_flag(notch, LV_OBJ_FLAG_SCROLLABLE | LV_OBJ_FLAG_CLICKABLE);
// 整体透明度初始为0隐藏状态
lv_obj_set_style_opa(notch, LV_OPA_TRANSP, 0);
// === 2. 电池主体 ===
int bat_total_w = BAT_W + BAT_CAP_W;
int bat_x = (NOTCH_W - bat_total_w) / 2;
int bat_y = (NOTCH_H - BAT_H) / 2;
lv_obj_t *body = lv_obj_create(notch);
lv_obj_remove_style_all(body);
lv_obj_set_size(body, BAT_W, BAT_H);
lv_obj_set_pos(body, bat_x, bat_y);
lv_obj_set_style_bg_opa(body, LV_OPA_TRANSP, 0);
lv_obj_set_style_border_color(body, lv_color_hex(BAT_OUTLINE_CLR), 0);
lv_obj_set_style_border_width(body, BAT_BORDER, 0);
lv_obj_set_style_radius(body, BAT_RADIUS, 0);
lv_obj_clear_flag(body, LV_OBJ_FLAG_SCROLLABLE | LV_OBJ_FLAG_CLICKABLE);
// === 3. 电池帽 ===
lv_obj_t *cap = lv_obj_create(notch);
lv_obj_remove_style_all(cap);
lv_obj_set_size(cap, BAT_CAP_W, BAT_CAP_H);
lv_obj_set_pos(cap, bat_x + BAT_W, bat_y + (BAT_H - BAT_CAP_H) / 2);
lv_obj_set_style_bg_color(cap, lv_color_hex(BAT_OUTLINE_CLR), 0);
lv_obj_set_style_bg_opa(cap, LV_OPA_COVER, 0);
lv_obj_set_style_radius(cap, BAT_CAP_RADIUS, 0);
lv_obj_set_style_border_width(cap, 0, 0);
lv_obj_clear_flag(cap, LV_OBJ_FLAG_SCROLLABLE | LV_OBJ_FLAG_CLICKABLE);
// === 4. 电量填充条 ===
int fill_x = bat_x + BAT_BORDER + BAT_PAD;
int fill_y = bat_y + BAT_BORDER + BAT_PAD;
int fill_w = FILL_MAX_W * level / 100;
uint32_t fill_color = (level <= BAT_LOW_THRESH) ? BAT_COLOR_RED : BAT_COLOR_GREEN;
lv_obj_t *fill = lv_obj_create(notch);
lv_obj_remove_style_all(fill);
lv_obj_set_size(fill, (fill_w > 0) ? fill_w : 0, FILL_H);
lv_obj_set_pos(fill, fill_x, fill_y);
lv_obj_set_style_bg_color(fill, lv_color_hex(fill_color), 0);
lv_obj_set_style_bg_opa(fill, (fill_w > 0) ? LV_OPA_COVER : LV_OPA_TRANSP, 0);
lv_obj_set_style_radius(fill, FILL_RADIUS, 0);
lv_obj_set_style_border_width(fill, 0, 0);
lv_obj_clear_flag(fill, LV_OBJ_FLAG_SCROLLABLE | LV_OBJ_FLAG_CLICKABLE);
// 注册到跟踪列表
slots[slot_count].notch = notch;
slots[slot_count].fill = fill;
slots[slot_count].fill_x = fill_x;
slots[slot_count].fill_y = fill_y;
slot_count++;
}
void battery_ui_show_briefly(void) {
// 先更新电量显示为最新值
int fill_w = FILL_MAX_W * cached_level / 100;
uint32_t fill_color = (cached_level <= BAT_LOW_THRESH) ? BAT_COLOR_RED : BAT_COLOR_GREEN;
// 清理已销毁的slot
int valid = 0;
for (int i = 0; i < slot_count; i++) {
if (lv_obj_is_valid(slots[i].notch)) {
slots[valid] = slots[i];
valid++;
}
}
slot_count = valid;
// 取消正在进行的渐隐动画并立即显示
for (int i = 0; i < slot_count; i++) {
lv_obj_t *notch = slots[i].notch;
lv_anim_del(notch, fade_anim_cb); // 取消之前的渐隐动画
lv_obj_set_style_opa(notch, LV_OPA_COVER, 0); // 立即完全可见
// 更新填充条
lv_obj_t *fill = slots[i].fill;
if (lv_obj_is_valid(fill)) {
lv_obj_set_size(fill, (fill_w > 0) ? fill_w : 0, FILL_H);
lv_obj_set_style_bg_color(fill, lv_color_hex(fill_color), 0);
lv_obj_set_style_bg_opa(fill, (fill_w > 0) ? LV_OPA_COVER : LV_OPA_TRANSP, 0);
}
}
// 重置定时器3秒后渐隐
if (hide_timer) {
lv_timer_del(hide_timer);
}
hide_timer = lv_timer_create(hide_timer_cb, SHOW_DURATION_MS, NULL);
lv_timer_set_repeat_count(hide_timer, 1); // 单次触发
}
void battery_ui_update_level(int level) {
if (level < 0) level = 0;
if (level > 100) level = 100;
cached_level = level;
// 不主动更新UI显示等 show_briefly 时才刷新)
// 这样避免在隐藏状态下做无用的UI操作
}