Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 2 | /* Copyright (c) FIRST 2008-2016. All Rights Reserved. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #ifndef JOYSTICK_H_ |
| 9 | #define JOYSTICK_H_ |
| 10 | |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 11 | #include <memory> |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 12 | #include "GenericHID.h" |
| 13 | #include "ErrorBase.h" |
| 14 | |
| 15 | class DriverStation; |
| 16 | |
| 17 | /** |
| 18 | * Handle input from standard Joysticks connected to the Driver Station. |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 19 | * This class handles standard input that comes from the Driver Station. Each time a value is requested |
| 20 | * the most recent value is returned. There is a single class instance for each joystick and the mapping |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 21 | * of ports to hardware buttons depends on the code in the driver station. |
| 22 | */ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 23 | class Joystick : public GenericHID, public ErrorBase |
| 24 | { |
| 25 | public: |
| 26 | static const uint32_t kDefaultXAxis = 1; |
| 27 | static const uint32_t kDefaultYAxis = 2; |
| 28 | static const uint32_t kDefaultZAxis = 3; |
| 29 | static const uint32_t kDefaultTwistAxis = 4; |
| 30 | static const uint32_t kDefaultThrottleAxis = 3; |
| 31 | typedef enum |
| 32 | { |
| 33 | kXAxis, kYAxis, kZAxis, kTwistAxis, kThrottleAxis, kNumAxisTypes |
| 34 | } AxisType; |
| 35 | static const uint32_t kDefaultTriggerButton = 1; |
| 36 | static const uint32_t kDefaultTopButton = 2; |
| 37 | typedef enum |
| 38 | { |
| 39 | kTriggerButton, kTopButton, kNumButtonTypes |
| 40 | } ButtonType; |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 41 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 42 | explicit Joystick(uint32_t port); |
| 43 | Joystick(uint32_t port, uint32_t numAxisTypes, uint32_t numButtonTypes); |
| 44 | virtual ~Joystick() = default; |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 45 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 46 | Joystick(const Joystick&) = delete; |
| 47 | Joystick& operator=(const Joystick&) = delete; |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 48 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 49 | uint32_t GetAxisChannel(AxisType axis); |
| 50 | void SetAxisChannel(AxisType axis, uint32_t channel); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 51 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 52 | virtual float GetX(JoystickHand hand = kRightHand) const override; |
| 53 | virtual float GetY(JoystickHand hand = kRightHand) const override; |
| 54 | virtual float GetZ() const override; |
| 55 | virtual float GetTwist() const override; |
| 56 | virtual float GetThrottle() const override; |
| 57 | virtual float GetAxis(AxisType axis) const; |
| 58 | float GetRawAxis(uint32_t axis) const override; |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 59 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 60 | virtual bool GetTrigger(JoystickHand hand = kRightHand) const override; |
| 61 | virtual bool GetTop(JoystickHand hand = kRightHand) const override; |
| 62 | virtual bool GetBumper(JoystickHand hand = kRightHand) const override; |
| 63 | virtual bool GetRawButton(uint32_t button) const override; |
| 64 | virtual int GetPOV(uint32_t pov = 1) const override; |
| 65 | bool GetButton(ButtonType button) const; |
| 66 | static Joystick* GetStickForPort(uint32_t port); |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 67 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 68 | virtual float GetMagnitude() const; |
| 69 | virtual float GetDirectionRadians() const; |
| 70 | virtual float GetDirectionDegrees() const; |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 71 | |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 72 | private: |
| 73 | DriverStation &m_ds; |
| 74 | uint32_t m_port; |
| 75 | std::unique_ptr<uint32_t[]> m_axes; |
| 76 | std::unique_ptr<uint32_t[]> m_buttons; |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | #endif |