[commands] GenericHIDController: use composition in C++ (#6296)

This commit is contained in:
Jade
2024-05-25 07:36:05 +08:00
committed by GitHub
parent 221d568bd9
commit f1e072fc98
12 changed files with 469 additions and 129 deletions

View File

@@ -9,15 +9,29 @@
#include "frc2/command/CommandScheduler.h"
namespace frc2 {
/**
* A subclass of {@link GenericHID} with {@link Trigger} factories for
* A version of {@link frc::GenericHID} with {@link Trigger} factories for
* command-based.
*
* @see GenericHID
*/
class CommandGenericHID : public frc::GenericHID {
class CommandGenericHID {
public:
using GenericHID::GenericHID;
/**
* Construct an instance of a device.
*
* @param port The port index on the Driver Station that the device is plugged
* into.
*/
explicit CommandGenericHID(int port);
/**
* Get the underlying GenericHID object.
*
* @return the wrapped GenericHID object
*/
frc::GenericHID& GetHID();
/**
* Constructs an event instance around this button's digital signal.
@@ -215,5 +229,8 @@ class CommandGenericHID : public frc::GenericHID {
int axis, double threshold,
frc::EventLoop* loop =
CommandScheduler::GetInstance().GetDefaultButtonLoop()) const;
private:
frc::GenericHID m_hid;
};
} // namespace frc2

View File

@@ -7,30 +7,31 @@
#include "Trigger.h"
#include "frc2/command/CommandScheduler.h"
#include "frc2/command/button/CommandGenericHID.h"
namespace frc2 {
/**
* A version of {@link Joystick} with {@link Trigger} factories for
* A version of {@link frc::Joystick} with {@link Trigger} factories for
* command-based.
*
* @see Joystick
* @see frc::Joystick
*/
class CommandJoystick : public frc::Joystick {
class CommandJoystick : public CommandGenericHID {
public:
using Joystick::Joystick;
/**
* Construct an instance of a controller.
*
* @param port The port index on the Driver Station that the controller is
* plugged into.
*/
explicit CommandJoystick(int port);
/**
* Constructs an event instance around this button's digital signal.
* Get the underlying GenericHID object.
*
* @param button the button index
* @param loop the event loop instance to attach the event to. Defaults to the
* CommandScheduler's default loop.
* @return an event instance representing the button's digital signal attached
* to the given loop.
* @return the wrapped GenericHID object
*/
class Trigger Button(
int button, frc::EventLoop* loop = CommandScheduler::GetInstance()
.GetDefaultButtonLoop()) const;
frc::Joystick& GetHID();
/**
* Constructs an event instance around the trigger button's digital signal.
@@ -54,5 +55,22 @@ class CommandJoystick : public frc::Joystick {
*/
class Trigger Top(frc::EventLoop* loop = CommandScheduler::GetInstance()
.GetDefaultButtonLoop()) const;
/**
* Get the magnitude of the direction vector formed by the joystick's
* current position relative to its origin.
*
* @return The magnitude of the direction vector
*/
double GetMagnitude() const;
/**
* Get the direction of the vector formed by the joystick and its origin.
*
* @return The direction of the vector.
*/
units::radian_t GetDirection() const;
private:
frc::Joystick m_hid;
};
} // namespace frc2

View File

@@ -7,17 +7,31 @@
#include "Trigger.h"
#include "frc2/command/CommandScheduler.h"
#include "frc2/command/button/CommandGenericHID.h"
namespace frc2 {
/**
* A version of {@link PS4Controller} with {@link Trigger} factories for
* A version of {@link frc::PS4Controller} with {@link Trigger} factories for
* command-based.
*
* @see PS4Controller
* @see frc::PS4Controller
*/
class CommandPS4Controller : public frc::PS4Controller {
class CommandPS4Controller : public frc2::CommandGenericHID {
public:
using PS4Controller::PS4Controller;
/**
* Construct an instance of a device.
*
* @param port The port index on the Driver Station that the device is plugged
* into.
*/
explicit CommandPS4Controller(int port);
/**
* Get the underlying GenericHID object.
*
* @return the wrapped GenericHID object
*/
frc::PS4Controller& GetHID();
/**
* Constructs an event instance around this button's digital signal.
@@ -174,5 +188,52 @@ class CommandPS4Controller : public frc::PS4Controller {
*/
Trigger Touchpad(frc::EventLoop* loop = CommandScheduler::GetInstance()
.GetDefaultButtonLoop()) 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();
/**
* 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();
/**
* Get the Y axis value of right side of the controller.
*
* @return the axis value.
*/
double GetRightY();
/**
* Get the Y axis value of left side of the controller.
*
* @return the axis value.
*/
double GetLeftY();
/**
* Get the X axis value of right side of the controller.
*
* @return the axis value.
*/
double GetRightX();
/**
* Get the X axis value of left side of the controller.
*
* @return the axis value.
*/
double GetLeftX();
private:
frc::PS4Controller m_hid;
};
} // namespace frc2

View File

@@ -7,30 +7,31 @@
#include "Trigger.h"
#include "frc2/command/CommandScheduler.h"
#include "frc2/command/button/CommandGenericHID.h"
namespace frc2 {
/**
* A version of {@link PS5Controller} with {@link Trigger} factories for
* A version of {@link frc::PS5Controller} with {@link Trigger} factories for
* command-based.
*
* @see PS5Controller
* @see frc::PS5Controller
*/
class CommandPS5Controller : public frc::PS5Controller {
class CommandPS5Controller : public CommandGenericHID {
public:
using PS5Controller::PS5Controller;
/**
* Construct an instance of a device.
*
* @param port The port index on the Driver Station that the device is plugged
* into.
*/
explicit CommandPS5Controller(int port);
/**
* Constructs an event instance around this button's digital signal.
* Get the underlying GenericHID object.
*
* @param button the button index
* @param loop the event loop instance to attach the event to. Defaults to the
* CommandScheduler's default loop.
* @return an event instance representing the button's digital signal attached
* to the given loop.
* @return the wrapped GenericHID object
*/
Trigger Button(int button,
frc::EventLoop* loop = CommandScheduler::GetInstance()
.GetDefaultButtonLoop()) const;
frc::PS5Controller& GetHID();
/**
* Constructs an event instance around the square button's digital signal.
@@ -174,5 +175,52 @@ class CommandPS5Controller : public frc::PS5Controller {
*/
Trigger Touchpad(frc::EventLoop* loop = CommandScheduler::GetInstance()
.GetDefaultButtonLoop()) 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();
/**
* 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();
/**
* Get the Y axis value of right side of the controller.
*
* @return the axis value.
*/
double GetRightY();
/**
* Get the Y axis value of left side of the controller.
*
* @return the axis value.
*/
double GetLeftY();
/**
* Get the X axis value of right side of the controller.
*
* @return the axis value.
*/
double GetRightX();
/**
* Get the X axis value of left side of the controller.
*
* @return the axis value.
*/
double GetLeftX();
private:
frc::PS5Controller m_hid;
};
} // namespace frc2

View File

@@ -7,30 +7,31 @@
#include "Trigger.h"
#include "frc2/command/CommandScheduler.h"
#include "frc2/command/button/CommandGenericHID.h"
namespace frc2 {
/**
* A version of {@link StadiaController} with {@link Trigger} factories for
* A version of {@link frc::StadiaController} with {@link Trigger} factories for
* command-based.
*
* @see StadiaController
* @see frc::StadiaController
*/
class CommandStadiaController : public frc::StadiaController {
class CommandStadiaController : public CommandGenericHID {
public:
using StadiaController::StadiaController;
/**
* Construct an instance of a controller.
*
* @param port The port index on the Driver Station that the controller is
* plugged into.
*/
explicit CommandStadiaController(int port);
/**
* Constructs an event instance around this button's digital signal.
* Get the underlying GenericHID object.
*
* @param button the button index
* @param loop the event loop instance to attach the event to. Defaults to the
* CommandScheduler's default loop.
* @return an event instance representing the button's digital signal attached
* to the given loop.
* @return the wrapped GenericHID object
*/
Trigger Button(int button,
frc::EventLoop* loop = CommandScheduler::GetInstance()
.GetDefaultButtonLoop()) const;
frc::StadiaController& GetHID();
/**
* Constructs an event instance around the left bumper's digital signal.
@@ -197,5 +198,36 @@ class CommandStadiaController : public frc::StadiaController {
Trigger RightTrigger(
frc::EventLoop* loop =
CommandScheduler::GetInstance().GetDefaultButtonLoop()) const;
/**
* 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;
private:
frc::StadiaController m_hid;
};
} // namespace frc2

View File

@@ -7,30 +7,31 @@
#include "Trigger.h"
#include "frc2/command/CommandScheduler.h"
#include "frc2/command/button/CommandGenericHID.h"
namespace frc2 {
/**
* A version of {@link XboxController} with {@link Trigger} factories for
* A version of {@link frc::XboxController} with {@link Trigger} factories for
* command-based.
*
* @see XboxController
* @see frc::XboxController
*/
class CommandXboxController : public frc::XboxController {
class CommandXboxController : CommandGenericHID {
public:
using XboxController::XboxController;
/**
* Construct an instance of a controller.
*
* @param port The port index on the Driver Station that the controller is
* plugged into.
*/
explicit CommandXboxController(int port);
/**
* Constructs an event instance around this button's digital signal.
* Get the underlying GenericHID object.
*
* @param button the button index
* @param loop the event loop instance to attach the event to. Defaults to the
* CommandScheduler's default loop.
* @return an event instance representing the button's digital signal attached
* to the given loop.
* @return the wrapped GenericHID object
*/
Trigger Button(int button,
frc::EventLoop* loop = CommandScheduler::GetInstance()
.GetDefaultButtonLoop()) const;
frc::XboxController& GetHID();
/**
* Constructs an event instance around the left bumper's digital signal.
@@ -176,5 +177,52 @@ class CommandXboxController : public frc::XboxController {
double threshold = 0.5,
frc::EventLoop* loop =
CommandScheduler::GetInstance().GetDefaultButtonLoop()) const;
/**
* Get the right trigger (RT) 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 GetRightTriggerAxis();
/**
* Get the left trigger (LT) 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 GetLeftTriggerAxis();
/**
* Get the Y axis value of right side of the controller.
*
* @return The axis value.
*/
double GetRightY();
/**
* Get the Y axis value of left side of the controller.
*
* @return The axis value.
*/
double GetLeftY();
/**
* Get the X axis value of right side of the controller.
*
* @return The axis value.
*/
double GetRightX();
/**
* Get the X axis value of left side of the controller.
*
* @return The axis value.
*/
double GetLeftX();
private:
frc::XboxController m_hid;
};
} // namespace frc2