Follow+Star Public Account Number, don’t miss out on exciting content
Source | Big Orange Crazy Embedded
In embedded development, there are often many parameters that need to be managed (saved, transmitted, etc.).
Today, I will share a lightweight embedded code parameter management framework written in C language.
Introduction
-
Parameters are uniformly managed by adding defined variables (global variables) to the parameter table. Each parameter includes information such as current value, default value, minimum value, maximum value, parameter name, and attributes.
-
Different parameters can be added based on different scenarios.
No default value, minimum, or maximum limit, suitable for record-type parameters, such as status data or historical data.
Has a default value, but no minimum or maximum limit, suitable for configuration-type parameters.
Has a default value, minimum, and maximum limit, suitable for critical-type parameters, such as user parameters or critical status data.
-
If a single parameter table cannot meet the number of parameters or the management of parameter categories, multiple parameter tables can be defined.
The parameter ID in each parameter table is unique and cannot be duplicated; different parameter table IDs can be defined repeatedly.
-
Supports two types of parameters.
Numeric Type:
<span>int</span>,<span>float</span>,<span>double</span>, etc. basic type parameters.String Type:
<span>char</span>defines an array used to store strings.
-
Supports parameter validation.
Range Validation: Judged based on the maximum and minimum values of the parameter. For numeric type parameters, it is judged based on whether the value exceeds the range. For strings, it is judged based on whether the string length exceeds the range.
Custom Validation: Provides callback functions, allowing each parameter to set custom validation methods, such as requiring a parameter to be a multiple of a certain value or determining the current parameter’s value range based on other parameters.
-
Backward compatibility (key-value pairs).
Each parameter must specify a unique ID, ensuring backward compatibility when deleting, inserting, or adding parameters in later version iterations, without affecting other parameters.
Enabling key-value pairs will increase the serialized data length since each parameter serialization includes ID and length information.
-
Trimmable.
Depending on the platform, some functions can be trimmed or configurations modified to suit the development of chips with different capacities.
Usage Instructions
Demonstrated with code on how to quickly get started.
typedef struct
{
uint16_t usValue;
uint8_t ucValue;
uint32_t uiValue;
float fValue;
char szString_1[12];
double dValue;
int16_t sValue;
int8_t cValue;
int32_t iValue;
char szString_2[10];
}ParamDemo_t;
ParamDemo_t g_tTestVal = {
.usValue = 20,
.ucValue = 10,
.uiValue = 1000,
.fValue = 3.14,
.szString_1 = "abcd",
.dValue = 5.12,
.sValue = -100,
.cValue = -2,
.iValue = 300,
.szString_2 = "12234",
};
int8_t g_cTest = 50;
char g_szString[10] = "qwer";
static int CheckSValue(const void *pCurParam);
cotParamInfo_t sg_ParamTable[] = {
COT_PARAM_ITEM_BIND(1, g_tTestVal.usValue, COT_PARAM_UINT16, COT_PARAM_ATTR_WR),
COT_PARAM_ITEM_BIND(2, g_tTestVal.ucValue, COT_PARAM_UINT8, COT_PARAM_ATTR_WR, 20),
COT_PARAM_ITEM_BIND(3, g_tTestVal.uiValue, COT_PARAM_UINT32, COT_PARAM_ATTR_WR, 1000, 1000, 10000),
COT_PARAM_ITEM_BIND(4, g_tTestVal.fValue, COT_PARAM_FLOAT, COT_PARAM_ATTR_WR, 10, -10.5, 10.5),
COT_PARAM_ITEM_BIND(5, g_tTestVal.szString_1, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "abcd", 3, sizeof(g_tTestVal.szString_1)),
COT_PARAM_ITEM_BIND(6, g_tTestVal.dValue, COT_PARAM_DOUBLE, COT_PARAM_ATTR_WR, 0, -90.10, 100.10),
COT_PARAM_ITEM_BIND(7, g_tTestVal.sValue, COT_PARAM_INT16, COT_PARAM_ATTR_WR, 100, -200, 200, CheckSValue), // Add custom validation
COT_PARAM_ITEM_BIND_WITH_NAME(8, "g_cTest", g_cTest, COT_PARAM_INT8, COT_PARAM_ATTR_WR, 50, -100, 100), // Another parameter name
COT_PARAM_ITEM_BIND(9, g_szString, COT_PARAM_STRING, COT_PARAM_ATTR_WR, "XXX", 3, 6),
};
static int CheckSValue(const void *pCurParam)
{
const int16_t *p_sValue = (const int16_t *)pCurParam;
if ((*p_sValue) % 2 != 0)
{
return -1;
}
return 0;
}
int main()
{
cotParam_Init(&sg_tParamManager, sg_ParamTable, COT_PARAM_TABLE_SIZE(sg_ParamTable));
// Perform range validation on a variable's current parameter and handle the result
cotParam_SingleParamSelfCheck(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_3), &eCheckResult);
if (eCheckResult != COT_PARAM_CHECK_OK) // Check after modification
{
cotParam_SingleParamResetDefValue(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_3)); // If validation fails, restore to default value
}
// After a variable's parameter changes (current value has changed), perform validation, if out of range, restore default
g_test_3 = 1000;
cotParam_SingleParamCheckProcess(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_3), COT_PARAM_RESET_DEF);
// Before changing a variable parameter (current value has not changed), perform validation, handle the result
double tmp = 1000;
cotParam_SingleParamCheckInput(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_3), &tmp, &eCheckResult);
if (eCheckResult == COT_PARAM_CHECK_OK)
{
g_test_3 = tmp; // If validation succeeds, modify
}
// Before changing a variable parameter (current value has not changed), perform validation, if new value exceeds range, do not update the current value
double tmp = 1000;
cotParam_SingleParamUpdate(cotParam_FindParamByParamPtr(&sg_tParamManager, &g_test_3), &tmp, COT_PARAM_RESET_NONE);
}
Download Link
Download link (click to read the original text), or for updated content:
https://gitee.com/cot_package/cot_param
Disclaimer:This article’s material comes from the internet, and the copyright belongs to the original author. If there are copyright issues, please contact me for deletion.
———— END ————

● Column “Embedded Tools”
● Column “Embedded Development”
● Column “Keil Tutorial”
● Selected Tutorials in Embedded Column
Follow the public accountReply “Add Group” to join the technical exchange group according to the rules, reply “1024” to see more content.
Click “Read the Original Text” to see more shares.