mirror of
https://gitee.com/const-zpc/param.git
synced 2025-02-06 04:38:21 +08:00
✨ feat: 首次提交参数管理
This commit is contained in:
parent
0c9317783c
commit
9b14c89157
43
Demo/cmd_shell/demo.c
Normal file
43
Demo/cmd_shell/demo.c
Normal file
@ -0,0 +1,43 @@
|
||||
|
||||
#include "param.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
PARAM_DEFINE (test, PARAM_INT16, 10, -100, 100);
|
||||
PARAM_DEFINE (test_2, PARAM_UINT16, 20, 0, 100);
|
||||
PARAM_DEFINE (test_3, PARAM_DOUBLE, 3.15, -10, 10);
|
||||
PARAM_DEFINE_STR (test_str, 10, "abcdef");
|
||||
|
||||
extern void ShowAllParam(void);
|
||||
extern void SaveAllParam(void);
|
||||
extern int InitAllParam(void);
|
||||
extern int ResetParam(void);
|
||||
|
||||
extern int ModifyByName(const char *pszName, const void *pNewData);
|
||||
|
||||
int main ()
|
||||
{
|
||||
uint32_t data = 16;
|
||||
double fdata = 7.25;
|
||||
|
||||
uint8_t databuf[8];
|
||||
|
||||
InitAllParam();
|
||||
|
||||
ShowAllParam();
|
||||
|
||||
printf("\nModify: \n");
|
||||
memcpy(databuf, &data, sizeof(data));
|
||||
ModifyByName("test_2", databuf);
|
||||
|
||||
memcpy(databuf, &fdata, sizeof(fdata));
|
||||
ModifyByName("test_3", databuf);
|
||||
|
||||
ShowAllParam();
|
||||
|
||||
printf("\nReset: \n");
|
||||
ResetParam();
|
||||
ShowAllParam();
|
||||
|
||||
return 0;
|
||||
}
|
22
Demo/cmd_shell/makefile
Normal file
22
Demo/cmd_shell/makefile
Normal file
@ -0,0 +1,22 @@
|
||||
CURR_DIR_PATH=$(shell pwd)
|
||||
|
||||
CURR_DIR_PATH="H:\Users\const\Desktop\param\param\Demo\cmd_shell"
|
||||
|
||||
GCC:=gcc
|
||||
INC+=-I${CURR_DIR_PATH}/../../Param/inc
|
||||
INC+=-I${CURR_DIR_PATH}/
|
||||
|
||||
SRC+=${CURR_DIR_PATH}/../../Param/src/param.c
|
||||
SRC+=${CURR_DIR_PATH}/demo.c
|
||||
SRC+=${CURR_DIR_PATH}/param_table.c
|
||||
|
||||
|
||||
AIM_NAME:=demo
|
||||
|
||||
.PHONY:all
|
||||
all:
|
||||
${GCC} ${INC} ${SRC} -o ${CURR_DIR_PATH}/${AIM_NAME}
|
||||
|
||||
.PHONY:clean
|
||||
clean:
|
||||
rm -rf ${CURR_DIR_PATH}/${AIM_NAME}
|
109
Demo/cmd_shell/param_table.c
Normal file
109
Demo/cmd_shell/param_table.c
Normal file
@ -0,0 +1,109 @@
|
||||
#include "param.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
PARAM_EXTERN(test, PARAM_INT16);
|
||||
PARAM_EXTERN(test_2, PARAM_UINT16);
|
||||
PARAM_EXTERN(test_3, PARAM_DOUBLE);
|
||||
PARAM_STR_EXTERN(test_str, 10);
|
||||
|
||||
const ParamInfo_t sg_tParamList[]=
|
||||
{
|
||||
PARAM_REG(1, test, PARAM_INT16),
|
||||
PARAM_REG(2, test_2, PARAM_UINT16),
|
||||
PARAM_REG(3, test_3, PARAM_DOUBLE),
|
||||
PARAM_STR_REG(4, test_str),
|
||||
};
|
||||
|
||||
static ParamTable_t sg_tParamTable = {sg_tParamList, sizeof(sg_tParamList) / sizeof(sg_tParamList[0])};
|
||||
|
||||
|
||||
uint8_t sg_saveBuf[200];
|
||||
|
||||
void ShowAllParam(void)
|
||||
{
|
||||
for (int i = 0; i < sg_tParamTable.num; i++)
|
||||
{
|
||||
switch (sg_tParamTable.pTab[i].type)
|
||||
{
|
||||
case PARAM_INT8:
|
||||
printf ("%-10s : %d\n", sg_tParamTable.pTab[i].pszName, (PARAM_INT8_T)(*(PARAM_INT8_T *)sg_tParamTable.pTab[i].pCurValue));
|
||||
break;
|
||||
case PARAM_INT16:
|
||||
printf ("%-10s : %d\n", sg_tParamTable.pTab[i].pszName, (PARAM_INT16_T)(*(PARAM_INT16_T *)sg_tParamTable.pTab[i].pCurValue));
|
||||
break;
|
||||
case PARAM_INT32:
|
||||
printf ("%-10s : %d\n", sg_tParamTable.pTab[i].pszName, (PARAM_INT32_T)(*(PARAM_INT32_T *)sg_tParamTable.pTab[i].pCurValue));
|
||||
break;
|
||||
case PARAM_INT64:
|
||||
printf ("%-10s : %d\n", sg_tParamTable.pTab[i].pszName, (PARAM_INT64_T)(*(PARAM_INT64_T *)sg_tParamTable.pTab[i].pCurValue));
|
||||
break;
|
||||
case PARAM_UINT8:
|
||||
printf ("%-10s : %u\n", sg_tParamTable.pTab[i].pszName, (PARAM_UINT8_T)(*(PARAM_UINT8_T *)sg_tParamTable.pTab[i].pCurValue));
|
||||
break;
|
||||
case PARAM_UINT16:
|
||||
printf ("%-10s : %u\n", sg_tParamTable.pTab[i].pszName, (PARAM_UINT16_T)(*(PARAM_UINT16_T *)sg_tParamTable.pTab[i].pCurValue));
|
||||
break;
|
||||
case PARAM_UINT32:
|
||||
printf ("%-10s : %u\n", sg_tParamTable.pTab[i].pszName, (PARAM_UINT32_T)(*(PARAM_UINT32_T *)sg_tParamTable.pTab[i].pCurValue));
|
||||
break;
|
||||
case PARAM_UINT64:
|
||||
printf ("%-10s : %u\n", sg_tParamTable.pTab[i].pszName, (PARAM_UINT64_T)(*(PARAM_UINT64_T *)sg_tParamTable.pTab[i].pCurValue));
|
||||
break;
|
||||
case PARAM_FLOAT:
|
||||
printf ("%-10s : %f\n", sg_tParamTable.pTab[i].pszName, (PARAM_FLOAT_T)(*(PARAM_FLOAT_T *)sg_tParamTable.pTab[i].pCurValue));
|
||||
break;
|
||||
case PARAM_DOUBLE:
|
||||
printf ("%-10s : %f\n", sg_tParamTable.pTab[i].pszName, (PARAM_DOUBLE_T)(*(PARAM_DOUBLE_T *)sg_tParamTable.pTab[i].pCurValue));
|
||||
break;
|
||||
case PARAM_STARING:
|
||||
printf ("%-10s : %s\n", sg_tParamTable.pTab[i].pszName, sg_tParamTable.pTab[i].pCurValue);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SaveAllParam(void)
|
||||
{
|
||||
size_t bufzise;
|
||||
uint8_t buf[200];
|
||||
|
||||
bufzise = Param_Serialize(&sg_tParamTable, buf);
|
||||
|
||||
printf("Save: ");
|
||||
|
||||
for (int i = 0; i < bufzise; i++)
|
||||
{
|
||||
printf("%02x ", buf[i]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
sg_saveBuf[0] = 0x55;
|
||||
|
||||
memcpy(&sg_saveBuf[1], buf, bufzise);
|
||||
}
|
||||
|
||||
void InitAllParam(void)
|
||||
{
|
||||
if (sg_saveBuf[0] != 0x55)
|
||||
{
|
||||
printf("need frist save\n");
|
||||
SaveAllParam();
|
||||
}
|
||||
|
||||
Param_Parse(&sg_tParamTable, &sg_saveBuf[1]);
|
||||
}
|
||||
|
||||
int ModifyByName(const char *pszName, const void *pNewData)
|
||||
{
|
||||
return Param_ModifyByName(&sg_tParamTable, pszName, pNewData);
|
||||
}
|
||||
|
||||
int ResetParam(void)
|
||||
{
|
||||
return Param_Reset(&sg_tParamTable);
|
||||
}
|
140
Param/inc/param.h
Normal file
140
Param/inc/param.h
Normal file
@ -0,0 +1,140 @@
|
||||
/**
|
||||
* @file param.h
|
||||
* @author pczhou (pczhou@streamax.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-02-07
|
||||
*
|
||||
* @copyright Copyright (c) 2023 锐明技术股份有限公司
|
||||
*
|
||||
* @par 修改日志:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2023-02-07 <td>1.0 <td>pczhou <td>内容
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#ifndef _PARAM_H_
|
||||
#define _PARAM_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef int8_t PARAM_INT8_T;
|
||||
typedef int16_t PARAM_INT16_T;
|
||||
typedef int32_t PARAM_INT32_T;
|
||||
typedef int64_t PARAM_INT64_T;
|
||||
typedef uint8_t PARAM_UINT8_T;
|
||||
typedef uint16_t PARAM_UINT16_T;
|
||||
typedef uint32_t PARAM_UINT32_T;
|
||||
typedef uint64_t PARAM_UINT64_T;
|
||||
typedef float PARAM_FLOAT_T;
|
||||
typedef double PARAM_DOUBLE_T;
|
||||
typedef char PARAM_STARING_T;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
PARAM_INT8,
|
||||
PARAM_INT16,
|
||||
PARAM_INT32,
|
||||
PARAM_INT64,
|
||||
PARAM_UINT8,
|
||||
PARAM_UINT16,
|
||||
PARAM_UINT32,
|
||||
PARAM_UINT64,
|
||||
PARAM_FLOAT,
|
||||
PARAM_DOUBLE,
|
||||
PARAM_STARING,
|
||||
}ParamType_e;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const char *pszName; /*!< 名称 */
|
||||
const uint32_t id; /*!< 唯一ID */
|
||||
const ParamType_e type; /*!< 类型 */
|
||||
const uint16_t length; /*!< 长度 */
|
||||
void *pCurValue; /*!< 当前值指针 */
|
||||
const void *pDefValue; /*!< 默认值指针 */
|
||||
const void *pMinValue; /*!< 最小值指针 */
|
||||
const void *pMaxValue; /*!< 最大值指针 */
|
||||
} ParamInfo_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const ParamInfo_t *pTab;
|
||||
uint32_t num;
|
||||
} ParamTable_t;
|
||||
|
||||
/**
|
||||
* @brief 定义参数
|
||||
*
|
||||
* @param name 参数名(变量名)
|
||||
* @param type 参数类型 @enum ParamType_e 取值
|
||||
* @param defVal 默认值
|
||||
* @param minVal 最小值
|
||||
* @param maxVal 最大值
|
||||
*/
|
||||
#define PARAM_DEFINE(name, type, defVal, minVal, maxVal) \
|
||||
const ParamType_e type_##name = type;\
|
||||
type##_T name = defVal;\
|
||||
const type##_T def_##name = defVal;\
|
||||
const type##_T min_##name = minVal;\
|
||||
const type##_T max_##name = maxVal;
|
||||
|
||||
/**
|
||||
* @brief 定义字符串参数
|
||||
*
|
||||
* @param name 参数名(变量名)
|
||||
* @param length 字符串预留最大长度
|
||||
* @param defVal 默认值
|
||||
*/
|
||||
#define PARAM_DEFINE_STR(name, length, defVal) \
|
||||
char name[length] = {defVal};\
|
||||
const char def_##name[] = {defVal};
|
||||
|
||||
/**
|
||||
* @brief 参数注册
|
||||
*
|
||||
* @param id 参数唯一 ID
|
||||
* @param name 参数名(变量名)
|
||||
* @param type 参数类型 @enum ParamType_e 取值
|
||||
*/
|
||||
#define PARAM_REG(id, name, type) { #name, id, type, sizeof(name), &name, &def_##name, &min_##name, &max_##name }
|
||||
|
||||
/**
|
||||
* @brief 字符串参数注册
|
||||
*
|
||||
* @param id 参数唯一 ID
|
||||
* @param name 参数名(变量名)
|
||||
*/
|
||||
#define PARAM_STR_REG(id, name) { #name, id, PARAM_STARING, sizeof(name), name, def_##name, NULL, NULL }
|
||||
|
||||
/**
|
||||
* @brief 参数声明
|
||||
*
|
||||
* @param name 参数名(变量名)
|
||||
* @param type 参数类型 @enum ParamType_e 取值
|
||||
*/
|
||||
#define PARAM_EXTERN(name, type) \
|
||||
extern type##_T name;\
|
||||
extern type##_T def_##name;\
|
||||
extern type##_T min_##name;\
|
||||
extern type##_T max_##name;
|
||||
|
||||
/**
|
||||
* @brief 字符串参数声明
|
||||
*
|
||||
* @param name 参数名(变量名)
|
||||
* @param length 字符串预留最大长度
|
||||
*/
|
||||
#define PARAM_STR_EXTERN(name, length) \
|
||||
extern char name[length];\
|
||||
extern char def_##name[];
|
||||
|
||||
extern int Param_Reset(ParamTable_t *pParamTable);
|
||||
extern int Param_ModifyById(ParamTable_t *pParamTable, uint32_t id, const void *pNewData);
|
||||
extern int Param_ModifyByName(ParamTable_t *pParamTable, const char *pszName, const void *pNewData);
|
||||
extern size_t Param_Serialize(ParamTable_t *pParamTable, uint8_t *pBuf);
|
||||
extern void Param_Parse(ParamTable_t *pParamTable, const uint8_t *pBuf);
|
||||
|
||||
#endif // !_PARAM_H_
|
211
Param/src/param.c
Normal file
211
Param/src/param.c
Normal file
@ -0,0 +1,211 @@
|
||||
/**
|
||||
* @file param.c
|
||||
* @author pczhou (pczhou@streamax.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-02-07
|
||||
*
|
||||
* @copyright Copyright (c) 2023 锐明技术股份有限公司
|
||||
*
|
||||
* @par 修改日志:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2023-02-07 <td>1.0 <td>pczhou <td>内容
|
||||
* </table>
|
||||
*/
|
||||
|
||||
|
||||
#include "param.h"
|
||||
#include <string.h>
|
||||
|
||||
|
||||
|
||||
// const uint32_t sg_tParamTableNum = sizeof(sg_tParamTable) / sizeof(sg_tParamTable[0]);
|
||||
|
||||
#define CASE_CHANGE(type) \
|
||||
case type:\
|
||||
{\
|
||||
if ((*(type##_T *)pNewData) <= (*(type##_T *)pParamTable->pTab[i].pMaxValue) &&\
|
||||
(*(type##_T *)pNewData) >= (*(type##_T *)pParamTable->pTab[i].pMinValue))\
|
||||
{\
|
||||
*(type##_T *)pParamTable->pTab[i].pCurValue = *(type##_T *)pNewData;\
|
||||
}\
|
||||
}\
|
||||
break;
|
||||
|
||||
#define CASE_CHECK(type) \
|
||||
case type:\
|
||||
{\
|
||||
if ((*(type##_T *)pParamTable->pTab[i].pCurValue) > (*(type##_T *)pParamTable->pTab[i].pMaxValue) ||\
|
||||
(*(type##_T *)pParamTable->pTab[i].pCurValue) < (*(type##_T *)pParamTable->pTab[i].pMinValue))\
|
||||
{\
|
||||
*(type##_T *)pParamTable->pTab[i].pCurValue = *(type##_T *)pParamTable->pTab[i].pDefValue;\
|
||||
}\
|
||||
}\
|
||||
break;
|
||||
|
||||
int Param_Reset(ParamTable_t *pParamTable)
|
||||
{
|
||||
if (pParamTable == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < pParamTable->num; i++)
|
||||
{
|
||||
memcpy(pParamTable->pTab[i].pCurValue, pParamTable->pTab[i].pDefValue, pParamTable->pTab[i].length);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 根据参数ID进行修改参数
|
||||
*
|
||||
* @param[in] pParamTable 参数表指针
|
||||
* @param[in] id 参数ID
|
||||
* @param[in] pNewData 新数据指针
|
||||
* @return -1,失败 0,成功
|
||||
*/
|
||||
int Param_ModifyById(ParamTable_t *pParamTable, uint32_t id, const void *pNewData)
|
||||
{
|
||||
if (pParamTable == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < pParamTable->num; i++)
|
||||
{
|
||||
if (pParamTable->pTab[i].id == id)
|
||||
{
|
||||
switch (pParamTable->pTab[i].type)
|
||||
{
|
||||
CASE_CHANGE(PARAM_INT8);
|
||||
CASE_CHANGE(PARAM_INT16);
|
||||
CASE_CHANGE(PARAM_INT32);
|
||||
CASE_CHANGE(PARAM_INT64);
|
||||
CASE_CHANGE(PARAM_UINT8);
|
||||
CASE_CHANGE(PARAM_UINT16);
|
||||
CASE_CHANGE(PARAM_UINT32);
|
||||
CASE_CHANGE(PARAM_UINT64);
|
||||
CASE_CHANGE(PARAM_FLOAT);
|
||||
CASE_CHANGE(PARAM_DOUBLE);
|
||||
case PARAM_STARING:
|
||||
if (strlen((char *)pNewData) < pParamTable->pTab[i].length)
|
||||
{
|
||||
strcpy((char *)pParamTable->pTab[i].pCurValue, (char *)pNewData);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 根据参数名进行修改参数
|
||||
*
|
||||
* @param[in] pParamTable 参数表指针
|
||||
* @param[in] pszName 参数名
|
||||
* @param[in] pNewData 新数据指针
|
||||
* @return -1,失败 0,成功
|
||||
*/
|
||||
int Param_ModifyByName(ParamTable_t *pParamTable, const char *pszName, const void *pNewData)
|
||||
{
|
||||
|
||||
for (int i = 0; i < pParamTable->num; i++)
|
||||
{
|
||||
if (strcmp(pParamTable->pTab[i].pszName, pszName) == 0)
|
||||
{
|
||||
switch (pParamTable->pTab[i].type)
|
||||
{
|
||||
CASE_CHANGE(PARAM_INT8);
|
||||
CASE_CHANGE(PARAM_INT16);
|
||||
CASE_CHANGE(PARAM_INT32);
|
||||
CASE_CHANGE(PARAM_INT64);
|
||||
CASE_CHANGE(PARAM_UINT8);
|
||||
CASE_CHANGE(PARAM_UINT16);
|
||||
CASE_CHANGE(PARAM_UINT32);
|
||||
CASE_CHANGE(PARAM_UINT64);
|
||||
CASE_CHANGE(PARAM_FLOAT);
|
||||
CASE_CHANGE(PARAM_DOUBLE);
|
||||
case PARAM_STARING:
|
||||
if (strlen((char *)pNewData) < pParamTable->pTab[i].length)
|
||||
{
|
||||
strcpy((char *)pParamTable->pTab[i].pCurValue, (char *)pNewData);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 将参数表序列化
|
||||
*
|
||||
* @param[in] pParamTable 参数表指针
|
||||
* @param[out] pBuf 序列化后的数据buf
|
||||
* @return 序列化后的数据buf长度
|
||||
*/
|
||||
size_t Param_Serialize(ParamTable_t *pParamTable, uint8_t *pBuf)
|
||||
{
|
||||
size_t idx = 0;
|
||||
|
||||
for (int i = 0; i < pParamTable->num; i++)
|
||||
{
|
||||
memcpy(&pBuf[idx], pParamTable->pTab[i].pCurValue, pParamTable->pTab[i].length);
|
||||
idx += pParamTable->pTab[i].length;
|
||||
}
|
||||
|
||||
return idx;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 将序列化的数据转为参数表信息
|
||||
*
|
||||
* @param[in] pParamTable 参数表指针
|
||||
* @param[in] pBuf 序列化前的数据buf
|
||||
*/
|
||||
void Param_Parse(ParamTable_t *pParamTable, const uint8_t *pBuf)
|
||||
{
|
||||
size_t idx = 0;
|
||||
|
||||
for (int i = 0; i < pParamTable->num; i++)
|
||||
{
|
||||
memcpy(pParamTable->pTab[i].pCurValue, &pBuf[idx], pParamTable->pTab[i].length);
|
||||
idx += pParamTable->pTab[i].length;
|
||||
}
|
||||
|
||||
for (int i = 0; i < pParamTable->num; i++)
|
||||
{
|
||||
switch (pParamTable->pTab[i].type)
|
||||
{
|
||||
CASE_CHECK(PARAM_INT8);
|
||||
CASE_CHECK(PARAM_INT16);
|
||||
CASE_CHECK(PARAM_INT32);
|
||||
CASE_CHECK(PARAM_INT64);
|
||||
CASE_CHECK(PARAM_UINT8);
|
||||
CASE_CHECK(PARAM_UINT16);
|
||||
CASE_CHECK(PARAM_UINT32);
|
||||
CASE_CHECK(PARAM_UINT64);
|
||||
CASE_CHECK(PARAM_FLOAT);
|
||||
CASE_CHECK(PARAM_DOUBLE);
|
||||
case PARAM_STARING:
|
||||
if (strlen((char *)pParamTable->pTab[i].pCurValue) >= pParamTable->pTab[i].length)
|
||||
{
|
||||
strcpy((char *)pParamTable->pTab[i].pCurValue, (char *)pParamTable->pTab[i].pDefValue);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user