From 50542c4435ceafca7628a1fe3dd7c7a37ae5df34 Mon Sep 17 00:00:00 2001 From: LI Bin Date: Fri, 7 Mar 2014 17:40:47 +0800 Subject: [PATCH] Add XBoxController --- .../Tools/UsbUtility/UsbUtil/SO7_UtilDlg.cpp | 11 + .../UsbUtility/UsbUtil/So7_XBoxController.cpp | 457 ++++++++++++++++++ .../UsbUtility/UsbUtil/So7_XBoxController.h | 37 ++ .../Tools/UsbUtility/UsbUtil/Usb_Util.vcxproj | 2 + .../UsbUtil/Usb_Util.vcxproj.filters | 6 + 5 files changed, 513 insertions(+) create mode 100644 PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/So7_XBoxController.cpp create mode 100644 PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/So7_XBoxController.h diff --git a/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/SO7_UtilDlg.cpp b/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/SO7_UtilDlg.cpp index b27a788..249b73f 100644 --- a/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/SO7_UtilDlg.cpp +++ b/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/SO7_UtilDlg.cpp @@ -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); diff --git a/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/So7_XBoxController.cpp b/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/So7_XBoxController.cpp new file mode 100644 index 0000000..2e8dc8c --- /dev/null +++ b/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/So7_XBoxController.cpp @@ -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)_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)_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_send_cmd_SO7_CMD_MOVE_XY((char)((RX - 128) / 8160 + 1), (char)((RY - 128) / 8160 +1)); + XYWAITSTOP = true; + } + else if(RX>128 && abs(RY)_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_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); +} \ No newline at end of file diff --git a/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/So7_XBoxController.h b/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/So7_XBoxController.h new file mode 100644 index 0000000..1d5d6af --- /dev/null +++ b/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/So7_XBoxController.h @@ -0,0 +1,37 @@ +#include +#include + +#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); +}; \ No newline at end of file diff --git a/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/Usb_Util.vcxproj b/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/Usb_Util.vcxproj index 7a5bb89..87dcb8b 100644 --- a/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/Usb_Util.vcxproj +++ b/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/Usb_Util.vcxproj @@ -362,6 +362,7 @@ + Create Create @@ -443,6 +444,7 @@ + diff --git a/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/Usb_Util.vcxproj.filters b/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/Usb_Util.vcxproj.filters index 69df0b0..dfb5643 100644 --- a/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/Usb_Util.vcxproj.filters +++ b/PcDmis/Base/Interfac/Msi/Hsi/Tools/UsbUtility/UsbUtil/Usb_Util.vcxproj.filters @@ -193,6 +193,9 @@ Sources Files + + Sources Files + @@ -420,6 +423,9 @@ Header Files + + Header Files +