Add XBoxController

This commit is contained in:
LI Bin
2014-03-07 17:40:47 +08:00
parent be399952bc
commit 50542c4435
5 changed files with 513 additions and 0 deletions
@@ -22,6 +22,7 @@
#include "SetSo7MotionConfig.h"
#include "So7_Config_Pages.h"
#include "SO7_UtilDlg.h"
#include "So7_XBoxController.h"
// CSO7_UtilDlg ¶Ô»°¿ò
#define HBIT0 0X01
@@ -38,6 +39,7 @@ extern CSO7_Proto* m_pSO7_Proto;
CLogger* g_pLoggerDebug=NULL;
CSO7_VolComp* g_pVolComp=NULL;
CAutoZoom* m_pSO7_AutoZoom=NULL;
CXBOXController* XBoxPlayer = new CXBOXController(1);
IMPLEMENT_DYNAMIC(CSO7_UtilDlg, CDialog)
CSO7_UtilDlg::CSO7_UtilDlg(CWnd* pParent /*=NULL*/)
@@ -409,6 +411,10 @@ void CSO7_UtilDlg::OnBnClickedButtonTermSo7usb()
void CSO7_UtilDlg::OnBnClickedButtonStartSo7machine()
{
OnBnClickedButtonInitSo7usb();
if(XBoxPlayer->IsConnected())
{
XBoxPlayer->XBoxControllerInit();
}
m_pSO7_Proto->Load_So7_Config();
//m_pSO7_Proto->so7_motion_startup(0.5, 0.5, 0.5);
m_pSO7_Proto->_start_machine();
@@ -431,6 +437,11 @@ void CSO7_UtilDlg::OnBnClickedButtonStartSo7machine()
//=====================================================================
void CSO7_UtilDlg::OnBnClickedButtonStopSo7machine()
{
if(XBoxPlayer->IsConnected())
{
XBoxPlayer->XBoxControllerExit();
}
CString csPath;
m_pSO7_Proto->GetAppPath(csPath);
@@ -0,0 +1,457 @@
#include "StdAfx.h"
#include "So7_XBoxController.h"
#include "..\..\..\SevenOcean\SO7_Proto.h"
#include "..\..\..\Animatics\Animatics_Proto.h"
#include "math.h"
extern CSO7_Proto* m_pSO7_Proto;
extern SmartMotor_Proto* pSmartMotor_Proto;
HANDLE CXBOXController::XBoxhThread = NULL;
HANDLE CXBOXController::XBoxThreadMutex = NULL;
HANDLE CXBOXController::XBoxhTriggerEvent;
int CXBOXController::XBoxThreadState = THREAD_PAUSED;
bool CXBOXController::XYWAITSTOP = false;
bool CXBOXController::ZWAITSTOP = false;
short LX = 0, LY = 0, RX = 0, RY = 0;
WORD wBtns = 0;
BYTE TopLightValue = 0;
BYTE BottomLightValue = 0;
static int TopLightBtnDown = 0;
static int BottomLightBtnDown = 0;
CXBOXController::CXBOXController(int playerNumber)
{
_controllerNum = playerNumber - 1;
}
CXBOXController::~CXBOXController()
{
}
void CXBOXController::XBoxControllerInit(void)
{
XBoxThreadState = THREAD_RUNNING_STATE;
XBoxhTriggerEvent = CreateEvent(NULL, FALSE, NULL, NULL);
XBoxhThread = CreateThread((LPSECURITY_ATTRIBUTES)NULL, 0, (LPTHREAD_START_ROUTINE)g_XBox_Thread, (LPVOID)this, 0, NULL);
XBoxThreadMutex = CreateMutex(NULL, FALSE, NULL);
}
void CXBOXController::XBoxControllerExit(void)
{
XBoxThreadState = THREAD_EXIT;
SetEvent(XBoxhTriggerEvent);
if (XBoxhThread)
{
DWORD dwCode = STILL_ACTIVE;
while (dwCode == STILL_ACTIVE)
{
GetExitCodeThread(XBoxhThread, &dwCode);
Sleep(1);
}
}
SetEvent(XBoxhTriggerEvent);
CloseHandle(XBoxhTriggerEvent);
XBoxThreadState = THREAD_EXIT;
ReleaseMutex(XBoxThreadMutex);
CloseHandle(XBoxhThread);
}
bool CXBOXController::IsConnected()
{
ZeroMemory(&_controllerState, sizeof(XINPUT_STATE));
DWORD Result = XInputGetState(_controllerNum, &_controllerState);
if (Result == ERROR_SUCCESS)
{
return true;
}
else
{
return false;
}
}
XINPUT_STATE CXBOXController::GetState()
{
ZeroMemory(&_controllerState, sizeof(XINPUT_STATE));
XInputGetState(_controllerNum, &_controllerState);
return _controllerState;
}
void CXBOXController::Vibrate(int leftVal, int rightVal)
{
XINPUT_VIBRATION Vibration;
ZeroMemory(&Vibration, sizeof(XINPUT_VIBRATION));
Vibration.wLeftMotorSpeed = (WORD)leftVal;
Vibration.wRightMotorSpeed = (WORD)rightVal;
XInputSetState(_controllerNum, &Vibration);
}
unsigned __stdcall CXBOXController::g_XBox_Thread(LPVOID pThis)
{
CXBOXController* _This = (CXBOXController*)pThis;
for (;;)
{
LX = _This->GetState().Gamepad.sThumbLX;
LY = _This->GetState().Gamepad.sThumbLY;
RX = _This->GetState().Gamepad.sThumbRX;
RY = _This->GetState().Gamepad.sThumbRY;
wBtns = _This->GetState().Gamepad.wButtons;
//Z Direction
if(LY>128 && abs(LX)<LY/tan(XBOX_Y_ANGLE))
{
m_pSO7_Proto->_send_cmd_SO7_CMD_MOVE_Z((char)((LY - 128) / 8160 + 1));
ZWAITSTOP = true;
}
else if(LY<128 && abs(LX)<-LY/tan(XBOX_Y_ANGLE))
{
m_pSO7_Proto->_send_cmd_SO7_CMD_MOVE_Z((char)((LY + 128) / 8160.25 - 1));
ZWAITSTOP = true;
}
else if(ZWAITSTOP)
{
m_pSO7_Proto->_send_cmd_SO7_CMD_STOP_MOVE_XYZ();
ZWAITSTOP = false;
}
//XY Direction
if(RX<128 && abs(RY)<-RX*tan(XBOX_X_ANGLE))
{
m_pSO7_Proto->_send_cmd_SO7_CMD_MOVE_X((char)((RX + 128) / 8160.25 - 1));
XYWAITSTOP = true;
}
else if(RX<128 && RY>-RX*tan(XBOX_XY_ANGLE_BEGIN) && RY<-RX*tan(XBOX_XY_ANGLE_END))
{
m_pSO7_Proto->_send_cmd_SO7_CMD_MOVE_XY((char)((RX + 128) / 8160.25 - 1), (char)((RY - 128) / 8160 + 1));
XYWAITSTOP = true;
}
else if(RY>128 && abs(RX)<RY/tan(XBOX_Y_ANGLE))
{
m_pSO7_Proto->_send_cmd_SO7_CMD_MOVE_Y((char)((RY - 128) / 8160 + 1));
XYWAITSTOP = true;
}
else if(RX>128 && RY>RX*tan(XBOX_XY_ANGLE_BEGIN) && RY<RX*tan(XBOX_XY_ANGLE_END))
{
m_pSO7_Proto->_send_cmd_SO7_CMD_MOVE_XY((char)((RX - 128) / 8160 + 1), (char)((RY - 128) / 8160 +1));
XYWAITSTOP = true;
}
else if(RX>128 && abs(RY)<RX*tan(XBOX_X_ANGLE))
{
m_pSO7_Proto->_send_cmd_SO7_CMD_MOVE_X((char)((RX - 128) / 8160 + 1));
XYWAITSTOP = true;
}
else if(RX>128 && -RY>RX*tan(XBOX_XY_ANGLE_BEGIN) && -RY<RX*tan(XBOX_XY_ANGLE_END))
{
m_pSO7_Proto->_send_cmd_SO7_CMD_MOVE_XY((char)((RX - 128) / 8160 + 1), (char)((RY + 128) / 8160.25 - 1));
XYWAITSTOP = true;
}
else if(RY<128 && abs(RX)<-RY/tan(XBOX_Y_ANGLE))
{
m_pSO7_Proto->_send_cmd_SO7_CMD_MOVE_Y((char)((RY + 128) / 8160.25 - 1));
XYWAITSTOP = true;
}
else if(RX<128 && -RY>-RX*tan(XBOX_XY_ANGLE_BEGIN) && -RY<-RX*tan(XBOX_XY_ANGLE_END))
{
m_pSO7_Proto->_send_cmd_SO7_CMD_MOVE_XY((char)((RX + 128) / 8160.25 - 1), (char)((RY + 128) / 8160.25 - 1));
XYWAITSTOP = true;
}
else if(XYWAITSTOP)
{
m_pSO7_Proto->_send_cmd_SO7_CMD_STOP_MOVE_XYZ();
XYWAITSTOP = false;
}
//TopLight
if(wBtns & XINPUT_GAMEPAD_Y && TopLightValue<255)
{
if(TopLightValue > 205)
{
_This->Vibrate(0, 45000);
}
m_pSO7_Proto->g_machine.s_lights_value._top_light = ++TopLightValue;
m_pSO7_Proto->_send_cmd_SO7_CMD_SET_ALL_LIGHT_VALUE();
TopLightBtnDown++;
switch (TopLightBtnDown / 5)
{
case 4:
{
for(int i = 0; i < 10; i++)
{
if(TopLightValue == 255)
break;
TopLightValue++;
}
}
case 3:
{
for(int i = 0; i < 7; i++)
{
if(TopLightValue == 255)
break;
TopLightValue++;
}
}
case 2:
{
for(int i = 0; i < 5; i++)
{
if(TopLightValue == 255)
break;
TopLightValue++;
}
}
case 1:
{
for(int i = 0; i < 3; i++)
{
if(TopLightValue == 255)
break;
TopLightValue++;
}
break;
}
default:
break;
}
}
else if(wBtns & XINPUT_GAMEPAD_Y && TopLightValue==255)
{
_This->Vibrate(0, 65535);
TopLightBtnDown++;
}
else if(wBtns & XINPUT_GAMEPAD_B && TopLightValue>0)
{
if(TopLightValue < 50)
{
_This->Vibrate(30000, 0);
}
m_pSO7_Proto->g_machine.s_lights_value._top_light = --TopLightValue;
m_pSO7_Proto->_send_cmd_SO7_CMD_SET_ALL_LIGHT_VALUE();
TopLightBtnDown++;
switch (TopLightBtnDown / 5)
{
case 4:
{
for(int i = 0; i < 10; i++)
{
if(TopLightValue == 0)
break;
TopLightValue--;
}
}
case 3:
{
for(int i = 0; i < 7; i++)
{
if(TopLightValue == 0)
break;
TopLightValue--;
}
}
case 2:
{
for(int i = 0; i < 5; i++)
{
if(TopLightValue == 0)
break;
TopLightValue--;
}
}
case 1:
{
for(int i = 0; i < 3; i++)
{
if(TopLightValue == 0)
break;
TopLightValue--;
}
break;
}
default:
break;
}
}
else if(wBtns & XINPUT_GAMEPAD_B && TopLightValue==0)
{
_This->Vibrate(50000, 0);
TopLightBtnDown++;
}
else if(TopLightBtnDown > 0)
{
TopLightBtnDown = 0;
_This->Vibrate();
}
//BottomLight
if(wBtns & XINPUT_GAMEPAD_X && BottomLightValue<255)
{
if(BottomLightValue > 205)
{
_This->Vibrate(0, 45000);
}
m_pSO7_Proto->g_machine.s_lights_value._bottom_light = ++BottomLightValue;
m_pSO7_Proto->_send_cmd_SO7_CMD_SET_ALL_LIGHT_VALUE();
BottomLightBtnDown++;
switch (BottomLightBtnDown / 5)
{
case 4:
{
for(int i = 0; i < 10; i++)
{
if(BottomLightValue == 255)
break;
BottomLightValue++;
}
}
case 3:
{
for(int i = 0; i < 7; i++)
{
if(BottomLightValue == 255)
break;
BottomLightValue++;
}
}
case 2:
{
for(int i = 0; i < 5; i++)
{
if(BottomLightValue == 255)
break;
BottomLightValue++;
}
}
case 1:
{
for(int i = 0; i < 3; i++)
{
if(BottomLightValue == 255)
break;
BottomLightValue++;
}
break;
}
default:
break;
}
}
else if(wBtns & XINPUT_GAMEPAD_X && BottomLightValue==255)
{
_This->Vibrate(0, 65535);
BottomLightBtnDown++;
}
else if(wBtns & XINPUT_GAMEPAD_A && BottomLightValue>0)
{
if(BottomLightValue < 50)
{
_This->Vibrate(30000, 0);
}
m_pSO7_Proto->g_machine.s_lights_value._bottom_light = --BottomLightValue;
m_pSO7_Proto->_send_cmd_SO7_CMD_SET_ALL_LIGHT_VALUE();
BottomLightBtnDown++;
switch (BottomLightBtnDown / 5)
{
case 4:
{
for(int i = 0; i < 10; i++)
{
if(BottomLightValue == 0)
break;
BottomLightValue--;
}
}
case 3:
{
for(int i = 0; i < 7; i++)
{
if(BottomLightValue == 0)
break;
BottomLightValue--;
}
}
case 2:
{
for(int i = 0; i < 5; i++)
{
if(BottomLightValue == 0)
break;
BottomLightValue--;
}
}
case 1:
{
for(int i = 0; i < 3; i++)
{
if(BottomLightValue == 0)
break;
BottomLightValue--;
}
break;
}
default:
break;
}
}
else if(wBtns & XINPUT_GAMEPAD_A && BottomLightValue==0)
{
_This->Vibrate(50000, 0);
BottomLightBtnDown++;
}
else if(BottomLightBtnDown > 0)
{
BottomLightBtnDown = 0;
_This->Vibrate();
}
/*
if(_This->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP)
{
m_pSO7_Proto->_send_cmd_SO7_CMD_STOP_MOVE_XYZ();
}
if(_This->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN)
{
m_pSO7_Proto->_send_cmd_SO7_CMD_STOP_MOVE_XYZ();
}
if(_This->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT)
{
pSmartMotor_Proto->_send_cmd_SMARTMOTOR_DECEL_STOPX();
}
if(_This->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT)
{
pSmartMotor_Proto->_send_cmd_SMARTMOTOR_DECEL_STOPX();
}
if(_This->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_START)
{
m_pSO7_Proto->_send_cmd_SO7_CMD_STOP_MOVE_XYZ();
}*/
if(XBoxThreadState == THREAD_EXIT || _This->GetState().Gamepad.wButtons & XINPUT_GAMEPAD_BACK)
{
break;
}
Sleep(100);
};
ExitThread(0);
}
@@ -0,0 +1,37 @@
#include <windows.h>
#include <XInput.h>
#define XBTHREAD_PAUSED 1
#define XBTHREAD_RUNNING_STATE 0
#define XBTHREAD_EXIT -1
#define XBOX_X_ANGLE PI / 180 * 10
#define XBOX_Y_ANGLE PI / 180 * 80
#define XBOX_XY_ANGLE_BEGIN PI / 180 * 35
#define XBOX_XY_ANGLE_END PI / 180 * 55
#pragma comment(lib, "XInput.lib")
class CXBOXController
{
protected:
static HANDLE XBoxhThread;
static HANDLE XBoxThreadMutex;
static HANDLE XBoxhTriggerEvent;
static int XBoxThreadState;
static bool XYWAITSTOP;
static bool ZWAITSTOP;
XINPUT_STATE _controllerState;
int _controllerNum;
public:
CXBOXController(int playerNumber);
~CXBOXController();
void CXBOXController::XBoxControllerInit(void);
void CXBOXController::XBoxControllerExit(void);
bool IsConnected();
XINPUT_STATE GetState();
void Vibrate(int leftVal = 0, int rightVal = 0);
static unsigned __stdcall g_XBox_Thread(LPVOID pThis);
};
@@ -362,6 +362,7 @@
<ClCompile Include="So7_Util_Program.cpp" />
<ClCompile Include="SO7_Verfication_Algorithm.cpp" />
<ClCompile Include="SO7_VolComp.cpp" />
<ClCompile Include="So7_XBoxController.cpp" />
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
@@ -443,6 +444,7 @@
<ClInclude Include="So7_Util_Program.h" />
<ClInclude Include="SO7_Verfication_Algorithm.h" />
<ClInclude Include="SO7_VolComp.h" />
<ClInclude Include="So7_XBoxController.h" />
<ClInclude Include="STATUS_MOTION.H" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
@@ -193,6 +193,9 @@
<ClCompile Include="Keyence_LKG5000_Ethernet.cpp">
<Filter>Sources Files</Filter>
</ClCompile>
<ClCompile Include="So7_XBoxController.cpp">
<Filter>Sources Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CaptureDataDlg.h">
@@ -420,6 +423,9 @@
<ClInclude Include="Keyence_LKG5000_Ethernet.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="So7_XBoxController.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="ReadMe.txt" />