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

@@ -76,6 +76,110 @@ void DriverStation::ReportError(bool is_error, int32_t code,
location.c_str(locationTemp), stack.c_str(stackTemp), 1);
}
/**
* The state of one joystick button. Button indexes begin at 1.
*
* @param stick The joystick to read.
* @param button The button index, beginning at 1.
* @return The state of the joystick button.
*/
bool DriverStation::GetStickButton(int stick, int button) {
if (stick >= kJoystickPorts) {
wpi_setWPIError(BadJoystickIndex);
return false;
}
if (button == 0) {
ReportJoystickUnpluggedError(
"ERROR: Button indexes begin at 1 in WPILib for C++ and Java");
return false;
}
std::unique_lock<std::mutex> lock(m_joystickDataMutex);
if (button > m_joystickButtons[stick].count) {
// Unlock early so error printing isn't locked.
lock.unlock();
ReportJoystickUnpluggedWarning(
"Joystick Button missing, check if all controllers are "
"plugged in");
return false;
}
return m_joystickButtons[stick].buttons & 1 << (button - 1);
}
/**
* Whether one joystick button was pressed since the last check. Button indexes
* begin at 1.
*
* @param stick The joystick to read.
* @param button The button index, beginning at 1.
* @return Whether the joystick button was pressed since the last check.
*/
bool DriverStation::GetStickButtonPressed(int stick, int button) {
if (stick >= kJoystickPorts) {
wpi_setWPIError(BadJoystickIndex);
return false;
}
if (button == 0) {
ReportJoystickUnpluggedError(
"ERROR: Button indexes begin at 1 in WPILib for C++ and Java");
return false;
}
std::unique_lock<std::mutex> lock(m_joystickDataMutex);
if (button > m_joystickButtons[stick].count) {
// Unlock early so error printing isn't locked.
lock.unlock();
ReportJoystickUnpluggedWarning(
"Joystick Button missing, check if all controllers are "
"plugged in");
return false;
}
// If button was pressed, clear flag and return true
if (m_joystickButtonsPressed[stick] & 1 << (button - 1)) {
m_joystickButtonsPressed[stick] &= ~(1 << (button - 1));
return true;
} else {
return false;
}
}
/**
* Whether one joystick button was released since the last check. Button indexes
* begin at 1.
*
* @param stick The joystick to read.
* @param button The button index, beginning at 1.
* @return Whether the joystick button was released since the last check.
*/
bool DriverStation::GetStickButtonReleased(int stick, int button) {
if (stick >= kJoystickPorts) {
wpi_setWPIError(BadJoystickIndex);
return false;
}
if (button == 0) {
ReportJoystickUnpluggedError(
"ERROR: Button indexes begin at 1 in WPILib for C++ and Java");
return false;
}
std::unique_lock<std::mutex> lock(m_joystickDataMutex);
if (button > m_joystickButtons[stick].count) {
// Unlock early so error printing isn't locked.
lock.unlock();
ReportJoystickUnpluggedWarning(
"Joystick Button missing, check if all controllers are "
"plugged in");
return false;
}
// If button was released, clear flag and return true
if (m_joystickButtonsReleased[stick] & 1 << (button - 1)) {
m_joystickButtonsReleased[stick] &= ~(1 << (button - 1));
return true;
} else {
return false;
}
}
/**
* Get the value of the axis on a joystick.
*
@@ -146,36 +250,6 @@ int DriverStation::GetStickButtons(int stick) const {
return m_joystickButtons[stick].buttons;
}
/**
* The state of one joystick button. Button indexes begin at 1.
*
* @param stick The joystick to read.
* @param button The button index, beginning at 1.
* @return The state of the joystick button.
*/
bool DriverStation::GetStickButton(int stick, int button) {
if (stick >= kJoystickPorts) {
wpi_setWPIError(BadJoystickIndex);
return false;
}
if (button == 0) {
ReportJoystickUnpluggedError(
"ERROR: Button indexes begin at 1 in WPILib for C++ and Java");
return false;
}
std::unique_lock<std::mutex> lock(m_joystickDataMutex);
if (button > m_joystickButtons[stick].count) {
// Unlock early so error printing isn't locked.
lock.unlock();
ReportJoystickUnpluggedWarning(
"Joystick Button missing, check if all controllers are "
"plugged in");
return false;
}
return ((0x1 << (button - 1)) & m_joystickButtons[stick].buttons) != 0;
}
/**
* Returns the number of axes on a given joystick port.
*
@@ -529,6 +603,17 @@ void DriverStation::GetData() {
// Obtain a write lock on the data, swap the cached data into the
// main data arrays
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
for (int32_t i = 0; i < kJoystickPorts; i++) {
// If buttons weren't pressed and are now, set flags in m_buttonsPressed
m_joystickButtonsPressed[i] |=
~m_joystickButtons[i].buttons & m_joystickButtonsCache[i].buttons;
// If buttons were pressed and aren't now, set flags in m_buttonsReleased
m_joystickButtonsReleased[i] |=
m_joystickButtons[i].buttons & ~m_joystickButtonsCache[i].buttons;
}
m_joystickAxes.swap(m_joystickAxesCache);
m_joystickPOVs.swap(m_joystickPOVsCache);
m_joystickButtons.swap(m_joystickButtonsCache);
@@ -569,6 +654,9 @@ DriverStation::DriverStation() {
m_joystickDescriptorCache[i].isXbox = 0;
m_joystickDescriptorCache[i].type = -1;
m_joystickDescriptorCache[i].name[0] = '\0';
m_joystickButtonsPressed[i] = 0;
m_joystickButtonsReleased[i] = 0;
}
m_dsThread = std::thread(&DriverStation::Run, this);

View File

@@ -10,13 +10,53 @@
#include <HAL/HAL.h>
#include "DriverStation.h"
#include "WPIErrors.h"
using namespace frc;
GenericHID::GenericHID(int port) : m_ds(DriverStation::GetInstance()) {
if (port >= DriverStation::kJoystickPorts) {
wpi_setWPIError(BadJoystickIndex);
}
m_port = port;
}
/**
* Get the button value (starting at button 1).
*
* The buttons are returned in a single 16 bit value with one bit representing
* the state of each button. The appropriate button is returned as a boolean
* value.
*
* @param button The button number to be read (starting at 1)
* @return The state of the button.
*/
bool GenericHID::GetRawButton(int button) const {
return m_ds.GetStickButton(m_port, button);
}
/**
* Whether the button was pressed since the last check. Button indexes begin at
* 1.
*
* @param button The button index, beginning at 1.
* @return Whether the button was pressed since the last check.
*/
bool GenericHID::GetRawButtonPressed(int button) {
return m_ds.GetStickButtonPressed(m_port, button);
}
/**
* Whether the button was released since the last check. Button indexes begin at
* 1.
*
* @param button The button index, beginning at 1.
* @return Whether the button was released since the last check.
*/
bool GenericHID::GetRawButtonReleased(int button) {
return m_ds.GetStickButtonReleased(m_port, button);
}
/**
* Get the value of the axis.
*
@@ -27,20 +67,6 @@ double GenericHID::GetRawAxis(int axis) const {
return m_ds.GetStickAxis(m_port, axis);
}
/**
* Get the button value (starting at button 1)
*
* The buttons are returned in a single 16 bit value with one bit representing
* the state of each button. The appropriate button is returned as a boolean
* value.
*
* @param button The button number to be read (starting at 1)
* @return The state of the button.
**/
bool GenericHID::GetRawButton(int button) const {
return m_ds.GetStickButton(m_port, button);
}
/**
* Get the angle in degrees of a POV on the HID.
*
@@ -50,23 +76,30 @@ bool GenericHID::GetRawButton(int button) const {
* @param pov The index of the POV to read (starting at 0)
* @return the angle of the POV in degrees, or -1 if the POV is not pressed.
*/
int GenericHID::GetPOV(int pov) const {
return m_ds.GetStickPOV(GetPort(), pov);
}
int GenericHID::GetPOV(int pov) const { return m_ds.GetStickPOV(m_port, pov); }
/**
* Get the number of axes for the HID.
*
* @return the number of axis for the current HID
*/
int GenericHID::GetAxisCount() const { return m_ds.GetStickAxisCount(m_port); }
/**
* Get the number of POVs for the HID.
*
* @return the number of POVs for the current HID
*/
int GenericHID::GetPOVCount() const { return m_ds.GetStickPOVCount(GetPort()); }
int GenericHID::GetPOVCount() const { return m_ds.GetStickPOVCount(m_port); }
/**
* Get the port number of the HID.
* Get the number of buttons for the HID.
*
* @return The port number of the HID.
* @return the number of buttons on the current HID
*/
int GenericHID::GetPort() const { return m_port; }
int GenericHID::GetButtonCount() const {
return m_ds.GetStickButtonCount(m_port);
}
/**
* Get the type of the HID.
@@ -84,6 +117,22 @@ GenericHID::HIDType GenericHID::GetType() const {
*/
std::string GenericHID::GetName() const { return m_ds.GetJoystickName(m_port); }
/**
* Get the axis type of a joystick axis.
*
* @return the axis type of a joystick axis.
*/
int GenericHID::GetAxisType(int axis) const {
return m_ds.GetJoystickAxisType(m_port, axis);
}
/**
* Get the port number of the HID.
*
* @return The port number of the HID.
*/
int GenericHID::GetPort() const { return m_port; }
/**
* Set a single HID output value for the HID.
*

View File

@@ -7,6 +7,8 @@
#include "Joystick.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <HAL/HAL.h>
@@ -16,15 +18,9 @@
using namespace frc;
const int Joystick::kDefaultXAxis;
const int Joystick::kDefaultYAxis;
const int Joystick::kDefaultZAxis;
const int Joystick::kDefaultTwistAxis;
const int Joystick::kDefaultThrottleAxis;
const int Joystick::kDefaultTriggerButton;
const int Joystick::kDefaultTopButton;
static Joystick* joysticks[DriverStation::kJoystickPorts];
static bool joySticksInitialized = false;
constexpr int Joystick::kMinNumAxes;
constexpr double kPi = 3.14159265358979323846;
/**
* Construct an instance of a joystick.
@@ -34,53 +30,113 @@ static bool joySticksInitialized = false;
* @param port The port on the Driver Station that the joystick is plugged into
* (0-5).
*/
Joystick::Joystick(int port) : Joystick(port, kNumAxisTypes, kNumButtonTypes) {
m_axes[kXAxis] = kDefaultXAxis;
m_axes[kYAxis] = kDefaultYAxis;
m_axes[kZAxis] = kDefaultZAxis;
m_axes[kTwistAxis] = kDefaultTwistAxis;
m_axes[kThrottleAxis] = kDefaultThrottleAxis;
m_buttons[kTriggerButton] = kDefaultTriggerButton;
m_buttons[kTopButton] = kDefaultTopButton;
Joystick::Joystick(int port)
: GenericHID(port), m_axes(std::max(GetAxisCount(), kMinNumAxes)) {
m_axes[static_cast<int>(Axis::kX)] = kDefaultXAxis;
m_axes[static_cast<int>(Axis::kY)] = kDefaultYAxis;
m_axes[static_cast<int>(Axis::kZ)] = kDefaultZAxis;
m_axes[static_cast<int>(Axis::kTwist)] = kDefaultTwistAxis;
m_axes[static_cast<int>(Axis::kThrottle)] = kDefaultThrottleAxis;
HAL_Report(HALUsageReporting::kResourceType_Joystick, port);
}
/**
* Version of the constructor to be called by sub-classes.
* Set the channel associated with a specified axis.
*
* This constructor allows the subclass to configure the number of constants
* for axes and buttons.
*
* @param port The port on the Driver Station that the joystick is
* plugged into.
* @param numAxisTypes The number of axis types in the enum.
* @param numButtonTypes The number of button types in the enum.
* @param axis The axis to set the channel for.
* @param channel The channel to set the axis to.
*/
Joystick::Joystick(int port, int numAxisTypes, int numButtonTypes)
: JoystickBase(port),
m_ds(DriverStation::GetInstance()),
m_axes(numAxisTypes),
m_buttons(numButtonTypes) {
if (!joySticksInitialized) {
for (auto& joystick : joysticks) joystick = nullptr;
joySticksInitialized = true;
}
if (GetPort() >= DriverStation::kJoystickPorts) {
wpi_setWPIError(BadJoystickIndex);
} else {
joysticks[GetPort()] = this;
}
void Joystick::SetAxisChannel(AxisType axis, int channel) {
m_axes[axis] = channel;
}
Joystick* Joystick::GetStickForPort(int port) {
Joystick* stick = joysticks[port];
if (stick == nullptr) {
stick = new Joystick(port);
joysticks[port] = stick;
}
return stick;
/**
* Set the channel associated with the X axis.
*
* @param channel The channel to set the axis to.
*/
void Joystick::SetXChannel(int channel) {
m_axes[static_cast<int>(Axis::kX)] = channel;
}
/**
* Set the channel associated with the Y axis.
*
* @param axis The axis to set the channel for.
* @param channel The channel to set the axis to.
*/
void Joystick::SetYChannel(int channel) {
m_axes[static_cast<int>(Axis::kY)] = channel;
}
/**
* Set the channel associated with the Z axis.
*
* @param axis The axis to set the channel for.
* @param channel The channel to set the axis to.
*/
void Joystick::SetZChannel(int channel) {
m_axes[static_cast<int>(Axis::kZ)] = channel;
}
/**
* Set the channel associated with the twist axis.
*
* @param axis The axis to set the channel for.
* @param channel The channel to set the axis to.
*/
void Joystick::SetTwistChannel(int channel) {
m_axes[static_cast<int>(Axis::kTwist)] = channel;
}
/**
* Set the channel associated with the throttle axis.
*
* @param axis The axis to set the channel for.
* @param channel The channel to set the axis to.
*/
void Joystick::SetThrottleChannel(int channel) {
m_axes[static_cast<int>(Axis::kThrottle)] = channel;
}
/**
* Get the channel currently associated with the X axis.
*
* @return The channel for the axis.
*/
int Joystick::GetXChannel() const { return m_axes[static_cast<int>(Axis::kX)]; }
/**
* Get the channel currently associated with the Y axis.
*
* @return The channel for the axis.
*/
int Joystick::GetYChannel() const { return m_axes[static_cast<int>(Axis::kY)]; }
/**
* Get the channel currently associated with the Z axis.
*
* @return The channel for the axis.
*/
int Joystick::GetZChannel() const { return m_axes[static_cast<int>(Axis::kZ)]; }
/**
* Get the channel currently associated with the twist axis.
*
* @return The channel for the axis.
*/
int Joystick::GetTwistChannel() const {
return m_axes[static_cast<int>(Axis::kTwist)];
}
/**
* Get the channel currently associated with the throttle axis.
*
* @return The channel for the axis.
*/
int Joystick::GetThrottleChannel() const {
return m_axes[static_cast<int>(Axis::kThrottle)];
}
/**
@@ -92,7 +148,7 @@ Joystick* Joystick::GetStickForPort(int port) {
* here to complete the GenericHID interface.
*/
double Joystick::GetX(JoystickHand hand) const {
return GetRawAxis(m_axes[kXAxis]);
return GetRawAxis(m_axes[kDefaultXAxis]);
}
/**
@@ -104,7 +160,7 @@ double Joystick::GetX(JoystickHand hand) const {
* here to complete the GenericHID interface.
*/
double Joystick::GetY(JoystickHand hand) const {
return GetRawAxis(m_axes[kYAxis]);
return GetRawAxis(m_axes[kDefaultYAxis]);
}
/**
@@ -112,16 +168,16 @@ double Joystick::GetY(JoystickHand hand) const {
*
* This depends on the mapping of the joystick connected to the current port.
*/
double Joystick::GetZ(JoystickHand hand) const {
return GetRawAxis(m_axes[kZAxis]);
}
double Joystick::GetZ() const { return GetRawAxis(m_axes[kDefaultZAxis]); }
/**
* Get the twist value of the current joystick.
*
* This depends on the mapping of the joystick connected to the current port.
*/
double Joystick::GetTwist() const { return GetRawAxis(m_axes[kTwistAxis]); }
double Joystick::GetTwist() const {
return GetRawAxis(m_axes[kDefaultTwistAxis]);
}
/**
* Get the throttle value of the current joystick.
@@ -129,7 +185,7 @@ double Joystick::GetTwist() const { return GetRawAxis(m_axes[kTwistAxis]); }
* This depends on the mapping of the joystick connected to the current port.
*/
double Joystick::GetThrottle() const {
return GetRawAxis(m_axes[kThrottleAxis]);
return GetRawAxis(m_axes[kDefaultThrottleAxis]);
}
/**
@@ -145,15 +201,15 @@ double Joystick::GetThrottle() const {
double Joystick::GetAxis(AxisType axis) const {
switch (axis) {
case kXAxis:
return this->GetX();
return GetX();
case kYAxis:
return this->GetY();
return GetY();
case kZAxis:
return this->GetZ();
return GetZ();
case kTwistAxis:
return this->GetTwist();
return GetTwist();
case kThrottleAxis:
return this->GetThrottle();
return GetThrottle();
default:
wpi_setWPIError(BadJoystickAxis);
return 0.0;
@@ -165,12 +221,28 @@ double Joystick::GetAxis(AxisType axis) const {
*
* Look up which button has been assigned to the trigger and read its state.
*
* @param hand This parameter is ignored for the Joystick class and is only
* here to complete the GenericHID interface.
* @return The state of the trigger.
*/
bool Joystick::GetTrigger(JoystickHand hand) const {
return GetRawButton(m_buttons[kTriggerButton]);
bool Joystick::GetTrigger() const {
return GetRawButton(static_cast<int>(Button::kTrigger));
}
/**
* Whether the trigger was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
bool Joystick::GetTriggerPressed() {
return GetRawButtonPressed(static_cast<int>(Button::kTrigger));
}
/**
* Whether the trigger was released since the last check.
*
* @return Whether the button was released since the last check.
*/
bool Joystick::GetTriggerReleased() {
return GetRawButtonReleased(static_cast<int>(Button::kTrigger));
}
/**
@@ -178,12 +250,39 @@ bool Joystick::GetTrigger(JoystickHand hand) const {
*
* Look up which button has been assigned to the top and read its state.
*
* @param hand This parameter is ignored for the Joystick class and is only
* here to complete the GenericHID interface.
* @return The state of the top button.
*/
bool Joystick::GetTop(JoystickHand hand) const {
return GetRawButton(m_buttons[kTopButton]);
bool Joystick::GetTop() const {
return GetRawButton(static_cast<int>(Button::kTop));
}
/**
* Whether the top button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
bool Joystick::GetTopPressed() {
return GetRawButtonPressed(static_cast<int>(Button::kTop));
}
/**
* Whether the top button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
bool Joystick::GetTopReleased() {
return GetRawButtonReleased(static_cast<int>(Button::kTop));
}
Joystick* Joystick::GetStickForPort(int port) {
static std::array<std::unique_ptr<Joystick>, DriverStation::kJoystickPorts>
joysticks{};
auto stick = joysticks[port].get();
if (stick == nullptr) {
joysticks[port] = std::make_unique<Joystick>(port);
stick = joysticks[port].get();
}
return stick;
}
/**
@@ -195,57 +294,8 @@ bool Joystick::GetTop(JoystickHand hand) const {
* @return The state of the button.
*/
bool Joystick::GetButton(ButtonType button) const {
switch (button) {
case kTriggerButton:
return GetTrigger();
case kTopButton:
return GetTop();
default:
return false;
}
}
/**
* Get the number of axis for a joystick
*
* @return the number of axis for the current joystick
*/
int Joystick::GetAxisCount() const { return m_ds.GetStickAxisCount(GetPort()); }
/**
* Get the axis type of a joystick axis.
*
* @return the axis type of a joystick axis.
*/
int Joystick::GetAxisType(int axis) const {
return m_ds.GetJoystickAxisType(GetPort(), axis);
}
/**
* Get the number of buttons for a joystick.
*
* @return the number of buttons on the current joystick
*/
int Joystick::GetButtonCount() const {
return m_ds.GetStickButtonCount(GetPort());
}
/**
* Get the channel currently associated with the specified axis.
*
* @param axis The axis to look up the channel for.
* @return The channel fr the axis.
*/
int Joystick::GetAxisChannel(AxisType axis) const { return m_axes[axis]; }
/**
* Set the channel associated with a specified axis.
*
* @param axis The axis to set the channel for.
* @param channel The channel to set the axis to.
*/
void Joystick::SetAxisChannel(AxisType axis, int channel) {
m_axes[axis] = channel;
int temp = button;
return GetRawButton(static_cast<int>(static_cast<Button>(temp)));
}
/**
@@ -278,5 +328,5 @@ double Joystick::GetDirectionRadians() const {
* @return The direction of the vector in degrees
*/
double Joystick::GetDirectionDegrees() const {
return (180 / std::acos(-1)) * GetDirectionRadians();
return (180 / kPi) * GetDirectionRadians();
}

View File

@@ -9,20 +9,17 @@
#include <HAL/HAL.h>
#include "DriverStation.h"
using namespace frc;
/**
* Construct an instance of an Xbox controller.
*
* The joystick index is the USB port on the Driver Station.
* The controller index is the USB port on the Driver Station.
*
* @param port The port on the Driver Station that the joystick is plugged into
* (0-5).
* @param port The port on the Driver Station that the controller is plugged
* into (0-5).
*/
XboxController::XboxController(int port)
: GamepadBase(port), m_ds(DriverStation::GetInstance()) {
XboxController::XboxController(int port) : GenericHID(port) {
// HAL_Report(HALUsageReporting::kResourceType_XboxController, port);
HAL_Report(HALUsageReporting::kResourceType_Joystick, port);
}
@@ -53,33 +50,6 @@ double XboxController::GetY(JoystickHand hand) const {
}
}
/**
* Read the value of the bumper button on the controller.
*
* @param hand Side of controller whose value should be returned.
*/
bool XboxController::GetBumper(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawButton(5);
} else {
return GetRawButton(6);
}
}
/**
* Read the value of the stick button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetStickButton(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawButton(9);
} else {
return GetRawButton(10);
}
}
/**
* Get the trigger axis value of the controller.
*
@@ -94,36 +64,195 @@ double XboxController::GetTriggerAxis(JoystickHand hand) const {
}
/**
* Read the value of the A button on the controller.
* Read the value of the bumper button on the controller.
*
* @param hand Side of controller whose value should be returned.
*/
bool XboxController::GetBumper(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawButton(static_cast<int>(Button::kBumperLeft));
} else {
return GetRawButton(static_cast<int>(Button::kBumperRight));
}
}
/**
* Whether the bumper was pressed since the last check.
*
* @param hand Side of controller whose value should be returned.
* @return Whether the button was pressed since the last check.
*/
bool XboxController::GetBumperPressed(JoystickHand hand) {
if (hand == kLeftHand) {
return GetRawButtonPressed(static_cast<int>(Button::kBumperLeft));
} else {
return GetRawButtonPressed(static_cast<int>(Button::kBumperRight));
}
}
/**
* Whether the bumper was released since the last check.
*
* @param hand Side of controller whose value should be returned.
* @return Whether the button was released since the last check.
*/
bool XboxController::GetBumperReleased(JoystickHand hand) {
if (hand == kLeftHand) {
return GetRawButtonReleased(static_cast<int>(Button::kBumperLeft));
} else {
return GetRawButtonReleased(static_cast<int>(Button::kBumperRight));
}
}
/**
* Read the value of the stick button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetAButton() const { return GetRawButton(1); }
bool XboxController::GetStickButton(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawButton(static_cast<int>(Button::kStickLeft));
} else {
return GetRawButton(static_cast<int>(Button::kStickRight));
}
}
/**
* Whether the stick button was pressed since the last check.
*
* @param hand Side of controller whose value should be returned.
* @return Whether the button was pressed since the last check.
*/
bool XboxController::GetStickButtonPressed(JoystickHand hand) {
if (hand == kLeftHand) {
return GetRawButtonPressed(static_cast<int>(Button::kStickLeft));
} else {
return GetRawButtonPressed(static_cast<int>(Button::kStickRight));
}
}
/**
* Whether the stick button was released since the last check.
*
* @param hand Side of controller whose value should be returned.
* @return Whether the button was released since the last check.
*/
bool XboxController::GetStickButtonReleased(JoystickHand hand) {
if (hand == kLeftHand) {
return GetRawButtonReleased(static_cast<int>(Button::kStickLeft));
} else {
return GetRawButtonReleased(static_cast<int>(Button::kStickRight));
}
}
/**
* Read the value of the A button on the controller.
*
* @return The state of the button.
*/
bool XboxController::GetAButton() const {
return GetRawButton(static_cast<int>(Button::kA));
}
/**
* Whether the A button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
bool XboxController::GetAButtonPressed() {
return GetRawButtonPressed(static_cast<int>(Button::kA));
}
/**
* Whether the A button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
bool XboxController::GetAButtonReleased() {
return GetRawButtonReleased(static_cast<int>(Button::kA));
}
/**
* Read the value of the B button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetBButton() const { return GetRawButton(2); }
bool XboxController::GetBButton() const {
return GetRawButton(static_cast<int>(Button::kB));
}
/**
* Whether the B button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
bool XboxController::GetBButtonPressed() {
return GetRawButtonPressed(static_cast<int>(Button::kB));
}
/**
* Whether the B button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
bool XboxController::GetBButtonReleased() {
return GetRawButtonReleased(static_cast<int>(Button::kB));
}
/**
* Read the value of the X button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetXButton() const { return GetRawButton(3); }
bool XboxController::GetXButton() const {
return GetRawButton(static_cast<int>(Button::kX));
}
/**
* Whether the X button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
bool XboxController::GetXButtonPressed() {
return GetRawButtonPressed(static_cast<int>(Button::kX));
}
/**
* Whether the X button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
bool XboxController::GetXButtonReleased() {
return GetRawButtonReleased(static_cast<int>(Button::kX));
}
/**
* Read the value of the Y button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetYButton() const { return GetRawButton(4); }
bool XboxController::GetYButton() const {
return GetRawButton(static_cast<int>(Button::kY));
}
/**
* Whether the Y button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
bool XboxController::GetYButtonPressed() {
return GetRawButtonPressed(static_cast<int>(Button::kY));
}
/**
* Whether the Y button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
bool XboxController::GetYButtonReleased() {
return GetRawButtonReleased(static_cast<int>(Button::kY));
}
/**
* Read the value of the back button on the controller.
@@ -131,7 +260,27 @@ bool XboxController::GetYButton() const { return GetRawButton(4); }
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetBackButton() const { return GetRawButton(7); }
bool XboxController::GetBackButton() const {
return GetRawButton(static_cast<int>(Button::kBack));
}
/**
* Whether the back button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
bool XboxController::GetBackButtonPressed() {
return GetRawButtonPressed(static_cast<int>(Button::kBack));
}
/**
* Whether the back button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
bool XboxController::GetBackButtonReleased() {
return GetRawButtonReleased(static_cast<int>(Button::kBack));
}
/**
* Read the value of the start button on the controller.
@@ -139,4 +288,24 @@ bool XboxController::GetBackButton() const { return GetRawButton(7); }
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
bool XboxController::GetStartButton() const { return GetRawButton(8); }
bool XboxController::GetStartButton() const {
return GetRawButton(static_cast<int>(Button::kStart));
}
/**
* Whether the start button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
bool XboxController::GetStartButtonPressed() {
return GetRawButtonPressed(static_cast<int>(Button::kStart));
}
/**
* Whether the start button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
bool XboxController::GetStartButtonReleased() {
return GetRawButtonReleased(static_cast<int>(Button::kStart));
}

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