-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gamepad.cpp
448 lines (436 loc) · 11.2 KB
/
Gamepad.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#include "Gamepad.h"
#include "DriverStation.h"
#include "Utility.h"
#include "WPIStatus.h"
#include <math.h>
static Gamepad *gamepads[DriverStation::kJoystickPorts];
static bool gamePadsInitialized = false;
/**
* Construct an instance of a gamepad.
*
* @param port The USB port on the driver station to which the gamepad is attached.
*/
Gamepad::Gamepad(unsigned port)
: m_ds (NULL)
, m_port (port)
, m_axes (NULL)
, m_buttons (NULL)
{
InitGamepad(kNumAxisTypes, kNumButtonTypes);
m_axes[kLeftXAxis] = kDefaultLeftXAxis;
m_axes[kLeftYAxis] = kDefaultLeftYAxis;
m_axes[kRightXAxis] = kDefaultRightXAxis;
m_axes[kRightYAxis] = kDefaultRightYAxis;
m_axes[kDpadXAxis] = kDefaultDpadXAxis;
m_axes[kDpadYAxis] = kDefaultDpadYAxis;
m_buttons[kButton01] = kDefaultButton01;
m_buttons[kButton02] = kDefaultButton02;
m_buttons[kButton03] = kDefaultButton03;
m_buttons[kButton04] = kDefaultButton04;
m_buttons[kButton05] = kDefaultButton05;
m_buttons[kButton06] = kDefaultButton06;
m_buttons[kButton07] = kDefaultButton07;
m_buttons[kButton08] = kDefaultButton08;
m_buttons[kButton09] = kDefaultButton09;
m_buttons[kButton10] = kDefaultButton10;
m_buttons[kButton11] = kDefaultButton11;
m_buttons[kButton12] = kDefaultButton12;
}
/**
* Version of the constructor to be called by sub-classes.
*
* This constructor allows the subclass to configure the number of constants
* for axes and buttons.
*
* @param port The port on the driver station that the gamepad is plugged into.
* @param numAxisTypes The number of axis types in the enum.
* @param numButtonTypes The number of button types in the enum.
*/
Gamepad::Gamepad(unsigned port, unsigned numAxisTypes, unsigned numButtonTypes)
: m_ds (NULL)
, m_port (port)
, m_axes (NULL)
, m_buttons (NULL)
{
InitGamepad(numAxisTypes, numButtonTypes);
}
void Gamepad::InitGamepad(unsigned numAxisTypes, unsigned numButtonTypes)
{
if ( !gamePadsInitialized )
{
for (unsigned i = 0; i < DriverStation::kJoystickPorts; i++)
gamepads[i] = NULL;
gamePadsInitialized = true;
}
gamepads[m_port - 1] = this;
m_ds = DriverStation::GetInstance();
m_axes = new unsigned[numAxisTypes];
m_buttons = new unsigned[numButtonTypes];
}
Gamepad * Gamepad::GetStickForPort(unsigned port)
{
Gamepad *stick = gamepads[port - 1];
if (stick == NULL)
{
stick = new Gamepad(port);
gamepads[port - 1] = stick;
}
return stick;
}
Gamepad::~Gamepad()
{
delete [] m_buttons;
delete [] m_axes;
}
/**
* Get the left joystick X-value.
*
* @return The left joystick X-value of the gamepad.
*/
float Gamepad::GetLeftX(void)
{
return GetRawAxis(m_axes[kLeftXAxis]);
}
/**
* Get the left joystick Y-value.
*
* @return The left joystick Y-value of the gamepad.
*/
float Gamepad::GetLeftY(void)
{
return -GetRawAxis(m_axes[kLeftYAxis]);
}
/**
* Get the right joystick X-value.
*
* @return The right joystick X-value of the gamepad.
*/
float Gamepad::GetRightX(void)
{
return GetRawAxis(m_axes[kRightXAxis]);
}
/**
* Get the right joystick Y-value.
*
* @return The right joystick Y-value of the gamepad.
*/
float Gamepad::GetRightY(void)
{
return -GetRawAxis(m_axes[kRightYAxis]);
}
/**
* Get the dpad X-value.
*
* @return The dpad X-value of the gamepad.
*/
float Gamepad::GetDpadX(void)
{
return GetRawAxis(m_axes[kDpadXAxis]);
}
/**
* Get the dpad Y-value.
*
* @return The dpad Y-value of the gamepad.
*/
float Gamepad::GetDpadY(void)
{
return GetRawAxis(m_axes[kDpadYAxis]);
}
/**
* Get the axis value for axes 1 through 6.
*
* @param axis The axis to read (1-6)
* @return The value of the axis
*/
float Gamepad::GetRawAxis(unsigned axis)
{
return m_ds->GetStickAxis(m_port, axis);
}
/**
* Return the axis value 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
* @todo check signs of gamepad axes
*/
float Gamepad::GetAxis(AxisType axis)
{
switch(axis)
{
case kLeftXAxis: return this->GetLeftX();
case kLeftYAxis: return -this->GetLeftY();
case kRightXAxis: return this->GetRightX();
case kRightYAxis: return -this->GetRightY();
case kDpadXAxis: return this->GetDpadX();
case kDpadYAxis: return this->GetDpadY();
default:
wpi_fatal(BadJoystickAxis);
return 0.0;
}
}
/**
* Read the state of button 1 on the gamepad.
*
* @return The state of the button.
*/
bool Gamepad::GetButton01(void)
{
return GetRawButton(m_buttons[kButton01]);
}
/**
* Read the state of button 2 on the gamepad.
*
* @return The state of the button.
*/
bool Gamepad::GetButton02(void)
{
return GetRawButton(m_buttons[kButton02]);
}
/**
* Read the state of button 3 on the gamepad.
*
* @return The state of the button.
*/
bool Gamepad::GetButton03(void)
{
return GetRawButton(m_buttons[kButton03]);
}
/**
* Read the state of button 4 on the gamepad.
*
* @return The state of the button.
*/
bool Gamepad::GetButton04(void)
{
return GetRawButton(m_buttons[kButton04]);
}
/**
* Read the state of button 5 on the gamepad.
*
* @return The state of the button.
*/
bool Gamepad::GetButton05(void)
{
return GetRawButton(m_buttons[kButton05]);
}
/**
* Read the state of button 6 on the gamepad.
*
* @return The state of the button.
*/
bool Gamepad::GetButton06(void)
{
return GetRawButton(m_buttons[kButton06]);
}
/**
* Read the state of button 7 on the gamepad.
*
* @return The state of the button.
*/
bool Gamepad::GetButton07(void)
{
return GetRawButton(m_buttons[kButton07]);
}
/**
* Read the state of button 8 on the gamepad.
*
* @return The state of the button.
*/
bool Gamepad::GetButton08(void)
{
return GetRawButton(m_buttons[kButton08]);
}
/**
* Read the state of button 9 on the gamepad.
*
* @return The state of the button.
*/
bool Gamepad::GetButton09(void)
{
return GetRawButton(m_buttons[kButton09]);
}
/**
* Read the state of button 10 on the gamepad.
*
* @return The state of the button.
*/
bool Gamepad::GetButton10(void)
{
return GetRawButton(m_buttons[kButton10]);
}
/**
* Read the state of button 11 on the gamepad.
*
* @return The state of the button.
*/
bool Gamepad::GetButton11(void)
{
return GetRawButton(m_buttons[kButton11]);
}
/**
* Read the state of button 12 on the gamepad.
*
* @return The state of the button.
*/
bool Gamepad::GetButton12(void)
{
return GetRawButton(m_buttons[kButton12]);
}
/**
* Get the button value for buttons 1 through 12.
*
* @param button The button number to read (1-12)
* @return The state of the button
*/
bool Gamepad::GetRawButton(unsigned button)
{
return ((0x1 << (button-1)) & m_ds->GetStickButtons(m_port)) != 0;
}
/**
* Get buttons based on an enumerated type.
*
* The button type will be looked up in the list of buttons and then read.
*
* @param button The type of button to read.
* @return The state of the button.
*/
bool Gamepad::GetButton(ButtonType button)
{
switch (button)
{
case kButton01: return GetRawButton( 1 );
case kButton02: return GetRawButton( 2 );
case kButton03: return GetRawButton( 3 );
case kButton04: return GetRawButton( 4 );
case kButton05: return GetRawButton( 5 );
case kButton06: return GetRawButton( 6 );
case kButton07: return GetRawButton( 7 );
case kButton08: return GetRawButton( 8 );
case kButton09: return GetRawButton( 9 );
case kButton10: return GetRawButton( 10 );
case kButton11: return GetRawButton( 11 );
case kButton12: return GetRawButton( 12 );
default:
wpi_assert(false);
return false;
}
}
/**
* Get the channel currently associated with the specified axis.
*
* @param axis The axis to look up the channel for.
* @return The channel fr the axis.
*/
unsigned Gamepad::GetAxisChannel(AxisType axis)
{
return m_axes[axis];
}
/**
* Set the channel associated with a specified axis.
*
* @param axis The axis to set the channel for.
* @param channel The channel to set the axis to.
*/
void Gamepad::SetAxisChannel(AxisType axis, unsigned channel)
{
m_axes[axis] = channel;
}
/**
* Get the magnitude of the direction vector formed by the joystick's
* current position relative to its origin
*
* @return The magnitude of the direction vector
*/
float Gamepad::GetLeftMagnitude(){
return sqrt(pow(GetLeftX(),2) + pow(GetLeftY(),2) );
}
/**
* Get the magnitude of the direction vector formed by the joystick's
* current position relative to its origin
*
* @return The magnitude of the direction vector
*/
float Gamepad::GetRightMagnitude(){
return sqrt(pow(GetRightX(),2) + pow(GetRightY(),2) );
}
/**
* Get the magnitude of the direction vector formed by the joystick's
* current position relative to its origin
*
* @return The magnitude of the direction vector
*/
float Gamepad::GetDpadMagnitude(){
return sqrt(pow(GetDpadX(),2) + pow(GetDpadY(),2) );
}
/**
* Get the direction of the vector formed by the joystick and its origin
* in radians
*
* @return The direction of the vector in radians
*/
float Gamepad::GetLeftDirectionRadians(){
return atan2(GetLeftX(), -GetLeftY());
}
/**
* Get the direction of the vector formed by the joystick and its origin
* in radians
*
* @return The direction of the vector in radians
*/
float Gamepad::GetRightDirectionRadians(){
return atan2(GetRightX(), -GetRightY());
}
/**
* Get the direction of the vector formed by the joystick and its origin
* in radians
*
* @return The direction of the vector in radians
*/
float Gamepad::GetDpadDirectionRadians(){
return atan2(GetDpadX(), -GetDpadY());
}
/**
* Get the direction of the vector formed by the joystick and its origin
* in degrees
*
* uses acos(-1) to represent Pi due to absence of readily accessable Pi
* constant in C++
*
* @return The direction of the vector in degrees
*/
float Gamepad::GetLeftDirectionDegrees(){
return (180/acos(-1))*GetLeftDirectionRadians();
}
/**
* Get the direction of the vector formed by the joystick and its origin
* in degrees
*
* uses acos(-1) to represent Pi due to absence of readily accessable Pi
* constant in C++
*
* @return The direction of the vector in degrees
*/
float Gamepad::GetRightDirectionDegrees(){
return (180/acos(-1))*GetRightDirectionRadians();
}
/**
* Get the direction of the vector formed by the joystick and its origin
* in degrees
*
* uses acos(-1) to represent Pi due to absence of readily accessable Pi
* constant in C++
*
* @return The direction of the vector in degrees
*/
float Gamepad::GetDpadDirectionDegrees(){
return (180/acos(-1))*GetDpadDirectionRadians();
}