diff --git a/wpilibc/src/main/native/cpp/PS5Controller.cpp b/wpilibc/src/main/native/cpp/PS5Controller.cpp new file mode 100644 index 00000000000..4fce1dce952 --- /dev/null +++ b/wpilibc/src/main/native/cpp/PS5Controller.cpp @@ -0,0 +1,263 @@ +// 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 "PS5Controller.h" + +#include + +#include "frc/event/BooleanEvent.h" + +using namespace frc; + +PS5Controller::PS5Controller(int port) : GenericHID(port) { + HAL_Report(HALUsageReporting::kResourceType_PS4Controller, port + 1); +} + +double PS5Controller::GetLeftX() const { + return GetRawAxis(Axis::kLeftX); +} + +double PS5Controller::GetRightX() const { + return GetRawAxis(Axis::kRightX); +} + +double PS5Controller::GetLeftY() const { + return GetRawAxis(Axis::kLeftY); +} + +double PS5Controller::GetRightY() const { + return GetRawAxis(Axis::kRightY); +} + +double PS5Controller::GetL2Axis() const { + return GetRawAxis(Axis::kL2); +} + +double PS5Controller::GetR2Axis() const { + return GetRawAxis(Axis::kR2); +} + +bool PS5Controller::GetSquareButton() const { + return GetRawButton(Button::kSquare); +} + +bool PS5Controller::GetSquareButtonPressed() { + return GetRawButtonPressed(Button::kSquare); +} + +bool PS5Controller::GetSquareButtonReleased() { + return GetRawButtonReleased(Button::kSquare); +} + +BooleanEvent PS5Controller::Square(EventLoop* loop) const { + return BooleanEvent(loop, [this]() { return this->GetSquareButton(); }); +} + +bool PS5Controller::GetCrossButton() const { + return GetRawButton(Button::kCross); +} + +bool PS5Controller::GetCrossButtonPressed() { + return GetRawButtonPressed(Button::kCross); +} + +bool PS5Controller::GetCrossButtonReleased() { + return GetRawButtonReleased(Button::kCross); +} + +BooleanEvent PS5Controller::Cross(EventLoop* loop) const { + return BooleanEvent(loop, [this]() { return this->GetCrossButton(); }); +} + +bool PS5Controller::GetCircleButton() const { + return GetRawButton(Button::kCircle); +} + +bool PS5Controller::GetCircleButtonPressed() { + return GetRawButtonPressed(Button::kCircle); +} + +bool PS5Controller::GetCircleButtonReleased() { + return GetRawButtonReleased(Button::kCircle); +} + +BooleanEvent PS5Controller::Circle(EventLoop* loop) const { + return BooleanEvent(loop, [this]() { return this->GetCircleButton(); }); +} + +bool PS5Controller::GetTriangleButton() const { + return GetRawButton(Button::kTriangle); +} + +bool PS5Controller::GetTriangleButtonPressed() { + return GetRawButtonPressed(Button::kTriangle); +} + +bool PS5Controller::GetTriangleButtonReleased() { + return GetRawButtonReleased(Button::kTriangle); +} + +BooleanEvent PS5Controller::Triangle(EventLoop* loop) const { + return BooleanEvent(loop, [this]() { return this->GetTriangleButton(); }); +} + +bool PS5Controller::GetL1Button() const { + return GetRawButton(Button::kL1); +} + +bool PS5Controller::GetL1ButtonPressed() { + return GetRawButtonPressed(Button::kL1); +} + +bool PS5Controller::GetL1ButtonReleased() { + return GetRawButtonReleased(Button::kL1); +} + +BooleanEvent PS5Controller::L1(EventLoop* loop) const { + return BooleanEvent(loop, [this]() { return this->GetL1Button(); }); +} + +bool PS5Controller::GetR1Button() const { + return GetRawButton(Button::kR1); +} + +bool PS5Controller::GetR1ButtonPressed() { + return GetRawButtonPressed(Button::kR1); +} + +bool PS5Controller::GetR1ButtonReleased() { + return GetRawButtonReleased(Button::kR1); +} + +BooleanEvent PS5Controller::R1(EventLoop* loop) const { + return BooleanEvent(loop, [this]() { return this->GetR1Button(); }); +} + +bool PS5Controller::GetL2Button() const { + return GetRawButton(Button::kL2); +} + +bool PS5Controller::GetL2ButtonPressed() { + return GetRawButtonPressed(Button::kL2); +} + +bool PS5Controller::GetL2ButtonReleased() { + return GetRawButtonReleased(Button::kL2); +} + +BooleanEvent PS5Controller::L2(EventLoop* loop) const { + return BooleanEvent(loop, [this]() { return this->GetL2Button(); }); +} + +bool PS5Controller::GetR2Button() const { + return GetRawButton(Button::kR2); +} + +bool PS5Controller::GetR2ButtonPressed() { + return GetRawButtonPressed(Button::kR2); +} + +bool PS5Controller::GetR2ButtonReleased() { + return GetRawButtonReleased(Button::kR2); +} + +BooleanEvent PS5Controller::R2(EventLoop* loop) const { + return BooleanEvent(loop, [this]() { return this->GetR2Button(); }); +} + +bool PS5Controller::GetCreateButton() const { + return GetRawButton(Button::kCreate); +} + +bool PS5Controller::GetCreateButtonPressed() { + return GetRawButtonPressed(Button::kCreate); +} + +bool PS5Controller::GetCreateButtonReleased() { + return GetRawButtonReleased(Button::kCreate); +} + +BooleanEvent PS5Controller::Create(EventLoop* loop) const { + return BooleanEvent(loop, [this]() { return this->GetCreateButton(); }); +} + +bool PS5Controller::GetOptionsButton() const { + return GetRawButton(Button::kOptions); +} + +bool PS5Controller::GetOptionsButtonPressed() { + return GetRawButtonPressed(Button::kOptions); +} + +bool PS5Controller::GetOptionsButtonReleased() { + return GetRawButtonReleased(Button::kOptions); +} + +BooleanEvent PS5Controller::Options(EventLoop* loop) const { + return BooleanEvent(loop, [this]() { return this->GetOptionsButton(); }); +} + +bool PS5Controller::GetL3Button() const { + return GetRawButton(Button::kL3); +} + +bool PS5Controller::GetL3ButtonPressed() { + return GetRawButtonPressed(Button::kL3); +} + +bool PS5Controller::GetL3ButtonReleased() { + return GetRawButtonReleased(Button::kL3); +} + +BooleanEvent PS5Controller::L3(EventLoop* loop) const { + return BooleanEvent(loop, [this]() { return this->GetL3Button(); }); +} + +bool PS5Controller::GetR3Button() const { + return GetRawButton(Button::kR3); +} + +bool PS5Controller::GetR3ButtonPressed() { + return GetRawButtonPressed(Button::kR3); +} + +bool PS5Controller::GetR3ButtonReleased() { + return GetRawButtonReleased(Button::kR3); +} + +BooleanEvent PS5Controller::R3(EventLoop* loop) const { + return BooleanEvent(loop, [this]() { return this->GetR3Button(); }); +} + +bool PS5Controller::GetPSButton() const { + return GetRawButton(Button::kPS); +} + +bool PS5Controller::GetPSButtonPressed() { + return GetRawButtonPressed(Button::kPS); +} + +bool PS5Controller::GetPSButtonReleased() { + return GetRawButtonReleased(Button::kPS); +} + +BooleanEvent PS5Controller::PS(EventLoop* loop) const { + return BooleanEvent(loop, [this]() { return this->GetPSButton(); }); +} + +bool PS5Controller::GetTouchpad() const { + return GetRawButton(Button::kTouchpad); +} + +bool PS5Controller::GetTouchpadPressed() { + return GetRawButtonPressed(Button::kTouchpad); +} + +bool PS5Controller::GetTouchpadReleased() { + return GetRawButtonReleased(Button::kTouchpad); +} + +BooleanEvent PS5Controller::Touchpad(EventLoop* loop) const { + return BooleanEvent(loop, [this]() { return this->GetTouchpad(); }); +} diff --git a/wpilibc/src/main/native/include/frc/PS5Controller.h b/wpilibc/src/main/native/include/frc/PS5Controller.h new file mode 100644 index 00000000000..2a24f5e3612 --- /dev/null +++ b/wpilibc/src/main/native/include/frc/PS5Controller.h @@ -0,0 +1,529 @@ +// 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. + +#pragma once + +#include "frc/GenericHID.h" + +namespace frc { + +/** + * Handle input from PS5 controllers connected to the Driver Station. + * + *

