优化整定一版
This commit is contained in:
@ -7,21 +7,30 @@
|
||||
|
||||
#if MENU_CONFIG_ENABLE_PARAM
|
||||
|
||||
/************************** 全局变量定义 **************************/
|
||||
MenuParam s_menu_params[MENU_CONFIG_MAX_PARAMS];
|
||||
/************************** 内部函数声明 **************************/
|
||||
static MenuParam* menu_param_find(MenuGlobalCtx* global_ctx, uint16_t param_id);
|
||||
static void menu_param_float_to_internal(MenuParam* param, float value);
|
||||
static float menu_param_internal_to_float(const MenuParam* param);
|
||||
|
||||
/**
|
||||
* @brief 查找参数(通过参数ID,内部使用)
|
||||
* @param global_ctx 全局上下文指针
|
||||
* @param param_id 参数ID
|
||||
* @return 参数指针(NULL表示未找到)
|
||||
*/
|
||||
static MenuParam* menu_param_find(uint16_t param_id)
|
||||
static MenuParam* menu_param_find(MenuGlobalCtx* global_ctx, uint16_t param_id)
|
||||
{
|
||||
if (global_ctx == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
MenuParamCtx* param_ctx = &global_ctx->param;
|
||||
for (uint16_t i = 0; i < MENU_CONFIG_MAX_PARAMS; i++)
|
||||
{
|
||||
if (s_menu_params[i].is_registered && s_menu_params[i].id == param_id)
|
||||
if (param_ctx->params[i].is_registered && param_ctx->params[i].id == param_id)
|
||||
{
|
||||
return &s_menu_params[i];
|
||||
return ¶m_ctx->params[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
@ -124,6 +133,7 @@ static float menu_param_internal_to_float(const MenuParam* param)
|
||||
|
||||
/**
|
||||
* @brief 注册参数到菜单节点(参数管理功能)
|
||||
* @param global_ctx 全局上下文指针
|
||||
* @param node_id 菜单节点ID(参数与菜单绑定)
|
||||
* @param param_id 参数ID(唯一)
|
||||
* @param type 参数类型
|
||||
@ -133,33 +143,39 @@ static float menu_param_internal_to_float(const MenuParam* param)
|
||||
* @param scale 缩放因子(如0.1表示保留1位小数)
|
||||
* @return 错误码
|
||||
*/
|
||||
MenuErrCode menu_param_register(MenuNodeId node_id, uint16_t param_id, MenuParamType type, float min_val, float max_val, float default_val, float scale)
|
||||
MenuErrCode menu_param_register(MenuGlobalCtx* global_ctx, MenuNodeId node_id, uint16_t param_id, MenuParamType type, float min_val, float max_val, float default_val, float scale)
|
||||
{
|
||||
if (global_ctx == NULL || !global_ctx->is_initialized)
|
||||
{
|
||||
return MENU_ERR_NOT_INITIALIZED;
|
||||
}
|
||||
|
||||
if (scale <= 0.0f)
|
||||
{
|
||||
return MENU_ERR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
// 查找菜单节点
|
||||
MenuNode* node = menu_core_find_node(node_id);
|
||||
MenuNode* node = menu_core_find_node(global_ctx, node_id);
|
||||
if (node == NULL)
|
||||
{
|
||||
return MENU_ERR_NODE_NOT_FOUND;
|
||||
}
|
||||
|
||||
// 检查参数ID是否已存在
|
||||
if (menu_param_find(param_id) != NULL)
|
||||
if (menu_param_find(global_ctx, param_id) != NULL)
|
||||
{
|
||||
return MENU_ERR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
// 查找空闲参数位置
|
||||
MenuParamCtx* param_ctx = &global_ctx->param;
|
||||
MenuParam* param = NULL;
|
||||
for (uint16_t i = 0; i < MENU_CONFIG_MAX_PARAMS; i++)
|
||||
{
|
||||
if (!s_menu_params[i].is_registered)
|
||||
if (!param_ctx->params[i].is_registered)
|
||||
{
|
||||
param = &s_menu_params[i];
|
||||
param = ¶m_ctx->params[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -194,14 +210,20 @@ MenuErrCode menu_param_register(MenuNodeId node_id, uint16_t param_id, MenuParam
|
||||
|
||||
/**
|
||||
* @brief 设置参数值
|
||||
* @param global_ctx 全局上下文指针
|
||||
* @param param_id 参数ID
|
||||
* @param value 新值(浮点型,内部自动转换)
|
||||
* @return 错误码
|
||||
*/
|
||||
MenuErrCode menu_param_set_value(uint16_t param_id, float value)
|
||||
MenuErrCode menu_param_set_value(MenuGlobalCtx* global_ctx, uint16_t param_id, float value)
|
||||
{
|
||||
if (global_ctx == NULL || !global_ctx->is_initialized)
|
||||
{
|
||||
return MENU_ERR_NOT_INITIALIZED;
|
||||
}
|
||||
|
||||
// 查找参数
|
||||
MenuParam* param = menu_param_find(param_id);
|
||||
MenuParam* param = menu_param_find(global_ctx, param_id);
|
||||
if (param == NULL)
|
||||
{
|
||||
return MENU_ERR_NODE_NOT_FOUND;
|
||||
@ -218,19 +240,20 @@ MenuErrCode menu_param_set_value(uint16_t param_id, float value)
|
||||
|
||||
/**
|
||||
* @brief 获取参数值
|
||||
* @param global_ctx 全局上下文指针
|
||||
* @param param_id 参数ID
|
||||
* @param value 输出参数,当前值(浮点型)
|
||||
* @return 错误码
|
||||
*/
|
||||
MenuErrCode menu_param_get_value(uint16_t param_id, float* value)
|
||||
MenuErrCode menu_param_get_value(MenuGlobalCtx* global_ctx, uint16_t param_id, float* value)
|
||||
{
|
||||
if (value == NULL)
|
||||
if (global_ctx == NULL || !global_ctx->is_initialized || value == NULL)
|
||||
{
|
||||
return MENU_ERR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
|
||||
// 查找参数
|
||||
MenuParam* param = menu_param_find(param_id);
|
||||
MenuParam* param = menu_param_find(global_ctx, param_id);
|
||||
if (param == NULL)
|
||||
{
|
||||
return MENU_ERR_NODE_NOT_FOUND;
|
||||
@ -246,13 +269,19 @@ MenuErrCode menu_param_get_value(uint16_t param_id, float* value)
|
||||
|
||||
/**
|
||||
* @brief 将参数恢复为默认值
|
||||
* @param global_ctx 全局上下文指针
|
||||
* @param param_id 参数ID
|
||||
* @return 错误码
|
||||
*/
|
||||
MenuErrCode menu_param_restore_default(uint16_t param_id)
|
||||
MenuErrCode menu_param_restore_default(MenuGlobalCtx* global_ctx, uint16_t param_id)
|
||||
{
|
||||
if (global_ctx == NULL || !global_ctx->is_initialized)
|
||||
{
|
||||
return MENU_ERR_NOT_INITIALIZED;
|
||||
}
|
||||
|
||||
// 查找参数
|
||||
MenuParam* param = menu_param_find(param_id);
|
||||
MenuParam* param = menu_param_find(global_ctx, param_id);
|
||||
if (param == NULL)
|
||||
{
|
||||
return MENU_ERR_NODE_NOT_FOUND;
|
||||
@ -260,60 +289,72 @@ MenuErrCode menu_param_restore_default(uint16_t param_id)
|
||||
|
||||
// 恢复默认值
|
||||
float default_val = menu_param_internal_to_float((const MenuParam*)¶m->default_val);
|
||||
return menu_param_set_value(param_id, default_val);
|
||||
return menu_param_set_value(global_ctx, param_id, default_val);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 增加参数值(用于菜单上下键调整)
|
||||
* @param global_ctx 全局上下文指针
|
||||
* @param param_id 参数ID
|
||||
* @param step 步长(浮点型)
|
||||
* @return 错误码
|
||||
*/
|
||||
MenuErrCode menu_param_increase(uint16_t param_id, float step)
|
||||
MenuErrCode menu_param_increase(MenuGlobalCtx* global_ctx, uint16_t param_id, float step)
|
||||
{
|
||||
if (global_ctx == NULL || !global_ctx->is_initialized)
|
||||
{
|
||||
return MENU_ERR_NOT_INITIALIZED;
|
||||
}
|
||||
|
||||
float current_val = 0.0f;
|
||||
MenuErrCode err = menu_param_get_value(param_id, ¤t_val);
|
||||
MenuErrCode err = menu_param_get_value(global_ctx, param_id, ¤t_val);
|
||||
if (err != MENU_OK)
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
return menu_param_set_value(param_id, current_val + step);
|
||||
return menu_param_set_value(global_ctx, param_id, current_val + step);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 减少参数值(用于菜单上下键调整)
|
||||
* @param global_ctx 全局上下文指针
|
||||
* @param param_id 参数ID
|
||||
* @param step 步长(浮点型)
|
||||
* @return 错误码
|
||||
*/
|
||||
MenuErrCode menu_param_decrease(uint16_t param_id, float step)
|
||||
MenuErrCode menu_param_decrease(MenuGlobalCtx* global_ctx, uint16_t param_id, float step)
|
||||
{
|
||||
if (global_ctx == NULL || !global_ctx->is_initialized)
|
||||
{
|
||||
return MENU_ERR_NOT_INITIALIZED;
|
||||
}
|
||||
|
||||
float current_val = 0.0f;
|
||||
MenuErrCode err = menu_param_get_value(param_id, ¤t_val);
|
||||
MenuErrCode err = menu_param_get_value(global_ctx, param_id, ¤t_val);
|
||||
if (err != MENU_OK)
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
return menu_param_set_value(param_id, current_val - step);
|
||||
return menu_param_set_value(global_ctx, param_id, current_val - step);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取参数类型
|
||||
* @param global_ctx 全局上下文指针
|
||||
* @param param_id 参数ID
|
||||
* @param type 输出参数,参数类型
|
||||
* @return 错误码
|
||||
*/
|
||||
MenuErrCode menu_param_get_type(uint16_t param_id, MenuParamType* type)
|
||||
MenuErrCode menu_param_get_type(MenuGlobalCtx* global_ctx, uint16_t param_id, MenuParamType* type)
|
||||
{
|
||||
if (type == NULL)
|
||||
if (global_ctx == NULL || !global_ctx->is_initialized || type == NULL)
|
||||
{
|
||||
return MENU_ERR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
// 查找参数
|
||||
MenuParam* param = menu_param_find(param_id);
|
||||
|
||||
MenuParam* param = menu_param_find(global_ctx, param_id);
|
||||
if (param == NULL)
|
||||
{
|
||||
return MENU_ERR_NODE_NOT_FOUND;
|
||||
@ -324,4 +365,35 @@ MenuErrCode menu_param_get_type(uint16_t param_id, MenuParamType* type)
|
||||
return MENU_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 批量注册参数到菜单节点
|
||||
* @param global_ctx 全局上下文指针
|
||||
* @param configs 参数配置数组
|
||||
* @param config_count 配置数量
|
||||
* @return 错误码
|
||||
*/
|
||||
MenuErrCode menu_param_register_batch(MenuGlobalCtx* global_ctx, const MenuParamConfig* configs, uint16_t config_count)
|
||||
{
|
||||
if (global_ctx == NULL || !global_ctx->is_initialized || configs == NULL || config_count == 0)
|
||||
{
|
||||
return MENU_ERR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
MenuErrCode err = MENU_OK;
|
||||
for (uint16_t i = 0; i < config_count; i++)
|
||||
{
|
||||
err = menu_param_register(global_ctx, configs[i].node_id, configs[i].param_id,
|
||||
configs[i].type, configs[i].min_val, configs[i].max_val,
|
||||
configs[i].default_val, configs[i].scale);
|
||||
if (err != MENU_OK)
|
||||
{
|
||||
MENU_ERROR("Failed to register param %d at index %d: %s",
|
||||
configs[i].param_id, i, menu_err_code_to_str(err));
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
return MENU_OK;
|
||||
}
|
||||
|
||||
#endif // MENU_CONFIG_ENABLE_PARAM
|
||||
39
src/param/menu_param.h
Normal file
39
src/param/menu_param.h
Normal file
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* @file menu_param.h
|
||||
* @brief 菜单参数管理内部类型定义(用户无需关心)
|
||||
*/
|
||||
#ifndef MENU_PARAM_H
|
||||
#define MENU_PARAM_H
|
||||
|
||||
#include "menu_core.h"
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/**
|
||||
* @brief 参数值共用体(支持多种数据类型)
|
||||
*/
|
||||
typedef union {
|
||||
int8_t i8; ///< 8位有符号整数
|
||||
uint8_t u8; ///< 8位无符号整数
|
||||
int16_t i16; ///< 16位有符号整数
|
||||
uint16_t u16; ///< 16位无符号整数
|
||||
int32_t i32; ///< 32位有符号整数
|
||||
uint32_t u32; ///< 32位无符号整数
|
||||
float f; ///< 浮点数
|
||||
} MenuParamValue;
|
||||
|
||||
/**
|
||||
* @brief 参数结构体(参数管理的核心单元)
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t id; ///< 参数ID(唯一)
|
||||
MenuParamType type; ///< 参数类型
|
||||
MenuParamValue value; ///< 当前值
|
||||
MenuParamValue default_val;///< 默认值
|
||||
float min_val; ///< 最小值(浮点表示)
|
||||
float max_val; ///< 最大值(浮点表示)
|
||||
float scale; ///< 缩放因子
|
||||
bool is_registered : 1; ///< 是否已注册
|
||||
} MenuParam;
|
||||
|
||||
#endif // MENU_PARAM_H
|
||||
Reference in New Issue
Block a user