blob: a7a299ced8f80e4b6f2609c2802a43daf7a694a2 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. All Rights Reserved.
3 */
4/* Open Source Software - may be modified and shared by FRC teams. The code */
5/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
6/*----------------------------------------------------------------------------*/
7
8#include "Joystick.h"
9#include "DriverStation.h"
10//#include "NetworkCommunication/UsageReporting.h"
11#include "WPIErrors.h"
12#include <math.h>
13#include <string.h>
14
15const uint32_t Joystick::kDefaultXAxis;
16const uint32_t Joystick::kDefaultYAxis;
17const uint32_t Joystick::kDefaultZAxis;
18const uint32_t Joystick::kDefaultTwistAxis;
19const uint32_t Joystick::kDefaultThrottleAxis;
20const uint32_t Joystick::kDefaultTriggerButton;
21const uint32_t Joystick::kDefaultTopButton;
22static Joystick *joysticks[DriverStation::kJoystickPorts];
23static bool joySticksInitialized = false;
24
25/**
26 * Construct an instance of a joystick.
27 * The joystick index is the usb port on the drivers station.
28 *
29 * @param port The port on the driver station that the joystick is plugged into
30 * (0-5).
31 */
32Joystick::Joystick(uint32_t port)
33 : Joystick(port, kNumAxisTypes, kNumButtonTypes) {
34 m_axes[kXAxis] = kDefaultXAxis;
35 m_axes[kYAxis] = kDefaultYAxis;
36 m_axes[kZAxis] = kDefaultZAxis;
37 m_axes[kTwistAxis] = kDefaultTwistAxis;
38 m_axes[kThrottleAxis] = kDefaultThrottleAxis;
39
40 m_buttons[kTriggerButton] = kDefaultTriggerButton;
41 m_buttons[kTopButton] = kDefaultTopButton;
42
43 HALReport(HALUsageReporting::kResourceType_Joystick, port);
44}
45
46/**
47 * Version of the constructor to be called by sub-classes.
48 *
49 * This constructor allows the subclass to configure the number of constants
50 * for axes and buttons.
51 *
52 * @param port The port on the driver station that the joystick is plugged into.
53 * @param numAxisTypes The number of axis types in the enum.
54 * @param numButtonTypes The number of button types in the enum.
55 */
56Joystick::Joystick(uint32_t port, uint32_t numAxisTypes,
57 uint32_t numButtonTypes)
58 : m_ds(DriverStation::GetInstance()),
59 m_port(port),
60 m_axes(numAxisTypes),
61 m_buttons(numButtonTypes) {
62 if (!joySticksInitialized) {
63 for (auto& joystick : joysticks) joystick = nullptr;
64 joySticksInitialized = true;
65 }
66 if (m_port >= DriverStation::kJoystickPorts) {
67 wpi_setWPIError(BadJoystickIndex);
68 } else {
69 joysticks[m_port] = this;
70 }
71}
72
73Joystick *Joystick::GetStickForPort(uint32_t port) {
74 Joystick *stick = joysticks[port];
75 if (stick == nullptr) {
76 stick = new Joystick(port);
77 joysticks[port] = stick;
78 }
79 return stick;
80}
81
82/**
83 * Get the X value of the joystick.
84 * This depends on the mapping of the joystick connected to the current port.
85 * @param hand This parameter is ignored for the Joystick class and is only here
86 * to complete the GenericHID interface.
87 */
88float Joystick::GetX(JoystickHand hand) const {
89 return GetRawAxis(m_axes[kXAxis]);
90}
91
92/**
93 * Get the Y value of the joystick.
94 * This depends on the mapping of the joystick connected to the current port.
95 * @param hand This parameter is ignored for the Joystick class and is only here
96 * to complete the GenericHID interface.
97 */
98float Joystick::GetY(JoystickHand hand) const {
99 return GetRawAxis(m_axes[kYAxis]);
100}
101
102/**
103 * Get the Z value of the current joystick.
104 * This depends on the mapping of the joystick connected to the current port.
105 */
106float Joystick::GetZ() const { return GetRawAxis(m_axes[kZAxis]); }
107
108/**
109 * Get the twist value of the current joystick.
110 * This depends on the mapping of the joystick connected to the current port.
111 */
112float Joystick::GetTwist() const { return GetRawAxis(m_axes[kTwistAxis]); }
113
114/**
115 * Get the throttle value of the current joystick.
116 * This depends on the mapping of the joystick connected to the current port.
117 */
118float Joystick::GetThrottle() const {
119 return GetRawAxis(m_axes[kThrottleAxis]);
120}
121
122/**
123 * Get the value of the axis.
124 *
125 * @param axis The axis to read, starting at 0.
126 * @return The value of the axis.
127 */
128float Joystick::GetRawAxis(uint32_t axis) const {
129 return m_ds.GetStickAxis(m_port, axis);
130}
131
132/**
133 * For the current joystick, return the axis determined by the argument.
134 *
135 * This is for cases where the joystick axis is returned programatically,
136 * otherwise one of the
137 * previous functions would be preferable (for example GetX()).
138 *
139 * @param axis The axis to read.
140 * @return The value of the axis.
141 */
142float Joystick::GetAxis(AxisType axis) const {
143 switch (axis) {
144 case kXAxis:
145 return this->GetX();
146 case kYAxis:
147 return this->GetY();
148 case kZAxis:
149 return this->GetZ();
150 case kTwistAxis:
151 return this->GetTwist();
152 case kThrottleAxis:
153 return this->GetThrottle();
154 default:
155 wpi_setWPIError(BadJoystickAxis);
156 return 0.0;
157 }
158}
159
160/**
161 * Read the state of the trigger on the joystick.
162 *
163 * Look up which button has been assigned to the trigger and read its state.
164 *
165 * @param hand This parameter is ignored for the Joystick class and is only here
166 * to complete the GenericHID interface.
167 * @return The state of the trigger.
168 */
169bool Joystick::GetTrigger(JoystickHand hand) const {
170 return GetRawButton(m_buttons[kTriggerButton]);
171}
172
173/**
174 * Read the state of the top button on the joystick.
175 *
176 * Look up which button has been assigned to the top and read its state.
177 *
178 * @param hand This parameter is ignored for the Joystick class and is only here
179 * to complete the GenericHID interface.
180 * @return The state of the top button.
181 */
182bool Joystick::GetTop(JoystickHand hand) const {
183 return GetRawButton(m_buttons[kTopButton]);
184}
185
186/**
187 * This is not supported for the Joystick.
188 * This method is only here to complete the GenericHID interface.
189 */
190bool Joystick::GetBumper(JoystickHand hand) const {
191 // Joysticks don't have bumpers.
192 return false;
193}
194
195/**
196 * Get the button value (starting at button 1)
197 *
198 * The buttons are returned in a single 16 bit value with one bit representing
199 * the state
200 * of each button. The appropriate button is returned as a boolean value.
201 *
202 * @param button The button number to be read (starting at 1)
203 * @return The state of the button.
204 **/
205bool Joystick::GetRawButton(uint32_t button) const {
206 return m_ds.GetStickButton(m_port, button);
207}
208
209/**
210 * Get the state of a POV on the joystick.
211 *
212 * @param pov The index of the POV to read (starting at 0)
213 * @return the angle of the POV in degrees, or -1 if the POV is not pressed.
214 */
215int Joystick::GetPOV(uint32_t pov) const {
216 return m_ds.GetStickPOV(m_port, pov);
217}
218
219/**
220 * Get buttons based on an enumerated type.
221 *
222 * The button type will be looked up in the list of buttons and then read.
223 *
224 * @param button The type of button to read.
225 * @return The state of the button.
226 */
227bool Joystick::GetButton(ButtonType button) const {
228 switch (button) {
229 case kTriggerButton:
230 return GetTrigger();
231 case kTopButton:
232 return GetTop();
233 default:
234 return false;
235 }
236}
237
238/**
239 * Get the number of axis for a joystick
240 *
241 * @return the number of axis for the current joystick
242 */
243int Joystick::GetAxisCount() const { return m_ds.GetStickAxisCount(m_port); }
244
245/**
246 * Get the value of isXbox for the joystick.
247 *
248 * @return A boolean that is true if the joystick is an xbox controller.
249 */
250bool Joystick::GetIsXbox() const { return m_ds.GetJoystickIsXbox(m_port); }
251
252/**
253 * Get the HID type of the controller.
254 *
255 * @return the HID type of the controller.
256 */
257Joystick::HIDType Joystick::GetType() const {
258 return static_cast<HIDType>(m_ds.GetJoystickType(m_port));
259}
260
261/**
262 * Get the name of the joystick.
263 *
264 * @return the name of the controller.
265 */
266std::string Joystick::GetName() const { return m_ds.GetJoystickName(m_port); }
267
268// int Joystick::GetAxisType(uint8_t axis) const
269//{
270// return m_ds.GetJoystickAxisType(m_port, axis);
271//}
272
273/**
274 * Get the number of axis for a joystick
275 *
276* @return the number of buttons on the current joystick
277 */
278int Joystick::GetButtonCount() const {
279 return m_ds.GetStickButtonCount(m_port);
280}
281
282/**
283 * Get the number of axis for a joystick
284 *
285 * @return then umber of POVs for the current joystick
286 */
287int Joystick::GetPOVCount() const { return m_ds.GetStickPOVCount(m_port); }
288
289/**
290 * Get the channel currently associated with the specified axis.
291 *
292 * @param axis The axis to look up the channel for.
293 * @return The channel fr the axis.
294 */
295uint32_t Joystick::GetAxisChannel(AxisType axis) const { return m_axes[axis]; }
296
297/**
298 * Set the channel associated with a specified axis.
299 *
300 * @param axis The axis to set the channel for.
301 * @param channel The channel to set the axis to.
302 */
303void Joystick::SetAxisChannel(AxisType axis, uint32_t channel) {
304 m_axes[axis] = channel;
305}
306
307/**
308 * Get the magnitude of the direction vector formed by the joystick's
309 * current position relative to its origin
310 *
311 * @return The magnitude of the direction vector
312 */
313float Joystick::GetMagnitude() const {
314 return sqrt(pow(GetX(), 2) + pow(GetY(), 2));
315}
316
317/**
318 * Get the direction of the vector formed by the joystick and its origin
319 * in radians
320 *
321 * @return The direction of the vector in radians
322 */
323float Joystick::GetDirectionRadians() const { return atan2(GetX(), -GetY()); }
324
325/**
326 * Get the direction of the vector formed by the joystick and its origin
327 * in degrees
328 *
329 * uses acos(-1) to represent Pi due to absence of readily accessible Pi
330 * constant in C++
331 *
332 * @return The direction of the vector in degrees
333 */
334float Joystick::GetDirectionDegrees() const {
335 return (180 / acos(-1)) * GetDirectionRadians();
336}
337
338/**
339 * Set the rumble output for the joystick. The DS currently supports 2 rumble
340 * values,
341 * left rumble and right rumble
342 * @param type Which rumble value to set
343 * @param value The normalized value (0 to 1) to set the rumble to
344 */
345void Joystick::SetRumble(RumbleType type, float value) {
346 if (value < 0)
347 value = 0;
348 else if (value > 1)
349 value = 1;
350 if (type == kLeftRumble)
351 m_leftRumble = value * 65535;
352 else
353 m_rightRumble = value * 65535;
354 HALSetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble);
355}
356
357/**
358 * Set a single HID output value for the joystick.
359 * @param outputNumber The index of the output to set (1-32)
360 * @param value The value to set the output to
361 */
362
363void Joystick::SetOutput(uint8_t outputNumber, bool value) {
364 m_outputs =
365 (m_outputs & ~(1 << (outputNumber - 1))) | (value << (outputNumber - 1));
366
367 HALSetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble);
368}
369
370/**
371 * Set all HID output values for the joystick.
372 * @param value The 32 bit output value (1 bit for each output)
373 */
374void Joystick::SetOutputs(uint32_t value) {
375 m_outputs = value;
376 HALSetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble);
377}