优化整定一版
This commit is contained in:
@ -14,9 +14,10 @@
|
||||
* @brief 菜单事件结构体(事件队列元素)
|
||||
*/
|
||||
typedef struct {
|
||||
MenuEventType type; ///< 事件类型
|
||||
uint32_t param; ///< 事件附加参数
|
||||
uint32_t timestamp; ///< 事件产生时间(ms,用于超时处理)
|
||||
MenuEventType type; ///< 事件类型
|
||||
uint32_t param; ///< 事件附加参数
|
||||
uint32_t timestamp; ///< 事件产生时间(ms,用于超时处理)
|
||||
MenuEventPriority priority; ///< 事件优先级
|
||||
} MenuEvent;
|
||||
|
||||
/**
|
||||
@ -61,6 +62,27 @@ typedef struct {
|
||||
uint8_t count; ///< 队列元素数量
|
||||
} MenuEventQueue;
|
||||
|
||||
/**
|
||||
* @brief 菜单状态枚举(状态机核心)
|
||||
*/
|
||||
typedef enum {
|
||||
MENU_STATE_INIT = 0, ///< 初始化状态
|
||||
MENU_STATE_NORMAL, ///< 正常导航状态
|
||||
MENU_STATE_PARAM_EDIT, ///< 参数编辑状态
|
||||
MENU_STATE_CONFIRM, ///< 确认状态(如保存/退出确认)
|
||||
MENU_STATE_ERROR, ///< 错误状态
|
||||
} MenuState;
|
||||
|
||||
/**
|
||||
* @brief 菜单状态转换结构体
|
||||
*/
|
||||
typedef struct {
|
||||
MenuState current_state; ///< 当前状态
|
||||
MenuEventType event; ///< 触发事件
|
||||
MenuState next_state; ///< 下一个状态
|
||||
MenuErrCode (*action)(MenuGlobalCtx* global_ctx); ///< 状态转换动作
|
||||
} MenuStateTransition;
|
||||
|
||||
/**
|
||||
* @brief 菜单核心上下文(全局唯一,存储菜单状态)
|
||||
*/
|
||||
@ -69,6 +91,7 @@ typedef struct {
|
||||
MenuStack stack; ///< 菜单导航栈
|
||||
MenuEventQueue event_queue; ///< 事件队列
|
||||
MenuNodeId current_node_id; ///< 当前选中的节点ID
|
||||
MenuState current_state; ///< 当前菜单状态
|
||||
uint32_t last_refresh_tick; ///< 上次刷新时间(ms)
|
||||
bool is_initialized; ///< 是否已初始化
|
||||
} MenuCoreCtx;
|
||||
@ -76,34 +99,25 @@ typedef struct {
|
||||
/************************** 核心内部接口声明 **************************/
|
||||
/**
|
||||
* @brief 查找菜单节点(通过ID)
|
||||
* @param global_ctx 全局上下文指针
|
||||
* @param node_id 节点ID
|
||||
* @return 节点指针(NULL表示未找到)
|
||||
*/
|
||||
MenuNode* menu_core_find_node(MenuNodeId node_id);
|
||||
|
||||
/**
|
||||
* @brief 初始化菜单核心上下文
|
||||
* @return 错误码
|
||||
*/
|
||||
MenuErrCode menu_core_ctx_init(void);
|
||||
|
||||
/**
|
||||
* @brief 获取菜单核心上下文(内部唯一访问入口)
|
||||
* @return 菜单核心上下文指针
|
||||
*/
|
||||
MenuCoreCtx* menu_core_get_ctx(void);
|
||||
MenuNode* menu_core_find_node(MenuGlobalCtx* global_ctx, MenuNodeId node_id);
|
||||
|
||||
/**
|
||||
* @brief 处理单个菜单事件
|
||||
* @param global_ctx 全局上下文指针
|
||||
* @param event 事件指针
|
||||
* @return 错误码
|
||||
*/
|
||||
MenuErrCode menu_core_handle_event(const MenuEvent* event);
|
||||
MenuErrCode menu_core_handle_event(MenuGlobalCtx* global_ctx, const MenuEvent* event);
|
||||
|
||||
/**
|
||||
* @brief 刷新菜单显示(内部调用,对接port层显示接口)
|
||||
* @param global_ctx 全局上下文指针
|
||||
*/
|
||||
void menu_core_refresh_display(void);
|
||||
void menu_core_refresh_display(MenuGlobalCtx* global_ctx);
|
||||
|
||||
|
||||
|
||||
|
||||
@ -5,68 +5,58 @@
|
||||
#ifndef MENU_DATA_H
|
||||
#define MENU_DATA_H
|
||||
|
||||
#include "../api/menu.h"
|
||||
#include "menu_core.h"
|
||||
#include "menu_modbus.h"
|
||||
#include "menu_param.h"
|
||||
|
||||
/************************** 共享全局变量(声明,内部可见) **************************/
|
||||
/************************** 共享上下文结构(替代全局变量) **************************/
|
||||
/**
|
||||
* @brief 菜单核心上下文(全局,仅内部访问)
|
||||
*/
|
||||
extern MenuCoreCtx s_menu_core_ctx;
|
||||
|
||||
/**
|
||||
* @brief 参数管理上下文(启用参数时有效,全局)
|
||||
* @brief 参数管理上下文(启用参数时有效)
|
||||
*/
|
||||
#if MENU_CONFIG_ENABLE_PARAM
|
||||
typedef struct {
|
||||
uint16_t id; ///< 参数ID
|
||||
MenuParamType type; ///< 参数类型
|
||||
float min_val; ///< 最小值
|
||||
float max_val; ///< 最大值
|
||||
float scale; ///< 缩放因子
|
||||
union { ///< 共用体:减少内存占用
|
||||
int8_t i8;
|
||||
uint8_t u8;
|
||||
int16_t i16;
|
||||
uint16_t u16;
|
||||
int32_t i32;
|
||||
uint32_t u32;
|
||||
float f;
|
||||
} value; ///< 当前值
|
||||
union { ///< 默认值
|
||||
int8_t i8;
|
||||
uint8_t u8;
|
||||
int16_t i16;
|
||||
uint16_t u16;
|
||||
int32_t i32;
|
||||
uint32_t u32;
|
||||
float f;
|
||||
} default_val;
|
||||
bool is_registered; ///< 是否已注册
|
||||
} MenuParam;
|
||||
|
||||
extern MenuParam s_menu_params[MENU_CONFIG_MAX_PARAMS];
|
||||
MenuParam params[MENU_CONFIG_MAX_PARAMS]; ///< 参数数组
|
||||
uint16_t count; ///< 已注册参数数量
|
||||
} MenuParamCtx;
|
||||
#endif // MENU_CONFIG_ENABLE_PARAM
|
||||
|
||||
/**
|
||||
* @brief 多语言上下文(启用多语言时有效,全局)
|
||||
* @brief 多语言上下文(启用多语言时有效)
|
||||
*/
|
||||
#if MENU_CONFIG_ENABLE_LANG
|
||||
typedef struct {
|
||||
const char* str; ///< 语言字符串
|
||||
uint16_t str_id; ///< 字符串ID
|
||||
uint8_t lang_id; ///< 语言ID
|
||||
} MenuLangStr;
|
||||
|
||||
extern MenuLangStr s_menu_lang_strs[MENU_CONFIG_MAX_NODES * MENU_CONFIG_MAX_LANGS];
|
||||
extern uint8_t s_current_lang_id;
|
||||
MenuLangStr strs[MENU_CONFIG_MAX_NODES * MENU_CONFIG_MAX_LANGS]; ///< 语言字符串数组
|
||||
uint8_t count; ///< 已注册字符串数量
|
||||
uint8_t current_lang_id; ///< 当前语言ID
|
||||
} MenuLangCtx;
|
||||
#endif // MENU_CONFIG_ENABLE_LANG
|
||||
|
||||
/**
|
||||
* @brief Modbus映射上下文(启用Modbus映射时有效,全局)
|
||||
* @brief Modbus映射上下文(启用Modbus映射时有效)
|
||||
*/
|
||||
#if MENU_CONFIG_ENABLE_MODBUS_MAP
|
||||
extern ModbusMapInternal s_menu_modbus_maps[MENU_CONFIG_MAX_MODBUS_MAPS];
|
||||
typedef struct {
|
||||
ModbusMapInternal maps[MENU_CONFIG_MAX_MODBUS_MAPS]; ///< Modbus映射数组
|
||||
uint16_t count; ///< 已注册映射数量
|
||||
} MenuModbusCtx;
|
||||
#endif // MENU_CONFIG_ENABLE_MODBUS_MAP
|
||||
|
||||
/**
|
||||
* @brief 菜单组件全局上下文(替代所有全局变量)
|
||||
*/
|
||||
struct MenuGlobalCtx {
|
||||
MenuCoreCtx core; ///< 核心上下文
|
||||
#if MENU_CONFIG_ENABLE_PARAM
|
||||
MenuParamCtx param; ///< 参数上下文
|
||||
#endif
|
||||
#if MENU_CONFIG_ENABLE_LANG
|
||||
MenuLangCtx lang; ///< 多语言上下文
|
||||
#endif
|
||||
#if MENU_CONFIG_ENABLE_MODBUS_MAP
|
||||
MenuModbusCtx modbus; ///< Modbus映射上下文
|
||||
#endif
|
||||
bool is_initialized; ///< 是否已初始化
|
||||
};
|
||||
|
||||
#endif // MENU_DATA_H
|
||||
@ -26,13 +26,32 @@
|
||||
#define MENU_ASSERT(condition) ((void)0)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief 日志级别定义
|
||||
*/
|
||||
typedef enum {
|
||||
MENU_LOG_LEVEL_DEBUG, ///< 调试信息
|
||||
MENU_LOG_LEVEL_INFO, ///< 普通信息
|
||||
MENU_LOG_LEVEL_WARN, ///< 警告信息
|
||||
MENU_LOG_LEVEL_ERROR, ///< 错误信息
|
||||
MENU_LOG_LEVEL_CRITICAL, ///< 严重错误
|
||||
} MenuLogLevel;
|
||||
|
||||
/**
|
||||
* @brief 调试打印宏(工业级:集中控制调试输出)
|
||||
*/
|
||||
#if MENU_CONFIG_ENABLE_DEBUG
|
||||
#define MENU_DEBUG(fmt, ...) menu_utils_printf("[MENU DEBUG] " fmt "\r\n", ##__VA_ARGS__)
|
||||
#define MENU_DEBUG(fmt, ...) menu_utils_log(MENU_LOG_LEVEL_DEBUG, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#define MENU_INFO(fmt, ...) menu_utils_log(MENU_LOG_LEVEL_INFO, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#define MENU_WARN(fmt, ...) menu_utils_log(MENU_LOG_LEVEL_WARN, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#define MENU_ERROR(fmt, ...) menu_utils_log(MENU_LOG_LEVEL_ERROR, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#define MENU_CRITICAL(fmt, ...) menu_utils_log(MENU_LOG_LEVEL_CRITICAL, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
#else
|
||||
#define MENU_DEBUG(fmt, ...) ((void)0)
|
||||
#define MENU_INFO(fmt, ...) ((void)0)
|
||||
#define MENU_WARN(fmt, ...) ((void)0)
|
||||
#define MENU_ERROR(fmt, ...) ((void)0)
|
||||
#define MENU_CRITICAL(fmt, ...) ((void)0)
|
||||
#endif
|
||||
|
||||
/**
|
||||
@ -45,6 +64,18 @@
|
||||
*/
|
||||
#define MENU_INVALID_ID ((MenuNodeId)0xFFFF)
|
||||
|
||||
/**
|
||||
* @brief 错误处理宏(简化错误检查和返回)
|
||||
*/
|
||||
#define MENU_CHECK_ERR(expr) \
|
||||
do { \
|
||||
MenuErrCode err = (expr); \
|
||||
if (err != MENU_OK) { \
|
||||
MENU_ERROR("Error at %s:%d: %s", __FILE__, __LINE__, menu_err_code_to_str(err)); \
|
||||
return err; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/************************** 内部辅助函数声明 **************************/
|
||||
/**
|
||||
* @brief 断言失败处理函数
|
||||
@ -54,11 +85,14 @@
|
||||
void menu_utils_assert_failed(const char* file, uint32_t line);
|
||||
|
||||
/**
|
||||
* @brief 调试打印函数(对接port层的硬件打印接口)
|
||||
* @brief 通用日志函数(对接port层的硬件打印接口)
|
||||
* @param level 日志级别
|
||||
* @param file 文件名
|
||||
* @param line 行号
|
||||
* @param fmt 格式化字符串
|
||||
* @param ... 可变参数
|
||||
*/
|
||||
void menu_utils_printf(const char* fmt, ...);
|
||||
void menu_utils_log(MenuLogLevel level, const char* file, uint32_t line, const char* fmt, ...);
|
||||
|
||||
/**
|
||||
* @brief 获取系统滴答时间(ms,对接port层)
|
||||
@ -66,4 +100,11 @@ void menu_utils_printf(const char* fmt, ...);
|
||||
*/
|
||||
uint32_t menu_utils_get_tick(void);
|
||||
|
||||
/**
|
||||
* @brief 错误码转换为字符串
|
||||
* @param err_code 错误码
|
||||
* @return 错误信息字符串
|
||||
*/
|
||||
const char* menu_err_code_to_str(MenuErrCode err_code);
|
||||
|
||||
#endif // MENU_DEF_H
|
||||
Reference in New Issue
Block a user