-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamepad.cpp
More file actions
148 lines (132 loc) · 3.13 KB
/
Gamepad.cpp
File metadata and controls
148 lines (132 loc) · 3.13 KB
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
#include "Gamepad.h"
#include "DriverStation.h"
#include "Utility.h"
//#include "WPIStatus.h"
/**
* Construct an instance of a Gamepad.
*
* @param port The port on the driver station that the gamepad is plugged into.
*/
Gamepad::Gamepad(UINT32 port)
{
a_port = port;
ap_ds = DriverStation::GetInstance();
}
Gamepad::~Gamepad()
{
}
/**
* Get the X value of the left analog stick.
*/
float Gamepad::GetLeftX()
{
return GetRawAxis(kLeftXAxisNum);
}
/**
* Get the Y value of the left analog stick.
*/
float Gamepad::GetLeftY()
{
return GetRawAxis(kLeftYAxisNum);
}
/**
* Get the X value of the right analog stick.
*/
float Gamepad::GetRightX()
{
return GetRawAxis(kRightXAxisNum);
}
/**
* Get the Y value of the right analog stick.
*/
float Gamepad::GetRightY()
{
return GetRawAxis(kRightYAxisNum);
}
/**
* Get the value of the axis.
*
* @param axis The axis to read [1-6].
* @return The value of the axis.
*/
float Gamepad::GetRawAxis(UINT32 axis)
{
return ap_ds->GetStickAxis(a_port, axis);
}
/**
* Return the axis determined by the argument.
*
* This is for cases where the gamepad axis is returned programatically,
* otherwise one of the previous functions would be preferable (for example
* GetLeftX()).
*
* @param axis The axis to read.
* @return The value of the axis.
*/
float Gamepad::GetAxis(AxisType axis)
{
switch(axis)
{
case kLeftXAxis: return GetLeftX();
case kLeftYAxis: return GetLeftY();
case kRightXAxis: return GetRightX();
case kRightYAxis: return GetRightY();
default:
//wpi_fatal(BadJoystickAxis);
return 0.0;
}
}
/**
* Get the button value for buttons 1 through 12.
*
* The buttons are returned in a single 16 bit value with one bit representing
* the state of each button. The appropriate button is returned as a boolean
* value.
*
* @param button The button number to be read.
* @return The state of the button.
**/
bool Gamepad::GetNumberedButton(UINT32 button)
{
return ((0x1 << (button-1)) & ap_ds->GetStickButtons(a_port)) != 0;
}
/**
* Gets whether or not the left analog stick is depressed.
*
* @return The state of the left analog stick button.
*/
bool Gamepad::GetLeftPush()
{
return GetNumberedButton(kLeftAnalogStickButton);
}
/**
* Gets whether or not the right analog stick is depressed.
*
* @return The state of the right analog stick button.
*/
bool Gamepad::GetRightPush()
{
return GetNumberedButton(kRightAnalogStickButton);
}
Gamepad::DPadDirection Gamepad::GetDPad()
{
float x = GetRawAxis(kDPadXAxisNum);
float y = GetRawAxis(kDPadYAxisNum);
if (x < -0.5 && y < -0.5)
return kUpLeft;
if (x < -0.5 && y > 0.5)
return kDownLeft;
if (x > 0.5 && y > 0.5)
return kDownRight;
if (x > 0.5 && y < -0.5)
return kUpRight;
if (y < -0.5)
return kUp;
if (x < -0.5)
return kLeft;
if (y > 0.5)
return kDown;
if (x > 0.5)
return kRight;
return kCenter;
}