Files
STM32F4-Base/Drivers/BSP/UI/ui_w_setlist.c
T
wandering1 f711130b4c feat(UI组件): 新增数值编辑器/定值表/动作确认页三组件(道B task-5)
域5 新增 3 对文件(双端注册 uvprojx + CMakeLists),只做渲染与输入抽象、
不接真实数据源(数据绑定 = 批 2 道E ui_data/prot_set 经回调注入):
- ui_w_editor:定点整数(10^decimals)逐位编辑——上下步进(0..9 环绕无进位)、
  左右移数字光标(首末环绕)、ENTER 提交经 min/max 范围校验,越界拒绝并
  显示"越界"提示留在编辑态;光标反显盒与 ui_page_net_cfg 字节光标同画法
  (含"只画单字符防折行"实测坑注释);共享格式化 ui_w_editor_fmt_scaled()
  单一规则源(值列显示与编辑光标恒同串)
- ui_w_setlist:定值四视图(参数/定值/控制字/软压板)共用列表渲染 +
  整定输入——行表(名称<=7字/id/类型/小数位/范围/二态串表) + 回调注入
  (get/set/commit/state/bad);查看视图(commit=NULL)与整定视图只差绑定:
  整定视图末行固定"保存"行,值随交接态 ENTER/WAIT/OK/ERR(net_cfg 先例),
  整表一次提交(一次封存一次校验和,页面补齐规划 §3.2);二态行 ENTER
  循环切换;保存失败越界行经 bad 回调反显(页面扩充详细方案 §3.2.7)
- ui_page_action:可复用操作确认链——一次确认 -> 二次确认 -> ok_cb
  只投递不阻塞 -> done_text 回报(或成功即关页),拒收显示 ERR 可重试;
  ESC/LEFT 关页取消(实测取消后菜单与基线逐字节一致);单例描述符由
  开页装载(内容页框架全局单页前提),不定义 ui_page_action_desc
  (registration-check 页面模块判据不误拦,批 2 跳板各自出口)

组件文案用字全部落在上一分支预置字集内(font-charset WARN 60->52 印证
确/取/消/越/二/次/返/回 已转源码在用),无需再动字库。

BSP 公开 API 约定合规(首提被 cppcheck/coding-standard/bsp-err 三钩拦下,
按门禁口径修净后复验全绿):
- 键分发返回值取 uint8_t 1/0 不用 bool(bsp-err/err-type:bool 只许
  is_/has_ 谓词);ui_w_editor_value 直返 int32_t(出参形状无错误通道)
- ui_page_action_open 返回 bsp_err_t:BSP_ERR_PARAM 参数无效 /
  BSP_ERR_STATE 已有页打开 / BSP_ERR_NO_SPACE 框架开页失败(可判分支)
- get/state/bad 回调 typedef 各补"返回值:"注释块(bsp-err/return-doc)
- cppcheck variableScope 变量收scope 7 处;CODE-RULE-18 Yoda 1 处
  (sel+1u==total -> sel==count);提交内已不含 --wdemo/--wact harness

验证:
- 模拟器零警告;组件行为经临时 sim-only harness(--wdemo/--wact) 截图
  目检 7 态:基线视图/bad 反显/进编辑步进/越界拒绝/二态切换/保存整表
  提交 OK/确认链三段/取消回菜单;harness(main.c 4 处 + CMake 1 行 +
  ui_w_demo.c)已完整还原删除,提交内不含(最终树 main.c 无改动,
  CMakeLists diff 仅 3 行注册)
- 回归:菜单 8 状态 + 内容页 11 页黄金参考逐字节 PASS(ref 无变化,
  无基线更新提交);池基线 5368/往返 5448 不变
- 门禁:coding-standard PASS / font-charset PASS / layer-deps PASS
  (新文件零 App include,基线 6 不变——减基线归 task-6)
- @build 0 Error 0 Warning;Code +324 B、RO/RW/ZI 0(组件尚无引用方,
  ARM 链接器未剥离仅因新 TU;批 2 页面接线后实例 RAM 每页约 +40~60 B)

gate: 全钩子正常执行通过,无 --no-verify
- pre-commit run --all-files 全绿(含 mem-budget/mem-numbers:本分支基于
  回填后台账 d9ab955,RAM 0 变化、RO 不变,仅 Code +324 B 不涉受管区)
