Hey,
i have Input Data from a Gamepads DPad in form of 4 Booleans (Up, Down, Left and Right).
I need those to be converted into an Integer ranging from 0 to 8, where 0 is the Null State.
Up 1 0000 0001
Up-Right 2 0000 0010
Right 3 0000 0011
Down-Right 4 0000 0100
Down 5 0000 0101
Down-Left 6 0000 0110
Left 7 0000 0111
Up-Left 8 0000 1000
I haven't come up with a nice solution, nor found any pattern yet. So that's what i am asking for. Is there any nice Bit Operation Magic to put that into 1-5 nice lines?
Currently i am using this trivial but horrible way:
if (Up && Down)
{
Up = FALSE;
Down = FALSE;
}
if (Left && Right)
{
Left = FALSE;
Right = FALSE;
}
if (Left)
{
ReportByte[0] = 0x07;
}
else if (Right)
{
ReportByte[0] = 0x03;
}
if (Up)
{
if (Right)
{
ReportByte[0] = 0x02;
}
else if (Left)
{
ReportByte[0] = 0x08;
}
else
{
ReportByte[0] = 0x01;
}
}
else if (Down)
{
if (Right)
{
ReportByte[0] = 0x04;
}
else if (Left)
{
ReportByte[0] = 0x06;
}
else
{
ReportByte[0] = 0x05;
}
}
So any suggestions are welcome.