blob: 0922359bf36a79792161764c32e6cc5531de2852 [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#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
17class DriverStation;
18
19/**
20 * Handle input from standard Joysticks connected to the Driver Station.
21 * This class handles standard input that comes from the Driver Station. Each
22 * time a value is requested
23 * the most recent value is returned. There is a single class instance for each
24 * joystick and the mapping
25 * of ports to hardware buttons depends on the code in the driver station.
26 */
27class 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