blob: e4e46da77ac547534622bceadde85ccf627548b6 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05002/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
Brian Silverman26e4e522015-12-17 01:56:40 -05003/* Open Source Software - may be modified and shared by FRC teams. The code */
Brian Silverman1a675112016-02-20 20:42:49 -05004/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
Brian Silverman26e4e522015-12-17 01:56:40 -05006/*----------------------------------------------------------------------------*/
7
8#ifndef JOYSTICK_H_
9#define JOYSTICK_H_
10
Brian Silverman26e4e522015-12-17 01:56:40 -050011#include <memory>
Brian Silverman26e4e522015-12-17 01:56:40 -050012#include "GenericHID.h"
13#include "ErrorBase.h"
14
15class DriverStation;
16
17/**
18 * Handle input from standard Joysticks connected to the Driver Station.
Brian Silverman1a675112016-02-20 20:42:49 -050019 * 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 Silverman26e4e522015-12-17 01:56:40 -050021 * of ports to hardware buttons depends on the code in the driver station.
22 */
Brian Silverman1a675112016-02-20 20:42:49 -050023class Joystick : public GenericHID, public ErrorBase
24{
25public:
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 Silverman26e4e522015-12-17 01:56:40 -050041
Brian Silverman1a675112016-02-20 20:42:49 -050042 explicit Joystick(uint32_t port);
43 Joystick(uint32_t port, uint32_t numAxisTypes, uint32_t numButtonTypes);
44 virtual ~Joystick() = default;
Brian Silverman26e4e522015-12-17 01:56:40 -050045
Brian Silverman1a675112016-02-20 20:42:49 -050046 Joystick(const Joystick&) = delete;
47 Joystick& operator=(const Joystick&) = delete;
Brian Silverman26e4e522015-12-17 01:56:40 -050048
Brian Silverman1a675112016-02-20 20:42:49 -050049 uint32_t GetAxisChannel(AxisType axis);
50 void SetAxisChannel(AxisType axis, uint32_t channel);
Brian Silverman26e4e522015-12-17 01:56:40 -050051
Brian Silverman1a675112016-02-20 20:42:49 -050052 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 Silverman26e4e522015-12-17 01:56:40 -050059
Brian Silverman1a675112016-02-20 20:42:49 -050060 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 Silverman26e4e522015-12-17 01:56:40 -050067
Brian Silverman1a675112016-02-20 20:42:49 -050068 virtual float GetMagnitude() const;
69 virtual float GetDirectionRadians() const;
70 virtual float GetDirectionDegrees() const;
Brian Silverman26e4e522015-12-17 01:56:40 -050071
Brian Silverman1a675112016-02-20 20:42:49 -050072private:
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 Silverman26e4e522015-12-17 01:56:40 -050077};
78
79#endif