mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-02 02:51:42 +00:00
Added XboxController class (#140)
Joystick and Gamepad functionality was separated into cleaner interfaces.
This commit is contained in:
committed by
Peter Johnson
parent
8c93ceb728
commit
140c365e4b
26
wpilibc/shared/include/GamepadBase.h
Normal file
26
wpilibc/shared/include/GamepadBase.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "GenericHID.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* Gamepad Interface.
|
||||
*/
|
||||
class GamepadBase : public GenericHID {
|
||||
public:
|
||||
explicit GamepadBase(int port);
|
||||
virtual ~GamepadBase() = default;
|
||||
|
||||
virtual bool GetBumper(JoystickHand hand = kRightHand) const = 0;
|
||||
virtual bool GetStickButton(JoystickHand hand) const = 0;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
@@ -9,30 +9,67 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace frc {
|
||||
|
||||
class DriverStation;
|
||||
|
||||
/**
|
||||
* GenericHID Interface.
|
||||
*/
|
||||
class GenericHID {
|
||||
public:
|
||||
typedef enum { kLeftRumble, kRightRumble } RumbleType;
|
||||
|
||||
typedef enum {
|
||||
kUnknown = -1,
|
||||
kXInputUnknown = 0,
|
||||
kXInputGamepad = 1,
|
||||
kXInputWheel = 2,
|
||||
kXInputArcadeStick = 3,
|
||||
kXInputFlightStick = 4,
|
||||
kXInputDancePad = 5,
|
||||
kXInputGuitar = 6,
|
||||
kXInputGuitar2 = 7,
|
||||
kXInputDrumKit = 8,
|
||||
kXInputGuitar3 = 11,
|
||||
kXInputArcadePad = 19,
|
||||
kHIDJoystick = 20,
|
||||
kHIDGamepad = 21,
|
||||
kHIDDriving = 22,
|
||||
kHIDFlight = 23,
|
||||
kHID1stPerson = 24
|
||||
} HIDType;
|
||||
|
||||
enum JoystickHand { kLeftHand = 0, kRightHand = 1 };
|
||||
|
||||
explicit GenericHID(int port);
|
||||
virtual ~GenericHID() = default;
|
||||
|
||||
virtual float GetX(JoystickHand hand = kRightHand) const = 0;
|
||||
virtual float GetY(JoystickHand hand = kRightHand) const = 0;
|
||||
virtual float GetZ() const = 0;
|
||||
virtual float GetTwist() const = 0;
|
||||
virtual float GetThrottle() const = 0;
|
||||
virtual float GetRawAxis(int axis) const = 0;
|
||||
virtual float GetRawAxis(int axis) const;
|
||||
|
||||
virtual bool GetTrigger(JoystickHand hand = kRightHand) const = 0;
|
||||
virtual bool GetTop(JoystickHand hand = kRightHand) const = 0;
|
||||
virtual bool GetBumper(JoystickHand hand = kRightHand) const = 0;
|
||||
virtual bool GetRawButton(int button) const = 0;
|
||||
bool GetRawButton(int button) const;
|
||||
|
||||
virtual int GetPOV(int pov = 0) const = 0;
|
||||
int GetPOV(int pov = 0) const;
|
||||
int GetPOVCount() const;
|
||||
|
||||
int GetPort() const;
|
||||
GenericHID::HIDType GetType() const;
|
||||
std::string GetName() const;
|
||||
|
||||
void SetOutput(int outputNumber, bool value);
|
||||
void SetOutputs(int value);
|
||||
void SetRumble(RumbleType type, double value);
|
||||
|
||||
private:
|
||||
DriverStation& m_ds;
|
||||
int m_port;
|
||||
int m_outputs = 0;
|
||||
uint16_t m_leftRumble = 0;
|
||||
uint16_t m_rightRumble = 0;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
|
||||
88
wpilibc/shared/include/Joystick.h
Normal file
88
wpilibc/shared/include/Joystick.h
Normal file
@@ -0,0 +1,88 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "ErrorBase.h"
|
||||
#include "JoystickBase.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 {
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
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 SetAxisChannel(AxisType axis, int channel);
|
||||
|
||||
float GetX(JoystickHand hand = kRightHand) const override;
|
||||
float GetY(JoystickHand hand = kRightHand) const override;
|
||||
float GetZ(JoystickHand hand = kRightHand) const override;
|
||||
float GetTwist() const override;
|
||||
float GetThrottle() const override;
|
||||
virtual float GetAxis(AxisType axis) const;
|
||||
|
||||
bool GetTrigger(JoystickHand hand = kRightHand) const override;
|
||||
bool GetTop(JoystickHand hand = kRightHand) const override;
|
||||
bool GetButton(ButtonType button) const;
|
||||
static Joystick* GetStickForPort(int port);
|
||||
|
||||
virtual float GetMagnitude() const;
|
||||
virtual float GetDirectionRadians() const;
|
||||
virtual float GetDirectionDegrees() const;
|
||||
|
||||
int GetAxisType(int axis) const;
|
||||
|
||||
int GetAxisCount() const;
|
||||
int GetButtonCount() const;
|
||||
|
||||
private:
|
||||
DriverStation& m_ds;
|
||||
std::vector<int> m_axes;
|
||||
std::vector<int> m_buttons;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
30
wpilibc/shared/include/JoystickBase.h
Normal file
30
wpilibc/shared/include/JoystickBase.h
Normal file
@@ -0,0 +1,30 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "GenericHID.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
* Joystick Interface.
|
||||
*/
|
||||
class JoystickBase : public GenericHID {
|
||||
public:
|
||||
explicit JoystickBase(int port);
|
||||
virtual ~JoystickBase() = default;
|
||||
|
||||
virtual float GetZ(JoystickHand hand = kRightHand) const = 0;
|
||||
virtual float GetTwist() const = 0;
|
||||
virtual float GetThrottle() const = 0;
|
||||
|
||||
virtual bool GetTrigger(JoystickHand hand = kRightHand) const = 0;
|
||||
virtual bool GetTop(JoystickHand hand = kRightHand) const = 0;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
53
wpilibc/shared/include/XboxController.h
Normal file
53
wpilibc/shared/include/XboxController.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "ErrorBase.h"
|
||||
#include "GamepadBase.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
|
||||
* 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 {
|
||||
public:
|
||||
explicit XboxController(int port);
|
||||
virtual ~XboxController() = default;
|
||||
|
||||
XboxController(const XboxController&) = delete;
|
||||
XboxController& operator=(const XboxController&) = delete;
|
||||
|
||||
float GetX(JoystickHand hand) const override;
|
||||
float GetY(JoystickHand hand) const override;
|
||||
|
||||
bool GetBumper(JoystickHand hand) const override;
|
||||
bool GetStickButton(JoystickHand hand) const override;
|
||||
|
||||
virtual float GetTriggerAxis(JoystickHand hand) const;
|
||||
|
||||
bool GetAButton() const;
|
||||
bool GetBButton() const;
|
||||
bool GetXButton() const;
|
||||
bool GetYButton() const;
|
||||
bool GetBackButton() const;
|
||||
bool GetStartButton() const;
|
||||
|
||||
private:
|
||||
DriverStation& m_ds;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
Reference in New Issue
Block a user