-
Notifications
You must be signed in to change notification settings - Fork 0
/
xbox_controller.cpp
executable file
·200 lines (165 loc) · 5.17 KB
/
xbox_controller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "xbox_controller.h"
using namespace std;
const int XboxController::buttonMask[14] = {
XINPUT_GAMEPAD_DPAD_UP,
XINPUT_GAMEPAD_DPAD_DOWN,
XINPUT_GAMEPAD_DPAD_LEFT,
XINPUT_GAMEPAD_DPAD_RIGHT,
XINPUT_GAMEPAD_START,
XINPUT_GAMEPAD_BACK,
XINPUT_GAMEPAD_LEFT_THUMB,
XINPUT_GAMEPAD_RIGHT_THUMB,
XINPUT_GAMEPAD_LEFT_SHOULDER,
XINPUT_GAMEPAD_RIGHT_SHOULDER,
XINPUT_GAMEPAD_A,
XINPUT_GAMEPAD_B,
XINPUT_GAMEPAD_X,
XINPUT_GAMEPAD_Y
};
//*************************
// Constructor functions
//*************************
XboxController::XboxController(){
XInputEnable(true);
SetDeadZone(DEFAULT_DEADZONE, DEFAULT_DEADZONE);
}
XboxController::XboxController(DWORD controller_num){
if (controller_num >= 0 && controller_num <= 3){
index = controller_num;
}
else {
cout << "CONSTRUCTION_ERROR: Invalid Index." << endl;
Sleep(10000);
}
XInputEnable(true);
SetDeadZone(DEFAULT_DEADZONE, DEFAULT_DEADZONE);
}
//**************************************
// Functions for Battery Information
//**************************************
void XboxController::PrintBatteryInform(){
XInputGetBatteryInformation(index, BATTERY_DEVTYPE_GAMEPAD, &BatteryInformation);
switch (BatteryInformation.BatteryLevel)
{
case BATTERY_LEVEL_EMPTY: cout << "Battery level empty " << endl;
break;
case BATTERY_LEVEL_LOW: cout << "Battery level low " << endl;
break;
case BATTERY_LEVEL_MEDIUM: cout << "Battery level medium " << endl;
break;
case BATTERY_LEVEL_FULL: cout << "Battery level full" << endl;
}
Sleep(3000);
}
//**************************************
int XboxController::GetBatteryInform(){
XInputGetBatteryInformation(index, BATTERY_DEVTYPE_GAMEPAD, &BatteryInformation);
return BatteryInformation.BatteryLevel;
}
//******************************
// Set gamepad's status
//******************************
void XboxController::SetPushButtons(){
if (XInputGetState(index, &State) == ERROR_SUCCESS){
for (int i = 0; i < BUTTON_SIZE; i++){
pushButtons[i] = State.Gamepad.wButtons & buttonMask[i];
}
}
else {
cout << "SetPushButtons_ERROR: Controller_" << index + 1 << " is not connected." << endl;
}
}
//****************************************************
// Get Packet Number, use for check any in input
//****************************************************
DWORD XboxController::GetPacketNumber(){
return State.dwPacketNumber;
}
//*************************************************
// Functions for getting status of push buttons
//*************************************************
void XboxController::PrintPushButtons(){
for (int i = 0; i < BUTTON_SIZE; i++){
cout << pushButtons[i] << " ";
}
cout << endl;
}
//*************************************************
bool XboxController::GetPushButton(int buttonName){
return pushButtons[buttonName];
}
//********************************************
// Functions for getting position of thumbs
//********************************************
COORDINATE XboxController::GetStick(int side){
COORDINATE stick;
if (side == LEFT_SIDE){
stick.axisX = State.Gamepad.sThumbLX;
stick.axisY = State.Gamepad.sThumbLY;
}
else if (side == RIGHT_SIDE){
stick.axisX = State.Gamepad.sThumbRX;
stick.axisY = State.Gamepad.sThumbRY;
}
else {
cout << "GetThumb_ERROR: Invalid Parameter." << endl;
}
if (stick.axisX >= -deadZone.axisX && stick.axisX <= deadZone.axisX)
stick.axisX = 0;
if (stick.axisY >= -deadZone.axisY && stick.axisY <= deadZone.axisY)
stick.axisY = 0;
return stick;
}
//****************************************************************
void XboxController::SetDeadZone(int x, int y) {
if (x >= 0)
deadZone.axisX = x;
else
deadZone.axisX = -x;
if ( y >= 0 )
deadZone.axisY = y;
else
deadZone.axisY = -y;
}
//*************************************
// Function for get value of a Trigger
//*************************************
int XboxController::GetTrigger(int side){
if (side == LEFT_SIDE){
return State.Gamepad.bLeftTrigger;
}
else if (side == RIGHT_SIDE){
return State.Gamepad.bRightTrigger;
}
else {
cout << "GetTrigger_ERROR: Invalid parameter." << endl;
return 0;
}
}
//******************
// Set vibrations
//******************
void XboxController::SetVibration(WORD leftMotor, WORD rightMotor){
if ((leftMotor >= 0 && leftMotor <= MAX_MOTOR_SPEED) &&
(rightMotor >= 0 && rightMotor <= MAX_MOTOR_SPEED)){
Vibration.wLeftMotorSpeed = leftMotor;
Vibration.wRightMotorSpeed = rightMotor;
}
else {
cout << "SetVibration_ERROR: Invalid parameter." << endl;
}
if (XInputSetState(index, &Vibration) != ERROR_SUCCESS){
cout << "SetVibration_ERROR: Controller_" << index+1 <<" is not connected." << endl;
}
/*
Vibration.wLeftMotorSpeed = leftMotor;
Vibration.wRightMotorSpeed = rightMotor;
XInputSetState(index, &Vibration);
*/
}
//*********************************************
// Function to get Gamepad index
//*********************************************
int XboxController::GetIndex(){
return index;
}