[wpilib] Add PS4Controller, remove Hand from GenericHID/XboxController (#3345)

- GenericHID is now concrete, and has only getRawAxis/Button(int) functionality
- getXxx() has been moved into Joystick as that's the only place where it makes sense
- Hand (and therefore getXxx(Hand)) has been removed, replaced by specific getLeft/RightXxx() methods in XboxController and the new PS4Controller class
- C++ ::Button:: and ::Axis:: enums have been converted to identically-namespaced static constexpr ints
This commit is contained in:
Starlight220
2021-08-14 20:00:46 +03:00
committed by GitHub
parent 25f6f478a5
commit 031962608b
82 changed files with 2548 additions and 934 deletions

View File

@@ -61,11 +61,11 @@ int Joystick::GetThrottleChannel() const {
return m_axes[Axis::kThrottle];
}
double Joystick::GetX(JoystickHand hand) const {
double Joystick::GetX() const {
return GetRawAxis(m_axes[Axis::kX]);
}
double Joystick::GetY(JoystickHand hand) const {
double Joystick::GetY() const {
return GetRawAxis(m_axes[Axis::kY]);
}

View File

@@ -0,0 +1,205 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "frc/PS4Controller.h"
#include <hal/FRCUsageReporting.h>
using namespace frc;
PS4Controller::PS4Controller(int port) : GenericHID(port) {
HAL_Report(HALUsageReporting::kResourceType_PS4Controller, port + 1);
}
double PS4Controller::GetLeftX() const {
return GetRawAxis(Axis::kLeftX);
}
double PS4Controller::GetRightX() const {
return GetRawAxis(Axis::kRightX);
}
double PS4Controller::GetLeftY() const {
return GetRawAxis(Axis::kLeftY);
}
double PS4Controller::GetRightY() const {
return GetRawAxis(Axis::kRightY);
}
double PS4Controller::GetL2Axis() const {
return GetRawAxis(Axis::kL2);
}
double PS4Controller::GetR2Axis() const {
return GetRawAxis(Axis::kR2);
}
bool PS4Controller::GetSquareButton() const {
return GetRawButton(Button::kSquare);
}
bool PS4Controller::GetSquareButtonPressed() {
return GetRawButtonPressed(Button::kSquare);
}
bool PS4Controller::GetSquareButtonReleased() {
return GetRawButtonReleased(Button::kSquare);
}
bool PS4Controller::GetCrossButton() const {
return GetRawButton(Button::kCross);
}
bool PS4Controller::GetCrossButtonPressed() {
return GetRawButtonPressed(Button::kCross);
}
bool PS4Controller::GetCrossButtonReleased() {
return GetRawButtonReleased(Button::kCross);
}
bool PS4Controller::GetCircleButton() const {
return GetRawButton(Button::kCircle);
}
bool PS4Controller::GetCircleButtonPressed() {
return GetRawButtonPressed(Button::kCircle);
}
bool PS4Controller::GetCircleButtonReleased() {
return GetRawButtonReleased(Button::kCircle);
}
bool PS4Controller::GetTriangleButton() const {
return GetRawButton(Button::kTriangle);
}
bool PS4Controller::GetTriangleButtonPressed() {
return GetRawButtonPressed(Button::kTriangle);
}
bool PS4Controller::GetTriangleButtonReleased() {
return GetRawButtonReleased(Button::kTriangle);
}
bool PS4Controller::GetL1Button() const {
return GetRawButton(Button::kL1);
}
bool PS4Controller::GetL1ButtonPressed() {
return GetRawButtonPressed(Button::kL1);
}
bool PS4Controller::GetL1ButtonReleased() {
return GetRawButtonReleased(Button::kL1);
}
bool PS4Controller::GetR1Button() const {
return GetRawButton(Button::kR1);
}
bool PS4Controller::GetR1ButtonPressed() {
return GetRawButtonPressed(Button::kR1);
}
bool PS4Controller::GetR1ButtonReleased() {
return GetRawButtonReleased(Button::kR1);
}
bool PS4Controller::GetL2Button() const {
return GetRawButton(Button::kL2);
}
bool PS4Controller::GetL2ButtonPressed() {
return GetRawButtonPressed(Button::kL2);
}
bool PS4Controller::GetL2ButtonReleased() {
return GetRawButtonReleased(Button::kL2);
}
bool PS4Controller::GetR2Button() const {
return GetRawButton(Button::kR2);
}
bool PS4Controller::GetR2ButtonPressed() {
return GetRawButtonPressed(Button::kR2);
}
bool PS4Controller::GetR2ButtonReleased() {
return GetRawButtonReleased(Button::kR2);
}
bool PS4Controller::GetShareButton() const {
return GetRawButton(Button::kShare);
}
bool PS4Controller::GetShareButtonPressed() {
return GetRawButtonPressed(Button::kShare);
}
bool PS4Controller::GetShareButtonReleased() {
return GetRawButtonReleased(Button::kShare);
}
bool PS4Controller::GetOptionsButton() const {
return GetRawButton(Button::kOptions);
}
bool PS4Controller::GetOptionsButtonPressed() {
return GetRawButtonPressed(Button::kOptions);
}
bool PS4Controller::GetOptionsButtonReleased() {
return GetRawButtonReleased(Button::kOptions);
}
bool PS4Controller::GetL3Button() const {
return GetRawButton(Button::kL3);
}
bool PS4Controller::GetL3ButtonPressed() {
return GetRawButtonPressed(Button::kL3);
}
bool PS4Controller::GetL3ButtonReleased() {
return GetRawButtonReleased(Button::kL3);
}
bool PS4Controller::GetR3Button() const {
return GetRawButton(Button::kR3);
}
bool PS4Controller::GetR3ButtonPressed() {
return GetRawButtonPressed(Button::kR3);
}
bool PS4Controller::GetR3ButtonReleased() {
return GetRawButtonReleased(Button::kR3);
}
bool PS4Controller::GetPSButton() const {
return GetRawButton(Button::kPS);
}
bool PS4Controller::GetPSButtonPressed() {
return GetRawButtonPressed(Button::kPS);
}
bool PS4Controller::GetPSButtonReleased() {
return GetRawButtonReleased(Button::kPS);
}
bool PS4Controller::GetTouchpad() const {
return GetRawButton(Button::kTouchpad);
}
bool PS4Controller::GetTouchpadPressed() {
return GetRawButtonPressed(Button::kTouchpad);
}
bool PS4Controller::GetTouchpadReleased() {
return GetRawButtonReleased(Button::kTouchpad);
}

View File

@@ -12,146 +12,146 @@ XboxController::XboxController(int port) : GenericHID(port) {
HAL_Report(HALUsageReporting::kResourceType_XboxController, port + 1);
}
double XboxController::GetX(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawAxis(static_cast<int>(Axis::kLeftX));
} else {
return GetRawAxis(static_cast<int>(Axis::kRightX));
}
double XboxController::GetLeftX() const {
return GetRawAxis(Axis::kLeftX);
}
double XboxController::GetY(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawAxis(static_cast<int>(Axis::kLeftY));
} else {
return GetRawAxis(static_cast<int>(Axis::kRightY));
}
double XboxController::GetRightX() const {
return GetRawAxis(Axis::kRightX);
}
double XboxController::GetTriggerAxis(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawAxis(static_cast<int>(Axis::kLeftTrigger));
} else {
return GetRawAxis(static_cast<int>(Axis::kRightTrigger));
}
double XboxController::GetLeftY() const {
return GetRawAxis(Axis::kLeftY);
}
bool XboxController::GetBumper(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawButton(static_cast<int>(Button::kBumperLeft));
} else {
return GetRawButton(static_cast<int>(Button::kBumperRight));
}
double XboxController::GetRightY() const {
return GetRawAxis(Axis::kRightY);
}
bool XboxController::GetBumperPressed(JoystickHand hand) {
if (hand == kLeftHand) {
return GetRawButtonPressed(static_cast<int>(Button::kBumperLeft));
} else {
return GetRawButtonPressed(static_cast<int>(Button::kBumperRight));
}
double XboxController::GetLeftTriggerAxis() const {
return GetRawAxis(Axis::kLeftTrigger);
}
bool XboxController::GetBumperReleased(JoystickHand hand) {
if (hand == kLeftHand) {
return GetRawButtonReleased(static_cast<int>(Button::kBumperLeft));
} else {
return GetRawButtonReleased(static_cast<int>(Button::kBumperRight));
}
double XboxController::GetRightTriggerAxis() const {
return GetRawAxis(Axis::kRightTrigger);
}
bool XboxController::GetStickButton(JoystickHand hand) const {
if (hand == kLeftHand) {
return GetRawButton(static_cast<int>(Button::kStickLeft));
} else {
return GetRawButton(static_cast<int>(Button::kStickRight));
}
bool XboxController::GetLeftBumper() const {
return GetRawButton(Button::kLeftBumper);
}
bool XboxController::GetStickButtonPressed(JoystickHand hand) {
if (hand == kLeftHand) {
return GetRawButtonPressed(static_cast<int>(Button::kStickLeft));
} else {
return GetRawButtonPressed(static_cast<int>(Button::kStickRight));
}
bool XboxController::GetRightBumper() const {
return GetRawButton(Button::kRightBumper);
}
bool XboxController::GetStickButtonReleased(JoystickHand hand) {
if (hand == kLeftHand) {
return GetRawButtonReleased(static_cast<int>(Button::kStickLeft));
} else {
return GetRawButtonReleased(static_cast<int>(Button::kStickRight));
}
bool XboxController::GetLeftBumperPressed() {
return GetRawButtonPressed(Button::kLeftBumper);
}
bool XboxController::GetRightBumperPressed() {
return GetRawButtonPressed(Button::kRightBumper);
}
bool XboxController::GetLeftBumperReleased() {
return GetRawButtonReleased(Button::kLeftBumper);
}
bool XboxController::GetRightBumperReleased() {
return GetRawButtonReleased(Button::kRightBumper);
}
bool XboxController::GetLeftStickButton() const {
return GetRawButton(Button::kLeftStick);
}
bool XboxController::GetRightStickButton() const {
return GetRawButton(Button::kRightStick);
}
bool XboxController::GetLeftStickButtonPressed() {
return GetRawButtonPressed(Button::kLeftStick);
}
bool XboxController::GetRightStickButtonPressed() {
return GetRawButtonPressed(Button::kRightStick);
}
bool XboxController::GetLeftStickButtonReleased() {
return GetRawButtonReleased(Button::kLeftStick);
}
bool XboxController::GetRightStickButtonReleased() {
return GetRawButtonReleased(Button::kRightStick);
}
bool XboxController::GetAButton() const {
return GetRawButton(static_cast<int>(Button::kA));
return GetRawButton(Button::kA);
}
bool XboxController::GetAButtonPressed() {
return GetRawButtonPressed(static_cast<int>(Button::kA));
return GetRawButtonPressed(Button::kA);
}
bool XboxController::GetAButtonReleased() {
return GetRawButtonReleased(static_cast<int>(Button::kA));
return GetRawButtonReleased(Button::kA);
}
bool XboxController::GetBButton() const {
return GetRawButton(static_cast<int>(Button::kB));
return GetRawButton(Button::kB);
}
bool XboxController::GetBButtonPressed() {
return GetRawButtonPressed(static_cast<int>(Button::kB));
return GetRawButtonPressed(Button::kB);
}
bool XboxController::GetBButtonReleased() {
return GetRawButtonReleased(static_cast<int>(Button::kB));
return GetRawButtonReleased(Button::kB);
}
bool XboxController::GetXButton() const {
return GetRawButton(static_cast<int>(Button::kX));
return GetRawButton(Button::kX);
}
bool XboxController::GetXButtonPressed() {
return GetRawButtonPressed(static_cast<int>(Button::kX));
return GetRawButtonPressed(Button::kX);
}
bool XboxController::GetXButtonReleased() {
return GetRawButtonReleased(static_cast<int>(Button::kX));
return GetRawButtonReleased(Button::kX);
}
bool XboxController::GetYButton() const {
return GetRawButton(static_cast<int>(Button::kY));
return GetRawButton(Button::kY);
}
bool XboxController::GetYButtonPressed() {
return GetRawButtonPressed(static_cast<int>(Button::kY));
return GetRawButtonPressed(Button::kY);
}
bool XboxController::GetYButtonReleased() {
return GetRawButtonReleased(static_cast<int>(Button::kY));
return GetRawButtonReleased(Button::kY);
}
bool XboxController::GetBackButton() const {
return GetRawButton(static_cast<int>(Button::kBack));
return GetRawButton(Button::kBack);
}
bool XboxController::GetBackButtonPressed() {
return GetRawButtonPressed(static_cast<int>(Button::kBack));
return GetRawButtonPressed(Button::kBack);
}
bool XboxController::GetBackButtonReleased() {
return GetRawButtonReleased(static_cast<int>(Button::kBack));
return GetRawButtonReleased(Button::kBack);
}
bool XboxController::GetStartButton() const {
return GetRawButton(static_cast<int>(Button::kStart));
return GetRawButton(Button::kStart);
}
bool XboxController::GetStartButtonPressed() {
return GetRawButtonPressed(static_cast<int>(Button::kStart));
return GetRawButtonPressed(Button::kStart);
}
bool XboxController::GetStartButtonReleased() {
return GetRawButtonReleased(static_cast<int>(Button::kStart));
return GetRawButtonReleased(Button::kStart);
}

View File

@@ -0,0 +1,101 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "frc/simulation/PS4ControllerSim.h"
#include "frc/PS4Controller.h"
using namespace frc;
using namespace frc::sim;
PS4ControllerSim::PS4ControllerSim(const PS4Controller& joystick)
: GenericHIDSim{joystick} {
SetAxisCount(6);
SetButtonCount(14);
}
PS4ControllerSim::PS4ControllerSim(int port) : GenericHIDSim{port} {
SetAxisCount(6);
SetButtonCount(14);
}
void PS4ControllerSim::SetLeftX(double value) {
SetRawAxis(PS4Controller::Axis::kLeftX, value);
}
void PS4ControllerSim::SetRightX(double value) {
SetRawAxis(PS4Controller::Axis::kRightX, value);
}
void PS4ControllerSim::SetLeftY(double value) {
SetRawAxis(PS4Controller::Axis::kLeftY, value);
}
void PS4ControllerSim::SetRightY(double value) {
SetRawAxis(PS4Controller::Axis::kRightY, value);
}
void PS4ControllerSim::SetL2Axis(double value) {
SetRawAxis(PS4Controller::Axis::kL2, value);
}
void PS4ControllerSim::SetR2Axis(double value) {
SetRawAxis(PS4Controller::Axis::kR2, value);
}
void PS4ControllerSim::SetSquareButton(bool value) {
SetRawButton(PS4Controller::Button::kSquare, value);
}
void PS4ControllerSim::SetCrossButton(bool value) {
SetRawButton(PS4Controller::Button::kCross, value);
}
void PS4ControllerSim::SetCircleButton(bool value) {
SetRawButton(PS4Controller::Button::kCircle, value);
}
void PS4ControllerSim::SetTriangleButton(bool value) {
SetRawButton(PS4Controller::Button::kTriangle, value);
}
void PS4ControllerSim::SetL1Button(bool value) {
SetRawButton(PS4Controller::Button::kL1, value);
}
void PS4ControllerSim::SetR1Button(bool value) {
SetRawButton(PS4Controller::Button::kR1, value);
}
void PS4ControllerSim::SetL2Button(bool value) {
SetRawButton(PS4Controller::Button::kL2, value);
}
void PS4ControllerSim::SetR2Button(bool value) {
SetRawButton(PS4Controller::Button::kR2, value);
}
void PS4ControllerSim::SetShareButton(bool value) {
SetRawButton(PS4Controller::Button::kShare, value);
}
void PS4ControllerSim::SetOptionsButton(bool value) {
SetRawButton(PS4Controller::Button::kOptions, value);
}
void PS4ControllerSim::SetL3Button(bool value) {
SetRawButton(PS4Controller::Button::kL3, value);
}
void PS4ControllerSim::SetR3Button(bool value) {
SetRawButton(PS4Controller::Button::kR3, value);
}
void PS4ControllerSim::SetPSButton(bool value) {
SetRawButton(PS4Controller::Button::kPS, value);
}
void PS4ControllerSim::SetTouchpad(bool value) {
SetRawButton(PS4Controller::Button::kTouchpad, value);
}

View File

@@ -20,68 +20,66 @@ XboxControllerSim::XboxControllerSim(int port) : GenericHIDSim{port} {
SetButtonCount(10);
}
void XboxControllerSim::SetX(GenericHID::JoystickHand hand, double value) {
if (hand == GenericHID::kLeftHand) {
SetRawAxis(static_cast<int>(XboxController::Axis::kLeftX), value);
} else {
SetRawAxis(static_cast<int>(XboxController::Axis::kRightX), value);
}
void XboxControllerSim::SetLeftX(double value) {
SetRawAxis(XboxController::Axis::kLeftX, value);
}
void XboxControllerSim::SetY(GenericHID::JoystickHand hand, double value) {
if (hand == GenericHID::kLeftHand) {
SetRawAxis(static_cast<int>(XboxController::Axis::kLeftY), value);
} else {
SetRawAxis(static_cast<int>(XboxController::Axis::kRightY), value);
}
void XboxControllerSim::SetRightX(double value) {
SetRawAxis(XboxController::Axis::kRightX, value);
}
void XboxControllerSim::SetTriggerAxis(GenericHID::JoystickHand hand,
double value) {
if (hand == GenericHID::kLeftHand) {
SetRawAxis(static_cast<int>(XboxController::Axis::kLeftTrigger), value);
} else {
SetRawAxis(static_cast<int>(XboxController::Axis::kRightTrigger), value);
}
void XboxControllerSim::SetLeftY(double value) {
SetRawAxis(XboxController::Axis::kLeftY, value);
}
void XboxControllerSim::SetBumper(GenericHID::JoystickHand hand, bool state) {
if (hand == GenericHID::kLeftHand) {
SetRawButton(static_cast<int>(XboxController::Button::kBumperLeft), state);
} else {
SetRawButton(static_cast<int>(XboxController::Button::kBumperRight), state);
}
void XboxControllerSim::SetRightY(double value) {
SetRawAxis(XboxController::Axis::kRightY, value);
}
void XboxControllerSim::SetStickButton(GenericHID::JoystickHand hand,
bool state) {
if (hand == GenericHID::kLeftHand) {
SetRawButton(static_cast<int>(XboxController::Button::kStickLeft), state);
} else {
SetRawButton(static_cast<int>(XboxController::Button::kStickRight), state);
}
void XboxControllerSim::SetLeftTriggerAxis(double value) {
SetRawAxis(XboxController::Axis::kLeftTrigger, value);
}
void XboxControllerSim::SetRightTriggerAxis(double value) {
SetRawAxis(XboxController::Axis::kRightTrigger, value);
}
void XboxControllerSim::SetLeftBumper(bool state) {
SetRawButton(XboxController::Button::kLeftBumper, state);
}
void XboxControllerSim::SetRightBumper(bool state) {
SetRawButton(XboxController::Button::kRightBumper, state);
}
void XboxControllerSim::SetLeftStickButton(bool state) {
SetRawButton(XboxController::Button::kLeftStick, state);
}
void XboxControllerSim::SetRightStickButton(bool state) {
SetRawButton(XboxController::Button::kRightStick, state);
}
void XboxControllerSim::SetAButton(bool state) {
SetRawButton(static_cast<int>(XboxController::Button::kA), state);
SetRawButton(XboxController::Button::kA, state);
}
void XboxControllerSim::SetBButton(bool state) {
SetRawButton(static_cast<int>(XboxController::Button::kB), state);
SetRawButton(XboxController::Button::kB, state);
}
void XboxControllerSim::SetXButton(bool state) {
SetRawButton(static_cast<int>(XboxController::Button::kX), state);
SetRawButton(XboxController::Button::kX, state);
}
void XboxControllerSim::SetYButton(bool state) {
SetRawButton(static_cast<int>(XboxController::Button::kY), state);
SetRawButton(XboxController::Button::kY, state);
}
void XboxControllerSim::SetBackButton(bool state) {
SetRawButton(static_cast<int>(XboxController::Button::kBack), state);
SetRawButton(XboxController::Button::kBack, state);
}
void XboxControllerSim::SetStartButton(bool state) {
SetRawButton(static_cast<int>(XboxController::Button::kStart), state);
SetRawButton(XboxController::Button::kStart, state);
}