WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
Joystick.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef JOYSTICK_H_
9 #define JOYSTICK_H_
10 
11 #include <cstdint>
12 #include <memory>
13 #include <vector>
14 #include "GenericHID.h"
15 #include "ErrorBase.h"
16 
17 class DriverStation;
18 
27 class Joystick : public GenericHID, public ErrorBase {
28  public:
29  static const uint32_t kDefaultXAxis = 0;
30  static const uint32_t kDefaultYAxis = 1;
31  static const uint32_t kDefaultZAxis = 2;
32  static const uint32_t kDefaultTwistAxis = 2;
33  static const uint32_t kDefaultThrottleAxis = 3;
34  typedef enum {
35  kXAxis,
36  kYAxis,
37  kZAxis,
38  kTwistAxis,
39  kThrottleAxis,
40  kNumAxisTypes
41  } AxisType;
42  static const uint32_t kDefaultTriggerButton = 1;
43  static const uint32_t kDefaultTopButton = 2;
44  typedef enum { kTriggerButton, kTopButton, kNumButtonTypes } ButtonType;
45  typedef enum { kLeftRumble, kRightRumble } RumbleType;
46  typedef enum {
47  kUnknown = -1,
48  kXInputUnknown = 0,
49  kXInputGamepad = 1,
50  kXInputWheel = 2,
51  kXInputArcadeStick = 3,
52  kXInputFlightStick = 4,
53  kXInputDancePad = 5,
54  kXInputGuitar = 6,
55  kXInputGuitar2 = 7,
56  kXInputDrumKit = 8,
57  kXInputGuitar3 = 11,
58  kXInputArcadePad = 19,
59  kHIDJoystick = 20,
60  kHIDGamepad = 21,
61  kHIDDriving = 22,
62  kHIDFlight = 23,
63  kHID1stPerson = 24
64  } HIDType;
65  explicit Joystick(uint32_t port);
66  Joystick(uint32_t port, uint32_t numAxisTypes, uint32_t numButtonTypes);
67  virtual ~Joystick() = default;
68 
69  Joystick(const Joystick&) = delete;
70  Joystick& operator=(const Joystick&) = delete;
71 
72  uint32_t GetAxisChannel(AxisType axis) const;
73  void SetAxisChannel(AxisType axis, uint32_t channel);
74 
75  virtual float GetX(JoystickHand hand = kRightHand) const override;
76  virtual float GetY(JoystickHand hand = kRightHand) const override;
77  virtual float GetZ() const override;
78  virtual float GetTwist() const override;
79  virtual float GetThrottle() const override;
80  virtual float GetAxis(AxisType axis) const;
81  float GetRawAxis(uint32_t axis) const override;
82 
83  virtual bool GetTrigger(JoystickHand hand = kRightHand) const override;
84  virtual bool GetTop(JoystickHand hand = kRightHand) const override;
85  virtual bool GetBumper(JoystickHand hand = kRightHand) const override;
86  virtual bool GetRawButton(uint32_t button) const override;
87  virtual int GetPOV(uint32_t pov = 0) const override;
88  bool GetButton(ButtonType button) const;
89  static Joystick *GetStickForPort(uint32_t port);
90 
91  virtual float GetMagnitude() const;
92  virtual float GetDirectionRadians() const;
93  virtual float GetDirectionDegrees() const;
94 
95  bool GetIsXbox() const;
96  Joystick::HIDType GetType() const;
97  std::string GetName() const;
98  int GetAxisType(uint8_t axis) const;
99 
100  int GetAxisCount() const;
101  int GetButtonCount() const;
102  int GetPOVCount() const;
103 
104  void SetRumble(RumbleType type, float value);
105  void SetOutput(uint8_t outputNumber, bool value);
106  void SetOutputs(uint32_t value);
107 
108  private:
109  DriverStation &m_ds;
110  uint32_t m_port;
111  std::vector<uint32_t> m_axes;
112  std::vector<uint32_t> m_buttons;
113  uint32_t m_outputs = 0;
114  uint16_t m_leftRumble = 0;
115  uint16_t m_rightRumble = 0;
116 };
117 
118 #endif
virtual float GetMagnitude() const
Get the magnitude of the direction vector formed by the joystick's current position relative to its o...
Definition: Joystick.cpp:312
bool GetButton(ButtonType button) const
Get buttons based on an enumerated type.
Definition: Joystick.cpp:226
void SetOutputs(uint32_t value)
Set all HID output values for the joystick.
Definition: Joystick.cpp:373
virtual float GetY(JoystickHand hand=kRightHand) const override
Get the Y value of the joystick.
Definition: Joystick.cpp:97
int GetButtonCount() const
Get the number of axis for a joystick.
Definition: Joystick.cpp:277
virtual int GetPOV(uint32_t pov=0) const override
Get the state of a POV on the joystick.
Definition: Joystick.cpp:214
std::string GetName() const
Get the name of the joystick.
Definition: Joystick.cpp:265
bool GetIsXbox() const
Get the value of isXbox for the joystick.
Definition: Joystick.cpp:249
Base class for most objects.
Definition: ErrorBase.h:66
virtual float GetDirectionDegrees() const
Get the direction of the vector formed by the joystick and its origin in degrees. ...
Definition: Joystick.cpp:333
virtual float GetTwist() const override
Get the twist value of the current joystick.
Definition: Joystick.cpp:111
void SetRumble(RumbleType type, float value)
Set the rumble output for the joystick.
Definition: Joystick.cpp:344
int GetPOVCount() const
Get the number of axis for a joystick.
Definition: Joystick.cpp:286
Joystick::HIDType GetType() const
Get the HID type of the controller.
Definition: Joystick.cpp:256
virtual bool GetTrigger(JoystickHand hand=kRightHand) const override
Read the state of the trigger on the joystick.
Definition: Joystick.cpp:168
Joystick(uint32_t port)
Construct an instance of a joystick.
Definition: Joystick.cpp:31
virtual float GetX(JoystickHand hand=kRightHand) const override
Get the X value of the joystick.
Definition: Joystick.cpp:87
virtual bool GetRawButton(uint32_t button) const override
Get the button value (starting at button 1)
Definition: Joystick.cpp:204
Handle input from standard Joysticks connected to the Driver Station.
Definition: Joystick.h:27
int GetAxisCount() const
Get the number of axis for a joystick.
Definition: Joystick.cpp:242
virtual float GetZ() const override
Get the Z value of the current joystick.
Definition: Joystick.cpp:105
void SetOutput(uint8_t outputNumber, bool value)
Set a single HID output value for the joystick.
Definition: Joystick.cpp:362
uint32_t GetAxisChannel(AxisType axis) const
Get the channel currently associated with the specified axis.
Definition: Joystick.cpp:294
virtual float GetThrottle() const override
Get the throttle value of the current joystick.
Definition: Joystick.cpp:117
virtual bool GetTop(JoystickHand hand=kRightHand) const override
Read the state of the top button on the joystick.
Definition: Joystick.cpp:181
virtual float GetAxis(AxisType axis) const
For the current joystick, return the axis determined by the argument.
Definition: Joystick.cpp:141
virtual bool GetBumper(JoystickHand hand=kRightHand) const override
This is not supported for the Joystick.
Definition: Joystick.cpp:189
virtual float GetDirectionRadians() const
Get the direction of the vector formed by the joystick and its origin in radians. ...
Definition: Joystick.cpp:322
GenericHID Interface.
Definition: GenericHID.h:14
void SetAxisChannel(AxisType axis, uint32_t channel)
Set the channel associated with a specified axis.
Definition: Joystick.cpp:302
float GetRawAxis(uint32_t axis) const override
Get the value of the axis.
Definition: Joystick.cpp:127
Provide access to the network communication data to / from the Driver Station.
Definition: DriverStation.h:27