#include "JoystickReader.h"
JoystickReader
::JoystickReader(QObject *parent, JoystickModel joyType
) :{
fd.open( "/dev/input/js0", ifstream::in | ifstream::binary );
joystickType = joyType;
}
JoystickReader::~JoystickReader()
{
fd.close();
}
void JoystickReader::run()
{
if( joystickType == LOGITECH )
{
readLogitechJoystick();
}
else
{
readOfficialJoystick();
}
}
void JoystickReader::readLogitechJoystick()
{
// These variables aren't used in our program, THEY DON'T HAVE SOULS!
char unknown, timeChar, timeChar2, timeChar3;
unknown = timeChar = timeChar2 = timeChar3 = 0;
int myTime = 0;
// Initialize the important variables (the ones with souls)
buttonPress = magnitude = commandType = messageClarify = 0;
joystick.setXPosition( 0 );
joystick.setYPosition( 0 );
// FOR- -EH- -VER
while( 1 )
{
if( fd.is_open() )
{
// Read in the data
fd.read(&unknown, sizeof(char));
fd.read(&timeChar, sizeof(char));
fd.read(&timeChar2, sizeof(char));
fd.read(&timeChar3, sizeof(char));
fd.read(&buttonPress, sizeof(char));
fd.read(&magnitude, sizeof(char));
fd.read(&commandType, sizeof(char));
fd.read(&messageClarify, sizeof(char));
// With their powers combined, they are the current time!
myTime = (((timeChar3<<16)&0x00FF0000)
+ ((timeChar2<<8)&0x0000FF00))
+ (timeChar & 0x000000FF);
// Joystick is obviously connected
joystick.setConnectionState( true );
// Is this a button press update?
if( commandType == LOGITECH_BUTTON )
{
// Is this button released or pressed?
if( buttonPress == LOGITECH_RELEASE )
{
handleLogitechButtonRelease();
}
else if( buttonPress == LOGITECH_PRESS )
{
handleLogitechButtonPress();
}
}
// Else this is a joystick movement update
else if( commandType == LOGITECH_JOYSTICK )
{
handleLogitechJoystickMovement();
}
}
}
}
void JoystickReader::handleLogitechButtonRelease()
{
// Which button?
switch( messageClarify )
{
case LOGITECH_DEADMAN:
{
// deadman released
joystick.setDeadmanState( false );
break;
}
case LOGITECH_DOWN:// down released
case LOGITECH_UP:// up released
case LOGITECH_LEFT:// left released
case LOGITECH_RIGHT:// right released
{
joystick.setButtonPressed( Joystick::NONE );
break;
}
default:
{
// It was one of the buttons we don't care about, do nothing
break;
}
};
}
void JoystickReader::handleLogitechButtonPress()
{
// Which button?
switch( messageClarify )
{
case LOGITECH_DEADMAN:
{
// deadman pressed
joystick.setDeadmanState( true );
break;
}
case LOGITECH_DOWN:// down pressed
{
joystick.setButtonPressed( Joystick::DOWN );
break;
}
case LOGITECH_UP:// up pressed
{
joystick.setButtonPressed( Joystick::UP );
break;
}
case LOGITECH_LEFT:// left pressed
{
joystick.setButtonPressed( Joystick::LEFT );
break;
}
case LOGITECH_RIGHT:// right pressed
{
joystick.setButtonPressed( Joystick::RIGHT );
break;
}
default:
{
// Do nothing
break;
}
};
}
void JoystickReader::handleLogitechJoystickMovement()
{
// Which axis?
switch( messageClarify )
{
case LOGITECH_AXIS_X: // X Axis
{
joystick.setXPosition( magnitude );
break;
}
case LOGITECH_AXIS_Y: // Y Axis
{
joystick.setYPosition( magnitude );
break;
}
default:
{
break;
}
}
}
void JoystickReader::readOfficialJoystick()
{
// Currently does nothing!
}
Bookmarks