Added functions for detecting button press and release events (#626)

I also shuffled around the HID interfaces to be more intuitive, deprecated some
Joystick and XboxController member functions, and deprecated the JoystickBase
and GamepadBase classes.

Supersedes #89.
This commit is contained in:
Tyler Veness
2017-10-27 21:45:56 -07:00
committed by Peter Johnson
parent c33fca34e9
commit 21585f70a8
17 changed files with 1334 additions and 724 deletions

View File

@@ -7,6 +7,7 @@
#pragma once
#include <array>
#include <atomic>
#include <memory>
#include <mutex>
@@ -38,10 +39,13 @@ class DriverStation : public SensorBase, public RobotStateInterface {
static const int kJoystickPorts = 6;
bool GetStickButton(int stick, int button);
bool GetStickButtonPressed(int stick, int button);
bool GetStickButtonReleased(int stick, int button);
double GetStickAxis(int stick, int axis);
int GetStickPOV(int stick, int pov);
int GetStickButtons(int stick) const;
bool GetStickButton(int stick, int button);
int GetStickAxisCount(int stick) const;
int GetStickPOVCount(int stick) const;
@@ -112,6 +116,10 @@ class DriverStation : public SensorBase, public RobotStateInterface {
std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtonsCache;
std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptorCache;
// Joystick button rising/falling edge flags
std::array<uint32_t, kJoystickPorts> m_joystickButtonsPressed;
std::array<uint32_t, kJoystickPorts> m_joystickButtonsReleased;
// Internal Driver Station thread
std::thread m_dsThread;
std::atomic<bool> m_isRunning{false};

View File

@@ -7,6 +7,8 @@
#pragma once
#include <support/deprecated.h>
#include "GenericHID.h"
namespace frc {
@@ -14,7 +16,8 @@ namespace frc {
/**
* Gamepad Interface.
*/
class GamepadBase : public GenericHID {
class WPI_DEPRECATED("Inherit directly from GenericHID instead.") GamepadBase
: public GenericHID {
public:
explicit GamepadBase(int port);
virtual ~GamepadBase() = default;

View File

@@ -11,6 +11,8 @@
#include <string>
#include "ErrorBase.h"
namespace frc {
class DriverStation;
@@ -18,11 +20,11 @@ class DriverStation;
/**
* GenericHID Interface.
*/
class GenericHID {
class GenericHID : public ErrorBase {
public:
typedef enum { kLeftRumble, kRightRumble } RumbleType;
enum RumbleType { kLeftRumble, kRightRumble };
typedef enum {
enum HIDType {
kUnknown = -1,
kXInputUnknown = 0,
kXInputGamepad = 1,
@@ -40,7 +42,7 @@ class GenericHID {
kHIDDriving = 22,
kHIDFlight = 23,
kHID1stPerson = 24
} HIDType;
};
enum JoystickHand { kLeftHand = 0, kRightHand = 1 };
@@ -49,16 +51,23 @@ class GenericHID {
virtual double GetX(JoystickHand hand = kRightHand) const = 0;
virtual double GetY(JoystickHand hand = kRightHand) const = 0;
virtual double GetRawAxis(int axis) const;
bool GetRawButton(int button) const;
bool GetRawButtonPressed(int button);
bool GetRawButtonReleased(int button);
double GetRawAxis(int axis) const;
int GetPOV(int pov = 0) const;
int GetPOVCount() const;
int GetPort() const;
int GetAxisCount() const;
int GetPOVCount() const;
int GetButtonCount() const;
GenericHID::HIDType GetType() const;
std::string GetName() const;
int GetAxisType(int axis) const;
int GetPort() const;
void SetOutput(int outputNumber, bool value);
void SetOutputs(int value);

View File

@@ -7,82 +7,90 @@
#pragma once
#include <stdint.h>
#include <memory>
#include <vector>
#include "ErrorBase.h"
#include "JoystickBase.h"
#include <support/deprecated.h>
#include "GenericHID.h"
namespace frc {
class DriverStation;
/**
* Handle input from standard Joysticks connected to the Driver Station.
*
* This class handles standard input that comes from the Driver Station. Each
* time a value is requested the most recent value is returned. There is a
* single class instance for each joystick and the mapping of ports to hardware
* buttons depends on the code in the Driver Station.
*/
class Joystick : public JoystickBase, public ErrorBase {
class Joystick : public GenericHID {
public:
static const int kDefaultXAxis = 0;
static const int kDefaultYAxis = 1;
static const int kDefaultZAxis = 2;
static const int kDefaultTwistAxis = 2;
static const int kDefaultThrottleAxis = 3;
static constexpr int kDefaultXAxis = 0;
static constexpr int kDefaultYAxis = 1;
static constexpr int kDefaultZAxis = 2;
static constexpr int kDefaultTwistAxis = 2;
static constexpr int kDefaultThrottleAxis = 3;
static constexpr int kMinNumAxes = 4;
typedef enum {
kXAxis,
kYAxis,
kZAxis,
kTwistAxis,
kThrottleAxis,
kNumAxisTypes
} AxisType;
static const int kDefaultTriggerButton = 1;
static const int kDefaultTopButton = 2;
typedef enum { kTriggerButton, kTopButton, kNumButtonTypes } ButtonType;
enum AxisType { kXAxis, kYAxis, kZAxis, kTwistAxis, kThrottleAxis };
enum ButtonType { kTriggerButton, kTopButton };
explicit Joystick(int port);
Joystick(int port, int numAxisTypes, int numButtonTypes);
virtual ~Joystick() = default;
Joystick(const Joystick&) = delete;
Joystick& operator=(const Joystick&) = delete;
int GetAxisChannel(AxisType axis) const;
void SetXChannel(int channel);
void SetYChannel(int channel);
void SetZChannel(int channel);
void SetTwistChannel(int channel);
void SetThrottleChannel(int channel);
WPI_DEPRECATED("Use the more specific axis channel setter functions.")
void SetAxisChannel(AxisType axis, int channel);
int GetXChannel() const;
int GetYChannel() const;
int GetZChannel() const;
int GetTwistChannel() const;
int GetThrottleChannel() const;
WPI_DEPRECATED("Use the more specific axis channel getter functions.")
int GetAxisChannel(AxisType axis) const;
double GetX(JoystickHand hand = kRightHand) const override;
double GetY(JoystickHand hand = kRightHand) const override;
double GetZ(JoystickHand hand = kRightHand) const override;
double GetTwist() const override;
double GetThrottle() const override;
virtual double GetAxis(AxisType axis) const;
double GetZ() const;
double GetTwist() const;
double GetThrottle() const;
bool GetTrigger(JoystickHand hand = kRightHand) const override;
bool GetTop(JoystickHand hand = kRightHand) const override;
bool GetButton(ButtonType button) const;
WPI_DEPRECATED("Use the more specific axis channel getter functions.")
double GetAxis(AxisType axis) const;
bool GetTrigger() const;
bool GetTriggerPressed();
bool GetTriggerReleased();
bool GetTop() const;
bool GetTopPressed();
bool GetTopReleased();
WPI_DEPRECATED("Use Joystick instances instead.")
static Joystick* GetStickForPort(int port);
virtual double GetMagnitude() const;
virtual double GetDirectionRadians() const;
virtual double GetDirectionDegrees() const;
WPI_DEPRECATED("Use the more specific button getter functions.")
bool GetButton(ButtonType button) const;
int GetAxisType(int axis) const;
int GetAxisCount() const;
int GetButtonCount() const;
double GetMagnitude() const;
double GetDirectionRadians() const;
double GetDirectionDegrees() const;
private:
DriverStation& m_ds;
enum class Axis { kX, kY, kZ, kTwist, kThrottle };
enum class Button { kTrigger = 1, kTop = 2 };
std::vector<int> m_axes;
std::vector<int> m_buttons;
};
} // namespace frc

View File

@@ -7,6 +7,8 @@
#pragma once
#include <support/deprecated.h>
#include "GenericHID.h"
namespace frc {
@@ -14,7 +16,8 @@ namespace frc {
/**
* Joystick Interface.
*/
class JoystickBase : public GenericHID {
class WPI_DEPRECATED("Inherit directly from GenericHID instead.") JoystickBase
: public GenericHID {
public:
explicit JoystickBase(int port);
virtual ~JoystickBase() = default;
@@ -22,9 +25,6 @@ class JoystickBase : public GenericHID {
virtual double GetZ(JoystickHand hand = kRightHand) const = 0;
virtual double GetTwist() const = 0;
virtual double GetThrottle() const = 0;
virtual bool GetTrigger(JoystickHand hand = kRightHand) const = 0;
virtual bool GetTop(JoystickHand hand = kRightHand) const = 0;
};
} // namespace frc

View File

@@ -7,23 +7,20 @@
#pragma once
#include "ErrorBase.h"
#include "GamepadBase.h"
#include "GenericHID.h"
namespace frc {
class DriverStation;
/**
* Handle input from Xbox 360 or Xbox One controllers connected to the Driver
* Station.
*
* This class handles Xbox input that comes from the Driver Station. Each time a
* value is requested the most recent value is returend. There is a single class
* value is requested the most recent value is returned. There is a single class
* instance for each controller and the mapping of ports to hardware buttons
* depends on the code in the Driver Station.
*/
class XboxController : public GamepadBase, public ErrorBase {
class XboxController : public GenericHID {
public:
explicit XboxController(int port);
virtual ~XboxController() = default;
@@ -33,21 +30,53 @@ class XboxController : public GamepadBase, public ErrorBase {
double GetX(JoystickHand hand) const override;
double GetY(JoystickHand hand) const override;
double GetTriggerAxis(JoystickHand hand) const;
bool GetBumper(JoystickHand hand) const override;
bool GetStickButton(JoystickHand hand) const override;
bool GetBumper(JoystickHand hand) const;
bool GetBumperPressed(JoystickHand hand);
bool GetBumperReleased(JoystickHand hand);
virtual double GetTriggerAxis(JoystickHand hand) const;
bool GetStickButton(JoystickHand hand) const;
bool GetStickButtonPressed(JoystickHand hand);
bool GetStickButtonReleased(JoystickHand hand);
bool GetAButton() const;
bool GetAButtonPressed();
bool GetAButtonReleased();
bool GetBButton() const;
bool GetBButtonPressed();
bool GetBButtonReleased();
bool GetXButton() const;
bool GetXButtonPressed();
bool GetXButtonReleased();
bool GetYButton() const;
bool GetYButtonPressed();
bool GetYButtonReleased();
bool GetBackButton() const;
bool GetBackButtonPressed();
bool GetBackButtonReleased();
bool GetStartButton() const;
bool GetStartButtonPressed();
bool GetStartButtonReleased();
private:
DriverStation& m_ds;
enum class Button {
kBumperLeft = 5,
kBumperRight = 6,
kStickLeft = 9,
kStickRight = 10,
kA = 1,
kB = 2,
kX = 3,
kY = 4,
kBack = 7,
kStart = 8
};
};
} // namespace frc