Files
sil/SIL/otherFiles/param_mgr.c
T
2020-10-22 10:50:04 +08:00

168 lines
3.1 KiB
C

#include "param_mgr.h"
#include <string.h>
#define PARAM_PROPS_MAXLEN (350)
param_prop props[PARAM_PROPS_MAXLEN];
uint16_t param_count = 0u;
param_prop *param_get_by_idx(uint16_t idx)
{
if (idx < param_count)
{
return props + idx;
}
return NULL;
}
uint16_t param_get_count(void)
{
return param_count;
}
static int16_t find_idx_by_name2(uint16_t b, uint16_t e, const char name[16])
{
if (b + 1u == e)
{
return e;
}
uint16_t m;
m = (b + e) >> 1;
int cmp = strncmp(name, props[m].name, 16);
if (cmp < 0)
{
return find_idx_by_name2(b, m, name);
}
else
{
return find_idx_by_name2(m, e, name);
}
}
uint16_t find_idx_by_name(const char name[16])
{
int cmp;
if (param_count == 0u)
{
return 0u;
}
else if (param_count == 1u)
{
cmp = strncmp(name, props[0].name, 16);
if (cmp < 0)
{
return 0u;
}
else
{
return 1u;
}
}
else
{
cmp = strncmp(name, props[0].name, 16);
if (cmp < 0)
{
return 0;
}
else
{
cmp = strncmp(name, props[param_count - 1].name, 16);
if (cmp < 0)
{
return find_idx_by_name2(0, param_count - 1, name);
}
else
{
return param_count;
}
}
}
}
param_prop *param_get_by_name(const char name[16])
{
uint16_t idx = find_idx_by_name(name);
if (idx > 0u && idx <= param_count && strncmp(name, props[idx - 1].name, 16) == 0)
{
return props + (idx - 1);
}
return NULL;
}
int16_t param_get_idx_by_name(const char name[16])
{
uint16_t idx = find_idx_by_name(name);
if (idx > 0u && idx <= param_count && strncmp(name, props[idx - 1].name, 16) == 0)
{
return idx - 1;
}
return -1;
}
int param_mgr_regist(param_prop *prop)
{
if (param_count < PARAM_PROPS_MAXLEN)
{
uint16_t idx = find_idx_by_name(prop->name);
for (uint16_t i = param_count; i > idx; --i)
{
memcpy(&props[i], &props[i - 1u], sizeof(param_prop));
}
memcpy(&props[idx], prop, sizeof(param_prop));
++param_count;
return 0;
}
return -1;
}
int param_get_size(param_prop *prop)
{
switch (prop->typ)
{
case PARAM_TYPE_UINT8:
case PARAM_TYPE_INT8:
return 1;
case PARAM_TYPE_UINT16:
case PARAM_TYPE_INT16:
return 2;
default:
return 4;
}
}
void param_set_value(param_prop *prop, const uint8_t value[4])
{
param_value_ptr_t val_ptr;
val_ptr.B = (uint8_t *)value;
if (prop->setter_ptr)
{
(*props->setter_ptr)(val_ptr);
}
else
{
memcpy(prop->val_ptr.B, value, param_get_size(prop));
}
}
void param_get_value(param_prop *prop, uint8_t value[4])
{
float val;
param_value_ptr_t val_ptr;
if (prop->getter_ptr)
{
val_ptr.B = value;
(*props->getter_ptr)(val_ptr);
}
else
{
val_ptr = prop->val_ptr;
memcpy(value, val_ptr.B, param_get_size(prop));
}
}