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

View File

@@ -81,6 +81,11 @@ public class DriverStation implements RobotState.Interface {
private HALJoystickAxes[] m_joystickAxesCache = new HALJoystickAxes[kJoystickPorts];
private HALJoystickPOVs[] m_joystickPOVsCache = new HALJoystickPOVs[kJoystickPorts];
private HALJoystickButtons[] m_joystickButtonsCache = new HALJoystickButtons[kJoystickPorts];
// Joystick button rising/falling edge flags
HALJoystickButtons[] m_joystickButtonsPressed = new HALJoystickButtons[kJoystickPorts];
HALJoystickButtons[] m_joystickButtonsReleased = new HALJoystickButtons[kJoystickPorts];
// preallocated byte buffer for button count
private ByteBuffer m_buttonCountBuffer = ByteBuffer.allocateDirect(1);
@@ -126,6 +131,9 @@ public class DriverStation implements RobotState.Interface {
m_joystickButtonsCache[i] = new HALJoystickButtons();
m_joystickAxesCache[i] = new HALJoystickAxes(HAL.kMaxJoystickAxes);
m_joystickPOVsCache[i] = new HALJoystickPOVs(HAL.kMaxJoystickPOVs);
m_joystickButtonsPressed[i].m_buttons = 0;
m_joystickButtonsReleased[i].m_buttons = 0;
}
m_controlWordMutex = new Object();
@@ -188,6 +196,89 @@ public class DriverStation implements RobotState.Interface {
HAL.sendError(isError, code, false, error, locString, printTrace ? traceString : "", true);
}
/**
* 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.
*/
public boolean getStickButton(final int stick, final int button) {
if (button <= 0) {
reportJoystickUnpluggedError("Button indexes begin at 1 in WPILib for C++ and Java\n");
return false;
}
if (stick < 0 || stick >= kJoystickPorts) {
throw new RuntimeException("Joystick index is out of range, should be 0-3");
}
boolean error = false;
boolean retVal = false;
synchronized (m_joystickMutex) {
if (button > m_joystickButtons[stick].m_count) {
error = true;
retVal = false;
} else {
retVal = (m_joystickButtons[stick].m_buttons & 1 << (button - 1)) != 0;
}
}
if (error) {
reportJoystickUnpluggedWarning("Joystick Button " + button + " on port " + stick
+ " not available, check if controller is plugged in");
}
return retVal;
}
/**
* 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.
*/
boolean getStickButtonPressed(final int stick, final int button) {
if (button <= 0) {
reportJoystickUnpluggedError("Button indexes begin at 1 in WPILib for C++ and Java\n");
return false;
}
if (stick < 0 || stick >= kJoystickPorts) {
throw new RuntimeException("Joystick index is out of range, should be 0-3");
}
// If button was pressed, clear flag and return true
if ((m_joystickButtonsPressed[stick].m_buttons & 1 << (button - 1)) != 0) {
m_joystickButtonsPressed[stick].m_buttons &= ~(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.
*/
boolean getStickButtonReleased(final int stick, final int button) {
if (button <= 0) {
reportJoystickUnpluggedError("Button indexes begin at 1 in WPILib for C++ and Java\n");
return false;
}
if (stick < 0 || stick >= kJoystickPorts) {
throw new RuntimeException("Joystick index is out of range, should be 0-3");
}
// If button was released, clear flag and return true
if ((m_joystickButtonsReleased[stick].m_buttons & 1 << (button - 1)) != 0) {
m_joystickButtonsReleased[stick].m_buttons &= ~(1 << (button - 1));
return true;
} else {
return false;
}
}
/**
* Get the value of the axis on a joystick. This depends on the mapping of the joystick connected
* to the specified port.
@@ -266,38 +357,6 @@ public class DriverStation implements RobotState.Interface {
}
}
/**
* 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.
*/
public boolean getStickButton(final int stick, byte button) {
if (button <= 0) {
reportJoystickUnpluggedError("Button indexes begin at 1 in WPILib for C++ and Java\n");
return false;
}
if (stick < 0 || stick >= kJoystickPorts) {
throw new RuntimeException("Joystick index is out of range, should be 0-3");
}
boolean error = false;
boolean retVal = false;
synchronized (m_joystickMutex) {
if (button > m_joystickButtons[stick].m_count) {
error = true;
retVal = false;
} else {
retVal = ((0x1 << (button - 1)) & m_joystickButtons[stick].m_buttons) != 0;
}
}
if (error) {
reportJoystickUnpluggedWarning("Joystick Button " + button + " on port " + stick
+ " not available, check if controller is plugged in");
}
return retVal;
}
/**
* Returns the number of axes on a given joystick port.
*
@@ -717,6 +776,16 @@ public class DriverStation implements RobotState.Interface {
// lock joystick mutex to swap cache data
synchronized (m_joystickMutex) {
for (int i = 0; i < kJoystickPorts; i++) {
// If buttons weren't pressed and are now, set flags in m_buttonsPressed
m_joystickButtonsPressed[i].m_buttons |=
~m_joystickButtons[i].m_buttons & m_joystickButtonsCache[i].m_buttons;
// If buttons were pressed and aren't now, set flags in m_buttonsReleased
m_joystickButtonsReleased[i].m_buttons |=
m_joystickButtons[i].m_buttons & ~m_joystickButtonsCache[i].m_buttons;
}
// move cache to actual data
HALJoystickAxes[] currentAxes = m_joystickAxes;
m_joystickAxes = m_joystickAxesCache;

View File

@@ -9,7 +9,10 @@ package edu.wpi.first.wpilibj;
/**
* Gamepad Interface.
*
* @deprecated Inherit directly from GenericHID instead.
*/
@Deprecated
public abstract class GamepadBase extends GenericHID {
public GamepadBase(int port) {
super(port);

View File

@@ -7,6 +7,8 @@
package edu.wpi.first.wpilibj;
import edu.wpi.first.wpilibj.hal.HAL;
/**
* GenericHID Interface.
*/
@@ -59,9 +61,14 @@ public abstract class GenericHID {
}
}
private DriverStation m_ds;
private final int m_port;
private int m_outputs;
private short m_leftRumble;
private short m_rightRumble;
public GenericHID(int port) {
m_ds = DriverStation.getInstance();
m_port = port;
}
@@ -100,20 +107,49 @@ public abstract class GenericHID {
public abstract double getY(Hand hand);
/**
* Get the raw axis.
* Get the button value (starting at button 1).
*
* @param which index of the axis
* @return the raw value of the selected axis
* <p>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.
*/
public abstract double getRawAxis(int which);
public boolean getRawButton(int button) {
return m_ds.getStickButton(m_port, (byte) button);
}
/**
* Is the given button pressed.
* Whether the button was pressed since the last check. Button indexes begin at
* 1.
*
* @param button which button number
* @return true if the button is pressed
* @param button The button index, beginning at 1.
* @return Whether the button was pressed since the last check.
*/
public abstract boolean getRawButton(int button);
public boolean getRawButtonPressed(int button) {
return m_ds.getStickButtonPressed(m_port, (byte) 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.
*/
public boolean getRawButtonReleased(int button) {
return m_ds.getStickButtonReleased(m_port, button);
}
/**
* Get the value of the axis.
*
* @param axis The axis to read, starting at 0.
* @return The value of the axis.
*/
public double getRawAxis(int axis) {
return m_ds.getStickAxis(m_port, axis);
}
/**
* Get the angle in degrees of a POV on the HID.
@@ -124,16 +160,63 @@ public abstract class GenericHID {
* @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.
*/
public abstract int getPOV(int pov);
public int getPOV(int pov) {
return m_ds.getStickPOV(m_port, pov);
}
public int getPOV() {
return getPOV(0);
}
/**
* Get the number of axes for the HID.
*
* @return the number of axis for the current HID
*/
public int getAxisCount() {
return m_ds.getStickAxisCount(m_port);
}
/**
* For the current HID, return the number of POVs.
*/
public abstract int getPOVCount();
public int getPOVCount() {
return m_ds.getStickPOVCount(m_port);
}
/**
* For the current HID, return the number of buttons.
*/
public int getButtonCount() {
return m_ds.getStickButtonCount(m_port);
}
/**
* Get the type of the HID.
*
* @return the type of the HID.
*/
public HIDType getType() {
return HIDType.values()[m_ds.getJoystickType(m_port)];
}
/**
* Get the name of the HID.
*
* @return the name of the HID.
*/
public String getName() {
return m_ds.getJoystickName(m_port);
}
/**
* Get the axis type of a joystick axis.
*
* @return the axis type of a joystick axis.
*/
public int getAxisType(int axis) {
return m_ds.getJoystickAxisType(m_port, axis);
}
/**
* Get the port number of the HID.
@@ -144,34 +227,26 @@ public abstract class GenericHID {
return m_port;
}
/**
* Get the type of the HID.
*
* @return the type of the HID.
*/
public abstract HIDType getType();
/**
* Get the name of the HID.
*
* @return the name of the HID.
*/
public abstract String getName();
/**
* Set a single HID output value for the HID.
*
* @param outputNumber The index of the output to set (1-32)
* @param value The value to set the output to
*/
public abstract void setOutput(int outputNumber, boolean value);
public void setOutput(int outputNumber, boolean value) {
m_outputs = (m_outputs & ~(1 << (outputNumber - 1))) | ((value ? 1 : 0) << (outputNumber - 1));
HAL.setJoystickOutputs((byte) m_port, m_outputs, m_leftRumble, m_rightRumble);
}
/**
* Set all HID output values for the HID.
*
* @param value The 32 bit output value (1 bit for each output)
*/
public abstract void setOutputs(int value);
public void setOutputs(int value) {
m_outputs = value;
HAL.setJoystickOutputs((byte) m_port, m_outputs, m_leftRumble, m_rightRumble);
}
/**
* Set the rumble output for the HID. The DS currently supports 2 rumble values, left rumble and
@@ -180,5 +255,17 @@ public abstract class GenericHID {
* @param type Which rumble value to set
* @param value The normalized value (0 to 1) to set the rumble to
*/
public abstract void setRumble(RumbleType type, double value);
public void setRumble(RumbleType type, double value) {
if (value < 0) {
value = 0;
} else if (value > 1) {
value = 1;
}
if (type == RumbleType.kLeftRumble) {
m_leftRumble = (short) (value * 65535);
} else {
m_rightRumble = (short) (value * 65535);
}
HAL.setJoystickOutputs((byte) m_port, m_outputs, m_leftRumble, m_rightRumble);
}
}

View File

@@ -11,26 +11,25 @@ import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType;
import edu.wpi.first.wpilibj.hal.HAL;
/**
* 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.
* Handle input from standard Joysticks connected to the Driver Station.
*
* <p>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.
*/
public class Joystick extends JoystickBase {
public class Joystick extends GenericHID {
static final byte kDefaultXAxis = 0;
static final byte kDefaultYAxis = 1;
static final byte kDefaultZAxis = 2;
static final byte kDefaultTwistAxis = 2;
static final byte kDefaultThrottleAxis = 3;
static final int kDefaultTriggerButton = 1;
static final int kDefaultTopButton = 2;
static final byte kMinNumAxes = 4;
/**
* Represents an analog axis on a joystick.
*/
public enum AxisType {
kX(0), kY(1), kZ(2), kTwist(3), kThrottle(4), kNumAxis(5);
kX(0), kY(1), kZ(2), kTwist(3), kThrottle(4);
@SuppressWarnings("MemberName")
public final int value;
@@ -41,10 +40,10 @@ public class Joystick extends JoystickBase {
}
/**
* Represents a digital button on the JoyStick.
* Represents a digital button on a joystick.
*/
public enum ButtonType {
kTrigger(0), kTop(1), kNumButton(2);
kTrigger(1), kTop(2);
@SuppressWarnings("MemberName")
public final int value;
@@ -54,12 +53,35 @@ public class Joystick extends JoystickBase {
}
}
private final DriverStation m_ds;
/**
* Represents a digital button on a joystick.
*/
private enum Button {
kTrigger(1), kTop(2);
@SuppressWarnings("MemberName")
public final int value;
Button(int value) {
this.value = value;
}
}
/**
* Represents an analog axis on a joystick.
*/
private enum Axis {
kX(0), kY(1), kZ(2), kTwist(3), kThrottle(4);
@SuppressWarnings("MemberName")
public final int value;
Axis(int value) {
this.value = value;
}
}
private final byte[] m_axes;
private final byte[] m_buttons;
private int m_outputs;
private short m_leftRumble;
private short m_rightRumble;
/**
* Construct an instance of a joystick. The joystick index is the USB port on the drivers
@@ -68,36 +90,131 @@ public class Joystick extends JoystickBase {
* @param port The port on the Driver Station that the joystick is plugged into.
*/
public Joystick(final int port) {
this(port, AxisType.kNumAxis.value, ButtonType.kNumButton.value);
super(port);
m_axes[AxisType.kX.value] = kDefaultXAxis;
m_axes[AxisType.kY.value] = kDefaultYAxis;
m_axes[AxisType.kZ.value] = kDefaultZAxis;
m_axes[AxisType.kTwist.value] = kDefaultTwistAxis;
m_axes[AxisType.kThrottle.value] = kDefaultThrottleAxis;
m_axes = new byte[Math.max(getAxisCount(), kMinNumAxes)];
m_buttons[ButtonType.kTrigger.value] = kDefaultTriggerButton;
m_buttons[ButtonType.kTop.value] = kDefaultTopButton;
m_axes[Axis.kX.value] = kDefaultXAxis;
m_axes[Axis.kY.value] = kDefaultYAxis;
m_axes[Axis.kZ.value] = kDefaultZAxis;
m_axes[Axis.kTwist.value] = kDefaultTwistAxis;
m_axes[Axis.kThrottle.value] = kDefaultThrottleAxis;
HAL.report(tResourceType.kResourceType_Joystick, port);
}
/**
* Protected version of the constructor to be called by sub-classes.
* Set the channel associated with the X axis.
*
* <p>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 channel The channel to set the axis to.
*/
protected Joystick(int port, int numAxisTypes, int numButtonTypes) {
super(port);
public void setXChannel(int channel) {
m_axes[Axis.kX.value] = (byte) channel;
}
m_ds = DriverStation.getInstance();
m_axes = new byte[numAxisTypes];
m_buttons = new byte[numButtonTypes];
/**
* Set the channel associated with the Y axis.
*
* @param channel The channel to set the axis to.
*/
public void setYChannel(int channel) {
m_axes[Axis.kY.value] = (byte) channel;
}
/**
* Set the channel associated with the Z axis.
*
* @param channel The channel to set the axis to.
*/
public void setZChannel(int channel) {
m_axes[Axis.kZ.value] = (byte) channel;
}
/**
* Set the channel associated with the throttle axis.
*
* @param channel The channel to set the axis to.
*/
public void setThrottleChannel(int channel) {
m_axes[Axis.kThrottle.value] = (byte) channel;
}
/**
* Set the channel associated with the twist axis.
*
* @param channel The channel to set the axis to.
*/
public void setTwistChannel(int channel) {
m_axes[Axis.kTwist.value] = (byte) channel;
}
/**
* Set the channel associated with a specified axis.
*
* @deprecated Use the more specific axis channel setter functions.
* @param axis The axis to set the channel for.
* @param channel The channel to set the axis to.
*/
@Deprecated
public void setAxisChannel(AxisType axis, int channel) {
m_axes[axis.value] = (byte) channel;
}
/**
* Get the channel currently associated with the X axis.
*
* @return The channel for the axis.
*/
public int getXChannel() {
return m_axes[Axis.kX.value];
}
/**
* Get the channel currently associated with the Y axis.
*
* @return The channel for the axis.
*/
public int getYChannel() {
return m_axes[Axis.kY.value];
}
/**
* Get the channel currently associated with the Z axis.
*
* @return The channel for the axis.
*/
public int getZChannel() {
return m_axes[Axis.kZ.value];
}
/**
* Get the channel currently associated with the twist axis.
*
* @return The channel for the axis.
*/
public int getTwistChannel() {
return m_axes[Axis.kTwist.value];
}
/**
* Get the channel currently associated with the throttle axis.
*
* @return The channel for the axis.
*/
public int getThrottleChannel() {
return m_axes[Axis.kThrottle.value];
}
/**
* Get the channel currently associated with the specified axis.
*
* @deprecated Use the more specific axis channel getter functions.
* @param axis The axis to look up the channel for.
* @return The channel for the axis.
*/
@Deprecated
public int getAxisChannel(AxisType axis) {
return m_axes[axis.value];
}
/**
@@ -109,7 +226,7 @@ public class Joystick extends JoystickBase {
*/
@Override
public final double getX(Hand hand) {
return getRawAxis(m_axes[AxisType.kX.value]);
return getRawAxis(m_axes[Axis.kX.value]);
}
/**
@@ -121,12 +238,16 @@ public class Joystick extends JoystickBase {
*/
@Override
public final double getY(Hand hand) {
return getRawAxis(m_axes[AxisType.kY.value]);
return getRawAxis(m_axes[Axis.kY.value]);
}
@Override
public final double getZ(Hand hand) {
return getRawAxis(m_axes[AxisType.kZ.value]);
/**
* Get the z position of the HID.
*
* @return the z position
*/
public double getZ() {
return getRawAxis(m_axes[Axis.kZ.value]);
}
/**
@@ -136,7 +257,7 @@ public class Joystick extends JoystickBase {
* @return The Twist value of the joystick.
*/
public double getTwist() {
return getRawAxis(m_axes[AxisType.kTwist.value]);
return getRawAxis(m_axes[Axis.kTwist.value]);
}
/**
@@ -146,17 +267,7 @@ public class Joystick extends JoystickBase {
* @return The Throttle value of the joystick.
*/
public double getThrottle() {
return getRawAxis(m_axes[AxisType.kThrottle.value]);
}
/**
* Get the value of the axis.
*
* @param axis The axis to read, starting at 0.
* @return The value of the axis.
*/
public double getRawAxis(final int axis) {
return m_ds.getStickAxis(getPort(), axis);
return getRawAxis(m_axes[Axis.kThrottle.value]);
}
/**
@@ -165,9 +276,11 @@ public class Joystick extends JoystickBase {
* <p>This is for cases where the joystick axis is returned programmatically, otherwise one of the
* previous functions would be preferable (for example getX()).
*
* @deprecated Use the more specific axis getter functions.
* @param axis The axis to read.
* @return The value of the axis.
*/
@Deprecated
public double getAxis(final AxisType axis) {
switch (axis) {
case kX:
@@ -186,80 +299,57 @@ public class Joystick extends JoystickBase {
}
/**
* For the current joystick, return the number of axis.
* Read the state of the trigger on the joystick.
*
* @return The state of the trigger.
*/
public int getAxisCount() {
return m_ds.getStickAxisCount(getPort());
public boolean getTrigger() {
return getRawButton(Button.kTrigger.value);
}
/**
* Read the state of the trigger on the joystick.
* Whether the trigger was pressed since the last check.
*
* <p>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.
* @return Whether the button was pressed since the last check.
*/
@SuppressWarnings("PMD.UnusedFormalParameter")
public boolean getTrigger(Hand hand) {
return getRawButton(m_buttons[ButtonType.kTrigger.value]);
public boolean getTriggerPressed() {
return getRawButtonPressed(Button.kTrigger.value);
}
/**
* Whether the trigger was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getTriggerReleased() {
return getRawButtonReleased(Button.kTrigger.value);
}
/**
* Read the state of the top button on the joystick.
*
* <p>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.
*/
@SuppressWarnings("PMD.UnusedFormalParameter")
public boolean getTop(Hand hand) {
return getRawButton(m_buttons[ButtonType.kTop.value]);
}
@Override
public int getPOV(int pov) {
return m_ds.getStickPOV(getPort(), pov);
}
@Override
public int getPOVCount() {
return m_ds.getStickPOVCount(getPort());
public boolean getTop() {
return getRawButton(Button.kTop.value);
}
/**
* This is not supported for the Joystick. This method is only here to complete the GenericHID
* interface.
* Whether the top button was pressed since the last check.
*
* @param hand This parameter is ignored for the Joystick class and is only here to complete the
* GenericHID interface.
* @return The state of the bumper (always false)
* @return Whether the button was pressed since the last check.
*/
@SuppressWarnings("PMD.UnusedFormalParameter")
public boolean getBumper(Hand hand) {
return false;
public boolean getTopPressed() {
return getRawButtonPressed(Button.kTop.value);
}
/**
* Get the button value (starting at button 1).
* Whether the top button was released since the last check.
*
* <p>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.
* @return Whether the button was released since the last check.
*/
public boolean getRawButton(final int button) {
return m_ds.getStickButton(getPort(), (byte) button);
}
/**
* For the current joystick, return the number of buttons.
*/
public int getButtonCount() {
return m_ds.getStickButtonCount(getPort());
public boolean getTopReleased() {
return getRawButtonReleased(Button.kTop.value);
}
/**
@@ -267,18 +357,13 @@ public class Joystick extends JoystickBase {
*
* <p>The button type will be looked up in the list of buttons and then read.
*
* @deprecated Use Button enum values instead of ButtonType.
* @param button The type of button to read.
* @return The state of the button.
*/
@Deprecated
public boolean getButton(ButtonType button) {
switch (button) {
case kTrigger:
return getTrigger();
case kTop:
return getTop();
default:
return false;
}
return getRawButton(button.value);
}
/**
@@ -308,89 +393,4 @@ public class Joystick extends JoystickBase {
public double getDirectionDegrees() {
return Math.toDegrees(getDirectionRadians());
}
/**
* 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.
*/
public int getAxisChannel(AxisType axis) {
return m_axes[axis.value];
}
/**
* 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.
*/
public void setAxisChannel(AxisType axis, int channel) {
m_axes[axis.value] = (byte) channel;
}
/**
* Get the value of isXbox for the current joystick.
*
* @return A boolean that is true if the controller is an xbox controller.
*/
public boolean getIsXbox() {
return m_ds.getJoystickIsXbox(getPort());
}
/**
* Get the axis type of a joystick axis.
*
* @return the axis type of a joystick axis.
*/
public int getAxisType(int axis) {
return m_ds.getJoystickAxisType(getPort(), axis);
}
/**
* Get the type of the HID.
*
* @return the type of the HID.
*/
@Override
public HIDType getType() {
return HIDType.values()[m_ds.getJoystickType(getPort())];
}
/**
* Get the name of the HID.
*
* @return the name of the HID.
*/
@Override
public String getName() {
return m_ds.getJoystickName(getPort());
}
@Override
public void setOutput(int outputNumber, boolean value) {
m_outputs = (m_outputs & ~(1 << (outputNumber - 1))) | ((value ? 1 : 0) << (outputNumber - 1));
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
}
@Override
public void setOutputs(int value) {
m_outputs = value;
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
}
@Override
public void setRumble(RumbleType type, double value) {
if (value < 0) {
value = 0;
} else if (value > 1) {
value = 1;
}
if (type == RumbleType.kLeftRumble) {
m_leftRumble = (short) (value * 65535);
} else {
m_rightRumble = (short) (value * 65535);
}
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
}
}

View File

@@ -9,7 +9,10 @@ package edu.wpi.first.wpilibj;
/**
* JoystickBase Interface.
*
* @deprecated Inherit directly from GenericHID instead.
*/
@Deprecated
public abstract class JoystickBase extends GenericHID {
public JoystickBase(int port) {
super(port);
@@ -40,52 +43,4 @@ public abstract class JoystickBase extends GenericHID {
* @return the throttle value
*/
public abstract double getThrottle();
/**
* Is the trigger pressed.
*
* @return true if pressed
*/
public final boolean getTrigger() {
return getTrigger(Hand.kRight);
}
/**
* Is the trigger pressed.
*
* @param hand which hand
* @return true if the trigger for the given hand is pressed
*/
public abstract boolean getTrigger(Hand hand);
/**
* Is the top button pressed.
*
* @return true if the top button is pressed
*/
public final boolean getTop() {
return getTop(Hand.kRight);
}
/**
* Is the top button pressed.
*
* @param hand which hand
* @return true if hte top button for the given hand is pressed
*/
public abstract boolean getTop(Hand hand);
public abstract int getPOV(int pov);
public abstract int getPOVCount();
public abstract HIDType getType();
public abstract String getName();
public abstract void setOutput(int outputNumber, boolean value);
public abstract void setOutputs(int value);
public abstract void setRumble(RumbleType type, double value);
}

View File

@@ -17,11 +17,29 @@ import edu.wpi.first.wpilibj.hal.HAL;
* 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.
*/
public class XboxController extends GamepadBase {
private DriverStation m_ds;
private int m_outputs;
private short m_leftRumble;
private short m_rightRumble;
public class XboxController extends GenericHID {
/**
* Represents a digital button on an XboxController.
*/
private enum Button {
kBumperLeft(5),
kBumperRight(6),
kStickLeft(9),
kStickRight(10),
kA(1),
kB(2),
kX(3),
kY(4),
kBack(7),
kStart(8);
@SuppressWarnings("MemberName")
private int value;
Button(int value) {
this.value = value;
}
}
/**
* Construct an instance of a joystick. The joystick index is the USB port on the drivers
@@ -31,7 +49,6 @@ public class XboxController extends GamepadBase {
*/
public XboxController(final int port) {
super(port);
m_ds = DriverStation.getInstance();
// HAL.report(tResourceType.kResourceType_XboxController, port);
HAL.report(tResourceType.kResourceType_Joystick, port);
@@ -67,69 +84,6 @@ public class XboxController extends GamepadBase {
}
}
/**
* Get the value of the axis.
*
* @param axis The axis to read, starting at 0.
* @return The value of the axis.
*/
public double getRawAxis(final int axis) {
return m_ds.getStickAxis(getPort(), axis);
}
/**
* Read the value of the bumper button on the controller.
*
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
@Override
public boolean getBumper(Hand hand) {
if (hand.equals(Hand.kLeft)) {
return getRawButton(5);
} else {
return getRawButton(6);
}
}
/**
* This is not supported for the XboxController. This method is only here to complete the
* GenericHID interface.
*
* @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 (always false)
*/
@SuppressWarnings("PMD.UnusedFormalParameter")
public boolean getTrigger(Hand hand) {
return false;
}
/**
* This is not supported for the XboxController. This method is only here to complete the
* GenericHID interface.
*
* @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 (always false)
*/
@SuppressWarnings("PMD.UnusedFormalParameter")
public boolean getTop(Hand hand) {
return false;
}
/**
* Get the button value (starting at button 1).
*
* <p>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.
*/
public boolean getRawButton(final int button) {
return m_ds.getStickButton(getPort(), (byte) button);
}
/**
* Get the trigger axis value of the controller.
*
@@ -145,39 +99,45 @@ public class XboxController extends GamepadBase {
}
/**
* 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.
* @return The state of the button.
*/
public boolean getAButton() {
return getRawButton(1);
public boolean getBumper(Hand hand) {
if (hand.equals(Hand.kLeft)) {
return getRawButton(Button.kBumperLeft.value);
} else {
return getRawButton(Button.kBumperRight.value);
}
}
/**
* Read the value of the B button on the controller.
* Whether the bumper was pressed since the last check.
*
* @return The state of the button.
* @param hand Side of controller whose value should be returned.
* @return Whether the button was pressed since the last check.
*/
public boolean getBButton() {
return getRawButton(2);
public boolean getBumperPressed(Hand hand) {
if (hand == Hand.kLeft) {
return getRawButtonPressed(Button.kBumperLeft.value);
} else {
return getRawButtonPressed(Button.kBumperRight.value);
}
}
/**
* Read the value of the X button on the controller.
* Whether the bumper was released since the last check.
*
* @return The state of the button.
* @param hand Side of controller whose value should be returned.
* @return Whether the button was released since the last check.
*/
public boolean getXButton() {
return getRawButton(3);
}
/**
* Read the value of the Y button on the controller.
*
* @return The state of the button.
*/
public boolean getYButton() {
return getRawButton(4);
public boolean getBumperReleased(Hand hand) {
if (hand == Hand.kLeft) {
return getRawButtonReleased(Button.kBumperLeft.value);
} else {
return getRawButtonReleased(Button.kBumperRight.value);
}
}
/**
@@ -186,22 +146,175 @@ public class XboxController extends GamepadBase {
* @param hand Side of controller whose value should be returned.
* @return The state of the button.
*/
@Override
public boolean getStickButton(Hand hand) {
if (hand.equals(Hand.kLeft)) {
return getRawButton(9);
return getRawButton(Button.kStickLeft.value);
} else {
return getRawButton(10);
return getRawButton(Button.kStickRight.value);
}
}
/**
* 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.
*/
public boolean getStickButtonPressed(Hand hand) {
if (hand == Hand.kLeft) {
return getRawButtonPressed(Button.kStickLeft.value);
} else {
return getRawButtonPressed(Button.kStickRight.value);
}
}
/**
* 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.
*/
public boolean getStickButtonReleased(Hand hand) {
if (hand == Hand.kLeft) {
return getRawButtonReleased(Button.kStickLeft.value);
} else {
return getRawButtonReleased(Button.kStickRight.value);
}
}
/**
* Read the value of the A button on the controller.
*
* @return The state of the button.
*/
public boolean getAButton() {
return getRawButton(Button.kA.value);
}
/**
* Whether the A button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getAButtonPressed() {
return getRawButtonPressed(Button.kA.value);
}
/**
* Whether the A button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getAButtonReleased() {
return getRawButtonReleased(Button.kA.value);
}
/**
* Read the value of the B button on the controller.
*
* @return The state of the button.
*/
public boolean getBButton() {
return getRawButton(Button.kB.value);
}
/**
* Whether the B button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getBButtonPressed() {
return getRawButtonPressed(Button.kB.value);
}
/**
* Whether the B button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getBButtonReleased() {
return getRawButtonReleased(Button.kB.value);
}
/**
* Read the value of the X button on the controller.
*
* @return The state of the button.
*/
public boolean getXButton() {
return getRawButton(Button.kX.value);
}
/**
* Whether the X button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getXButtonPressed() {
return getRawButtonPressed(Button.kX.value);
}
/**
* Whether the X button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getXButtonReleased() {
return getRawButtonReleased(Button.kX.value);
}
/**
* Read the value of the Y button on the controller.
*
* @return The state of the button.
*/
public boolean getYButton() {
return getRawButton(Button.kY.value);
}
/**
* Whether the Y button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getYButtonPressed() {
return getRawButtonPressed(Button.kY.value);
}
/**
* Whether the Y button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getYButtonReleased() {
return getRawButtonReleased(Button.kY.value);
}
/**
* Read the value of the back button on the controller.
*
* @return The state of the button.
*/
public boolean getBackButton() {
return getRawButton(7);
return getRawButton(Button.kBack.value);
}
/**
* Whether the back button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getBackButtonPressed() {
return getRawButtonPressed(Button.kBack.value);
}
/**
* Whether the back button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getBackButtonReleased() {
return getRawButtonReleased(Button.kBack.value);
}
/**
@@ -210,53 +323,24 @@ public class XboxController extends GamepadBase {
* @return The state of the button.
*/
public boolean getStartButton() {
return getRawButton(8);
return getRawButton(Button.kStart.value);
}
@Override
public int getPOV(int pov) {
return m_ds.getStickPOV(getPort(), pov);
/**
* Whether the start button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
public boolean getStartButtonPressed() {
return getRawButtonPressed(Button.kStart.value);
}
@Override
public int getPOVCount() {
return m_ds.getStickPOVCount(getPort());
}
@Override
public HIDType getType() {
return HIDType.values()[m_ds.getJoystickType(getPort())];
}
@Override
public String getName() {
return m_ds.getJoystickName(getPort());
}
@Override
public void setOutput(int outputNumber, boolean value) {
m_outputs = (m_outputs & ~(1 << (outputNumber - 1))) | ((value ? 1 : 0) << (outputNumber - 1));
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
}
@Override
public void setOutputs(int value) {
m_outputs = value;
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
}
@Override
public void setRumble(RumbleType type, double value) {
if (value < 0) {
value = 0;
} else if (value > 1) {
value = 1;
}
if (type == RumbleType.kLeftRumble) {
m_leftRumble = (short) (value * 65535);
} else {
m_rightRumble = (short) (value * 65535);
}
HAL.setJoystickOutputs((byte) getPort(), m_outputs, m_leftRumble, m_rightRumble);
/**
* Whether the start button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
public boolean getStartButtonReleased() {
return getRawButtonReleased(Button.kStart.value);
}
}

View File

@@ -11,7 +11,6 @@ import edu.wpi.first.wpilibj.AnalogGyro;
import edu.wpi.first.wpilibj.AnalogInput;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Joystick.AxisType;
import edu.wpi.first.wpilibj.Spark;
import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.SpeedControllerGroup;
@@ -104,7 +103,7 @@ public class DriveTrain extends Subsystem {
* @param joy The ps3 style joystick to use to drive tank style.
*/
public void drive(Joystick joy) {
drive(-joy.getY(), -joy.getAxis(AxisType.kThrottle));
drive(-joy.getY(), -joy.getThrottle());
}
/**