Within our project we've used a lot of boolean setOutputValue(char pinNumber, boolean pinValue)
kind of functions.
Within those functions we do the following (for example):
hardware_layer:
#define L9823_HIGH 0
#define L9823_LOW 1
void hardware_L9823_setOutput(U8BIT pin, U8BIT value)
{
/* some SPI interfacing is handled here */
}
IO_layer:
#define IO_ACTIVE 1
#define IO_INACTIVE 0
void io_outputDriver_setOutput(U8BIT pin, U8BIT value)
{
if (value == IO_ACTIVE)
{
hardware_L9823_setOutput(pinMapping[pin], L9823_HIGH);
}
else
{
hardware_L9823_setOutput(pinMapping[pin], L9823_LOW);
}
}
component_layer:
#define COMPONENT_ASIMPLEDEVICE_A_ON 1
#define COMPONENT_ASIMPLEDEVICE_A_OFF 0
void component_aSimpleDeviceA_set(U8BIT value)
{
if (value == COMPONENT_ASIMPLEDEVICE_A_ON)
{
io_outputDriver_setOutput(DEVICE_PIN_A, IO_INACTIVE);
}
else
{
io_outputDriver_setOutput(DEVICE_PIN_A, IO_ACTIVE);
}
}
#define COMPONENT_ASIMPLEDEVICE_B_ON 1
#define COMPONENT_ASIMPLEDEVICE_B_OFF 0
void component_aSimpleDeviceB_set(U8BIT value)
{
if (value == COMPONENT_ASIMPLEDEVICE_B_ON)
{
io_outputDriver_setOutput(DEVICE_PIN_B, IO_INACTIVE);
}
else
{
io_outputDriver_setOutput(DEVICE_PIN_B, IO_ACTIVE);
}
}
unit_layer:
#define COMPLEXDEVICE_ON 1
#define COMPLEXDEVICE_OFF 0
void component_aComplexDeviceWithMultipleComponents_set(U8BIT value)
{
if (value == COMPLEXDEVICE_ON)
{
component_aSimpleDeviceA_set(COMPONENT_ASIMPLEDEVICE_A_ON);
component_aSimpleDeviceB_set(COMPONENT_ASIMPLEDEVICE_A_ON);
}
else
{
component_aSimpleDeviceA_set(COMPONENT_ASIMPLEDEVICE_A_OFF);
component_aSimpleDeviceB_set(COMPONENT_ASIMPLEDEVICE_A_OFF);
}
}
This is a rather simple example, but in our case we have a total of 4 layers to seperate hardware/io/component and module implementations. So this has to propagate 4 layers down before it reaches the hardware register.
It does give less code to maintain and less comments to maintain (each function has a header of atleast 9 lines of code.
I've raised the concern of having many if-statements while trying to set a hardware register and proposed to start splitting these functions in to each having their own function like so:
boolean setOutputLow(char pinNumber)
{
if (pinNumber is within limits of output register mapping)
{
regMapping[pinNumber] = OUTPUTLOW;
}
}
and the same ofc. for the High variant. Which seems to save a lot of if-else logic down the road but does increase code size a tiny bit.
Which would be a prefered way of handling these kind of things? Are there even better ways? Or is this the right road to walk?
void aModuleSetValue(boolean lowHigh) { setOutputValue(PINBELONGINGTOTHISMODULE, lowHigh); }
would be a starting point. – Morwenn Apr 22 '13 at 14:01if (a==false) b(false); else b(true);
byb(a)
. It is just a static analysis optimization. It seems that you are talking as if I tried to modifysetOutputValue
in your previous comment :o – Morwenn Apr 22 '13 at 14:24