由于实验需要读取Logitech G27的数据,就基于DirectX写了个程序读取数据,比较仓促,这里做简单总结(需要的朋友看一遍就应该很容易理解了~~).
开发环境: VS2008+Qt库(设计界面)+DirectX SDK
1.连接硬件,需要连接电源
2.安装自带的驱动软件
3.安装DirectX SDK (微软官网可以下载到)
4.关键的代码如下:
// #############################################################################
//DirectInput
// #############################################################################
#include
#pragma comment(lib, "dinput8.lib")
#pragma comment(lib, "dxguid.lib")
// #############################################################################
// #############################################################################
//G27状态变量
extern DIJOYSTATE2 g_G27State;
extern QMutex g_mutexG27;
class CG27 :
public QThread
{
Q_OBJECT
public:
CG27(void);
~CG27(void);
public:
bool InitializeG27();
protected:
void run();
signals:
void GetG27Info();
};
#include "G27.h"
LPDIRECTINPUT8 g_pDI = NULL;
LPDIRECTINPUTDEVICE8 g_pJoystick = NULL;
CG27::CG27(void)
{
// InitializeG27();
}
CG27::~CG27(void)
{
}
//-----------------------------------------------------------------------------
// Name: EnumJoysticksCallback()
// Desc: Called once for each enumerated joystick. If we find one, create a
// device interface on it so we can play with it.
//-----------------------------------------------------------------------------
BOOL CALLBACK
enumCallback(const DIDEVICEINSTANCE* instance, VOID* context)
{
HRESULT hr;
// Obtain an interface to the enumerated joystick.
hr = g_pDI->CreateDevice(instance->guidInstance, &g_pJoystick, NULL);
// If it failed, then we can't use this joystick. (Maybe the user unplugged
// it while we were in the middle of enumerating it.)
if (FAILED(hr)) {
return DIENUM_CONTINUE;
}
// Stop enumeration. Note: we're just taking the first joystick we get. You
// could store all the enumerated joysticks and let the user pick.
//目前只考虑一个方向盘
return DIENUM_STOP;
}
BOOL CALLBACK
enumAxesCallback(const DIDEVICEOBJECTINSTANCE* instance, VOID* context)
{
HWND hDlg = (HWND)context;
DIPROPRANGE propRange;
propRange.diph.dwSize = sizeof(DIPROPRANGE);
propRange.diph.dwHeaderSize = sizeof(DIPROPHEADER);
propRange.diph.dwHow = DIPH_BYID;
propRange.diph.dwObj = instance->dwType;
if (instance->guidType == GUID_XAxis )//方向盘
{
propRange.lMin = -4500;
propRange.lMax = +4500;
}
else if(instance->guidType == GUID_Slider )//离合
{
propRange.lMin = 0;
propRange.lMax = 1000;
}
else if(instance->guidType == GUID_RzAxis )//刹车
{
propRange.lMin = 0;
propRange.lMax = +1000;
}
else if(instance->guidType == GUID_YAxis )//油门
{
propRange.lMin = 0;
propRange.lMax = +1000;
}
else
{
propRange.lMin = -1000;
propRange.lMax = +1000;
}
// Set the range for the axis
if (FAILED(g_pJoystick->SetProperty(DIPROP_RANGE, &propRange.diph))) {
return DIENUM_STOP;
}
return DIENUM_CONTINUE;
}
bool CG27::InitializeG27()
{
HRESULT hr;
// Register with the DirectInput subsystem and get a pointer to a IDirectInput interface we can use.
// Create a DInput object
//initialize directinput library
if( FAILED( hr = DirectInput8Create( GetModuleHandle( NULL ), DIRECTINPUT_VERSION,
IID_IDirectInput8, ( VOID** )&g_pDI, NULL ) ) )
return false;
LPDIRECTINPUTDEVICE8 joystick;
// Look for the first simple joystick we can find.
if (FAILED(hr = g_pDI->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback,
NULL, DIEDFL_ATTACHEDONLY))) {
return false;
}
if (g_pJoystick == NULL)
{
ShowMessageBox("g27 not found, please check the connection, exiting........");
return false;
}
if (FAILED(hr = g_pJoystick->SetDataFormat(&c_dfDIJoystick2)))
{
ShowMessageBox(" set g27 data format error, exiting.......");
return false;
}
g_pJoystick->SetCooperativeLevel(NULL, DISCL_EXCLUSIVE|DISCL_FOREGROUND);
DIDEVCAPS capabilities;
capabilities.dwSize = sizeof(DIDEVCAPS);
g_pJoystick->GetCapabilities(&capabilities);
if (FAILED(hr=g_pJoystick->EnumObjects(enumAxesCallback, NULL, DIDFT_AXIS)))
{
}
return true;
}
void CG27::run()
{
HRESULT hr;
if (g_pJoystick == NULL)
{
return;
}
while(1)
{
hr = g_pJoystick->Poll();
g_pJoystick->Acquire();
g_mutexG27.lock();
hr = g_pJoystick->GetDeviceState( sizeof( DIJOYSTATE2 ), &g_G27State);
emit(GetG27Info());
g_mutexG27.unlock();
usleep(1000);
}
return;
}
class G27_Test1 : public QMainWindow
{
Q_OBJECT
public:
G27_Test1(QWidget *parent = 0, Qt::WFlags flags = 0);
~G27_Test1();
CG27* m_pThreadG27;//g27 thread
DIJOYSTATE2 m_g27State;
public slots:
void OnGetG27Info();
private:
Ui::G27_Test1Class ui;
};
G27_Test1::G27_Test1(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
m_pThreadG27 = new CG27();
connect(m_pThreadG27, SIGNAL(GetG27Info()), this, SLOT(OnGetG27Info()) );
if (!m_pThreadG27->InitializeG27())
{
exit(-1);
}
m_pThreadG27->start();
}
G27_Test1::~G27_Test1()
{
}
void G27_Test1::OnGetG27Info()
{
//copy data
g_mutexG27.lock();
memcpy(&m_g27State , &g_G27State, sizeof(DIJOYSTATE2));
g_mutexG27.unlock();
//display data
this->ui.labelXAxis->setNum(m_g27State.lX);
this->ui.labelYAxis->setNum(m_g27State.lY);
this->ui.labelZAxis->setNum(m_g27State.lZ);
this->ui.labelXRotation->setNum(m_g27State.lRx);
this->ui.labelYRoation->setNum(m_g27State.lRy);
this->ui.labelZRotation->setNum(m_g27State.lRz);
this->ui.labelVelXAxis->setNum(m_g27State.lVX);
this->ui.labelVelYAxis->setNum(m_g27State.lVY);
this->ui.labelVelZAxis->setNum(m_g27State.lVZ);
this->ui.labelVelXRotation->setNum(m_g27State.lVRx);
this->ui.labelVelYRotation->setNum(m_g27State.lVRy);
this->ui.labelVelZRotation->setNum(m_g27State.lVRz);
this->ui.labelAccXAxis->setNum(m_g27State.lAX);
this->ui.labelAccYAxis->setNum(m_g27State.lAY);
this->ui.labelAccZAxis->setNum(m_g27State.lAZ);
this->ui.labelAccXRotation->setNum(m_g27State.lARx);
this->ui.labelAccYRotation->setNum(m_g27State.lARy);
this->ui.labelAccZRotation->setNum(m_g27State.lARz);
this->ui.labelForceXAxis->setNum(m_g27State.lFX);
this->ui.labelForceYAxis->setNum(m_g27State.lFY);
this->ui.labelForceZAxis->setNum(m_g27State.lFZ);
this->ui.labelForceXRotation->setNum(m_g27State.lFRx);
this->ui.labelForceYRotation->setNum(m_g27State.lFRy);
this->ui.labelForceZRotation->setNum(m_g27State.lFRz);
this->ui.labelSlider0->setNum(m_g27State.rglSlider[0]);
this->ui.labelSlider1->setNum(m_g27State.rglSlider[1]);
this->ui.labelVelSlider0->setNum(m_g27State.rglVSlider[0]);
this->ui.labelVelSlider0->setNum(m_g27State.rglVSlider[1]);
this->ui.labelAccSlider0->setNum(m_g27State.rglASlider[0]);
this->ui.labelAccSlider1->setNum(m_g27State.rglASlider[1]);
this->ui.labelForceSlider0->setNum(m_g27State.rglFSlider[0]);
this->ui.labelForceSlider1->setNum(m_g27State.rglFSlider[1]);
this->ui.labelPOV0->setNum(int(m_g27State.rgdwPOV[0]));
this->ui.labelPOV1->setNum(int(m_g27State.rgdwPOV[1]));
this->ui.labelPOV2->setNum(int(m_g27State.rgdwPOV[2]));
this->ui.labelPOV3->setNum(int(m_g27State.rgdwPOV[3]));
(m_g27State.rgbButtons[0] & 0x80) ? (this->ui.label00->setNum(0)):(this->ui.label00->setNum(-1));
(m_g27State.rgbButtons[1] & 0x80) ? (this->ui.label01->setNum(1)):(this->ui.label01->setNum(-1));
(m_g27State.rgbButtons[2] & 0x80) ? (this->ui.label02->setNum(2)):(this->ui.label02->setNum(-1));
(m_g27State.rgbButtons[3] & 0x80) ? (this->ui.label03->setNum(3)):(this->ui.label03->setNum(-1));
(m_g27State.rgbButtons[4] & 0x80) ? (this->ui.label04->setNum(4)):(this->ui.label04->setNum(-1));
(m_g27State.rgbButtons[5] & 0x80) ? (this->ui.label05->setNum(5)):(this->ui.label05->setNum(-1));
(m_g27State.rgbButtons[6] & 0x80) ? (this->ui.label06->setNum(6)):(this->ui.label06->setNum(-1));
(m_g27State.rgbButtons[7] & 0x80) ? (this->ui.label07->setNum(7)):(this->ui.label07->setNum(-1));
(m_g27State.rgbButtons[8] & 0x80) ? (this->ui.label08->setNum(8)):(this->ui.label08->setNum(-1));
(m_g27State.rgbButtons[9] & 0x80) ? (this->ui.label09->setNum(9)):(this->ui.label09->setNum(-1));
(m_g27State.rgbButtons[10] & 0x80) ? (this->ui.label10->setNum(10)):(this->ui.label10->setNum(-1));
(m_g27State.rgbButtons[11] & 0x80) ? (this->ui.label11->setNum(11)):(this->ui.label11->setNum(-1));
(m_g27State.rgbButtons[12] & 0x80) ? (this->ui.label12->setNum(12)):(this->ui.label12->setNum(-1));
(m_g27State.rgbButtons[13] & 0x80) ? (this->ui.label13->setNum(13)):(this->ui.label13->setNum(-1));
(m_g27State.rgbButtons[15] & 0x80) ? (this->ui.label15->setNum(15)):(this->ui.label15->setNum(-1));
(m_g27State.rgbButtons[16] & 0x80) ? (this->ui.label16->setNum(16)):(this->ui.label16->setNum(-1));
(m_g27State.rgbButtons[17] & 0x80) ? (this->ui.label17->setNum(17)):(this->ui.label17->setNum(-1));
(m_g27State.rgbButtons[18] & 0x80) ? (this->ui.label18->setNum(18)):(this->ui.label18->setNum(-1));
(m_g27State.rgbButtons[19] & 0x80) ? (this->ui.label19->setNum(19)):(this->ui.label19->setNum(-1));
(m_g27State.rgbButtons[20] & 0x80) ? (this->ui.label20->setNum(20)):(this->ui.label20->setNum(-1));
(m_g27State.rgbButtons[21] & 0x80) ? (this->ui.label21->setNum(21)):(this->ui.label21->setNum(-1));
(m_g27State.rgbButtons[22] & 0x80) ? (this->ui.label22->setNum(22)):(this->ui.label22->setNum(-1));
this->ui.labelSteer->setNum(m_g27State.lX);
this->ui.labelClutch->setNum(m_g27State.rglSlider[1]);
this->ui.labelBrake->setNum(m_g27State.lRz);
this->ui.labelThrottle->setNum(m_g27State.lY);
//BYTE rgbButtons[128];
}