初始化版本
This commit is contained in:
108
internal/menu_core.h
Normal file
108
internal/menu_core.h
Normal file
@ -0,0 +1,108 @@
|
||||
/**
|
||||
* @file menu_core.h
|
||||
* @brief 菜单组件核心类型定义(用户无需关心)
|
||||
*/
|
||||
#ifndef MENU_CORE_H
|
||||
#define MENU_CORE_H
|
||||
|
||||
#include "menu_def.h"
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/************************** 核心数据结构 **************************/
|
||||
/**
|
||||
* @brief 菜单事件结构体(事件队列元素)
|
||||
*/
|
||||
typedef struct {
|
||||
MenuEventType type; ///< 事件类型
|
||||
uint32_t param; ///< 事件附加参数
|
||||
uint32_t timestamp; ///< 事件产生时间(ms,用于超时处理)
|
||||
} MenuEvent;
|
||||
|
||||
/**
|
||||
* @brief 菜单节点结构体(菜单树的基本单元)
|
||||
* @note 紧凑设计:使用位域和共用体减少内存占用(工业级嵌入式低内存优化)
|
||||
*/
|
||||
typedef struct MenuNode {
|
||||
MenuNodeId id; ///< 节点ID(唯一)
|
||||
MenuNodeId parent_id; ///< 父节点ID(根节点为0)
|
||||
const char* name; ///< 菜单名称(或多语言索引)
|
||||
MenuCallback enter_cb; ///< 进入回调
|
||||
MenuCallback exit_cb; ///< 退出回调
|
||||
struct MenuNode* first_child;///< 第一个子节点
|
||||
struct MenuNode* next_sibling;///< 下一个兄弟节点
|
||||
struct MenuNode* prev_sibling;///< 上一个兄弟节点
|
||||
// 位域:减少内存占用(1字节代替多个u8变量)
|
||||
struct {
|
||||
bool is_registered : 1; ///< 是否已注册
|
||||
bool is_selected : 1; ///< 是否被选中
|
||||
unsigned int reserved : 6; ///< 保留位
|
||||
} flags;
|
||||
#if MENU_CONFIG_ENABLE_PARAM
|
||||
uint16_t param_id; ///< 绑定的参数ID(启用参数时有效)
|
||||
#endif
|
||||
} MenuNode;
|
||||
|
||||
/**
|
||||
* @brief 菜单栈结构体(管理导航层级)
|
||||
*/
|
||||
typedef struct {
|
||||
MenuNodeId nodes[MENU_CONFIG_STACK_DEPTH]; ///< 栈元素(存储节点ID)
|
||||
uint8_t top; ///< 栈顶指针
|
||||
} MenuStack;
|
||||
|
||||
/**
|
||||
* @brief 事件队列结构体(环形队列,工业级:避免溢出)
|
||||
*/
|
||||
typedef struct {
|
||||
MenuEvent buffer[MENU_CONFIG_EVENT_QUEUE_LEN]; ///< 队列缓冲区
|
||||
uint8_t head; ///< 入队指针
|
||||
uint8_t tail; ///< 出队指针
|
||||
uint8_t count; ///< 队列元素数量
|
||||
} MenuEventQueue;
|
||||
|
||||
/**
|
||||
* @brief 菜单核心上下文(全局唯一,存储菜单状态)
|
||||
*/
|
||||
typedef struct {
|
||||
MenuNode nodes[MENU_CONFIG_MAX_NODES]; ///< 静态菜单节点池(无动态分配)
|
||||
MenuStack stack; ///< 菜单导航栈
|
||||
MenuEventQueue event_queue; ///< 事件队列
|
||||
MenuNodeId current_node_id; ///< 当前选中的节点ID
|
||||
uint32_t last_refresh_tick; ///< 上次刷新时间(ms)
|
||||
bool is_initialized; ///< 是否已初始化
|
||||
} MenuCoreCtx;
|
||||
|
||||
/************************** 核心内部接口声明 **************************/
|
||||
/**
|
||||
* @brief 查找菜单节点(通过ID)
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @brief 处理单个菜单事件
|
||||
* @param event 事件指针
|
||||
* @return 错误码
|
||||
*/
|
||||
MenuErrCode menu_core_handle_event(const MenuEvent* event);
|
||||
|
||||
/**
|
||||
* @brief 刷新菜单显示(内部调用,对接port层显示接口)
|
||||
*/
|
||||
void menu_core_refresh_display(void);
|
||||
|
||||
#endif // MENU_CORE_H
|
||||
64
internal/menu_data.h
Normal file
64
internal/menu_data.h
Normal file
@ -0,0 +1,64 @@
|
||||
/**
|
||||
* @file menu_data.h
|
||||
* @brief 菜单组件共享全局变量(用户无需关心,内部仅通过接口访问)
|
||||
*/
|
||||
#ifndef MENU_DATA_H
|
||||
#define MENU_DATA_H
|
||||
|
||||
#include "menu_core.h"
|
||||
|
||||
/************************** 共享全局变量(声明,内部可见) **************************/
|
||||
/**
|
||||
* @brief 菜单核心上下文(全局,仅内部访问)
|
||||
*/
|
||||
extern MenuCoreCtx s_menu_core_ctx;
|
||||
|
||||
/**
|
||||
* @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];
|
||||
#endif // MENU_CONFIG_ENABLE_PARAM
|
||||
|
||||
/**
|
||||
* @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;
|
||||
#endif // MENU_CONFIG_ENABLE_LANG
|
||||
|
||||
#endif // MENU_DATA_H
|
||||
69
internal/menu_def.h
Normal file
69
internal/menu_def.h
Normal file
@ -0,0 +1,69 @@
|
||||
/**
|
||||
* @file menu_def.h
|
||||
* @brief 菜单组件内部宏定义和辅助函数(用户无需关心)
|
||||
*/
|
||||
#ifndef MENU_DEF_H
|
||||
#define MENU_DEF_H
|
||||
|
||||
#include "menu_config.h"
|
||||
#include "menu.h"
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
/************************** 内部宏定义 **************************/
|
||||
/**
|
||||
* @brief 断言宏(工业级:调试时检查,发布时忽略)
|
||||
*/
|
||||
#if MENU_CONFIG_ENABLE_ASSERT
|
||||
#define MENU_ASSERT(condition) \
|
||||
do { \
|
||||
if (!(condition)) { \
|
||||
menu_utils_assert_failed(__FILE__, __LINE__); \
|
||||
while (1); \
|
||||
} \
|
||||
} while (0)
|
||||
#else
|
||||
#define MENU_ASSERT(condition) ((void)0)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief 调试打印宏(工业级:集中控制调试输出)
|
||||
*/
|
||||
#if MENU_CONFIG_ENABLE_DEBUG
|
||||
#define MENU_DEBUG(fmt, ...) menu_utils_printf("[MENU DEBUG] " fmt "\r\n", ##__VA_ARGS__)
|
||||
#else
|
||||
#define MENU_DEBUG(fmt, ...) ((void)0)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief 内存清零宏
|
||||
*/
|
||||
#define MENU_MEM_SET_ZERO(ptr, size) memset((ptr), 0, (size))
|
||||
|
||||
/**
|
||||
* @brief 无效ID定义
|
||||
*/
|
||||
#define MENU_INVALID_ID ((MenuNodeId)0xFFFF)
|
||||
|
||||
/************************** 内部辅助函数声明 **************************/
|
||||
/**
|
||||
* @brief 断言失败处理函数
|
||||
* @param file 文件名
|
||||
* @param line 行号
|
||||
*/
|
||||
void menu_utils_assert_failed(const char* file, uint32_t line);
|
||||
|
||||
/**
|
||||
* @brief 调试打印函数(对接port层的硬件打印接口)
|
||||
* @param fmt 格式化字符串
|
||||
* @param ... 可变参数
|
||||
*/
|
||||
void menu_utils_printf(const char* fmt, ...);
|
||||
|
||||
/**
|
||||
* @brief 获取系统滴答时间(ms,对接port层)
|
||||
* @return 当前滴答时间
|
||||
*/
|
||||
uint32_t menu_utils_get_tick(void);
|
||||
|
||||
#endif // MENU_DEF_H
|
||||
Reference in New Issue
Block a user