Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to replace this switch with preprocessor code

0 views
Skip to first unread message

Rohit

unread,
Jul 4, 2008, 2:47:08 AM7/4/08
to
Hi,

I am working on a switch module which after reading voltage through a
port pin and caterogizing it into three ranges(open,low or high),
passes this range to a function switch_status() with parameters value
and signal ID. Signal Id is used to get a user configurable parameter
inside a configuration file, which depends on the type of switch.

I have implemented it as under. Please ignore those magic numbers as I
have mimized logic to simplify it. Now I want to optimize it. Somebody
told me that the logical connection between variables: value,
switchconfTable[signal_id].contact and the return value(TRUE or FALSE)
should be solved via preprocessor not code.

I am unclear about what it means. Can anybody suggest an insight what
it means ?


.h file

typedef enum
{
Switch_Low, /*!< 0 - value of digital Input is low */
Switch_High, /*!< 1 - value of digital Input is high */
Switch_Open /*!< 2 - value of digital input being not
connected (break) */
} Switch_value_t;

.c file
/* This function is called after determining that value
is in one of the ranges defined by Switch_value_t */
int static switch_status(Switch_value_t value, signal_id)
{
int retval = 0;
switch(value)
{
case Switch_Low:
{
if(switchconfTable[signal_id].contact==0x01)
retval = 1;

else
retval = 2;

}

case Switch_High:
/* Similar code as Switch_Low case*/

case Switch_Open:
/* Similar code as Switch_Low case*/
}

return retval;
}

Cheers
Rohit
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Jasen Betts

unread,
Jul 10, 2008, 2:02:42 PM7/10/08
to
On 2008-07-04, Rohit <papak...@gmail.com> wrote:
> Hi,
>
> I am working on a switch module which after reading voltage through a
> port pin and caterogizing it into three ranges(open,low or high),
> passes this range to a function switch_status() with parameters value
> and signal ID. Signal Id is used to get a user configurable parameter
> inside a configuration file, which depends on the type of switch.
>
> I have implemented it as under. Please ignore those magic numbers as I
> have mimized logic to simplify it. Now I want to optimize it. Somebody
> told me that the logical connection between variables: value,
> switchconfTable[signal_id].contact and the return value(TRUE or FALSE)
> should be solved via preprocessor not code.

I can't see how that's possible unless the configueation file is
compile-time configuration.

Bye.
Jasen

0 new messages