This class handles PS5 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 controller and the mapping of ports to hardware + * buttons depends on the code in the Driver Station. + */ +class PS5Controller : public GenericHID { + public: + /** + * Construct an instance of an PS5 controller. + * + * The controller index is the USB port on the Driver Station. + * + * @param port The port on the Driver Station that the controller is plugged + * into (0-5). + */ + explicit PS5Controller(int port); + + ~PS5Controller() override = default; + + PS5Controller(PS5Controller&&) = default; + PS5Controller& operator=(PS5Controller&&) = default; + + /** + * Get the X axis value of left side of the controller. + * + * @return the axis value. + */ + double GetLeftX() const; + + /** + * Get the X axis value of right side of the controller. + * + * @return the axis value. + */ + double GetRightX() const; + + /** + * Get the Y axis value of left side of the controller. + * + * @return the axis value. + */ + double GetLeftY() const; + + /** + * Get the Y axis value of right side of the controller. + * + * @return the axis value. + */ + double GetRightY() const; + + /** + * Get the L2 axis value of the controller. Note that this axis is bound to + * the range of [0, 1] as opposed to the usual [-1, 1]. + * + * @return the axis value. + */ + double GetL2Axis() const; + + /** + * Get the R2 axis value of the controller. Note that this axis is bound to + * the range of [0, 1] as opposed to the usual [-1, 1]. + * + * @return the axis value. + */ + double GetR2Axis() const; + + /** + * Read the value of the Square button on the controller. + * + * @return The state of the button. + */ + bool GetSquareButton() const; + + /** + * Whether the Square button was pressed since the last check. + * + * @return Whether the button was pressed since the last check. + */ + bool GetSquareButtonPressed(); + + /** + * Whether the Square button was released since the last check. + * + * @return Whether the button was released since the last check. + */ + bool GetSquareButtonReleased(); + + /** + * Constructs an event instance around the square button's digital signal. + * + * @param loop the event loop instance to attach the event to. + * @return an event instance representing the square button's digital signal + * attached to the given loop. + */ + BooleanEvent Square(EventLoop* loop) const; + + /** + * Read the value of the Cross button on the controller. + * + * @return The state of the button. + */ + bool GetCrossButton() const; + + /** + * Whether the Cross button was pressed since the last check. + * + * @return Whether the button was pressed since the last check. + */ + bool GetCrossButtonPressed(); + + /** + * Whether the Cross button was released since the last check. + * + * @return Whether the button was released since the last check. + */ + bool GetCrossButtonReleased(); + + /** + * Constructs an event instance around the cross button's digital signal. + * + * @param loop the event loop instance to attach the event to. + * @return an event instance representing the cross button's digital signal + * attached to the given loop. + */ + BooleanEvent Cross(EventLoop* loop) const; + + /** + * Read the value of the Circle button on the controller. + * + * @return The state of the button. + */ + bool GetCircleButton() const; + + /** + * Whether the Circle button was pressed since the last check. + * + * @return Whether the button was pressed since the last check. + */ + bool GetCircleButtonPressed(); + + /** + * Whether the Circle button was released since the last check. + * + * @return Whether the button was released since the last check. + */ + bool GetCircleButtonReleased(); + + /** + * Constructs an event instance around the circle button's digital signal. + * + * @param loop the event loop instance to attach the event to. + * @return an event instance representing the circle button's digital signal + * attached to the given loop. + */ + BooleanEvent Circle(EventLoop* loop) const; + + /** + * Read the value of the Triangle button on the controller. + * + * @return The state of the button. + */ + bool GetTriangleButton() const; + + /** + * Whether the Triangle button was pressed since the last check. + * + * @return Whether the button was pressed since the last check. + */ + bool GetTriangleButtonPressed(); + + /** + * Whether the Triangle button was released since the last check. + * + * @return Whether the button was released since the last check. + */ + bool GetTriangleButtonReleased(); + + /** + * Constructs an event instance around the triangle button's digital signal. + * + * @param loop the event loop instance to attach the event to. + * @return an event instance representing the triangle button's digital signal + * attached to the given loop. + */ + BooleanEvent Triangle(EventLoop* loop) const; + + /** + * Read the value of the L1 button on the controller. + * + * @return The state of the button. + */ + bool GetL1Button() const; + + /** + * Whether the L1 button was pressed since the last check. + * + * @return Whether the button was pressed since the last check. + */ + bool GetL1ButtonPressed(); + + /** + * Whether the L1 button was released since the last check. + * + * @return Whether the button was released since the last check. + */ + bool GetL1ButtonReleased(); + + /** + * Constructs an event instance around the L1 button's digital signal. + * + * @param loop the event loop instance to attach the event to. + * @return an event instance representing the L1 button's digital signal + * attached to the given loop. + */ + BooleanEvent L1(EventLoop* loop) const; + + /** + * Read the value of the R1 button on the controller. + * + * @return The state of the button. + */ + bool GetR1Button() const; + + /** + * Whether the R1 button was pressed since the last check. + * + * @return Whether the button was pressed since the last check. + */ + bool GetR1ButtonPressed(); + + /** + * Whether the R1 button was released since the last check. + * + * @return Whether the button was released since the last check. + */ + bool GetR1ButtonReleased(); + + /** + * Constructs an event instance around the R1 button's digital signal. + * + * @param loop the event loop instance to attach the event to. + * @return an event instance representing the R1 button's digital signal + * attached to the given loop. + */ + BooleanEvent R1(EventLoop* loop) const; + + /** + * Read the value of the L2 button on the controller. + * + * @return The state of the button. + */ + bool GetL2Button() const; + + /** + * Whether the L2 button was pressed since the last check. + * + * @return Whether the button was pressed since the last check. + */ + bool GetL2ButtonPressed(); + + /** + * Whether the L2 button was released since the last check. + * + * @return Whether the button was released since the last check. + */ + bool GetL2ButtonReleased(); + + /** + * Constructs an event instance around the L2 button's digital signal. + * + * @param loop the event loop instance to attach the event to. + * @return an event instance representing the L2 button's digital signal + * attached to the given loop. + */ + BooleanEvent L2(EventLoop* loop) const; + + /** + * Read the value of the R2 button on the controller. + * + * @return The state of the button. + */ + bool GetR2Button() const; + + /** + * Whether the R2 button was pressed since the last check. + * + * @return Whether the button was pressed since the last check. + */ + bool GetR2ButtonPressed(); + + /** + * Whether the R2 button was released since the last check. + * + * @return Whether the button was released since the last check. + */ + bool GetR2ButtonReleased(); + + /** + * Constructs an event instance around the R2 button's digital signal. + * + * @param loop the event loop instance to attach the event to. + * @return an event instance representing the R2 button's digital signal + * attached to the given loop. + */ + BooleanEvent R2(EventLoop* loop) const; + + /** + * Read the value of the Create button on the controller. + * + * @return The state of the button. + */ + bool GetCreateButton() const; + + /** + * Whether the Create button was pressed since the last check. + * + * @return Whether the button was pressed since the last check. + */ + bool GetCreateButtonPressed(); + + /** + * Whether the Create button was released since the last check. + * + * @return Whether the button was released since the last check. + */ + bool GetCreateButtonReleased(); + + /** + * Constructs an event instance around the Create button's digital signal. + * + * @param loop the event loop instance to attach the event to. + * @return an event instance representing the Create button's digital signal + * attached to the given loop. + */ + BooleanEvent Create(EventLoop* loop) const; + + /** + * Read the value of the Options button on the controller. + * + * @return The state of the button. + */ + bool GetOptionsButton() const; + + /** + * Whether the Options button was pressed since the last check. + * + * @return Whether the button was pressed since the last check. + */ + bool GetOptionsButtonPressed(); + + /** + * Whether the Options button was released since the last check. + * + * @return Whether the button was released since the last check. + */ + bool GetOptionsButtonReleased(); + + /** + * Constructs an event instance around the options button's digital signal. + * + * @param loop the event loop instance to attach the event to. + * @return an event instance representing the options button's digital signal + * attached to the given loop. + */ + BooleanEvent Options(EventLoop* loop) const; + + /** + * Read the value of the L3 button (pressing the left analog stick) on the + * controller. + * + * @return The state of the button. + */ + bool GetL3Button() const; + + /** + * Whether the L3 (left stick) button was pressed since the last check. + * + * @return Whether the button was pressed since the last check. + */ + bool GetL3ButtonPressed(); + + /** + * Whether the L3 (left stick) button was released since the last check. + * + * @return Whether the button was released since the last check. + */ + bool GetL3ButtonReleased(); + + /** + * Constructs an event instance around the L3 button's digital signal. + * + * @param loop the event loop instance to attach the event to. + * @return an event instance representing the L3 button's digital signal + * attached to the given loop. + */ + BooleanEvent L3(EventLoop* loop) const; + + /** + * Read the value of the R3 button (pressing the right analog stick) on the + * controller. + * + * @return The state of the button. + */ + bool GetR3Button() const; + + /** + * Whether the R3 (right stick) button was pressed since the last check. + * + * @return Whether the button was pressed since the last check. + */ + bool GetR3ButtonPressed(); + + /** + * Whether the R3 (right stick) button was released since the last check. + * + * @return Whether the button was released since the last check. + */ + bool GetR3ButtonReleased(); + + /** + * Constructs an event instance around the R3 button's digital signal. + * + * @param loop the event loop instance to attach the event to. + * @return an event instance representing the R3 button's digital signal + * attached to the given loop. + */ + BooleanEvent R3(EventLoop* loop) const; + + /** + * Read the value of the PS button on the controller. + * + * @return The state of the button. + */ + bool GetPSButton() const; + + /** + * Whether the PS button was pressed since the last check. + * + * @return Whether the button was pressed since the last check. + */ + bool GetPSButtonPressed(); + + /** + * Whether the PS button was released since the last check. + * + * @return Whether the button was released since the last check. + */ + bool GetPSButtonReleased(); + + /** + * Constructs an event instance around the PS button's digital signal. + * + * @param loop the event loop instance to attach the event to. + * @return an event instance representing the PS button's digital signal + * attached to the given loop. + */ + BooleanEvent PS(EventLoop* loop) const; + + /** + * Read the value of the touchpad button on the controller. + * + * @return The state of the button. + */ + bool GetTouchpad() const; + + /** + * Whether the touchpad was pressed since the last check. + * + * @return Whether the touchpad was pressed since the last check. + */ + bool GetTouchpadPressed(); + + /** + * Whether the touchpad was released since the last check. + * + * @return Whether the touchpad was released since the last check. + */ + bool GetTouchpadReleased(); + + /** + * Constructs an event instance around the touchpad's digital signal. + * + * @param loop the event loop instance to attach the event to. + * @return an event instance representing the touchpad's digital signal + * attached to the given loop. + */ + BooleanEvent Touchpad(EventLoop* loop) const; + + struct Button { + static constexpr int kSquare = 3; + static constexpr int kCross = 1; + static constexpr int kCircle = 2; + static constexpr int kTriangle = 4; + static constexpr int kL1 = 5; + static constexpr int kR1 = 6; + static constexpr int kL2 = 7; + static constexpr int kR2 = 8; + static constexpr int kCreate = 9; + static constexpr int kOptions = 10; + static constexpr int kL3 = 12; + static constexpr int kR3 = 13; + static constexpr int kPS = 11; + static constexpr int kTouchpad = 14; + }; + + struct Axis { + static constexpr int kLeftX = 0; + static constexpr int kLeftY = 1; + static constexpr int kRightX = 3; + static constexpr int kRightY = 4; + static constexpr int kL2 = 2; + static constexpr int kR2 = 5; + }; +}; + +} // namespace frc