On Jul 1, 5:25*pm, Thad Smith <ThadSm...@acm.orgwrote:
Rohit 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 am unclear about what it means. Can anybody suggest an insight what
it means ?
I don't know what the comment means or how it might apply to your code. *I
see nothing in the code that would be better with the use of macros and I
use more macros than most programmers.
.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)
I suggest adding a blank line for visual separation between the comment
line and the function statement.
You omitted the type of signal_id. *To avoid this kind of problem in posted
code, cut and paste compiled code.
{
* int retval = 0;
* switch(value)
* * * * {
* * * * * * case Switch_Low:
* * * * * * * * * {
* * * * * * * * * * * if(switchconfTable[signal_id].contact==0x01)
* * * * * * * * * * * * * * retval = 1;
* * * * * * * * * * * else
* * * * * * * * * * * * * * retval = 2;
* * * * * * * * * }
You probably want a break here before the next case.
* * * * * * case Switch_High:
* * * * * * * * * * */* Similar code as Switch_Low case*/
* * * * * *case Switch_Open:
* * * * * * * * * * */* Similar code as Switch_Low case*/
* * * * }
* * return retval;
}
--
Thad- Hide quoted text -
- Show quoted text -
OK here is the real code:
Content of configuration table SwitchConfigTab can be understood
easily by going through the code. its an array of structures
containing configuration for each switch.
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;
typedef enum
{
Switch_01, /*!< ID for Switches / Push buttons */
Switch_02, /*!< ID for Switches / Push buttons */
Switch_03, /*!< ID for Switches / Push buttons */
Switch_04, /*!< ID for Switches / Push buttons */
Switch_05, /*!< ID for Switches / Push buttons */
Switch_06, /*!< ID for Switches / Push buttons */
Switch_07, /*!< ID for Switches / Push buttons */
} signalID_t;
static void CheckSwitchStatus(Switch_value_t value,
signalID_t
SignalID)
{
/* Check value */
switch (value)
{
/* IF the value is open for enabled switches then,irrespective
of
switch type and its test condition update switch status &
value based
on Switch contact.
*/
case Switch_Open:
{
/* Check Switch contact:
For normally open switches - Switch_open means switch is
in
Neutral position.
For normally closed switches-Switch_open means switch is
in
Active position
*/
if (SwitchConfigTab[SignalID].SwitchContact == Switch_NC
{
SwitchObjTable[SignalID].SwitchCurrentStatus =
Switch_Active;
}
else
{
SwitchObjTable[SignalID].SwitchCurrentStatus
=Switch_Neutral
}
SwitchObjTable[SignalID].ValueState = VALUE_FULLVALID;
break;
}
case Switch_Low:
{
/* For EnableLow type switches-value = low means no error
condition.
Check for switch contact and update the status.
*/
if (SwitchConfigTab[SignalID].SwitchType ==
Switch_EnableLow
{
/* If Noramally closed contact switches update switch
status as
Neutral.For normally open switches update switch status as
Active.
Call UpdateSwitchStatus function to update status.
*/
if (SwitchConfigTab[SignalID].SwitchContact == Switch_NC
{
SwitchObjTable[SignalID].SwitchCurrentStatus =
Switch_Active;
}
else
{
SwitchObjTable[SignalID].SwitchCurrentStatus
=Switch_Neutral
}
SwitchObjTable[SignalID].ValueState = VALUE_FULLVALID;
break;
}
/* For enable high switches value = Low means error is
present
for the switch.*/
else if (SwitchConfigTab[SignalID].SwitchType ==
Switch_EnableHigh)
{
/* In error conditions assign default status to the switch
status
and update value state as error.*/
SwitchObjTable[SignalID].SwitchCurrentStatus =
Switch_DefaultState;
SwitchObjTable[SignalID].ValueState =
VALUE_ERROR;
/* Check if condition is enabled by the user.
If it's enabled,then call the error handler function to
set the
error.*/
if (SwitchConfigTab[SignalID].TestEnable ==
Switch_Test_SwitchToLow )
{
(void)ErrHdl_SetTestFailed(SwitchConfigTab[SignalID].ErrorNumber);
}
break;
}
break;
} /*End case Switch_Low*/
case Switch_High:
{
SIMILIAR CODE AS IN ABOVE CASE;
break;
}/*End case Switch_High*/
default:
break;
}/* End of Switch statement*/
}
Cheers
Rohit