- 手工自查:6 新文件 BOM=EF BB BF 齐全、全 LF、U+FFFD=0、字面量仅
  UTF-8 中文(UI 带 BOM 先例口径:保存/越界/二次确认/告警/跳闸等,
  全部已在字体子集);ASCII 串仅 ENTER/WAIT/OK/ERR 状态字
2026-10-25 04:34:43 +08:00

327 lines
11 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.
/*
* 模块名称:UI 定值表共享组件(ui_w_setlist)实现
* 模块功能:见 ui_w_setlist.h 头注释(四视图共用列表渲染 + 整定输入抽象,
* 数据经回调注入,数值行走 ui_w_editor,二态行 ENTER 循环切换)
* 适用平台:STM32F407ZGTx / PC 模拟器(两端共用)
* 作者:王建锋
* 创建日期:2026-10-25
* 修改记录:
* v1.0 2026-10-25 王建锋 创建(道B task-5 三组件之一)
*/
/* 头文件包含区 */
#include <string.h>
#include "ui_w_setlist.h"
#include "ui_tokens.h"
/* 私有宏定义区 */
#define SL_SAVE_NAME "保存" /* 保存行名(字体子集已含 保/存) */
#define SL_WIN_ROWS UI_LIST_ROWS /* 窗口行数(与框架 rows_per_page 同令牌) */
/* 函数定义区 */
/*
* 函数功能:判断两区域相交(与本目录各绘制文件同实现:static 局部)
* 入口参数:a - 区域 1 const lv_area_t *
* b - 区域 2 const lv_area_t *
* 返回值:true = 相交 bool
* 限定条件:无
*/
static bool sl_areas_intersect(const lv_area_t *a, const lv_area_t *b) {
int32_t x1 = (a->x1 > b->x1) ? a->x1 : b->x1;
int32_t y1 = (a->y1 > b->y1) ? a->y1 : b->y1;
int32_t x2 = (a->x2 < b->x2) ? a->x2 : b->x2;
int32_t y2 = (a->y2 < b->y2) ? a->y2 : b->y2;
return (x1 <= x2 && y1 <= y2);
}
/*
* 函数功能:整定视图判定(挂了提交回调 = 有保存行、可编辑)
* 入口参数:inst - 定值表实例 const ui_w_setlist_t *
* 返回值:true = 整定视图 bool
* 限定条件:inst 非空由调用链保证
*/
static bool sl_is_rw(const ui_w_setlist_t *a_inst) {
return (a_inst->commit != NULL);
}
/*
* 函数功能:二态行串表长度(NULL 结尾)
* 入口参数:opts - 串表 const char *const *
* 返回值:条目数(>=1;空表防御返回 1) uint8_t
* 限定条件:无
*/
static uint8_t sl_opts_count(const char *const *a_opts) {
uint8_t n = 0u;
if (a_opts == NULL) {
return 1u;
}
while (a_opts[n] != NULL) {
n++;
}
return (n > 0u) ? n : 1u;
}
/*
* 函数功能:当前页窗口顶行下标(与框架 cp_draw_rows / cp_draw_footer 同式)
* 入口参数:inst - 定值表实例 const ui_w_setlist_t *
* 返回值:窗口顶行(全局下标) uint16_t
* 限定条件:编辑中行恒在选中行所在页(编辑态 ↑↓ 被编辑器消费,选中不移)
*/
static uint16_t sl_window_first(const ui_w_setlist_t *a_inst) {
uint16_t sel = ui_content_page_selected_row();
(void)a_inst;
return (uint16_t)((sel / SL_WIN_ROWS) * SL_WIN_ROWS);
}
/*
* 函数功能:行槽顶 y(窗口内)
* 入口参数:inst - 定值表实例 const ui_w_setlist_t *
* row - 全局行下标 uint16_t
* 返回值:行槽顶 y int32_t
* 限定条件:row 在当前窗口内(绘制侧由 bad/ed 行可见性决定,越窗行不绘)
*/
static int32_t sl_slot_y(const ui_w_setlist_t *a_inst, uint16_t a_row) {
return UI_LIST_TOP + ((int32_t)a_row - (int32_t)sl_window_first(a_inst)) * UI_ROW_PITCH;
}
/*
* 函数功能:开页复位(清编辑态)
*/
void ui_w_setlist_reset(ui_w_setlist_t *a_inst) {
ui_w_editor_reset(&a_inst->ed);
a_inst->ed_row = 0u;
}
/*
* 函数功能:总行数(数据行 + 保存行[整定视图])
*/
uint16_t ui_w_setlist_count(const ui_w_setlist_t *a_inst) {
if (a_inst->rows == NULL) {
return 0u;
}
return (uint16_t)(a_inst->count + (sl_is_rw(a_inst) ? 1u : 0u));
}
/*
* 函数功能:逐行取名/值(框架 refresh 转发)
*/
void ui_w_setlist_refresh(ui_w_setlist_t *a_inst, uint16_t a_row,
char *a_name, char *a_value) {
uint16_t total = ui_w_setlist_count(a_inst);
a_name[0] = '\0';
a_value[0] = '\0';
if ((a_inst->rows == NULL) || (a_row >= total)) {
return; /* 防御:空表/行越界(动态收缩瞬间) */
}
if (a_row < a_inst->count) {
const ui_w_setlist_row_t *r = &a_inst->rows[a_row];
(void)lv_snprintf(a_name, UI_BUF_STRLEN, "%s", r->name);
if (r->kind == UI_W_SETLIST_KIND_ENUM) {
int32_t idx = a_inst->get(r->id);
uint8_t n = sl_opts_count(r->opts);
if (idx < 0) {
idx = 0;
}
if (idx >= (int32_t)n) {
idx = (int32_t)n - 1;
}
(void)lv_snprintf(a_value, UI_BUF_STRLEN, "%s", r->opts[idx]);
return;
}
if ((a_inst->ed.editing != 0u) && (a_inst->ed_row == a_row)) {
ui_w_editor_format(&a_inst->ed, a_value, UI_BUF_STRLEN); /* 编辑实时串 */
}
else {
ui_w_editor_fmt_scaled(a_inst->get(r->id), r->decimals, a_value, UI_BUF_STRLEN);
}
return;
}
/* 保存行(整定视图最后一行;名"保存",值随交接态——net_cfg 保存行先例) */
(void)lv_snprintf(a_name, UI_BUF_STRLEN, "%s", SL_SAVE_NAME);
{
ui_w_setlist_st_t st = (a_inst->state != NULL) ? a_inst->state()
: UI_W_SETLIST_ST_IDLE;
switch (st) {
case UI_W_SETLIST_ST_WAIT:
(void)lv_snprintf(a_value, UI_BUF_STRLEN, "%s", "WAIT");
break;
case UI_W_SETLIST_ST_OK:
(void)lv_snprintf(a_value, UI_BUF_STRLEN, "%s", "OK");
break;
case UI_W_SETLIST_ST_FAIL:
(void)lv_snprintf(a_value, UI_BUF_STRLEN, "%s", "ERR");
break;
default:
(void)lv_snprintf(a_value, UI_BUF_STRLEN, "%s", "ENTER");
break;
}
}
}
/*
* 函数功能:按键处理(框架 key_cb 转发)
*/
uint8_t ui_w_setlist_key(ui_w_setlist_t *a_inst, lv_key_t a_key) {
uint16_t sel = ui_content_page_selected_row();
if (a_inst->rows == NULL) {
return 0u;
}
/* 编辑态:键先交编辑器 */
if (a_inst->ed.editing != 0u) {
if ((sel != a_inst->ed_row) || (sel >= a_inst->count)) {
/* 防御:编辑中选中被外部改动(正常不会——编辑态 ↑↓ 被消费)→ 弃编辑 */
ui_w_editor_reset(&a_inst->ed);
return 0u;
}
if (ui_w_editor_key(&a_inst->ed, a_key) == 0u) {
return 0u; /* 未消费键(本组件编辑态只接方向/ENTER) */
}
if (a_inst->ed.editing == 0u) {
/* ENTER 提交成功:值写 RAM 工作副本,落盘统一走保存行整表提交 */
a_inst->set(a_inst->rows[sel].id, ui_w_editor_value(&a_inst->ed));
}
ui_content_page_invalidate();
return 1u;
}
if (a_key == LV_KEY_ENTER) {
if (ui_content_page_is_editable() == false) {
return 0u; /* 只读权限 ENTER 一律忽略(框架内置也无 ENTER 语义) */
}
if (sel < a_inst->count) {
const ui_w_setlist_row_t *r = &a_inst->rows[sel];
if (r->kind == UI_W_SETLIST_KIND_NUM) {
ui_w_editor_begin(&a_inst->ed, a_inst->get(r->id),
r->min, r->max, r->decimals);
a_inst->ed_row = sel;
ui_content_page_invalidate();
return 1u;
}
/* 二态行:循环切换(控制字 告警<->跳闸、软压板 投入<->退出) */
{
int32_t cur = a_inst->get(r->id);
uint8_t n = sl_opts_count(r->opts);
int32_t nxt = cur + 1;
if ((cur < 0) || (cur >= (int32_t)n)) {
nxt = 0;
}
a_inst->set(r->id, nxt);
ui_content_page_invalidate();
return 1u;
}
}
if ((sel == a_inst->count) && sl_is_rw(a_inst)) {
/* 保存行(数据行之后的最后一行):整表提交投递(一次封存一次校验和,
* 显示由 state 回读) */
a_inst->commit();
ui_content_page_invalidate();
return 1u;
}
}
return 0u; /* 其余键交回框架:↑↓ 移选、LEFT 关页(编辑态才在上面消费) */
}
/*
* 函数功能:反显一个名区(保存失败越界行:白底黑字覆盖名文本区)
* 入口参数:inst - 定值表实例 const ui_w_setlist_t *
* layer - 绘制层 lv_layer_t *
* clip - 重绘区 const lv_area_t *
* row - 数据行下标 uint16_t
* 返回值:无
* 限定条件:行在当前窗口内(调用方已筛);编辑行不重复反显(选中带已反)
*/
static void sl_invert_name(const ui_w_setlist_t *a_inst, lv_layer_t *a_layer,
const lv_area_t *a_clip, uint16_t a_row) {
lv_area_t box;
int32_t top = sl_slot_y(a_inst, a_row);
box.x1 = UI_SEL_BAND_INSET_L;
box.x2 = UI_LIST_RIGHT_X - 3; /* 名区止于值列前留 3px 缝 */
box.y1 = top;
box.y2 = top + UI_ROW_BAND_H - 1;
if (!sl_areas_intersect(&box, a_clip)) {
return;
}
{
lv_draw_rect_dsc_t fd;
lv_draw_label_dsc_t ld;
char name[UI_BUF_STRLEN];
lv_draw_rect_dsc_init(&fd);
fd.base.layer = a_layer;
fd.bg_color = UI_COL_SEL_BG;
fd.bg_opa = LV_OPA_COVER;
lv_draw_rect(a_layer, &fd, &box);
(void)lv_snprintf(name, sizeof(name), "%s", a_inst->rows[a_row].name);
lv_draw_label_dsc_init(&ld);
ld.base.layer = a_layer;
ld.text = name;
ld.text_static = 0u; /* 栈缓冲(同帧有效) */
ld.color = UI_COL_SEL_FG;
ld.ofs_x = UI_LIST_LEFT_X - UI_SEL_BAND_INSET_L; /* ofs 相对 box.x1 */
ld.ofs_y = 0;
lv_draw_label(a_layer, &ld, &box);
}
}
/*
* 函数功能:自绘(编辑光标反显 + 越界行反显;框架内置区块之后)
*/
void ui_w_setlist_draw(ui_w_setlist_t *a_inst, lv_layer_t *a_layer,
const lv_area_t *a_clip) {
uint16_t first;
if (a_inst->rows == NULL) {
return;
}
first = sl_window_first(a_inst);
/* 1. 保存失败越界行反显(编辑行跳过:选中带已反,避免双重叠绘歧义) */
if (a_inst->bad != NULL) {
uint16_t total = ui_w_setlist_count(a_inst);
for (uint16_t r = first; (r < a_inst->count) && (r < total); r++) {
if ((r == a_inst->ed_row) && (a_inst->ed.editing != 0u)) {
continue;
}
if (r == ui_content_page_selected_row()) {
continue;
}
if (a_inst->bad(r)) {
sl_invert_name(a_inst, a_layer, a_clip, r);
}
}
}
/* 2. 数值编辑光标(含"越界"提示;几何交编辑器按传入行槽绘制) */
if (a_inst->ed.editing != 0u) {
if (a_inst->ed_row >= first) {
ui_w_editor_draw(&a_inst->ed, a_layer, a_clip,
sl_slot_y(a_inst, a_inst->ed_row),
0, UI_ROW_BAND_H, UI_LIST_RIGHT_X);
}
}
}