From 981a4dafac2d2e7ed237cb31af68c7bf2cee8665 Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Tue, 13 Oct 2015 00:40:04 +0200 Subject: Support transform views --- pcilib/pcilib.h | 5 ++++ pcilib/py.c | 2 +- pcilib/value.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ pcilib/view.c | 37 ++++++++++++++++++------- views/transform.c | 74 ++++++++++++++++---------------------------------- 5 files changed, 138 insertions(+), 61 deletions(-) diff --git a/pcilib/pcilib.h b/pcilib/pcilib.h index 6acceb6..5c1ca70 100644 --- a/pcilib/pcilib.h +++ b/pcilib/pcilib.h @@ -223,7 +223,12 @@ void pcilib_clean_value(pcilib_t *ctx, pcilib_value_t *val); int pcilib_copy_value(pcilib_t *ctx, pcilib_value_t *dst, const pcilib_value_t *src); int pcilib_set_value_from_float(pcilib_t *ctx, pcilib_value_t *val, double fval); int pcilib_set_value_from_int(pcilib_t *ctx, pcilib_value_t *val, long ival); +int pcilib_set_value_from_register_value(pcilib_t *ctx, pcilib_value_t *value, pcilib_register_value_t regval); int pcilib_set_value_from_static_string(pcilib_t *ctx, pcilib_value_t *value, const char *str); +double pcilib_get_value_as_float(pcilib_t *ctx, const pcilib_value_t *val, int *err); +long pcilib_get_value_as_int(pcilib_t *ctx, const pcilib_value_t *val, int *err); +pcilib_register_value_t pcilib_get_value_as_register_value(pcilib_t *ctx, const pcilib_value_t *val, int *err); + int pcilib_convert_value_unit(pcilib_t *ctx, pcilib_value_t *val, const char *unit_name); int pcilib_convert_value_type(pcilib_t *ctx, pcilib_value_t *val, pcilib_value_type_t type); diff --git a/pcilib/py.c b/pcilib/py.c index 2248892..6aaa22f 100644 --- a/pcilib/py.c +++ b/pcilib/py.c @@ -63,7 +63,7 @@ static int pcilib_py_realloc_string(pcilib_t *ctx, size_t required, size_t *size static char *pcilib_py_parse_string(pcilib_t *ctx, const char *codestr, pcilib_value_t *value) { int i; int err = 0; - pcilib_value_t val; + pcilib_value_t val = {0}; pcilib_register_value_t regval; char save; diff --git a/pcilib/value.c b/pcilib/value.c index b039e5f..1a8cc45 100644 --- a/pcilib/value.c +++ b/pcilib/value.c @@ -57,6 +57,10 @@ int pcilib_set_value_from_int(pcilib_t *ctx, pcilib_value_t *value, long ival) { return 0; } +int pcilib_set_value_from_register_value(pcilib_t *ctx, pcilib_value_t *value, pcilib_register_value_t regval) { + return pcilib_set_value_from_int(ctx, value, regval); +} + int pcilib_set_value_from_static_string(pcilib_t *ctx, pcilib_value_t *value, const char *str) { pcilib_clean_value(ctx, value); @@ -66,6 +70,83 @@ int pcilib_set_value_from_static_string(pcilib_t *ctx, pcilib_value_t *value, co return 0; } +double pcilib_get_value_as_float(pcilib_t *ctx, const pcilib_value_t *val, int *ret) { + int err; + double res; + pcilib_value_t copy = {0}; + + err = pcilib_copy_value(ctx, ©, val); + if (err) { + if (ret) *ret = err; + return 0.; + } + + err = pcilib_convert_value_type(ctx, ©, PCILIB_TYPE_DOUBLE); + if (err) { + if (ret) *ret = err; + return 0.; + } + + if (ret) *ret = 0; + res = copy.fval; + + pcilib_clean_value(ctx, ©); + + return res; +} + +long pcilib_get_value_as_int(pcilib_t *ctx, const pcilib_value_t *val, int *ret) { + int err; + long res; + pcilib_value_t copy = {0}; + + err = pcilib_copy_value(ctx, ©, val); + if (err) { + if (ret) *ret = err; + return 0; + } + + err = pcilib_convert_value_type(ctx, ©, PCILIB_TYPE_LONG); + if (err) { + if (ret) *ret = err; + return 0; + } + + if (ret) *ret = 0; + res = copy.ival; + + pcilib_clean_value(ctx, ©); + + return res; +} + +pcilib_register_value_t pcilib_get_value_as_register_value(pcilib_t *ctx, const pcilib_value_t *val, int *ret) { + int err; + pcilib_register_value_t res; + pcilib_value_t copy = {0}; + + err = pcilib_copy_value(ctx, ©, val); + if (err) { + if (ret) *ret = err; + return 0.; + } + + err = pcilib_convert_value_type(ctx, ©, PCILIB_TYPE_LONG); + if (err) { + if (ret) *ret = err; + return 0.; + } + + if (ret) *ret = 0; + res = copy.ival; + + pcilib_clean_value(ctx, ©); + + return res; +} + + + /* double pcilib_value_get_float(pcilib_value_t *val) { diff --git a/pcilib/view.c b/pcilib/view.c index e9fdab5..c1ad44a 100644 --- a/pcilib/view.c +++ b/pcilib/view.c @@ -135,17 +135,21 @@ pcilib_view_context_t *pcilib_find_register_view_context_by_name(pcilib_t *ctx, } // We expect symmetric units. Therefore, we don't distringuish if we read or write -pcilib_view_context_t *pcilib_find_register_view_context(pcilib_t *ctx, pcilib_register_t reg, const char *name) { +static int pcilib_detect_register_view_and_unit(pcilib_t *ctx, pcilib_register_t reg, const char *name, pcilib_view_context_t **ret_view, pcilib_unit_transform_t **ret_trans) { pcilib_view_t i; pcilib_view_context_t *view_ctx; pcilib_view_description_t *view_desc; pcilib_register_context_t *regctx = &ctx->register_ctx[reg]; - if (!regctx->views) return NULL; + if (!regctx->views) return PCILIB_ERROR_NOTFOUND; // Check if view is just a name of listed view view_ctx = pcilib_find_register_view_context_by_name(ctx, reg, name); - if (view_ctx) return view_ctx; + if (view_ctx) { + if (ret_view) *ret_view = view_ctx; + if (ret_trans) *ret_trans = NULL; + return 0; + } // Check if view is a unit for (i = 0; regctx->views[i].name; i++) { @@ -157,13 +161,26 @@ pcilib_view_context_t *pcilib_find_register_view_context(pcilib_t *ctx, pcilib_r view_desc = ctx->views[view_ctx->view]; if (view_desc->unit) { trans = pcilib_find_transform_by_unit_names(ctx, view_desc->unit, name); - if (trans) return pcilib_find_view_context_by_name(ctx, view_desc->name); + if (trans) { + if (ret_trans) *ret_trans = trans; + if (ret_view) *ret_view = pcilib_find_view_context_by_name(ctx, view_desc->name); + return 0; + } } } - return NULL; + return PCILIB_ERROR_NOTFOUND; } +pcilib_view_context_t *pcilib_find_register_view_context(pcilib_t *ctx, pcilib_register_t reg, const char *name) { + int err; + pcilib_view_context_t *view; + + err = pcilib_detect_register_view_and_unit(ctx, reg, name, &view, NULL); + if (err) return NULL; + + return view; +} typedef struct { pcilib_register_t reg; @@ -172,6 +189,7 @@ typedef struct { } pcilib_view_configuration_t; static int pcilib_detect_view_configuration(pcilib_t *ctx, const char *bank, const char *regname, const char *view_cname, int write_direction, pcilib_view_configuration_t *cfg) { + int err = 0; pcilib_view_t view; pcilib_view_context_t *view_ctx; pcilib_unit_transform_t *trans = NULL; @@ -199,9 +217,9 @@ static int pcilib_detect_view_configuration(pcilib_t *ctx, const char *bank, con // get value if (unit_name) view_ctx = pcilib_find_register_view_context_by_name(ctx, reg, view_name); - else view_ctx = pcilib_find_register_view_context(ctx, reg, view_name); + else err = pcilib_detect_register_view_and_unit(ctx, reg, view_name, &view_ctx, &trans); - if (!view_ctx) { + if ((err)||(!view_ctx)) { pcilib_error("Can't find the specified view %s for register %s", view_name, regname); return PCILIB_ERROR_NOTFOUND; } @@ -222,10 +240,11 @@ static int pcilib_detect_view_configuration(pcilib_t *ctx, const char *bank, con pcilib_error("Don't know how to get the requested unit %s for view %s", unit_name, ctx->views[view]->name); return PCILIB_ERROR_NOTFOUND; } - // No transform is required - if (!trans->transform) trans = NULL; } + // No transform is required + if (!trans->transform) trans = NULL; + cfg->reg = reg; cfg->view = view_ctx; cfg->trans = trans; diff --git a/views/transform.c b/views/transform.c index 818c1d6..7bc5420 100644 --- a/views/transform.c +++ b/views/transform.c @@ -8,64 +8,36 @@ #include "version.h" #include "model.h" #include "transform.h" +#include "py.h" -static int pcilib_transform_view_read(pcilib_t *ctx, pcilib_view_context_t *view, pcilib_register_value_t regval, pcilib_value_t *val) { - pcilib_set_value_from_int(ctx, val, 0); - return 0; -/* int j=0; - pcilib_register_value_t value=0; - char* formula=NULL; +static int pcilib_transform_view_read(pcilib_t *ctx, pcilib_view_context_t *view_ctx, pcilib_register_value_t regval, pcilib_value_t *val) { + int err; - if(view2reg==0) { - if(!(strcasecmp(unit,((pcilib_view_t*)viewval)->base_unit.name))) { - formula=malloc(sizeof(char)*strlen(((pcilib_formula_t*)params)->read_formula)); - if(!(formula)) { - pcilib_error("can't allocate memory for the formula"); - return PCILIB_ERROR_MEMORY; - } - strncpy(formula,((pcilib_formula_t*)params)->read_formula,strlen(((pcilib_formula_t*)params)->read_formula)); - pcilib_view_apply_formula(ctx,formula,regval); - return 0; - } + const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx); + pcilib_transform_view_description_t *v = (pcilib_transform_view_description_t*)(model_info->views[view_ctx->view]); - for(j=0; ((pcilib_view_t*)viewval)->base_unit.transforms[j].name; j++) { - if(!(strcasecmp(((pcilib_view_t*)viewval)->base_unit.transforms[j].name,unit))) { - // when we have found the correct view of type formula, we apply the formula, that get the good value for return - formula=malloc(sizeof(char)*strlen(((pcilib_formula_t*)params)->read_formula)); - if(!(formula)) { - pcilib_error("can't allocate memory for the formula"); - return PCILIB_ERROR_MEMORY; - } - strncpy(formula,((pcilib_formula_t*)params)->read_formula,strlen(((pcilib_formula_t*)params)->read_formula)); - pcilib_view_apply_formula(ctx,formula, regval); - // pcilib_view_apply_unit(((pcilib_view_t*)viewval)->base_unit.transforms[j],unit,regval); - return 0; - } - }*/ + err = pcilib_set_value_from_register_value(ctx, val, regval); + if (err) return err; + + return pcilib_py_eval_string(ctx, v->read_from_reg, val); } -static int pcilib_transform_view_write(pcilib_t *ctx, pcilib_view_context_t *view, pcilib_register_value_t *regval, const pcilib_value_t *val) { -/* if(!(strcasecmp(unit, ((pcilib_view_t*)viewval)->base_unit.name))) { - formula=malloc(sizeof(char)*strlen(((pcilib_formula_t*)params)->write_formula)); - strncpy(formula,((pcilib_formula_t*)params)->write_formula,strlen(((pcilib_formula_t*)params)->write_formula)); - pcilib_view_apply_formula(ctx,formula,regval); - return 0; - } +static int pcilib_transform_view_write(pcilib_t *ctx, pcilib_view_context_t *view_ctx, pcilib_register_value_t *regval, const pcilib_value_t *val) { + int err = 0; + pcilib_value_t val_copy = {0}; + + const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx); + pcilib_transform_view_description_t *v = (pcilib_transform_view_description_t*)(model_info->views[view_ctx->view]); + + err = pcilib_copy_value(ctx, &val_copy, val); + if (err) return err; + + err = pcilib_py_eval_string(ctx, v->write_to_reg, &val_copy); + if (err) return err; - for(j=0; ((pcilib_view_t*)viewval)->base_unit.transforms[j].name; j++) { - if(!(strcasecmp(((pcilib_view_t*)viewval)->base_unit.transforms[j].name,unit))) { - // when we have found the correct view of type formula, we apply the formula, that get the good value for return - formula=malloc(sizeof(char)*strlen(((pcilib_formula_t*)params)->write_formula)); - strncpy(formula,((pcilib_formula_t*)params)->write_formula,strlen((( pcilib_formula_t*)params)->write_formula)); - //pcilib_view_apply_unit(((pcilib_view_t*)viewval)->base_unit.transforms[j],unit,regval); - pcilib_view_apply_formula(ctx,formula,regval); - // we maybe need some error checking there , like temp_value >min and