I have a program that works with a hardware via an ActiveX API. The API have events that is triggered by the hardware. In the hardware there is an IO card. There is 16 in and 16 out. Changes in input signal triggers the event handler below. The event uses an int to show which signal is set/unset. This by using binary addresses for each signal
Signal Mask
INPUT_0 0x01;
INPUT_1 0x02;
INPUT_2 0x04;
INPUT_3 0x08;
...
INPUT_11 0x800
(there are some signals that are used for something else and can't be used as Input)
The signals can be set/unset in any order, but the event will trigger every time a change is done! The value, p_nBits, will include all signals state. This means that if INPUT_0 is set and then INPUT_1 is set then p_nBits will be 3, i.e. both signals! But INPUT_0 has already been handled and should not be handled again until it is unset. I have an integer currentBits that is the current state of the input signals.
Any error in logic? What can be improved? Is it possible to make it faster?
// Current state of Input signals
int currentBits = 0;
public void IoPort_sigInputChange(int p_nBits)
{
lock (lockObj)
{
if ((p_nBits & INPUT_0) == INPUT_0)
{
if ((currentBits & INPUT_0) != INPUT_0)
{
currentBits |= INPUT_0;
RaiseRunModeEvent(true);
}
}
else
{
if ((currentBits & INPUT_0) == INPUT_0)
{
currentBits &= ~INPUT_0;
RaiseRunModeEvent(false);
}
}
if ((p_nBits & INPUT_1) == INPUT_1)
{
if ((currentBits & INPUT_1) != INPUT_1)
{
currentBits |= INPUT_1;
RaiseAllPositionsSetEvent(true);
}
}
else
{
if ((currentBits & INPUT_1) == INPUT_1)
{
currentBits &= ~INPUT_1;
RaiseAllPositionsSetEvent(false);
}
}
if ((p_nBits & INPUT_2) == INPUT_2)
{
if ((currentBits & INPUT_2) != INPUT_2)
{
currentBits |= INPUT_2;
RaiseOrderEndedEvent(true);
}
}
else
{
if ((currentBits & INPUT_2) == INPUT_2)
{
currentBits &= ~INPUT_2;
RaiseOrderEndedEvent(false);
}
}
// more checks may occur
}
}