C
Chris Carlen
Hi:
I have an embedded system, platform: TI TMS320F2812 32-bit integer DSP.
A module (.c file) contains external (to the funcs in that module)
variables which are used to affect the operation of the program (other
modules). These variables are only read by other parts of the program,
but are to be set by a user interface which consists of an RS-232
terminal command line interface. The command parser for this interface
responds to commands to get or set the variables.
In the interest of good programming practice I desired to have the
parser know as little as possible about the variables. It doesn't set
or read them directly, but through an interface. The parser contains a
table of pointers to variable attribute structures. Each pointer is
associated with a packed string id for the variable. When a match is
made, it passes the pointer (and perhaps a set value) to the variable
attribute struct to the get or set accessor function for the variable.
Here is the interface to the variables. Note, that it only needs to be
general up to the point of handling the machine's native int16 and int32
access sizes.
If you have any better ideas or criticisms, please let me know. I am
not fully happy with the use of void * . I tried to use a union in two
different ways, but not successfully.
Thanks:
--------------------------------------------------------------------
// use-vars.h
// header for module with variables to be interfaced:
// a var may be signed or unsigned (u)int16 or (u)int32
extern uint16 Var1;
extern int32 Var2;
--------------------------------------------------------------------
// vars.h
// header for module which implements interface:
#include "use-vars.h"
#define VAR_RW_OK 0
#define VAR_RW_ERR_RANGE -1
#define VAR_RW_ERR_TYPE -2
/* This defines which type is used at the interface level to pass values
to/from variable read/write access functions, and to define the bounds.
This must be the largest type for which any variable might need to be
accessed: */
typedef int32 VarRW_t;
struct VarAttribs {
void *vptr; // pointer to variable -- tried union unsuccessfully
int vsize; // sizeof() variable
VarRW_t vmin; // lower bound
VarRW_t vmax; // upper bound
};
typedef struct VarAttribs VarAttribs_t;
int var_set(VarAttribs_t *, VarRW_t);
VarRW_t var_get(VarAttribs_t *);
extern VarAttribs_t Var1_attr;
extern VarAttribs_t Var2_attr;
--------------------------------------------------------------------
// vars.c
#include "use-vars.h"
#include "vars.h"
// Here we define the variable attributes:
VarAttribs_t Var1_attr = {&Var1, sizeof(Var1), 1, 10};
VarAttribs_t Var1_attr = {&Var2, sizeof(Var2), -50000, 50000};
// Functions for accessing variables (only set shown):
int var_set(VarAttribs_t *vi, VarRW_t val)
{
if ( val < vi->vmin || val > vi->vmax ) return(VAR_RW_ERR_RANGE);
else {
if ( vi->vsize == sizeof(int16) )
*(int16 *)vi->vptr = (int16) val;
else if ( vi->vsize == sizeof(int32) )
*(int32 *)vi->vptr = (int32) val;
else return (VAR_RW_ERR_TYPE);
}
return(VAR_RW_OK);
}
----------------------------------------------------------------------
// parse.c
// module which accesses the variables
#include "vars.h"
/* if a command is parsed which provides the value v to set the
variable identified "Var1", and the table lookup has returned a
pointer to the Var1_attr structure, then: */
if ( var_set(&Var1_attr, v) != VAR_RW_OK )
deal_with_badness();
// else continue...
--
Good day!
________________________________________
Christopher R. Carlen
Principal Laser&Electronics Technologist
Sandia National Laboratories CA USA
(e-mail address removed)
NOTE, delete texts: "RemoveThis" and
"BOGUS" from email address to reply.
I have an embedded system, platform: TI TMS320F2812 32-bit integer DSP.
A module (.c file) contains external (to the funcs in that module)
variables which are used to affect the operation of the program (other
modules). These variables are only read by other parts of the program,
but are to be set by a user interface which consists of an RS-232
terminal command line interface. The command parser for this interface
responds to commands to get or set the variables.
In the interest of good programming practice I desired to have the
parser know as little as possible about the variables. It doesn't set
or read them directly, but through an interface. The parser contains a
table of pointers to variable attribute structures. Each pointer is
associated with a packed string id for the variable. When a match is
made, it passes the pointer (and perhaps a set value) to the variable
attribute struct to the get or set accessor function for the variable.
Here is the interface to the variables. Note, that it only needs to be
general up to the point of handling the machine's native int16 and int32
access sizes.
If you have any better ideas or criticisms, please let me know. I am
not fully happy with the use of void * . I tried to use a union in two
different ways, but not successfully.
Thanks:
--------------------------------------------------------------------
// use-vars.h
// header for module with variables to be interfaced:
// a var may be signed or unsigned (u)int16 or (u)int32
extern uint16 Var1;
extern int32 Var2;
--------------------------------------------------------------------
// vars.h
// header for module which implements interface:
#include "use-vars.h"
#define VAR_RW_OK 0
#define VAR_RW_ERR_RANGE -1
#define VAR_RW_ERR_TYPE -2
/* This defines which type is used at the interface level to pass values
to/from variable read/write access functions, and to define the bounds.
This must be the largest type for which any variable might need to be
accessed: */
typedef int32 VarRW_t;
struct VarAttribs {
void *vptr; // pointer to variable -- tried union unsuccessfully
int vsize; // sizeof() variable
VarRW_t vmin; // lower bound
VarRW_t vmax; // upper bound
};
typedef struct VarAttribs VarAttribs_t;
int var_set(VarAttribs_t *, VarRW_t);
VarRW_t var_get(VarAttribs_t *);
extern VarAttribs_t Var1_attr;
extern VarAttribs_t Var2_attr;
--------------------------------------------------------------------
// vars.c
#include "use-vars.h"
#include "vars.h"
// Here we define the variable attributes:
VarAttribs_t Var1_attr = {&Var1, sizeof(Var1), 1, 10};
VarAttribs_t Var1_attr = {&Var2, sizeof(Var2), -50000, 50000};
// Functions for accessing variables (only set shown):
int var_set(VarAttribs_t *vi, VarRW_t val)
{
if ( val < vi->vmin || val > vi->vmax ) return(VAR_RW_ERR_RANGE);
else {
if ( vi->vsize == sizeof(int16) )
*(int16 *)vi->vptr = (int16) val;
else if ( vi->vsize == sizeof(int32) )
*(int32 *)vi->vptr = (int32) val;
else return (VAR_RW_ERR_TYPE);
}
return(VAR_RW_OK);
}
----------------------------------------------------------------------
// parse.c
// module which accesses the variables
#include "vars.h"
/* if a command is parsed which provides the value v to set the
variable identified "Var1", and the table lookup has returned a
pointer to the Var1_attr structure, then: */
if ( var_set(&Var1_attr, v) != VAR_RW_OK )
deal_with_badness();
// else continue...
--
Good day!
________________________________________
Christopher R. Carlen
Principal Laser&Electronics Technologist
Sandia National Laboratories CA USA
(e-mail address removed)
NOTE, delete texts: "RemoveThis" and
"BOGUS" from email address to reply.