diff --git a/commandsv2/src/main/java/org/wpilib/command2/button/CommandGamepad.java b/commandsv2/src/main/java/org/wpilib/command2/button/CommandGamepad.java index 39cab6ef58..6c3291108d 100644 --- a/commandsv2/src/main/java/org/wpilib/command2/button/CommandGamepad.java +++ b/commandsv2/src/main/java/org/wpilib/command2/button/CommandGamepad.java @@ -37,6 +37,29 @@ public class CommandGamepad extends CommandGenericHID { return m_hid; } + /** + * Constructs an event instance around this button's digital signal. + * + * @param button the button + * @return an event instance representing the button's digital signal attached to the {@link + * CommandScheduler#getDefaultButtonLoop() default scheduler button loop}. + * @see #button(Gamepad.Button, EventLoop) + */ + public Trigger button(Gamepad.Button button) { + return super.button(button.value); + } + + /** + * Constructs an event instance around this button's digital signal. + * + * @param button the button + * @param loop the event loop instance to attach the event to. + * @return an event instance representing the button's digital signal attached to the given loop. + */ + public Trigger button(Gamepad.Button button, EventLoop loop) { + return button(button.value, loop); + } + /** * Constructs a Trigger instance around the South Face button's digital signal. * @@ -56,7 +79,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger southFace(EventLoop loop) { - return button(Gamepad.Button.kSouthFace.value, loop); + return button(Gamepad.Button.SOUTH_FACE, loop); } /** @@ -78,7 +101,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger eastFace(EventLoop loop) { - return button(Gamepad.Button.kEastFace.value, loop); + return button(Gamepad.Button.EAST_FACE, loop); } /** @@ -100,7 +123,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger westFace(EventLoop loop) { - return button(Gamepad.Button.kWestFace.value, loop); + return button(Gamepad.Button.WEST_FACE, loop); } /** @@ -122,7 +145,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger northFace(EventLoop loop) { - return button(Gamepad.Button.kNorthFace.value, loop); + return button(Gamepad.Button.NORTH_FACE, loop); } /** @@ -144,7 +167,7 @@ public class CommandGamepad extends CommandGenericHID { * loop. */ public Trigger back(EventLoop loop) { - return button(Gamepad.Button.kBack.value, loop); + return button(Gamepad.Button.BACK, loop); } /** @@ -166,7 +189,7 @@ public class CommandGamepad extends CommandGenericHID { * loop. */ public Trigger guide(EventLoop loop) { - return button(Gamepad.Button.kGuide.value, loop); + return button(Gamepad.Button.GUIDE, loop); } /** @@ -188,7 +211,7 @@ public class CommandGamepad extends CommandGenericHID { * loop. */ public Trigger start(EventLoop loop) { - return button(Gamepad.Button.kStart.value, loop); + return button(Gamepad.Button.START, loop); } /** @@ -210,7 +233,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger leftStick(EventLoop loop) { - return button(Gamepad.Button.kLeftStick.value, loop); + return button(Gamepad.Button.LEFT_STICK, loop); } /** @@ -232,7 +255,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger rightStick(EventLoop loop) { - return button(Gamepad.Button.kRightStick.value, loop); + return button(Gamepad.Button.RIGHT_STICK, loop); } /** @@ -254,7 +277,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger leftBumper(EventLoop loop) { - return button(Gamepad.Button.kLeftBumper.value, loop); + return button(Gamepad.Button.LEFT_BUMPER, loop); } /** @@ -276,7 +299,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger rightBumper(EventLoop loop) { - return button(Gamepad.Button.kRightBumper.value, loop); + return button(Gamepad.Button.RIGHT_BUMPER, loop); } /** @@ -298,7 +321,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger dpadUp(EventLoop loop) { - return button(Gamepad.Button.kDpadUp.value, loop); + return button(Gamepad.Button.DPAD_UP, loop); } /** @@ -320,7 +343,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger dpadDown(EventLoop loop) { - return button(Gamepad.Button.kDpadDown.value, loop); + return button(Gamepad.Button.DPAD_DOWN, loop); } /** @@ -342,7 +365,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger dpadLeft(EventLoop loop) { - return button(Gamepad.Button.kDpadLeft.value, loop); + return button(Gamepad.Button.DPAD_LEFT, loop); } /** @@ -364,7 +387,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger dpadRight(EventLoop loop) { - return button(Gamepad.Button.kDpadRight.value, loop); + return button(Gamepad.Button.DPAD_RIGHT, loop); } /** @@ -386,7 +409,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger misc1(EventLoop loop) { - return button(Gamepad.Button.kMisc1.value, loop); + return button(Gamepad.Button.MISC_1, loop); } /** @@ -408,7 +431,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger rightPaddle1(EventLoop loop) { - return button(Gamepad.Button.kRightPaddle1.value, loop); + return button(Gamepad.Button.RIGHT_PADDLE_1, loop); } /** @@ -430,7 +453,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger leftPaddle1(EventLoop loop) { - return button(Gamepad.Button.kLeftPaddle1.value, loop); + return button(Gamepad.Button.LEFT_PADDLE_1, loop); } /** @@ -452,7 +475,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger rightPaddle2(EventLoop loop) { - return button(Gamepad.Button.kRightPaddle2.value, loop); + return button(Gamepad.Button.RIGHT_PADDLE_2, loop); } /** @@ -474,7 +497,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger leftPaddle2(EventLoop loop) { - return button(Gamepad.Button.kLeftPaddle2.value, loop); + return button(Gamepad.Button.LEFT_PADDLE_2, loop); } /** @@ -496,7 +519,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger touchpad(EventLoop loop) { - return button(Gamepad.Button.kTouchpad.value, loop); + return button(Gamepad.Button.TOUCHPAD, loop); } /** @@ -518,7 +541,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger misc2(EventLoop loop) { - return button(Gamepad.Button.kMisc2.value, loop); + return button(Gamepad.Button.MISC_2, loop); } /** @@ -540,7 +563,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger misc3(EventLoop loop) { - return button(Gamepad.Button.kMisc3.value, loop); + return button(Gamepad.Button.MISC_3, loop); } /** @@ -562,7 +585,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger misc4(EventLoop loop) { - return button(Gamepad.Button.kMisc4.value, loop); + return button(Gamepad.Button.MISC_4, loop); } /** @@ -584,7 +607,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger misc5(EventLoop loop) { - return button(Gamepad.Button.kMisc5.value, loop); + return button(Gamepad.Button.MISC_5, loop); } /** @@ -606,7 +629,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger misc6(EventLoop loop) { - return button(Gamepad.Button.kMisc6.value, loop); + return button(Gamepad.Button.MISC_6, loop); } /** @@ -620,7 +643,7 @@ public class CommandGamepad extends CommandGenericHID { * threshold, attached to the given event loop */ public Trigger leftTrigger(double threshold, EventLoop loop) { - return axisGreaterThan(Gamepad.Axis.kLeftTrigger.value, threshold, loop); + return axisGreaterThan(Gamepad.Axis.LEFT_TRIGGER, threshold, loop); } /** @@ -659,7 +682,7 @@ public class CommandGamepad extends CommandGenericHID { * threshold, attached to the given event loop */ public Trigger rightTrigger(double threshold, EventLoop loop) { - return axisGreaterThan(Gamepad.Axis.kRightTrigger.value, threshold, loop); + return axisGreaterThan(Gamepad.Axis.RIGHT_TRIGGER, threshold, loop); } /** @@ -687,6 +710,99 @@ public class CommandGamepad extends CommandGenericHID { return rightTrigger(0.5); } + /** + * Constructs a Trigger instance that is true when the axis value is less than {@code threshold}, + * attached to {@link CommandScheduler#getDefaultButtonLoop() the default command scheduler button + * loop}. + * + * @param axis The axis to read + * @param threshold The value below which this trigger should return true. + * @return a Trigger instance that is true when the axis value is less than the provided + * threshold. + */ + public Trigger axisLessThan(Gamepad.Axis axis, double threshold) { + return super.axisLessThan(axis.value, threshold); + } + + /** + * Constructs a Trigger instance that is true when the axis value is less than {@code threshold}, + * attached to the given loop. + * + * @param axis The axis to read + * @param threshold The value below which this trigger should return true. + * @param loop the event loop instance to attach the trigger to + * @return a Trigger instance that is true when the axis value is less than the provided + * threshold. + */ + public Trigger axisLessThan(Gamepad.Axis axis, double threshold, EventLoop loop) { + return super.axisLessThan(axis.value, threshold, loop); + } + + /** + * Constructs a Trigger instance that is true when the axis value is less than {@code threshold}, + * attached to {@link CommandScheduler#getDefaultButtonLoop() the default command scheduler button + * loop}. + * + * @param axis The axis to read + * @param threshold The value above which this trigger should return true. + * @return a Trigger instance that is true when the axis value is greater than the provided + * threshold. + */ + public Trigger axisGreaterThan(Gamepad.Axis axis, double threshold) { + return super.axisGreaterThan(axis.value, threshold); + } + + /** + * Constructs a Trigger instance that is true when the axis value is greater than {@code + * threshold}, attached to the given loop. + * + * @param axis The axis to read + * @param threshold The value above which this trigger should return true. + * @param loop the event loop instance to attach the trigger to. + * @return a Trigger instance that is true when the axis value is greater than the provided + * threshold. + */ + public Trigger axisGreaterThan(Gamepad.Axis axis, double threshold, EventLoop loop) { + return super.axisGreaterThan(axis.value, threshold, loop); + } + + /** + * Constructs a Trigger instance that is true when the axis magnitude value is greater than {@code + * threshold}, attached to the given loop. + * + * @param axis The axis to read + * @param threshold The value above which this trigger should return true. + * @param loop the event loop instance to attach the trigger to. + * @return a Trigger instance that is true when the axis magnitude value is greater than the + * provided threshold. + */ + public Trigger axisMagnitudeGreaterThan(Gamepad.Axis axis, double threshold, EventLoop loop) { + return super.axisMagnitudeGreaterThan(axis.value, threshold, loop); + } + + /** + * Constructs a Trigger instance that is true when the axis magnitude value is greater than {@code + * threshold}, attached to {@link CommandScheduler#getDefaultButtonLoop() the default command + * scheduler button loop}. + * + * @param axis The axis to read + * @param threshold The value above which this trigger should return true. + * @return a Trigger instance that is true when the deadbanded axis value is active (non-zero). + */ + public Trigger axisMagnitudeGreaterThan(Gamepad.Axis axis, double threshold) { + return super.axisMagnitudeGreaterThan(axis.value, threshold); + } + + /** + * Get the value of the axis. + * + * @param axis The axis to read + * @return The value of the axis. + */ + public double getAxis(Gamepad.Axis axis) { + return getRawAxis(axis.value); + } + /** * Get the X axis value of left side of the controller. Right is positive. * diff --git a/commandsv2/src/main/java/org/wpilib/command2/button/GamepadButton.java b/commandsv2/src/main/java/org/wpilib/command2/button/GamepadButton.java new file mode 100644 index 0000000000..f69f46e971 --- /dev/null +++ b/commandsv2/src/main/java/org/wpilib/command2/button/GamepadButton.java @@ -0,0 +1,27 @@ +// 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. + +package org.wpilib.command2.button; + +import static org.wpilib.util.ErrorMessages.requireNonNullParam; + +import org.wpilib.driverstation.Gamepad; + +/** + * A {@link Trigger} that gets its state from a {@link Gamepad}. + * + *

This class is provided by the NewCommands VendorDep + */ +public class GamepadButton extends Trigger { + /** + * Creates a gamepad button for triggering commands. + * + * @param gamepad The gamepad on which the button is located. + * @param button The button + */ + public GamepadButton(Gamepad gamepad, Gamepad.Button button) { + super(() -> gamepad.getButton(button)); + requireNonNullParam(gamepad, "gamepad", "GamepadButton"); + } +} diff --git a/commandsv2/src/main/native/cpp/frc2/command/button/CommandGamepad.cpp b/commandsv2/src/main/native/cpp/frc2/command/button/CommandGamepad.cpp index d48deb41f2..c646fa30f3 100644 --- a/commandsv2/src/main/native/cpp/frc2/command/button/CommandGamepad.cpp +++ b/commandsv2/src/main/native/cpp/frc2/command/button/CommandGamepad.cpp @@ -4,6 +4,8 @@ #include "wpi/commands2/button/CommandGamepad.hpp" +#include "wpi/commands2/button/CommandGenericHID.hpp" + using namespace wpi::cmd; CommandGamepad::CommandGamepad(int port) @@ -13,108 +15,113 @@ wpi::Gamepad& CommandGamepad::GetHID() { return m_hid; } +Trigger CommandGamepad::Button(enum wpi::Gamepad::Button button, + wpi::EventLoop* loop) const { + return CommandGenericHID::Button(static_cast(button), loop); +} + Trigger CommandGamepad::SouthFace(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kSouthFace, loop); + return Button(wpi::Gamepad::Button::SOUTH_FACE, loop); } Trigger CommandGamepad::EastFace(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kEastFace, loop); + return Button(wpi::Gamepad::Button::EAST_FACE, loop); } Trigger CommandGamepad::WestFace(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kWestFace, loop); + return Button(wpi::Gamepad::Button::WEST_FACE, loop); } Trigger CommandGamepad::NorthFace(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kNorthFace, loop); + return Button(wpi::Gamepad::Button::NORTH_FACE, loop); } Trigger CommandGamepad::Back(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kBack, loop); + return Button(wpi::Gamepad::Button::BACK, loop); } Trigger CommandGamepad::Guide(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kGuide, loop); + return Button(wpi::Gamepad::Button::GUIDE, loop); } Trigger CommandGamepad::Start(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kStart, loop); + return Button(wpi::Gamepad::Button::START, loop); } Trigger CommandGamepad::LeftStick(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kLeftStick, loop); + return Button(wpi::Gamepad::Button::LEFT_STICK, loop); } Trigger CommandGamepad::RightStick(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kRightStick, loop); + return Button(wpi::Gamepad::Button::RIGHT_STICK, loop); } Trigger CommandGamepad::LeftBumper(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kLeftBumper, loop); + return Button(wpi::Gamepad::Button::LEFT_BUMPER, loop); } Trigger CommandGamepad::RightBumper(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kRightBumper, loop); + return Button(wpi::Gamepad::Button::RIGHT_BUMPER, loop); } Trigger CommandGamepad::DpadUp(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kDpadUp, loop); + return Button(wpi::Gamepad::Button::DPAD_UP, loop); } Trigger CommandGamepad::DpadDown(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kDpadDown, loop); + return Button(wpi::Gamepad::Button::DPAD_DOWN, loop); } Trigger CommandGamepad::DpadLeft(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kDpadLeft, loop); + return Button(wpi::Gamepad::Button::DPAD_LEFT, loop); } Trigger CommandGamepad::DpadRight(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kDpadRight, loop); + return Button(wpi::Gamepad::Button::DPAD_RIGHT, loop); } Trigger CommandGamepad::Misc1(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kMisc1, loop); + return Button(wpi::Gamepad::Button::MISC_1, loop); } Trigger CommandGamepad::RightPaddle1(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kRightPaddle1, loop); + return Button(wpi::Gamepad::Button::RIGHT_PADDLE_1, loop); } Trigger CommandGamepad::LeftPaddle1(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kLeftPaddle1, loop); + return Button(wpi::Gamepad::Button::LEFT_PADDLE_1, loop); } Trigger CommandGamepad::RightPaddle2(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kRightPaddle2, loop); + return Button(wpi::Gamepad::Button::RIGHT_PADDLE_2, loop); } Trigger CommandGamepad::LeftPaddle2(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kLeftPaddle2, loop); + return Button(wpi::Gamepad::Button::LEFT_PADDLE_2, loop); } Trigger CommandGamepad::Touchpad(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kTouchpad, loop); + return Button(wpi::Gamepad::Button::TOUCHPAD, loop); } Trigger CommandGamepad::Misc2(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kMisc2, loop); + return Button(wpi::Gamepad::Button::MISC_2, loop); } Trigger CommandGamepad::Misc3(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kMisc3, loop); + return Button(wpi::Gamepad::Button::MISC_3, loop); } Trigger CommandGamepad::Misc4(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kMisc4, loop); + return Button(wpi::Gamepad::Button::MISC_4, loop); } Trigger CommandGamepad::Misc5(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kMisc5, loop); + return Button(wpi::Gamepad::Button::MISC_5, loop); } Trigger CommandGamepad::Misc6(wpi::EventLoop* loop) const { - return Button(wpi::Gamepad::Button::kMisc6, loop); + return Button(wpi::Gamepad::Button::MISC_6, loop); } Trigger CommandGamepad::LeftTrigger(double threshold, @@ -131,6 +138,26 @@ Trigger CommandGamepad::RightTrigger(double threshold, }); } +Trigger CommandGamepad::AxisLessThan(wpi::Gamepad::Axis axis, double threshold, + wpi::EventLoop* loop) const { + return CommandGenericHID::AxisLessThan(static_cast(axis), threshold, + loop); +} + +Trigger CommandGamepad::AxisGreaterThan(wpi::Gamepad::Axis axis, + double threshold, + wpi::EventLoop* loop) const { + return CommandGenericHID::AxisGreaterThan(static_cast(axis), threshold, + loop); +} + +Trigger CommandGamepad::AxisMagnitudeGreaterThan(wpi::Gamepad::Axis axis, + double threshold, + wpi::EventLoop* loop) const { + return CommandGenericHID::AxisMagnitudeGreaterThan(static_cast(axis), + threshold, loop); +} + double CommandGamepad::GetLeftX() const { return m_hid.GetLeftX(); } diff --git a/commandsv2/src/main/native/include/wpi/commands2/button/CommandGamepad.hpp b/commandsv2/src/main/native/include/wpi/commands2/button/CommandGamepad.hpp index 30db1f4cb0..121a722433 100644 --- a/commandsv2/src/main/native/include/wpi/commands2/button/CommandGamepad.hpp +++ b/commandsv2/src/main/native/include/wpi/commands2/button/CommandGamepad.hpp @@ -32,6 +32,19 @@ class CommandGamepad : public CommandGenericHID { */ wpi::Gamepad& GetHID(); + /** + * Constructs an event instance around this button's digital signal. + * + * @param button the button + * @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. + */ + Trigger Button(enum wpi::Gamepad::Button button, + wpi::EventLoop* loop = CommandScheduler::GetInstance() + .GetDefaultButtonLoop()) const; + /** * Constructs a Trigger instance around the South Face button's * digital signal. @@ -381,6 +394,57 @@ class CommandGamepad : public CommandGenericHID { wpi::EventLoop* loop = CommandScheduler::GetInstance().GetDefaultButtonLoop()) const; + /** + * Constructs a Trigger instance that is true when the axis value is less than + * {@code threshold}, attached to {@link + * CommandScheduler::GetDefaultButtonLoop() the default command scheduler + * button loop}. + * @param axis The axis to read, starting at 0. + * @param threshold The value below which this trigger should return true. + * @param loop the event loop instance to attach the event to. Defaults to + * {@link CommandScheduler::GetDefaultButtonLoop() the default command + * scheduler button loop}. + * @return a Trigger instance that is true when the axis value is less than + * the provided threshold. + */ + Trigger AxisLessThan( + wpi::Gamepad::Axis axis, double threshold, + wpi::EventLoop* loop = + CommandScheduler::GetInstance().GetDefaultButtonLoop()) const; + + /** + * Constructs a Trigger instance that is true when the axis value is greater + * than {@code threshold}, attached to {@link + * CommandScheduler::GetDefaultButtonLoop() the default command scheduler + * button loop}. + * @param axis The axis to read + * @param threshold The value below which this trigger should return true. + * @param loop the event loop instance to attach the event to. Defaults to + * {@link CommandScheduler::GetDefaultButtonLoop() the default command + * scheduler button loop}. + * @return a Trigger instance that is true when the axis value is greater than + * the provided threshold. + */ + Trigger AxisGreaterThan( + wpi::Gamepad::Axis axis, double threshold, + wpi::EventLoop* loop = + CommandScheduler::GetInstance().GetDefaultButtonLoop()) const; + + /** + * Constructs a Trigger instance that is true when the axis magnitude value is + * greater than {@code threshold}, attached to the given loop. + * + * @param axis The axis to read + * @param threshold The value above which this trigger should return true. + * @param loop the event loop instance to attach the trigger to. + * @return a Trigger instance that is true when the axis magnitude value is + * greater than the provided threshold. + */ + Trigger AxisMagnitudeGreaterThan( + wpi::Gamepad::Axis axis, double threshold, + wpi::EventLoop* loop = + CommandScheduler::GetInstance().GetDefaultButtonLoop()) const; + /** * Get the X axis value of left side of the controller. Right is positive. * diff --git a/commandsv2/src/main/native/include/wpi/commands2/button/GamepadButton.hpp b/commandsv2/src/main/native/include/wpi/commands2/button/GamepadButton.hpp new file mode 100644 index 0000000000..a777ee6428 --- /dev/null +++ b/commandsv2/src/main/native/include/wpi/commands2/button/GamepadButton.hpp @@ -0,0 +1,30 @@ +// 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 "wpi/commands2/button/Trigger.hpp" +#include "wpi/driverstation/Gamepad.hpp" + +namespace wpi::cmd { +/** + * A class used to bind command scheduling to gamepad button presses. Can be + * composed with other buttons with the operators in Trigger. + * + * This class is provided by the NewCommands VendorDep + * + * @see Trigger + */ +class GamepadButton : public Trigger { + public: + /** + * Creates a GamepadButton that commands can be bound to. + * + * @param gamepad The gamepad on which the button is located. + * @param button The button + */ + explicit GamepadButton(wpi::Gamepad* gamepad, + enum wpi::Gamepad::Button button) + : Trigger([gamepad, button] { return gamepad->GetButton(button); }) {} +}; +} // namespace wpi::cmd diff --git a/commandsv3/src/main/java/org/wpilib/command3/button/CommandGamepad.java b/commandsv3/src/main/java/org/wpilib/command3/button/CommandGamepad.java index 4e19867fb3..2342710478 100644 --- a/commandsv3/src/main/java/org/wpilib/command3/button/CommandGamepad.java +++ b/commandsv3/src/main/java/org/wpilib/command3/button/CommandGamepad.java @@ -70,7 +70,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger southFace(EventLoop loop) { - return button(Gamepad.Button.kSouthFace.value, loop); + return button(Gamepad.Button.SOUTH_FACE.value, loop); } /** @@ -94,7 +94,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger eastFace(EventLoop loop) { - return button(Gamepad.Button.kEastFace.value, loop); + return button(Gamepad.Button.EAST_FACE.value, loop); } /** @@ -118,7 +118,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger westFace(EventLoop loop) { - return button(Gamepad.Button.kWestFace.value, loop); + return button(Gamepad.Button.WEST_FACE.value, loop); } /** @@ -142,7 +142,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger northFace(EventLoop loop) { - return button(Gamepad.Button.kNorthFace.value, loop); + return button(Gamepad.Button.NORTH_FACE.value, loop); } /** @@ -166,7 +166,7 @@ public class CommandGamepad extends CommandGenericHID { * loop. */ public Trigger back(EventLoop loop) { - return button(Gamepad.Button.kBack.value, loop); + return button(Gamepad.Button.BACK.value, loop); } /** @@ -190,7 +190,7 @@ public class CommandGamepad extends CommandGenericHID { * loop. */ public Trigger guide(EventLoop loop) { - return button(Gamepad.Button.kGuide.value, loop); + return button(Gamepad.Button.GUIDE.value, loop); } /** @@ -214,7 +214,7 @@ public class CommandGamepad extends CommandGenericHID { * loop. */ public Trigger start(EventLoop loop) { - return button(Gamepad.Button.kStart.value, loop); + return button(Gamepad.Button.START.value, loop); } /** @@ -238,7 +238,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger leftStick(EventLoop loop) { - return button(Gamepad.Button.kLeftStick.value, loop); + return button(Gamepad.Button.LEFT_STICK.value, loop); } /** @@ -262,7 +262,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger rightStick(EventLoop loop) { - return button(Gamepad.Button.kRightStick.value, loop); + return button(Gamepad.Button.RIGHT_STICK.value, loop); } /** @@ -286,7 +286,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger leftBumper(EventLoop loop) { - return button(Gamepad.Button.kLeftBumper.value, loop); + return button(Gamepad.Button.LEFT_BUMPER.value, loop); } /** @@ -310,7 +310,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger rightBumper(EventLoop loop) { - return button(Gamepad.Button.kRightBumper.value, loop); + return button(Gamepad.Button.RIGHT_BUMPER.value, loop); } /** @@ -334,7 +334,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger dpadUp(EventLoop loop) { - return button(Gamepad.Button.kDpadUp.value, loop); + return button(Gamepad.Button.DPAD_UP.value, loop); } /** @@ -358,7 +358,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger dpadDown(EventLoop loop) { - return button(Gamepad.Button.kDpadDown.value, loop); + return button(Gamepad.Button.DPAD_DOWN.value, loop); } /** @@ -382,7 +382,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger dpadLeft(EventLoop loop) { - return button(Gamepad.Button.kDpadLeft.value, loop); + return button(Gamepad.Button.DPAD_LEFT.value, loop); } /** @@ -406,7 +406,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger dpadRight(EventLoop loop) { - return button(Gamepad.Button.kDpadRight.value, loop); + return button(Gamepad.Button.DPAD_RIGHT.value, loop); } /** @@ -430,7 +430,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger misc1(EventLoop loop) { - return button(Gamepad.Button.kMisc1.value, loop); + return button(Gamepad.Button.MISC_1.value, loop); } /** @@ -454,7 +454,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger rightPaddle1(EventLoop loop) { - return button(Gamepad.Button.kRightPaddle1.value, loop); + return button(Gamepad.Button.RIGHT_PADDLE_1.value, loop); } /** @@ -478,7 +478,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger leftPaddle1(EventLoop loop) { - return button(Gamepad.Button.kLeftPaddle1.value, loop); + return button(Gamepad.Button.LEFT_PADDLE_1.value, loop); } /** @@ -502,7 +502,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger rightPaddle2(EventLoop loop) { - return button(Gamepad.Button.kRightPaddle2.value, loop); + return button(Gamepad.Button.RIGHT_PADDLE_2.value, loop); } /** @@ -526,7 +526,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger leftPaddle2(EventLoop loop) { - return button(Gamepad.Button.kLeftPaddle2.value, loop); + return button(Gamepad.Button.LEFT_PADDLE_2.value, loop); } /** @@ -550,7 +550,7 @@ public class CommandGamepad extends CommandGenericHID { * given loop. */ public Trigger touchpad(EventLoop loop) { - return button(Gamepad.Button.kTouchpad.value, loop); + return button(Gamepad.Button.TOUCHPAD.value, loop); } /** @@ -574,7 +574,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger misc2(EventLoop loop) { - return button(Gamepad.Button.kMisc2.value, loop); + return button(Gamepad.Button.MISC_2.value, loop); } /** @@ -598,7 +598,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger misc3(EventLoop loop) { - return button(Gamepad.Button.kMisc3.value, loop); + return button(Gamepad.Button.MISC_3.value, loop); } /** @@ -622,7 +622,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger misc4(EventLoop loop) { - return button(Gamepad.Button.kMisc4.value, loop); + return button(Gamepad.Button.MISC_4.value, loop); } /** @@ -646,7 +646,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger misc5(EventLoop loop) { - return button(Gamepad.Button.kMisc5.value, loop); + return button(Gamepad.Button.MISC_5.value, loop); } /** @@ -670,7 +670,7 @@ public class CommandGamepad extends CommandGenericHID { * the given loop. */ public Trigger misc6(EventLoop loop) { - return button(Gamepad.Button.kMisc6.value, loop); + return button(Gamepad.Button.MISC_6.value, loop); } /** @@ -684,7 +684,7 @@ public class CommandGamepad extends CommandGenericHID { * threshold, attached to the given event loop */ public Trigger leftTrigger(double threshold, EventLoop loop) { - return axisGreaterThan(Gamepad.Axis.kLeftTrigger.value, threshold, loop); + return axisGreaterThan(Gamepad.Axis.LEFT_TRIGGER.value, threshold, loop); } /** @@ -725,7 +725,7 @@ public class CommandGamepad extends CommandGenericHID { * threshold, attached to the given event loop */ public Trigger rightTrigger(double threshold, EventLoop loop) { - return axisGreaterThan(Gamepad.Axis.kRightTrigger.value, threshold, loop); + return axisGreaterThan(Gamepad.Axis.RIGHT_TRIGGER.value, threshold, loop); } /** diff --git a/wpilibc/robotpy_pybind_build_info.bzl b/wpilibc/robotpy_pybind_build_info.bzl index 42c76e1fb8..447d1c293d 100644 --- a/wpilibc/robotpy_pybind_build_info.bzl +++ b/wpilibc/robotpy_pybind_build_info.bzl @@ -125,8 +125,6 @@ def wpilib_extension(srcs = [], header_to_dat_deps = [], extra_hdrs = [], includ tmpl_class_names = [], trampolines = [ ("wpi::Gamepad", "wpi__Gamepad.hpp"), - ("wpi::Gamepad::Button", "wpi__Gamepad__Button.hpp"), - ("wpi::Gamepad::Axis", "wpi__Gamepad__Axis.hpp"), ], ), struct( diff --git a/wpilibc/src/main/native/cpp/driverstation/Gamepad.cpp b/wpilibc/src/main/native/cpp/driverstation/Gamepad.cpp index d225f13645..c1a7665a22 100644 --- a/wpilibc/src/main/native/cpp/driverstation/Gamepad.cpp +++ b/wpilibc/src/main/native/cpp/driverstation/Gamepad.cpp @@ -15,23 +15,23 @@ Gamepad::Gamepad(int port) : GenericHID(port) { } double Gamepad::GetLeftX() const { - return GetRawAxis(Axis::kLeftX); + return GetAxis(Axis::LEFT_X); } double Gamepad::GetLeftY() const { - return GetRawAxis(Axis::kLeftY); + return GetAxis(Axis::LEFT_Y); } double Gamepad::GetRightX() const { - return GetRawAxis(Axis::kRightX); + return GetAxis(Axis::RIGHT_X); } double Gamepad::GetRightY() const { - return GetRawAxis(Axis::kRightY); + return GetAxis(Axis::RIGHT_Y); } double Gamepad::GetLeftTriggerAxis() const { - return GetRawAxis(Axis::kLeftTrigger); + return GetAxis(Axis::LEFT_TRIGGER); } BooleanEvent Gamepad::LeftTrigger(double threshold, EventLoop* loop) const { @@ -45,7 +45,7 @@ BooleanEvent Gamepad::LeftTrigger(EventLoop* loop) const { } double Gamepad::GetRightTriggerAxis() const { - return GetRawAxis(Axis::kRightTrigger); + return GetAxis(Axis::RIGHT_TRIGGER); } BooleanEvent Gamepad::RightTrigger(double threshold, EventLoop* loop) const { @@ -59,15 +59,15 @@ BooleanEvent Gamepad::RightTrigger(EventLoop* loop) const { } bool Gamepad::GetSouthFaceButton() const { - return GetRawButton(Button::kSouthFace); + return GetButton(Button::SOUTH_FACE); } bool Gamepad::GetSouthFaceButtonPressed() { - return GetRawButtonPressed(Button::kSouthFace); + return GetButtonPressed(Button::SOUTH_FACE); } bool Gamepad::GetSouthFaceButtonReleased() { - return GetRawButtonReleased(Button::kSouthFace); + return GetButtonReleased(Button::SOUTH_FACE); } BooleanEvent Gamepad::SouthFace(EventLoop* loop) const { @@ -75,15 +75,15 @@ BooleanEvent Gamepad::SouthFace(EventLoop* loop) const { } bool Gamepad::GetEastFaceButton() const { - return GetRawButton(Button::kEastFace); + return GetButton(Button::EAST_FACE); } bool Gamepad::GetEastFaceButtonPressed() { - return GetRawButtonPressed(Button::kEastFace); + return GetButtonPressed(Button::EAST_FACE); } bool Gamepad::GetEastFaceButtonReleased() { - return GetRawButtonReleased(Button::kEastFace); + return GetButtonReleased(Button::EAST_FACE); } BooleanEvent Gamepad::EastFace(EventLoop* loop) const { @@ -91,15 +91,15 @@ BooleanEvent Gamepad::EastFace(EventLoop* loop) const { } bool Gamepad::GetWestFaceButton() const { - return GetRawButton(Button::kWestFace); + return GetButton(Button::WEST_FACE); } bool Gamepad::GetWestFaceButtonPressed() { - return GetRawButtonPressed(Button::kWestFace); + return GetButtonPressed(Button::WEST_FACE); } bool Gamepad::GetWestFaceButtonReleased() { - return GetRawButtonReleased(Button::kWestFace); + return GetButtonReleased(Button::WEST_FACE); } BooleanEvent Gamepad::WestFace(EventLoop* loop) const { @@ -107,15 +107,15 @@ BooleanEvent Gamepad::WestFace(EventLoop* loop) const { } bool Gamepad::GetNorthFaceButton() const { - return GetRawButton(Button::kNorthFace); + return GetButton(Button::NORTH_FACE); } bool Gamepad::GetNorthFaceButtonPressed() { - return GetRawButtonPressed(Button::kNorthFace); + return GetButtonPressed(Button::NORTH_FACE); } bool Gamepad::GetNorthFaceButtonReleased() { - return GetRawButtonReleased(Button::kNorthFace); + return GetButtonReleased(Button::NORTH_FACE); } BooleanEvent Gamepad::NorthFace(EventLoop* loop) const { @@ -123,15 +123,15 @@ BooleanEvent Gamepad::NorthFace(EventLoop* loop) const { } bool Gamepad::GetBackButton() const { - return GetRawButton(Button::kBack); + return GetButton(Button::BACK); } bool Gamepad::GetBackButtonPressed() { - return GetRawButtonPressed(Button::kBack); + return GetButtonPressed(Button::BACK); } bool Gamepad::GetBackButtonReleased() { - return GetRawButtonReleased(Button::kBack); + return GetButtonReleased(Button::BACK); } BooleanEvent Gamepad::Back(EventLoop* loop) const { @@ -139,15 +139,15 @@ BooleanEvent Gamepad::Back(EventLoop* loop) const { } bool Gamepad::GetGuideButton() const { - return GetRawButton(Button::kGuide); + return GetButton(Button::GUIDE); } bool Gamepad::GetGuideButtonPressed() { - return GetRawButtonPressed(Button::kGuide); + return GetButtonPressed(Button::GUIDE); } bool Gamepad::GetGuideButtonReleased() { - return GetRawButtonReleased(Button::kGuide); + return GetButtonReleased(Button::GUIDE); } BooleanEvent Gamepad::Guide(EventLoop* loop) const { @@ -155,15 +155,15 @@ BooleanEvent Gamepad::Guide(EventLoop* loop) const { } bool Gamepad::GetStartButton() const { - return GetRawButton(Button::kStart); + return GetButton(Button::START); } bool Gamepad::GetStartButtonPressed() { - return GetRawButtonPressed(Button::kStart); + return GetButtonPressed(Button::START); } bool Gamepad::GetStartButtonReleased() { - return GetRawButtonReleased(Button::kStart); + return GetButtonReleased(Button::START); } BooleanEvent Gamepad::Start(EventLoop* loop) const { @@ -171,15 +171,15 @@ BooleanEvent Gamepad::Start(EventLoop* loop) const { } bool Gamepad::GetLeftStickButton() const { - return GetRawButton(Button::kLeftStick); + return GetButton(Button::LEFT_STICK); } bool Gamepad::GetLeftStickButtonPressed() { - return GetRawButtonPressed(Button::kLeftStick); + return GetButtonPressed(Button::LEFT_STICK); } bool Gamepad::GetLeftStickButtonReleased() { - return GetRawButtonReleased(Button::kLeftStick); + return GetButtonReleased(Button::LEFT_STICK); } BooleanEvent Gamepad::LeftStick(EventLoop* loop) const { @@ -187,15 +187,15 @@ BooleanEvent Gamepad::LeftStick(EventLoop* loop) const { } bool Gamepad::GetRightStickButton() const { - return GetRawButton(Button::kRightStick); + return GetButton(Button::RIGHT_STICK); } bool Gamepad::GetRightStickButtonPressed() { - return GetRawButtonPressed(Button::kRightStick); + return GetButtonPressed(Button::RIGHT_STICK); } bool Gamepad::GetRightStickButtonReleased() { - return GetRawButtonReleased(Button::kRightStick); + return GetButtonReleased(Button::RIGHT_STICK); } BooleanEvent Gamepad::RightStick(EventLoop* loop) const { @@ -203,15 +203,15 @@ BooleanEvent Gamepad::RightStick(EventLoop* loop) const { } bool Gamepad::GetLeftBumperButton() const { - return GetRawButton(Button::kLeftBumper); + return GetButton(Button::LEFT_BUMPER); } bool Gamepad::GetLeftBumperButtonPressed() { - return GetRawButtonPressed(Button::kLeftBumper); + return GetButtonPressed(Button::LEFT_BUMPER); } bool Gamepad::GetLeftBumperButtonReleased() { - return GetRawButtonReleased(Button::kLeftBumper); + return GetButtonReleased(Button::LEFT_BUMPER); } BooleanEvent Gamepad::LeftBumper(EventLoop* loop) const { @@ -219,15 +219,15 @@ BooleanEvent Gamepad::LeftBumper(EventLoop* loop) const { } bool Gamepad::GetRightBumperButton() const { - return GetRawButton(Button::kRightBumper); + return GetButton(Button::RIGHT_BUMPER); } bool Gamepad::GetRightBumperButtonPressed() { - return GetRawButtonPressed(Button::kRightBumper); + return GetButtonPressed(Button::RIGHT_BUMPER); } bool Gamepad::GetRightBumperButtonReleased() { - return GetRawButtonReleased(Button::kRightBumper); + return GetButtonReleased(Button::RIGHT_BUMPER); } BooleanEvent Gamepad::RightBumper(EventLoop* loop) const { @@ -235,15 +235,15 @@ BooleanEvent Gamepad::RightBumper(EventLoop* loop) const { } bool Gamepad::GetDpadUpButton() const { - return GetRawButton(Button::kDpadUp); + return GetButton(Button::DPAD_UP); } bool Gamepad::GetDpadUpButtonPressed() { - return GetRawButtonPressed(Button::kDpadUp); + return GetButtonPressed(Button::DPAD_UP); } bool Gamepad::GetDpadUpButtonReleased() { - return GetRawButtonReleased(Button::kDpadUp); + return GetButtonReleased(Button::DPAD_UP); } BooleanEvent Gamepad::DpadUp(EventLoop* loop) const { @@ -251,15 +251,15 @@ BooleanEvent Gamepad::DpadUp(EventLoop* loop) const { } bool Gamepad::GetDpadDownButton() const { - return GetRawButton(Button::kDpadDown); + return GetButton(Button::DPAD_DOWN); } bool Gamepad::GetDpadDownButtonPressed() { - return GetRawButtonPressed(Button::kDpadDown); + return GetButtonPressed(Button::DPAD_DOWN); } bool Gamepad::GetDpadDownButtonReleased() { - return GetRawButtonReleased(Button::kDpadDown); + return GetButtonReleased(Button::DPAD_DOWN); } BooleanEvent Gamepad::DpadDown(EventLoop* loop) const { @@ -267,15 +267,15 @@ BooleanEvent Gamepad::DpadDown(EventLoop* loop) const { } bool Gamepad::GetDpadLeftButton() const { - return GetRawButton(Button::kDpadLeft); + return GetButton(Button::DPAD_LEFT); } bool Gamepad::GetDpadLeftButtonPressed() { - return GetRawButtonPressed(Button::kDpadLeft); + return GetButtonPressed(Button::DPAD_LEFT); } bool Gamepad::GetDpadLeftButtonReleased() { - return GetRawButtonReleased(Button::kDpadLeft); + return GetButtonReleased(Button::DPAD_LEFT); } BooleanEvent Gamepad::DpadLeft(EventLoop* loop) const { @@ -283,15 +283,15 @@ BooleanEvent Gamepad::DpadLeft(EventLoop* loop) const { } bool Gamepad::GetDpadRightButton() const { - return GetRawButton(Button::kDpadRight); + return GetButton(Button::DPAD_RIGHT); } bool Gamepad::GetDpadRightButtonPressed() { - return GetRawButtonPressed(Button::kDpadRight); + return GetButtonPressed(Button::DPAD_RIGHT); } bool Gamepad::GetDpadRightButtonReleased() { - return GetRawButtonReleased(Button::kDpadRight); + return GetButtonReleased(Button::DPAD_RIGHT); } BooleanEvent Gamepad::DpadRight(EventLoop* loop) const { @@ -299,15 +299,15 @@ BooleanEvent Gamepad::DpadRight(EventLoop* loop) const { } bool Gamepad::GetMisc1Button() const { - return GetRawButton(Button::kMisc1); + return GetButton(Button::MISC_1); } bool Gamepad::GetMisc1ButtonPressed() { - return GetRawButtonPressed(Button::kMisc1); + return GetButtonPressed(Button::MISC_1); } bool Gamepad::GetMisc1ButtonReleased() { - return GetRawButtonReleased(Button::kMisc1); + return GetButtonReleased(Button::MISC_1); } BooleanEvent Gamepad::Misc1(EventLoop* loop) const { @@ -315,15 +315,15 @@ BooleanEvent Gamepad::Misc1(EventLoop* loop) const { } bool Gamepad::GetRightPaddle1Button() const { - return GetRawButton(Button::kRightPaddle1); + return GetButton(Button::RIGHT_PADDLE_1); } bool Gamepad::GetRightPaddle1ButtonPressed() { - return GetRawButtonPressed(Button::kRightPaddle1); + return GetButtonPressed(Button::RIGHT_PADDLE_1); } bool Gamepad::GetRightPaddle1ButtonReleased() { - return GetRawButtonReleased(Button::kRightPaddle1); + return GetButtonReleased(Button::RIGHT_PADDLE_1); } BooleanEvent Gamepad::RightPaddle1(EventLoop* loop) const { @@ -331,15 +331,15 @@ BooleanEvent Gamepad::RightPaddle1(EventLoop* loop) const { } bool Gamepad::GetLeftPaddle1Button() const { - return GetRawButton(Button::kLeftPaddle1); + return GetButton(Button::LEFT_PADDLE_1); } bool Gamepad::GetLeftPaddle1ButtonPressed() { - return GetRawButtonPressed(Button::kLeftPaddle1); + return GetButtonPressed(Button::LEFT_PADDLE_1); } bool Gamepad::GetLeftPaddle1ButtonReleased() { - return GetRawButtonReleased(Button::kLeftPaddle1); + return GetButtonReleased(Button::LEFT_PADDLE_1); } BooleanEvent Gamepad::LeftPaddle1(EventLoop* loop) const { @@ -347,15 +347,15 @@ BooleanEvent Gamepad::LeftPaddle1(EventLoop* loop) const { } bool Gamepad::GetRightPaddle2Button() const { - return GetRawButton(Button::kRightPaddle2); + return GetButton(Button::RIGHT_PADDLE_2); } bool Gamepad::GetRightPaddle2ButtonPressed() { - return GetRawButtonPressed(Button::kRightPaddle2); + return GetButtonPressed(Button::RIGHT_PADDLE_2); } bool Gamepad::GetRightPaddle2ButtonReleased() { - return GetRawButtonReleased(Button::kRightPaddle2); + return GetButtonReleased(Button::RIGHT_PADDLE_2); } BooleanEvent Gamepad::RightPaddle2(EventLoop* loop) const { @@ -363,15 +363,15 @@ BooleanEvent Gamepad::RightPaddle2(EventLoop* loop) const { } bool Gamepad::GetLeftPaddle2Button() const { - return GetRawButton(Button::kLeftPaddle2); + return GetButton(Button::LEFT_PADDLE_2); } bool Gamepad::GetLeftPaddle2ButtonPressed() { - return GetRawButtonPressed(Button::kLeftPaddle2); + return GetButtonPressed(Button::LEFT_PADDLE_2); } bool Gamepad::GetLeftPaddle2ButtonReleased() { - return GetRawButtonReleased(Button::kLeftPaddle2); + return GetButtonReleased(Button::LEFT_PADDLE_2); } BooleanEvent Gamepad::LeftPaddle2(EventLoop* loop) const { @@ -379,15 +379,15 @@ BooleanEvent Gamepad::LeftPaddle2(EventLoop* loop) const { } bool Gamepad::GetTouchpadButton() const { - return GetRawButton(Button::kTouchpad); + return GetButton(Button::TOUCHPAD); } bool Gamepad::GetTouchpadButtonPressed() { - return GetRawButtonPressed(Button::kTouchpad); + return GetButtonPressed(Button::TOUCHPAD); } bool Gamepad::GetTouchpadButtonReleased() { - return GetRawButtonReleased(Button::kTouchpad); + return GetButtonReleased(Button::TOUCHPAD); } BooleanEvent Gamepad::Touchpad(EventLoop* loop) const { @@ -395,15 +395,15 @@ BooleanEvent Gamepad::Touchpad(EventLoop* loop) const { } bool Gamepad::GetMisc2Button() const { - return GetRawButton(Button::kMisc2); + return GetButton(Button::MISC_2); } bool Gamepad::GetMisc2ButtonPressed() { - return GetRawButtonPressed(Button::kMisc2); + return GetButtonPressed(Button::MISC_2); } bool Gamepad::GetMisc2ButtonReleased() { - return GetRawButtonReleased(Button::kMisc2); + return GetButtonReleased(Button::MISC_2); } BooleanEvent Gamepad::Misc2(EventLoop* loop) const { @@ -411,15 +411,15 @@ BooleanEvent Gamepad::Misc2(EventLoop* loop) const { } bool Gamepad::GetMisc3Button() const { - return GetRawButton(Button::kMisc3); + return GetButton(Button::MISC_3); } bool Gamepad::GetMisc3ButtonPressed() { - return GetRawButtonPressed(Button::kMisc3); + return GetButtonPressed(Button::MISC_3); } bool Gamepad::GetMisc3ButtonReleased() { - return GetRawButtonReleased(Button::kMisc3); + return GetButtonReleased(Button::MISC_3); } BooleanEvent Gamepad::Misc3(EventLoop* loop) const { @@ -427,15 +427,15 @@ BooleanEvent Gamepad::Misc3(EventLoop* loop) const { } bool Gamepad::GetMisc4Button() const { - return GetRawButton(Button::kMisc4); + return GetButton(Button::MISC_4); } bool Gamepad::GetMisc4ButtonPressed() { - return GetRawButtonPressed(Button::kMisc4); + return GetButtonPressed(Button::MISC_4); } bool Gamepad::GetMisc4ButtonReleased() { - return GetRawButtonReleased(Button::kMisc4); + return GetButtonReleased(Button::MISC_4); } BooleanEvent Gamepad::Misc4(EventLoop* loop) const { @@ -443,15 +443,15 @@ BooleanEvent Gamepad::Misc4(EventLoop* loop) const { } bool Gamepad::GetMisc5Button() const { - return GetRawButton(Button::kMisc5); + return GetButton(Button::MISC_5); } bool Gamepad::GetMisc5ButtonPressed() { - return GetRawButtonPressed(Button::kMisc5); + return GetButtonPressed(Button::MISC_5); } bool Gamepad::GetMisc5ButtonReleased() { - return GetRawButtonReleased(Button::kMisc5); + return GetButtonReleased(Button::MISC_5); } BooleanEvent Gamepad::Misc5(EventLoop* loop) const { @@ -459,27 +459,60 @@ BooleanEvent Gamepad::Misc5(EventLoop* loop) const { } bool Gamepad::GetMisc6Button() const { - return GetRawButton(Button::kMisc6); + return GetButton(Button::MISC_6); } bool Gamepad::GetMisc6ButtonPressed() { - return GetRawButtonPressed(Button::kMisc6); + return GetButtonPressed(Button::MISC_6); } bool Gamepad::GetMisc6ButtonReleased() { - return GetRawButtonReleased(Button::kMisc6); + return GetButtonReleased(Button::MISC_6); } BooleanEvent Gamepad::Misc6(EventLoop* loop) const { return BooleanEvent(loop, [this]() { return this->GetMisc6Button(); }); } -double Gamepad::GetAxisForSendable(int axis) const { - return DriverStation::GetStickAxisIfAvailable(GetPort(), axis).value_or(0.0); +bool Gamepad::GetButton(Button button) const { + return GetRawButton(static_cast(button)); } -bool Gamepad::GetButtonForSendable(int button) const { - return DriverStation::GetStickButtonIfAvailable(GetPort(), button) +bool Gamepad::GetButtonPressed(Button button) { + return GetRawButtonPressed(static_cast(button)); +} + +bool Gamepad::GetButtonReleased(Button button) { + return GetRawButtonReleased(static_cast(button)); +} + +BooleanEvent Gamepad::ButtonEvent(Button button, EventLoop* loop) const { + return GenericHID::Button(static_cast(button), loop); +} + +double Gamepad::GetAxis(Axis axis) const { + return GetRawAxis(static_cast(axis)); +} + +BooleanEvent Gamepad::AxisLessThan(Axis axis, double threshold, + EventLoop* loop) const { + return GenericHID::AxisLessThan(static_cast(axis), threshold, loop); +} + +BooleanEvent Gamepad::AxisGreaterThan(Axis axis, double threshold, + EventLoop* loop) const { + return GenericHID::AxisGreaterThan(static_cast(axis), threshold, loop); +} + +double Gamepad::GetAxisForSendable(Axis axis) const { + return DriverStation::GetStickAxisIfAvailable(GetPort(), + static_cast(axis)) + .value_or(0.0); +} + +bool Gamepad::GetButtonForSendable(Button button) const { + return DriverStation::GetStickButtonIfAvailable(GetPort(), + static_cast(button)) .value_or(false); } @@ -488,93 +521,91 @@ void Gamepad::InitSendable(wpi::util::SendableBuilder& builder) { builder.PublishConstString("ControllerType", "Gamepad"); builder.AddDoubleProperty( "LeftTrigger Axis", - [this] { return GetAxisForSendable(Axis::kLeftTrigger); }, nullptr); + [this] { return GetAxisForSendable(Axis::LEFT_TRIGGER); }, nullptr); builder.AddDoubleProperty( "RightTrigger Axis", - [this] { return GetAxisForSendable(Axis::kRightTrigger); }, nullptr); + [this] { return GetAxisForSendable(Axis::RIGHT_TRIGGER); }, nullptr); builder.AddDoubleProperty( - "LeftX", [this] { return GetAxisForSendable(Axis::kLeftX); }, nullptr); + "LeftX", [this] { return GetAxisForSendable(Axis::LEFT_X); }, nullptr); builder.AddDoubleProperty( - "LeftY", [this] { return GetAxisForSendable(Axis::kLeftY); }, nullptr); + "LeftY", [this] { return GetAxisForSendable(Axis::LEFT_Y); }, nullptr); builder.AddDoubleProperty( - "RightX", [this] { return GetAxisForSendable(Axis::kRightX); }, nullptr); + "RightX", [this] { return GetAxisForSendable(Axis::RIGHT_X); }, nullptr); builder.AddDoubleProperty( - "RightY", [this] { return GetAxisForSendable(Axis::kRightY); }, nullptr); + "RightY", [this] { return GetAxisForSendable(Axis::RIGHT_Y); }, nullptr); builder.AddBooleanProperty( - "SouthFace", [this] { return GetButtonForSendable(Button::kSouthFace); }, + "SouthFace", [this] { return GetButtonForSendable(Button::SOUTH_FACE); }, nullptr); builder.AddBooleanProperty( - "EastFace", [this] { return GetButtonForSendable(Button::kEastFace); }, + "EastFace", [this] { return GetButtonForSendable(Button::EAST_FACE); }, nullptr); builder.AddBooleanProperty( - "WestFace", [this] { return GetButtonForSendable(Button::kWestFace); }, + "WestFace", [this] { return GetButtonForSendable(Button::WEST_FACE); }, nullptr); builder.AddBooleanProperty( - "NorthFace", [this] { return GetButtonForSendable(Button::kNorthFace); }, + "NorthFace", [this] { return GetButtonForSendable(Button::NORTH_FACE); }, nullptr); builder.AddBooleanProperty( - "Back", [this] { return GetButtonForSendable(Button::kBack); }, nullptr); + "Back", [this] { return GetButtonForSendable(Button::BACK); }, nullptr); builder.AddBooleanProperty( - "Guide", [this] { return GetButtonForSendable(Button::kGuide); }, - nullptr); + "Guide", [this] { return GetButtonForSendable(Button::GUIDE); }, nullptr); builder.AddBooleanProperty( - "Start", [this] { return GetButtonForSendable(Button::kStart); }, - nullptr); + "Start", [this] { return GetButtonForSendable(Button::START); }, nullptr); builder.AddBooleanProperty( - "LeftStick", [this] { return GetButtonForSendable(Button::kLeftStick); }, + "LeftStick", [this] { return GetButtonForSendable(Button::LEFT_STICK); }, nullptr); builder.AddBooleanProperty( "RightStick", - [this] { return GetButtonForSendable(Button::kRightStick); }, nullptr); + [this] { return GetButtonForSendable(Button::RIGHT_STICK); }, nullptr); builder.AddBooleanProperty( "LeftBumper", - [this] { return GetButtonForSendable(Button::kLeftBumper); }, nullptr); + [this] { return GetButtonForSendable(Button::LEFT_BUMPER); }, nullptr); builder.AddBooleanProperty( "RightBumper", - [this] { return GetButtonForSendable(Button::kRightBumper); }, nullptr); + [this] { return GetButtonForSendable(Button::RIGHT_BUMPER); }, nullptr); builder.AddBooleanProperty( - "DpadUp", [this] { return GetButtonForSendable(Button::kDpadUp); }, + "DpadUp", [this] { return GetButtonForSendable(Button::DPAD_UP); }, nullptr); builder.AddBooleanProperty( - "DpadDown", [this] { return GetButtonForSendable(Button::kDpadDown); }, + "DpadDown", [this] { return GetButtonForSendable(Button::DPAD_DOWN); }, nullptr); builder.AddBooleanProperty( - "DpadLeft", [this] { return GetButtonForSendable(Button::kDpadLeft); }, + "DpadLeft", [this] { return GetButtonForSendable(Button::DPAD_LEFT); }, nullptr); builder.AddBooleanProperty( - "DpadRight", [this] { return GetButtonForSendable(Button::kDpadRight); }, + "DpadRight", [this] { return GetButtonForSendable(Button::DPAD_RIGHT); }, nullptr); builder.AddBooleanProperty( - "Misc1", [this] { return GetButtonForSendable(Button::kMisc1); }, + "Misc1", [this] { return GetButtonForSendable(Button::MISC_1); }, nullptr); builder.AddBooleanProperty( "RightPaddle1", - [this] { return GetButtonForSendable(Button::kRightPaddle1); }, nullptr); + [this] { return GetButtonForSendable(Button::RIGHT_PADDLE_1); }, nullptr); builder.AddBooleanProperty( "LeftPaddle1", - [this] { return GetButtonForSendable(Button::kLeftPaddle1); }, nullptr); + [this] { return GetButtonForSendable(Button::LEFT_PADDLE_1); }, nullptr); builder.AddBooleanProperty( "RightPaddle2", - [this] { return GetButtonForSendable(Button::kRightPaddle2); }, nullptr); + [this] { return GetButtonForSendable(Button::RIGHT_PADDLE_2); }, nullptr); builder.AddBooleanProperty( "LeftPaddle2", - [this] { return GetButtonForSendable(Button::kLeftPaddle2); }, nullptr); + [this] { return GetButtonForSendable(Button::LEFT_PADDLE_2); }, nullptr); builder.AddBooleanProperty( - "Touchpad", [this] { return GetButtonForSendable(Button::kTouchpad); }, + "Touchpad", [this] { return GetButtonForSendable(Button::TOUCHPAD); }, nullptr); builder.AddBooleanProperty( - "Misc2", [this] { return GetButtonForSendable(Button::kMisc2); }, + "Misc2", [this] { return GetButtonForSendable(Button::MISC_2); }, nullptr); builder.AddBooleanProperty( - "Misc3", [this] { return GetButtonForSendable(Button::kMisc3); }, + "Misc3", [this] { return GetButtonForSendable(Button::MISC_3); }, nullptr); builder.AddBooleanProperty( - "Misc4", [this] { return GetButtonForSendable(Button::kMisc4); }, + "Misc4", [this] { return GetButtonForSendable(Button::MISC_4); }, nullptr); builder.AddBooleanProperty( - "Misc5", [this] { return GetButtonForSendable(Button::kMisc5); }, + "Misc5", [this] { return GetButtonForSendable(Button::MISC_5); }, nullptr); builder.AddBooleanProperty( - "Misc6", [this] { return GetButtonForSendable(Button::kMisc6); }, + "Misc6", [this] { return GetButtonForSendable(Button::MISC_6); }, nullptr); } diff --git a/wpilibc/src/main/native/cpp/driverstation/GenericHID.cpp b/wpilibc/src/main/native/cpp/driverstation/GenericHID.cpp index 393c31e746..a7cab0eead 100644 --- a/wpilibc/src/main/native/cpp/driverstation/GenericHID.cpp +++ b/wpilibc/src/main/native/cpp/driverstation/GenericHID.cpp @@ -163,13 +163,13 @@ void GenericHID::SetRumble(RumbleType type, double value) { value = std::clamp(value, 0.0, 1.0); double rumbleValue = value * 65535; - if (type == kLeftRumble) { + if (type == RumbleType::LEFT_RUMBLE) { m_leftRumble = rumbleValue; - } else if (type == kRightRumble) { + } else if (type == RumbleType::RIGHT_RUMBLE) { m_rightRumble = rumbleValue; - } else if (type == kLeftTriggerRumble) { + } else if (type == RumbleType::LEFT_TRIGGER_RUMBLE) { m_leftTriggerRumble = rumbleValue; - } else if (type == kRightTriggerRumble) { + } else if (type == RumbleType::RIGHT_TRIGGER_RUMBLE) { m_rightTriggerRumble = rumbleValue; } diff --git a/wpilibc/src/main/native/cpp/simulation/GamepadSim.cpp b/wpilibc/src/main/native/cpp/simulation/GamepadSim.cpp index a9666eda78..8c4742e3a4 100644 --- a/wpilibc/src/main/native/cpp/simulation/GamepadSim.cpp +++ b/wpilibc/src/main/native/cpp/simulation/GamepadSim.cpp @@ -9,7 +9,7 @@ using namespace wpi; using namespace wpi::sim; -GamepadSim::GamepadSim(const Gamepad& joystick) : GenericHIDSim{joystick} { +GamepadSim::GamepadSim(const wpi::Gamepad& joystick) : GenericHIDSim{joystick} { SetAxesMaximumIndex(6); SetButtonsMaximumIndex(26); SetPOVsMaximumIndex(1); @@ -21,130 +21,138 @@ GamepadSim::GamepadSim(int port) : GenericHIDSim{port} { SetPOVsMaximumIndex(1); } +void GamepadSim::SetButton(wpi::Gamepad::Button button, bool value) { + SetRawButton(static_cast(button), value); +} + +void GamepadSim::SetAxis(wpi::Gamepad::Axis axis, double value) { + SetRawAxis(static_cast(axis), value); +} + void GamepadSim::SetLeftX(double value) { - SetRawAxis(Gamepad::Axis::kLeftX, value); + SetAxis(Gamepad::Axis::LEFT_X, value); } void GamepadSim::SetLeftY(double value) { - SetRawAxis(Gamepad::Axis::kLeftY, value); + SetAxis(Gamepad::Axis::LEFT_Y, value); } void GamepadSim::SetRightX(double value) { - SetRawAxis(Gamepad::Axis::kRightX, value); + SetAxis(Gamepad::Axis::RIGHT_X, value); } void GamepadSim::SetRightY(double value) { - SetRawAxis(Gamepad::Axis::kRightY, value); + SetAxis(Gamepad::Axis::RIGHT_Y, value); } void GamepadSim::SetLeftTriggerAxis(double value) { - SetRawAxis(Gamepad::Axis::kLeftTrigger, value); + SetAxis(Gamepad::Axis::LEFT_TRIGGER, value); } void GamepadSim::SetRightTriggerAxis(double value) { - SetRawAxis(Gamepad::Axis::kRightTrigger, value); + SetAxis(Gamepad::Axis::RIGHT_TRIGGER, value); } void GamepadSim::SetSouthFaceButton(bool value) { - SetRawButton(Gamepad::Button::kSouthFace, value); + SetButton(Gamepad::Button::SOUTH_FACE, value); } void GamepadSim::SetEastFaceButton(bool value) { - SetRawButton(Gamepad::Button::kEastFace, value); + SetButton(Gamepad::Button::EAST_FACE, value); } void GamepadSim::SetWestFaceButton(bool value) { - SetRawButton(Gamepad::Button::kWestFace, value); + SetButton(Gamepad::Button::WEST_FACE, value); } void GamepadSim::SetNorthFaceButton(bool value) { - SetRawButton(Gamepad::Button::kNorthFace, value); + SetButton(Gamepad::Button::NORTH_FACE, value); } void GamepadSim::SetBackButton(bool value) { - SetRawButton(Gamepad::Button::kBack, value); + SetButton(Gamepad::Button::BACK, value); } void GamepadSim::SetGuideButton(bool value) { - SetRawButton(Gamepad::Button::kGuide, value); + SetButton(Gamepad::Button::GUIDE, value); } void GamepadSim::SetStartButton(bool value) { - SetRawButton(Gamepad::Button::kStart, value); + SetButton(Gamepad::Button::START, value); } void GamepadSim::SetLeftStickButton(bool value) { - SetRawButton(Gamepad::Button::kLeftStick, value); + SetButton(Gamepad::Button::LEFT_STICK, value); } void GamepadSim::SetRightStickButton(bool value) { - SetRawButton(Gamepad::Button::kRightStick, value); + SetButton(Gamepad::Button::RIGHT_STICK, value); } void GamepadSim::SetLeftBumperButton(bool value) { - SetRawButton(Gamepad::Button::kLeftBumper, value); + SetButton(Gamepad::Button::LEFT_BUMPER, value); } void GamepadSim::SetRightBumperButton(bool value) { - SetRawButton(Gamepad::Button::kRightBumper, value); + SetButton(Gamepad::Button::RIGHT_BUMPER, value); } void GamepadSim::SetDpadUpButton(bool value) { - SetRawButton(Gamepad::Button::kDpadUp, value); + SetButton(Gamepad::Button::DPAD_UP, value); } void GamepadSim::SetDpadDownButton(bool value) { - SetRawButton(Gamepad::Button::kDpadDown, value); + SetButton(Gamepad::Button::DPAD_DOWN, value); } void GamepadSim::SetDpadLeftButton(bool value) { - SetRawButton(Gamepad::Button::kDpadLeft, value); + SetButton(Gamepad::Button::DPAD_LEFT, value); } void GamepadSim::SetDpadRightButton(bool value) { - SetRawButton(Gamepad::Button::kDpadRight, value); + SetButton(Gamepad::Button::DPAD_RIGHT, value); } void GamepadSim::SetMisc1Button(bool value) { - SetRawButton(Gamepad::Button::kMisc1, value); + SetButton(Gamepad::Button::MISC_1, value); } void GamepadSim::SetRightPaddle1Button(bool value) { - SetRawButton(Gamepad::Button::kRightPaddle1, value); + SetButton(Gamepad::Button::RIGHT_PADDLE_1, value); } void GamepadSim::SetLeftPaddle1Button(bool value) { - SetRawButton(Gamepad::Button::kLeftPaddle1, value); + SetButton(Gamepad::Button::LEFT_PADDLE_1, value); } void GamepadSim::SetRightPaddle2Button(bool value) { - SetRawButton(Gamepad::Button::kRightPaddle2, value); + SetButton(Gamepad::Button::RIGHT_PADDLE_2, value); } void GamepadSim::SetLeftPaddle2Button(bool value) { - SetRawButton(Gamepad::Button::kLeftPaddle2, value); + SetButton(Gamepad::Button::LEFT_PADDLE_2, value); } void GamepadSim::SetTouchpadButton(bool value) { - SetRawButton(Gamepad::Button::kTouchpad, value); + SetButton(Gamepad::Button::TOUCHPAD, value); } void GamepadSim::SetMisc2Button(bool value) { - SetRawButton(Gamepad::Button::kMisc2, value); + SetButton(Gamepad::Button::MISC_2, value); } void GamepadSim::SetMisc3Button(bool value) { - SetRawButton(Gamepad::Button::kMisc3, value); + SetButton(Gamepad::Button::MISC_3, value); } void GamepadSim::SetMisc4Button(bool value) { - SetRawButton(Gamepad::Button::kMisc4, value); + SetButton(Gamepad::Button::MISC_4, value); } void GamepadSim::SetMisc5Button(bool value) { - SetRawButton(Gamepad::Button::kMisc5, value); + SetButton(Gamepad::Button::MISC_5, value); } void GamepadSim::SetMisc6Button(bool value) { - SetRawButton(Gamepad::Button::kMisc6, value); + SetButton(Gamepad::Button::MISC_6, value); } diff --git a/wpilibc/src/main/native/cpp/simulation/GenericHIDSim.cpp b/wpilibc/src/main/native/cpp/simulation/GenericHIDSim.cpp index 9b30a4eb7e..851799a009 100644 --- a/wpilibc/src/main/native/cpp/simulation/GenericHIDSim.cpp +++ b/wpilibc/src/main/native/cpp/simulation/GenericHIDSim.cpp @@ -61,12 +61,13 @@ void GenericHIDSim::SetButtonsAvailable(uint64_t count) { } void GenericHIDSim::SetGamepadType(GenericHID::HIDType type) { - DriverStationSim::SetJoystickGamepadType(m_port, type); + DriverStationSim::SetJoystickGamepadType(m_port, static_cast(type)); } void GenericHIDSim::SetSupportedOutputs( GenericHID::SupportedOutputs supportedOutputs) { - DriverStationSim::SetJoystickSupportedOutputs(m_port, supportedOutputs); + DriverStationSim::SetJoystickSupportedOutputs( + m_port, static_cast(supportedOutputs)); } void GenericHIDSim::SetName(const char* name) { @@ -80,16 +81,16 @@ int32_t GenericHIDSim::GetLeds() { double GenericHIDSim::GetRumble(GenericHID::RumbleType type) { int intType = 0; switch (type) { - case GenericHID::kLeftRumble: + case GenericHID::RumbleType::LEFT_RUMBLE: intType = 0; break; - case GenericHID::kRightRumble: + case GenericHID::RumbleType::RIGHT_RUMBLE: intType = 1; break; - case GenericHID::kLeftTriggerRumble: + case GenericHID::RumbleType::LEFT_TRIGGER_RUMBLE: intType = 2; break; - case GenericHID::kRightTriggerRumble: + case GenericHID::RumbleType::RIGHT_TRIGGER_RUMBLE: intType = 3; break; default: diff --git a/wpilibc/src/main/native/include/wpi/driverstation/Gamepad.hpp b/wpilibc/src/main/native/include/wpi/driverstation/Gamepad.hpp index 034d362df0..bbfe560502 100644 --- a/wpilibc/src/main/native/include/wpi/driverstation/Gamepad.hpp +++ b/wpilibc/src/main/native/include/wpi/driverstation/Gamepad.hpp @@ -26,6 +26,78 @@ class Gamepad : public GenericHID, public wpi::util::Sendable, public wpi::util::SendableHelper { public: + /** Represents a digital button on an Gamepad. */ + enum class Button { + /// South Face button. + SOUTH_FACE = 0, + /// East Face button. + EAST_FACE = 1, + /// West Face button. + WEST_FACE = 2, + /// North Face button. + NORTH_FACE = 3, + /// Back button. + BACK = 4, + /// Guide button. + GUIDE = 5, + /// Start button. + START = 6, + /// Left stick button. + LEFT_STICK = 7, + /// Right stick button. + RIGHT_STICK = 8, + /// Left bumper button. + LEFT_BUMPER = 9, + /// Right bumper button. + RIGHT_BUMPER = 10, + /// D-pad up button. + DPAD_UP = 11, + /// D-pad down button. + DPAD_DOWN = 12, + /// D-pad left button. + DPAD_LEFT = 13, + /// D-pad right button. + DPAD_RIGHT = 14, + /// Miscellaneous 1 button. + MISC_1 = 15, + /// Right Paddle 1 button. + RIGHT_PADDLE_1 = 16, + /// Left Paddle 1 button. + LEFT_PADDLE_1 = 17, + /// Right Paddle 2 button. + RIGHT_PADDLE_2 = 18, + /// Left Paddle 2 button. + LEFT_PADDLE_2 = 19, + /// Touchpad button. + TOUCHPAD = 20, + /// Miscellaneous 2 button. + MISC_2 = 21, + /// Miscellaneous 3 button. + MISC_3 = 22, + /// Miscellaneous 4 button. + MISC_4 = 23, + /// Miscellaneous 5 button. + MISC_5 = 24, + /// Miscellaneous 6 button. + MISC_6 = 25, + }; + + /** Represents an axis on an Gamepad. */ + enum class Axis { + /// Left X axis. + LEFT_X = 0, + /// Left Y axis. + LEFT_Y = 1, + /// Right X axis. + RIGHT_X = 2, + /// Right Y axis. + RIGHT_Y = 3, + /// Left trigger. + LEFT_TRIGGER = 4, + /// Right trigger. + RIGHT_TRIGGER = 5, + }; + /** * Construct an instance of a controller. * @@ -935,83 +1007,89 @@ class Gamepad : public GenericHID, */ BooleanEvent Misc6(EventLoop* loop) const; - /** Represents a digital button on an Gamepad. */ - struct Button { - /// South Face button. - static constexpr int kSouthFace = 0; - /// East Face button. - static constexpr int kEastFace = 1; - /// West Face button. - static constexpr int kWestFace = 2; - /// North Face button. - static constexpr int kNorthFace = 3; - /// Back button. - static constexpr int kBack = 4; - /// Guide button. - static constexpr int kGuide = 5; - /// Start button. - static constexpr int kStart = 6; - /// Left stick button. - static constexpr int kLeftStick = 7; - /// Right stick button. - static constexpr int kRightStick = 8; - /// right bumper button. - static constexpr int kLeftBumper = 9; - /// right bumper button. - static constexpr int kRightBumper = 10; - /// D-pad up button. - static constexpr int kDpadUp = 11; - /// D-pad down button. - static constexpr int kDpadDown = 12; - /// D-pad left button. - static constexpr int kDpadLeft = 13; - /// D-pad right button. - static constexpr int kDpadRight = 14; - /// Miscellaneous 1 button. - static constexpr int kMisc1 = 15; - /// Right Paddle 1 button. - static constexpr int kRightPaddle1 = 16; - /// Left Paddle 1 button. - static constexpr int kLeftPaddle1 = 17; - /// Right Paddle 2 button. - static constexpr int kRightPaddle2 = 18; - /// Left Paddle 2 button. - static constexpr int kLeftPaddle2 = 19; - /// Touchpad button. - static constexpr int kTouchpad = 20; - /// Miscellaneous 2 button. - static constexpr int kMisc2 = 21; - /// Miscellaneous 3 button. - static constexpr int kMisc3 = 22; - /// Miscellaneous 4 button. - static constexpr int kMisc4 = 23; - /// Miscellaneous 5 button. - static constexpr int kMisc5 = 24; - /// Miscellaneous 6 button. - static constexpr int kMisc6 = 25; - }; + /** + * Get the button value. + * + * This method returns true if the button is being held down at the time + * that this method is being called. + * + * @param button The button + * @return The state of the button. + */ + bool GetButton(Button button) const; - /** Represents an axis on an Gamepad. */ - struct Axis { - /// Left X axis. - static constexpr int kLeftX = 0; - /// Left Y axis. - static constexpr int kLeftY = 1; - /// Right X axis. - static constexpr int kRightX = 2; - /// Right Y axis. - static constexpr int kRightY = 3; - /// Left trigger. - static constexpr int kLeftTrigger = 4; - /// Right trigger. - static constexpr int kRightTrigger = 5; - }; + /** + * Whether the button was pressed since the last check. + * + * This method returns true if the button went from not pressed to held down + * since the last time this method was called. This is useful if you only + * want to call a function once when you press the button. + * + * @param button The button + * @return Whether the button was pressed since the last check. + */ + bool GetButtonPressed(Button button); + + /** + * Whether the button was released since the last check. + * + * This method returns true if the button went from held down to not pressed + * since the last time this method was called. This is useful if you only + * want to call a function once when you release the button. + * + * @param button The button + * @return Whether the button was released since the last check. + */ + bool GetButtonReleased(Button button); + + /** + * Constructs an event instance around this button's digital signal. + * + * @param button the button + * @param loop the event loop instance to attach the event to. + * @return an event instance representing the button's digital signal attached + * to the given loop. + */ + BooleanEvent ButtonEvent(Button button, EventLoop* loop) const; + + /** + * Get the value of the axis. + * + * @param axis The axis to read + * @return The value of the axis. + */ + double GetAxis(Axis axis) const; + + /** + * Constructs an event instance that is true when the axis value is less than + * threshold + * + * @param axis The axis to read + * @param threshold The value below which this trigger should return true. + * @param loop the event loop instance to attach the event to. + * @return an event instance that is true when the axis value is less than the + * provided threshold. + */ + BooleanEvent AxisLessThan(Axis axis, double threshold, EventLoop* loop) const; + + /** + * Constructs an event instance that is true when the axis value is greater + * than threshold + * + * @param axis The axis to read + * @param threshold The value above which this trigger should return true. + * @param loop the event loop instance to attach the event to. + * @return an event instance that is true when the axis value is greater than + * the provided threshold. + */ + BooleanEvent AxisGreaterThan(Axis axis, double threshold, + EventLoop* loop) const; void InitSendable(wpi::util::SendableBuilder& builder) override; private: - double GetAxisForSendable(int axis) const; - bool GetButtonForSendable(int button) const; + double GetAxisForSendable(Axis axis) const; + bool GetButtonForSendable(Button button) const; }; } // namespace wpi diff --git a/wpilibc/src/main/native/include/wpi/driverstation/GenericHID.hpp b/wpilibc/src/main/native/include/wpi/driverstation/GenericHID.hpp index 6752a821f0..5627398fcc 100644 --- a/wpilibc/src/main/native/include/wpi/driverstation/GenericHID.hpp +++ b/wpilibc/src/main/native/include/wpi/driverstation/GenericHID.hpp @@ -28,61 +28,61 @@ class GenericHID { /** * Represents a rumble output on the Joystick. */ - enum RumbleType { + enum class RumbleType { /// Left rumble motor. - kLeftRumble, + LEFT_RUMBLE, /// Right rumble motor. - kRightRumble, + RIGHT_RUMBLE, /// Left trigger rumble motor. - kLeftTriggerRumble, + LEFT_TRIGGER_RUMBLE, /// Right trigger rumble motor. - kRightTriggerRumble, + RIGHT_TRIGGER_RUMBLE, }; /** * Represents the various outputs that a HID may support. */ - enum SupportedOutputs { + enum class SupportedOutputs : int { /// No outputs supported. - kNone = 0x0, + NONE = 0x0, /// Mono LED support. - kMonoLed = 0x1, + MONO_LED = 0x1, /// RGB LED support. - kRgbLed = 0x2, + RGB_LED = 0x2, /// Player LED support. - kPlayerLed = 0x4, + PLAYER_LED = 0x4, /// Rumble support. - kRumble = 0x8, + RUMBLE = 0x8, /// Trigger rumble support. - kTriggerRumble = 0x10, + TRIGGER_RUMBLE = 0x10, }; /** * USB HID interface type. */ - enum HIDType { + enum class HIDType { /// Unknown. - kUnknown = 0, + UNKNOWN = 0, /// Standard HID device. - kStandard, + STANDARD, /// Xbox 360 controller. - kXbox360, + XBOX_360, /// Xbox One controller. - kXboxOne, + XBOX_ONE, /// PS3 controller. - kPS3, + PS3, /// PS4 controller. - kPS4, + PS4, /// PS5 controller. - kPS5, + PS5, /// Nintendo Switch Pro controller. - kSwitchPro, + SWITCH_PRO, /// Nintendo Switch Joycon Left controller. - kSwitchJoyconLeft, + SWITCH_JOYCON_LEFT, /// Nintendo Switch Joycon Right controller. - kSwitchJoyconRight, + SWITCH_JOYCON_RIGHT, /// Nintendo Switch Joycon controller pair. - kSwitchJoyconPair + SWITCH_JOYCON_PAIR }; explicit GenericHID(int port); @@ -334,7 +334,7 @@ class GenericHID { * * @return the supported outputs of the HID. */ - GenericHID::SupportedOutputs GetSupportedOutputs() const; + SupportedOutputs GetSupportedOutputs() const; /** * Get the name of the HID. @@ -396,4 +396,16 @@ class GenericHID { uint16_t m_rightTriggerRumble = 0; }; +inline GenericHID::SupportedOutputs operator|(GenericHID::SupportedOutputs A, + GenericHID::SupportedOutputs B) { + return static_cast(static_cast(A) | + static_cast(B)); +} + +inline GenericHID::SupportedOutputs& operator|=( + GenericHID::SupportedOutputs& A, GenericHID::SupportedOutputs B) { + A = A | B; + return A; +} + } // namespace wpi diff --git a/wpilibc/src/main/native/include/wpi/simulation/GamepadSim.hpp b/wpilibc/src/main/native/include/wpi/simulation/GamepadSim.hpp index bd98c0de6b..8c3ae27996 100644 --- a/wpilibc/src/main/native/include/wpi/simulation/GamepadSim.hpp +++ b/wpilibc/src/main/native/include/wpi/simulation/GamepadSim.hpp @@ -4,13 +4,10 @@ #pragma once +#include "wpi/driverstation/Gamepad.hpp" #include "wpi/simulation/GenericHIDSim.hpp" -namespace wpi { - -class Gamepad; - -namespace sim { +namespace wpi::sim { /** * Class to control a simulated Gamepad controller. @@ -22,7 +19,7 @@ class GamepadSim : public GenericHIDSim { * * @param joystick controller to simulate */ - explicit GamepadSim(const Gamepad& joystick); + explicit GamepadSim(const wpi::Gamepad& joystick); /** * Constructs from a joystick port number. @@ -31,6 +28,22 @@ class GamepadSim : public GenericHIDSim { */ explicit GamepadSim(int port); + /** + * Set the value of a given button. + * + * @param button the button to set + * @param value the new value + */ + void SetButton(wpi::Gamepad::Button button, bool value); + + /** + * Set the value of a given axis. + * + * @param axis the axis to set + * @param value the new value + */ + void SetAxis(wpi::Gamepad::Axis axis, double value); + /** * Change the left X value of the controller's joystick. * @@ -256,5 +269,4 @@ class GamepadSim : public GenericHIDSim { void SetMisc6Button(bool value); }; -} // namespace sim -} // namespace wpi +} // namespace wpi::sim diff --git a/wpilibc/src/main/python/semiwrap/Gamepad.yml b/wpilibc/src/main/python/semiwrap/Gamepad.yml index ba7d0b2c44..b288c084dc 100644 --- a/wpilibc/src/main/python/semiwrap/Gamepad.yml +++ b/wpilibc/src/main/python/semiwrap/Gamepad.yml @@ -2,6 +2,9 @@ classes: wpi::Gamepad: ignored_bases: - wpi::util::SendableHelper + enums: + Button: + Axis: methods: Gamepad: GetLeftX: @@ -122,40 +125,12 @@ classes: GetMisc6ButtonPressed: GetMisc6ButtonReleased: Misc6: + GetButton: + GetButtonPressed: + GetButtonReleased: + ButtonEvent: + rename: button + GetAxis: + AxisLessThan: + AxisGreaterThan: InitSendable: - wpi::Gamepad::Button: - attributes: - kSouthFace: - kEastFace: - kWestFace: - kNorthFace: - kBack: - kGuide: - kStart: - kLeftStick: - kRightStick: - kLeftBumper: - kRightBumper: - kDpadUp: - kDpadDown: - kDpadLeft: - kDpadRight: - kMisc1: - kRightPaddle1: - kLeftPaddle1: - kRightPaddle2: - kLeftPaddle2: - kTouchpad: - kMisc2: - kMisc3: - kMisc4: - kMisc5: - kMisc6: - wpi::Gamepad::Axis: - attributes: - kLeftX: - kLeftY: - kRightX: - kRightY: - kLeftTrigger: - kRightTrigger: diff --git a/wpilibc/src/main/python/semiwrap/GenericHID.yml b/wpilibc/src/main/python/semiwrap/GenericHID.yml index cc384ca931..15b90932fb 100644 --- a/wpilibc/src/main/python/semiwrap/GenericHID.yml +++ b/wpilibc/src/main/python/semiwrap/GenericHID.yml @@ -7,6 +7,7 @@ classes: enums: RumbleType: SupportedOutputs: + arithmetic: true HIDType: methods: GenericHID: @@ -46,7 +47,6 @@ classes: GetButtonsAvailable: GetTouchpadFingerAvailable: GetTouchpadFinger: - inline_code: | cls_GenericHID .def("__repr__", [](py::handle self) { diff --git a/wpilibc/src/main/python/semiwrap/simulation/GamepadSim.yml b/wpilibc/src/main/python/semiwrap/simulation/GamepadSim.yml index da3ab5e412..3fdf02f314 100644 --- a/wpilibc/src/main/python/semiwrap/simulation/GamepadSim.yml +++ b/wpilibc/src/main/python/semiwrap/simulation/GamepadSim.yml @@ -7,8 +7,10 @@ classes: methods: GamepadSim: overloads: - const Gamepad&: + const wpi::Gamepad&: int: + SetButton: + SetAxis: SetLeftX: SetLeftY: SetRightX: diff --git a/wpilibc/src/test/native/cpp/GenericHIDTest.cpp b/wpilibc/src/test/native/cpp/GenericHIDTest.cpp index 82c9e0b56d..84d4e88c9e 100644 --- a/wpilibc/src/test/native/cpp/GenericHIDTest.cpp +++ b/wpilibc/src/test/native/cpp/GenericHIDTest.cpp @@ -17,18 +17,18 @@ TEST(GenericHIDTest, RumbleRange) { for (int i = 0; i <= 100; i++) { double rumbleValue = i / 100.0; - hid.SetRumble(RumbleType::kLeftRumble, rumbleValue); - EXPECT_NEAR(rumbleValue, sim.GetRumble(RumbleType::kLeftRumble), kEpsilon); + hid.SetRumble(RumbleType::LEFT_RUMBLE, rumbleValue); + EXPECT_NEAR(rumbleValue, sim.GetRumble(RumbleType::LEFT_RUMBLE), kEpsilon); - hid.SetRumble(RumbleType::kRightRumble, rumbleValue); - EXPECT_NEAR(rumbleValue, sim.GetRumble(RumbleType::kRightRumble), kEpsilon); + hid.SetRumble(RumbleType::RIGHT_RUMBLE, rumbleValue); + EXPECT_NEAR(rumbleValue, sim.GetRumble(RumbleType::RIGHT_RUMBLE), kEpsilon); - hid.SetRumble(RumbleType::kLeftTriggerRumble, rumbleValue); - EXPECT_NEAR(rumbleValue, sim.GetRumble(RumbleType::kLeftTriggerRumble), + hid.SetRumble(RumbleType::LEFT_TRIGGER_RUMBLE, rumbleValue); + EXPECT_NEAR(rumbleValue, sim.GetRumble(RumbleType::LEFT_TRIGGER_RUMBLE), kEpsilon); - hid.SetRumble(RumbleType::kRightTriggerRumble, rumbleValue); - EXPECT_NEAR(rumbleValue, sim.GetRumble(RumbleType::kRightTriggerRumble), + hid.SetRumble(RumbleType::RIGHT_TRIGGER_RUMBLE, rumbleValue); + EXPECT_NEAR(rumbleValue, sim.GetRumble(RumbleType::RIGHT_TRIGGER_RUMBLE), kEpsilon); } } @@ -38,44 +38,44 @@ TEST(GenericHIDTest, RumbleTypes) { sim::GenericHIDSim sim{0}; // Make sure all are off - hid.SetRumble(RumbleType::kLeftRumble, 0); - hid.SetRumble(RumbleType::kLeftTriggerRumble, 0); - hid.SetRumble(RumbleType::kRightRumble, 0); - hid.SetRumble(RumbleType::kRightTriggerRumble, 0); - EXPECT_NEAR(0, sim.GetRumble(RumbleType::kLeftRumble), kEpsilon); - EXPECT_NEAR(0, sim.GetRumble(RumbleType::kLeftTriggerRumble), kEpsilon); - EXPECT_NEAR(0, sim.GetRumble(RumbleType::kRightRumble), kEpsilon); - EXPECT_NEAR(0, sim.GetRumble(RumbleType::kRightTriggerRumble), kEpsilon); + hid.SetRumble(RumbleType::LEFT_RUMBLE, 0); + hid.SetRumble(RumbleType::LEFT_TRIGGER_RUMBLE, 0); + hid.SetRumble(RumbleType::RIGHT_RUMBLE, 0); + hid.SetRumble(RumbleType::RIGHT_TRIGGER_RUMBLE, 0); + EXPECT_NEAR(0, sim.GetRumble(RumbleType::LEFT_RUMBLE), kEpsilon); + EXPECT_NEAR(0, sim.GetRumble(RumbleType::LEFT_TRIGGER_RUMBLE), kEpsilon); + EXPECT_NEAR(0, sim.GetRumble(RumbleType::RIGHT_RUMBLE), kEpsilon); + EXPECT_NEAR(0, sim.GetRumble(RumbleType::RIGHT_TRIGGER_RUMBLE), kEpsilon); // test left only - hid.SetRumble(RumbleType::kLeftRumble, 1); - EXPECT_NEAR(1, sim.GetRumble(RumbleType::kLeftRumble), kEpsilon); - EXPECT_NEAR(0, sim.GetRumble(RumbleType::kRightRumble), kEpsilon); - EXPECT_NEAR(0, sim.GetRumble(RumbleType::kLeftTriggerRumble), kEpsilon); - EXPECT_NEAR(0, sim.GetRumble(RumbleType::kRightTriggerRumble), kEpsilon); - hid.SetRumble(RumbleType::kLeftRumble, 0); + hid.SetRumble(RumbleType::LEFT_RUMBLE, 1); + EXPECT_NEAR(1, sim.GetRumble(RumbleType::LEFT_RUMBLE), kEpsilon); + EXPECT_NEAR(0, sim.GetRumble(RumbleType::RIGHT_RUMBLE), kEpsilon); + EXPECT_NEAR(0, sim.GetRumble(RumbleType::LEFT_TRIGGER_RUMBLE), kEpsilon); + EXPECT_NEAR(0, sim.GetRumble(RumbleType::RIGHT_TRIGGER_RUMBLE), kEpsilon); + hid.SetRumble(RumbleType::LEFT_RUMBLE, 0); // test right only - hid.SetRumble(RumbleType::kRightRumble, 1); - EXPECT_NEAR(0, sim.GetRumble(RumbleType::kLeftRumble), kEpsilon); - EXPECT_NEAR(1, sim.GetRumble(RumbleType::kRightRumble), kEpsilon); - EXPECT_NEAR(0, sim.GetRumble(RumbleType::kLeftTriggerRumble), kEpsilon); - EXPECT_NEAR(0, sim.GetRumble(RumbleType::kRightTriggerRumble), kEpsilon); - hid.SetRumble(RumbleType::kRightRumble, 0); + hid.SetRumble(RumbleType::RIGHT_RUMBLE, 1); + EXPECT_NEAR(0, sim.GetRumble(RumbleType::LEFT_RUMBLE), kEpsilon); + EXPECT_NEAR(1, sim.GetRumble(RumbleType::RIGHT_RUMBLE), kEpsilon); + EXPECT_NEAR(0, sim.GetRumble(RumbleType::LEFT_TRIGGER_RUMBLE), kEpsilon); + EXPECT_NEAR(0, sim.GetRumble(RumbleType::RIGHT_TRIGGER_RUMBLE), kEpsilon); + hid.SetRumble(RumbleType::RIGHT_RUMBLE, 0); // test left trigger only - hid.SetRumble(RumbleType::kLeftTriggerRumble, 1); - EXPECT_NEAR(0, sim.GetRumble(RumbleType::kLeftRumble), kEpsilon); - EXPECT_NEAR(0, sim.GetRumble(RumbleType::kRightRumble), kEpsilon); - EXPECT_NEAR(1, sim.GetRumble(RumbleType::kLeftTriggerRumble), kEpsilon); - EXPECT_NEAR(0, sim.GetRumble(RumbleType::kRightTriggerRumble), kEpsilon); - hid.SetRumble(RumbleType::kLeftTriggerRumble, 0); + hid.SetRumble(RumbleType::LEFT_TRIGGER_RUMBLE, 1); + EXPECT_NEAR(0, sim.GetRumble(RumbleType::LEFT_RUMBLE), kEpsilon); + EXPECT_NEAR(0, sim.GetRumble(RumbleType::RIGHT_RUMBLE), kEpsilon); + EXPECT_NEAR(1, sim.GetRumble(RumbleType::LEFT_TRIGGER_RUMBLE), kEpsilon); + EXPECT_NEAR(0, sim.GetRumble(RumbleType::RIGHT_TRIGGER_RUMBLE), kEpsilon); + hid.SetRumble(RumbleType::LEFT_TRIGGER_RUMBLE, 0); // test right trigger only - hid.SetRumble(RumbleType::kRightTriggerRumble, 1); - EXPECT_NEAR(0, sim.GetRumble(RumbleType::kLeftRumble), kEpsilon); - EXPECT_NEAR(0, sim.GetRumble(RumbleType::kRightRumble), kEpsilon); - EXPECT_NEAR(0, sim.GetRumble(RumbleType::kLeftTriggerRumble), kEpsilon); - EXPECT_NEAR(1, sim.GetRumble(RumbleType::kRightTriggerRumble), kEpsilon); - hid.SetRumble(RumbleType::kRightTriggerRumble, 0); + hid.SetRumble(RumbleType::RIGHT_TRIGGER_RUMBLE, 1); + EXPECT_NEAR(0, sim.GetRumble(RumbleType::LEFT_RUMBLE), kEpsilon); + EXPECT_NEAR(0, sim.GetRumble(RumbleType::RIGHT_RUMBLE), kEpsilon); + EXPECT_NEAR(0, sim.GetRumble(RumbleType::LEFT_TRIGGER_RUMBLE), kEpsilon); + EXPECT_NEAR(1, sim.GetRumble(RumbleType::RIGHT_TRIGGER_RUMBLE), kEpsilon); + hid.SetRumble(RumbleType::RIGHT_TRIGGER_RUMBLE, 0); } diff --git a/wpilibcExamples/src/main/cpp/examples/HatchbotTraditional/cpp/RobotContainer.cpp b/wpilibcExamples/src/main/cpp/examples/HatchbotTraditional/cpp/RobotContainer.cpp index f4d74778bf..b984ddf91b 100644 --- a/wpilibcExamples/src/main/cpp/examples/HatchbotTraditional/cpp/RobotContainer.cpp +++ b/wpilibcExamples/src/main/cpp/examples/HatchbotTraditional/cpp/RobotContainer.cpp @@ -8,7 +8,7 @@ #include "commands/GrabHatch.hpp" #include "commands/HalveDriveVelocity.hpp" #include "commands/ReleaseHatch.hpp" -#include "wpi/commands2/button/JoystickButton.hpp" +#include "wpi/commands2/button/GamepadButton.hpp" #include "wpi/smartdashboard/SmartDashboard.hpp" RobotContainer::RobotContainer() { @@ -40,15 +40,14 @@ void RobotContainer::ConfigureButtonBindings() { // the scheduler thus, no memory leaks! // Grab the hatch when the 'South Face' button is pressed. - wpi::cmd::JoystickButton(&m_driverController, - wpi::Gamepad::Button::kSouthFace) + wpi::cmd::GamepadButton(&m_driverController, wpi::Gamepad::Button::SOUTH_FACE) .OnTrue(GrabHatch(&m_hatch).ToPtr()); // Release the hatch when the 'East Face' button is pressed. - wpi::cmd::JoystickButton(&m_driverController, wpi::Gamepad::Button::kEastFace) + wpi::cmd::GamepadButton(&m_driverController, wpi::Gamepad::Button::EAST_FACE) .OnTrue(ReleaseHatch(&m_hatch).ToPtr()); // While holding the bumper button, drive at half velocity - wpi::cmd::JoystickButton(&m_driverController, - wpi::Gamepad::Button::kRightBumper) + wpi::cmd::GamepadButton(&m_driverController, + wpi::Gamepad::Button::RIGHT_BUMPER) .WhileTrue(HalveDriveVelocity(&m_drive).ToPtr()); } diff --git a/wpilibj/src/main/java/org/wpilib/driverstation/Gamepad.java b/wpilibj/src/main/java/org/wpilib/driverstation/Gamepad.java index 0a3dac0624..82d739ebef 100644 --- a/wpilibj/src/main/java/org/wpilib/driverstation/Gamepad.java +++ b/wpilibj/src/main/java/org/wpilib/driverstation/Gamepad.java @@ -25,68 +25,70 @@ public class Gamepad extends GenericHID implements Sendable { /** Represents a digital button on a Gamepad. */ public enum Button { /** South Face button. */ - kSouthFace(0), + SOUTH_FACE(0, "SouthFaceButton"), /** East Face button. */ - kEastFace(1), + EAST_FACE(1, "EastFaceButton"), /** West Face button. */ - kWestFace(2), + WEST_FACE(2, "WestFaceButton"), /** North Face button. */ - kNorthFace(3), + NORTH_FACE(3, "NorthFaceButton"), /** Back button. */ - kBack(4), + BACK(4, "BackButton"), /** Guide button. */ - kGuide(5), + GUIDE(5, "GuideButton"), /** Start button. */ - kStart(6), + START(6, "StartButton"), /** Left stick button. */ - kLeftStick(7), + LEFT_STICK(7, "LeftStickButton"), /** Right stick button. */ - kRightStick(8), + RIGHT_STICK(8, "RightStickButton"), /** Left bumper button. */ - kLeftBumper(9), + LEFT_BUMPER(9, "LeftBumperButton"), /** Right bumper button. */ - kRightBumper(10), + RIGHT_BUMPER(10, "RightBumperButton"), /** D-pad up button. */ - kDpadUp(11), + DPAD_UP(11, "DpadUpButton"), /** D-pad down button. */ - kDpadDown(12), + DPAD_DOWN(12, "DpadDownButton"), /** D-pad left button. */ - kDpadLeft(13), + DPAD_LEFT(13, "DpadLeftButton"), /** D-pad right button. */ - kDpadRight(14), + DPAD_RIGHT(14, "DpadRightButton"), /** Miscellaneous 1 button. */ - kMisc1(15), + MISC_1(15, "Misc1Button"), /** Right Paddle 1 button. */ - kRightPaddle1(16), + RIGHT_PADDLE_1(16, "RightPaddle1Button"), /** Left Paddle 1 button. */ - kLeftPaddle1(17), + LEFT_PADDLE_1(17, "LeftPaddle1Button"), /** Right Paddle 2 button. */ - kRightPaddle2(18), + RIGHT_PADDLE_2(18, "RightPaddle2Button"), /** Left Paddle 2 button. */ - kLeftPaddle2(19), + LEFT_PADDLE_2(19, "LeftPaddle2Button"), /** Touchpad button. */ - kTouchpad(20), + TOUCHPAD(20, "TouchpadButton"), /** Miscellaneous 2 button. */ - kMisc2(21), + MISC_2(21, "Misc2Button"), /** Miscellaneous 3 button. */ - kMisc3(22), + MISC_3(22, "Misc3Button"), /** Miscellaneous 4 button. */ - kMisc4(23), + MISC_4(23, "Misc4Button"), /** Miscellaneous 5 button. */ - kMisc5(24), + MISC_5(24, "Misc5Button"), /** Miscellaneous 6 button. */ - kMisc6(25); + MISC_6(25, "Misc6Button"); /** Button value. */ public final int value; - Button(int value) { + private final String m_name; + + Button(int value, String name) { this.value = value; + this.m_name = name; } /** - * Get the human-friendly name of the button, matching the relevant methods. This is done by - * stripping the leading `k`, and appending `Button`. + * Get the human-friendly name of the button, matching the relevant methods. * *

Primarily used for automated unit tests. * @@ -94,36 +96,37 @@ public class Gamepad extends GenericHID implements Sendable { */ @Override public String toString() { - // Remove leading `k` - return this.name().substring(1) + "Button"; + return m_name; } } /** Represents an axis on an Gamepad. */ public enum Axis { /** Left X axis. */ - kLeftX(0), + LEFT_X(0, "LeftX"), /** Left Y axis. */ - kLeftY(1), + LEFT_Y(1, "LeftY"), /** Right X axis. */ - kRightX(2), + RIGHT_X(2, "RightX"), /** Right Y axis. */ - kRightY(3), + RIGHT_Y(3, "RightY"), /** Left trigger. */ - kLeftTrigger(4), + LEFT_TRIGGER(4, "LeftTriggerAxis"), /** Right trigger. */ - kRightTrigger(5); + RIGHT_TRIGGER(5, "RightTriggerAxis"); /** Axis value. */ public final int value; - Axis(int value) { + private final String m_name; + + Axis(int value, String name) { this.value = value; + this.m_name = name; } /** - * Get the human-friendly name of the axis, matching the relevant methods. This is done by - * stripping the leading `k`, and appending `Axis` if the name ends with `Trigger`. + * Get the human-friendly name of the axis, matching the relevant methods. * *

Primarily used for automated unit tests. * @@ -131,11 +134,7 @@ public class Gamepad extends GenericHID implements Sendable { */ @Override public String toString() { - var name = this.name().substring(1); // Remove leading `k` - if (name.endsWith("Trigger")) { - return name + "Axis"; - } - return name; + return m_name; } } @@ -155,7 +154,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The axis value. */ public double getLeftX() { - return getRawAxis(Axis.kLeftX.value); + return getAxis(Axis.LEFT_X); } /** @@ -164,7 +163,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The axis value. */ public double getLeftY() { - return getRawAxis(Axis.kLeftY.value); + return getAxis(Axis.LEFT_Y); } /** @@ -173,7 +172,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The axis value. */ public double getRightX() { - return getRawAxis(Axis.kRightX.value); + return getAxis(Axis.RIGHT_X); } /** @@ -182,7 +181,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The axis value. */ public double getRightY() { - return getRawAxis(Axis.kRightY.value); + return getAxis(Axis.RIGHT_Y); } /** @@ -192,7 +191,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The axis value. */ public double getLeftTriggerAxis() { - return getRawAxis(Axis.kLeftTrigger.value); + return getAxis(Axis.LEFT_TRIGGER); } /** @@ -206,7 +205,7 @@ public class Gamepad extends GenericHID implements Sendable { * threshold, attached to the given event loop */ public BooleanEvent leftTrigger(double threshold, EventLoop loop) { - return axisGreaterThan(Axis.kLeftTrigger.value, threshold, loop); + return axisGreaterThan(Axis.LEFT_TRIGGER, threshold, loop); } /** @@ -228,7 +227,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The axis value. */ public double getRightTriggerAxis() { - return getRawAxis(Axis.kRightTrigger.value); + return getAxis(Axis.RIGHT_TRIGGER); } /** @@ -242,7 +241,7 @@ public class Gamepad extends GenericHID implements Sendable { * threshold, attached to the given event loop */ public BooleanEvent rightTrigger(double threshold, EventLoop loop) { - return axisGreaterThan(Axis.kRightTrigger.value, threshold, loop); + return axisGreaterThan(Axis.RIGHT_TRIGGER, threshold, loop); } /** @@ -263,7 +262,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getSouthFaceButton() { - return getRawButton(Button.kSouthFace.value); + return getButton(Button.SOUTH_FACE); } /** @@ -272,7 +271,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getSouthFaceButtonPressed() { - return getRawButtonPressed(Button.kSouthFace.value); + return getButtonPressed(Button.SOUTH_FACE); } /** @@ -281,7 +280,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getSouthFaceButtonReleased() { - return getRawButtonReleased(Button.kSouthFace.value); + return getButtonReleased(Button.SOUTH_FACE); } /** @@ -292,7 +291,7 @@ public class Gamepad extends GenericHID implements Sendable { * given loop. */ public BooleanEvent southFace(EventLoop loop) { - return button(Button.kSouthFace.value, loop); + return button(Button.SOUTH_FACE, loop); } /** @@ -301,7 +300,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getEastFaceButton() { - return getRawButton(Button.kEastFace.value); + return getButton(Button.EAST_FACE); } /** @@ -310,7 +309,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getEastFaceButtonPressed() { - return getRawButtonPressed(Button.kEastFace.value); + return getButtonPressed(Button.EAST_FACE); } /** @@ -319,7 +318,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getEastFaceButtonReleased() { - return getRawButtonReleased(Button.kEastFace.value); + return getButtonReleased(Button.EAST_FACE); } /** @@ -330,7 +329,7 @@ public class Gamepad extends GenericHID implements Sendable { * given loop. */ public BooleanEvent eastFace(EventLoop loop) { - return button(Button.kEastFace.value, loop); + return button(Button.EAST_FACE, loop); } /** @@ -339,7 +338,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getWestFaceButton() { - return getRawButton(Button.kWestFace.value); + return getButton(Button.WEST_FACE); } /** @@ -348,7 +347,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getWestFaceButtonPressed() { - return getRawButtonPressed(Button.kWestFace.value); + return getButtonPressed(Button.WEST_FACE); } /** @@ -357,7 +356,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getWestFaceButtonReleased() { - return getRawButtonReleased(Button.kWestFace.value); + return getButtonReleased(Button.WEST_FACE); } /** @@ -368,7 +367,7 @@ public class Gamepad extends GenericHID implements Sendable { * given loop. */ public BooleanEvent westFace(EventLoop loop) { - return button(Button.kWestFace.value, loop); + return button(Button.WEST_FACE, loop); } /** @@ -377,7 +376,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getNorthFaceButton() { - return getRawButton(Button.kNorthFace.value); + return getButton(Button.NORTH_FACE); } /** @@ -386,7 +385,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getNorthFaceButtonPressed() { - return getRawButtonPressed(Button.kNorthFace.value); + return getButtonPressed(Button.NORTH_FACE); } /** @@ -395,7 +394,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getNorthFaceButtonReleased() { - return getRawButtonReleased(Button.kNorthFace.value); + return getButtonReleased(Button.NORTH_FACE); } /** @@ -406,7 +405,7 @@ public class Gamepad extends GenericHID implements Sendable { * given loop. */ public BooleanEvent northFace(EventLoop loop) { - return button(Button.kNorthFace.value, loop); + return button(Button.NORTH_FACE, loop); } /** @@ -415,7 +414,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getBackButton() { - return getRawButton(Button.kBack.value); + return getButton(Button.BACK); } /** @@ -424,7 +423,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getBackButtonPressed() { - return getRawButtonPressed(Button.kBack.value); + return getButtonPressed(Button.BACK); } /** @@ -433,7 +432,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getBackButtonReleased() { - return getRawButtonReleased(Button.kBack.value); + return getButtonReleased(Button.BACK); } /** @@ -444,7 +443,7 @@ public class Gamepad extends GenericHID implements Sendable { * loop. */ public BooleanEvent back(EventLoop loop) { - return button(Button.kBack.value, loop); + return button(Button.BACK, loop); } /** @@ -453,7 +452,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getGuideButton() { - return getRawButton(Button.kGuide.value); + return getButton(Button.GUIDE); } /** @@ -462,7 +461,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getGuideButtonPressed() { - return getRawButtonPressed(Button.kGuide.value); + return getButtonPressed(Button.GUIDE); } /** @@ -471,7 +470,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getGuideButtonReleased() { - return getRawButtonReleased(Button.kGuide.value); + return getButtonReleased(Button.GUIDE); } /** @@ -482,7 +481,7 @@ public class Gamepad extends GenericHID implements Sendable { * loop. */ public BooleanEvent guide(EventLoop loop) { - return button(Button.kGuide.value, loop); + return button(Button.GUIDE, loop); } /** @@ -491,7 +490,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getStartButton() { - return getRawButton(Button.kStart.value); + return getButton(Button.START); } /** @@ -500,7 +499,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getStartButtonPressed() { - return getRawButtonPressed(Button.kStart.value); + return getButtonPressed(Button.START); } /** @@ -509,7 +508,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getStartButtonReleased() { - return getRawButtonReleased(Button.kStart.value); + return getButtonReleased(Button.START); } /** @@ -520,7 +519,7 @@ public class Gamepad extends GenericHID implements Sendable { * loop. */ public BooleanEvent start(EventLoop loop) { - return button(Button.kStart.value, loop); + return button(Button.START, loop); } /** @@ -529,7 +528,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getLeftStickButton() { - return getRawButton(Button.kLeftStick.value); + return getButton(Button.LEFT_STICK); } /** @@ -538,7 +537,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getLeftStickButtonPressed() { - return getRawButtonPressed(Button.kLeftStick.value); + return getButtonPressed(Button.LEFT_STICK); } /** @@ -547,7 +546,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getLeftStickButtonReleased() { - return getRawButtonReleased(Button.kLeftStick.value); + return getButtonReleased(Button.LEFT_STICK); } /** @@ -558,7 +557,7 @@ public class Gamepad extends GenericHID implements Sendable { * given loop. */ public BooleanEvent leftStick(EventLoop loop) { - return button(Button.kLeftStick.value, loop); + return button(Button.LEFT_STICK, loop); } /** @@ -567,7 +566,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getRightStickButton() { - return getRawButton(Button.kRightStick.value); + return getButton(Button.RIGHT_STICK); } /** @@ -576,7 +575,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getRightStickButtonPressed() { - return getRawButtonPressed(Button.kRightStick.value); + return getButtonPressed(Button.RIGHT_STICK); } /** @@ -585,7 +584,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getRightStickButtonReleased() { - return getRawButtonReleased(Button.kRightStick.value); + return getButtonReleased(Button.RIGHT_STICK); } /** @@ -596,7 +595,7 @@ public class Gamepad extends GenericHID implements Sendable { * given loop. */ public BooleanEvent rightStick(EventLoop loop) { - return button(Button.kRightStick.value, loop); + return button(Button.RIGHT_STICK, loop); } /** @@ -605,7 +604,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getLeftBumperButton() { - return getRawButton(Button.kLeftBumper.value); + return getButton(Button.LEFT_BUMPER); } /** @@ -614,7 +613,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getLeftBumperButtonPressed() { - return getRawButtonPressed(Button.kLeftBumper.value); + return getButtonPressed(Button.LEFT_BUMPER); } /** @@ -623,7 +622,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getLeftBumperButtonReleased() { - return getRawButtonReleased(Button.kLeftBumper.value); + return getButtonReleased(Button.LEFT_BUMPER); } /** @@ -634,7 +633,7 @@ public class Gamepad extends GenericHID implements Sendable { * given loop. */ public BooleanEvent leftBumper(EventLoop loop) { - return button(Button.kLeftBumper.value, loop); + return button(Button.LEFT_BUMPER, loop); } /** @@ -643,7 +642,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getRightBumperButton() { - return getRawButton(Button.kRightBumper.value); + return getButton(Button.RIGHT_BUMPER); } /** @@ -652,7 +651,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getRightBumperButtonPressed() { - return getRawButtonPressed(Button.kRightBumper.value); + return getButtonPressed(Button.RIGHT_BUMPER); } /** @@ -661,7 +660,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getRightBumperButtonReleased() { - return getRawButtonReleased(Button.kRightBumper.value); + return getButtonReleased(Button.RIGHT_BUMPER); } /** @@ -672,7 +671,7 @@ public class Gamepad extends GenericHID implements Sendable { * given loop. */ public BooleanEvent rightBumper(EventLoop loop) { - return button(Button.kRightBumper.value, loop); + return button(Button.RIGHT_BUMPER, loop); } /** @@ -681,7 +680,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getDpadUpButton() { - return getRawButton(Button.kDpadUp.value); + return getButton(Button.DPAD_UP); } /** @@ -690,7 +689,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getDpadUpButtonPressed() { - return getRawButtonPressed(Button.kDpadUp.value); + return getButtonPressed(Button.DPAD_UP); } /** @@ -699,7 +698,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getDpadUpButtonReleased() { - return getRawButtonReleased(Button.kDpadUp.value); + return getButtonReleased(Button.DPAD_UP); } /** @@ -710,7 +709,7 @@ public class Gamepad extends GenericHID implements Sendable { * given loop. */ public BooleanEvent dpadUp(EventLoop loop) { - return button(Button.kDpadUp.value, loop); + return button(Button.DPAD_UP, loop); } /** @@ -719,7 +718,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getDpadDownButton() { - return getRawButton(Button.kDpadDown.value); + return getButton(Button.DPAD_DOWN); } /** @@ -728,7 +727,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getDpadDownButtonPressed() { - return getRawButtonPressed(Button.kDpadDown.value); + return getButtonPressed(Button.DPAD_DOWN); } /** @@ -737,7 +736,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getDpadDownButtonReleased() { - return getRawButtonReleased(Button.kDpadDown.value); + return getButtonReleased(Button.DPAD_DOWN); } /** @@ -748,7 +747,7 @@ public class Gamepad extends GenericHID implements Sendable { * given loop. */ public BooleanEvent dpadDown(EventLoop loop) { - return button(Button.kDpadDown.value, loop); + return button(Button.DPAD_DOWN, loop); } /** @@ -757,7 +756,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getDpadLeftButton() { - return getRawButton(Button.kDpadLeft.value); + return getButton(Button.DPAD_LEFT); } /** @@ -766,7 +765,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getDpadLeftButtonPressed() { - return getRawButtonPressed(Button.kDpadLeft.value); + return getButtonPressed(Button.DPAD_LEFT); } /** @@ -775,7 +774,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getDpadLeftButtonReleased() { - return getRawButtonReleased(Button.kDpadLeft.value); + return getButtonReleased(Button.DPAD_LEFT); } /** @@ -786,7 +785,7 @@ public class Gamepad extends GenericHID implements Sendable { * given loop. */ public BooleanEvent dpadLeft(EventLoop loop) { - return button(Button.kDpadLeft.value, loop); + return button(Button.DPAD_LEFT, loop); } /** @@ -795,7 +794,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getDpadRightButton() { - return getRawButton(Button.kDpadRight.value); + return getButton(Button.DPAD_RIGHT); } /** @@ -804,7 +803,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getDpadRightButtonPressed() { - return getRawButtonPressed(Button.kDpadRight.value); + return getButtonPressed(Button.DPAD_RIGHT); } /** @@ -813,7 +812,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getDpadRightButtonReleased() { - return getRawButtonReleased(Button.kDpadRight.value); + return getButtonReleased(Button.DPAD_RIGHT); } /** @@ -824,7 +823,7 @@ public class Gamepad extends GenericHID implements Sendable { * given loop. */ public BooleanEvent dpadRight(EventLoop loop) { - return button(Button.kDpadRight.value, loop); + return button(Button.DPAD_RIGHT, loop); } /** @@ -833,7 +832,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getMisc1Button() { - return getRawButton(Button.kMisc1.value); + return getButton(Button.MISC_1); } /** @@ -842,7 +841,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getMisc1ButtonPressed() { - return getRawButtonPressed(Button.kMisc1.value); + return getButtonPressed(Button.MISC_1); } /** @@ -851,7 +850,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getMisc1ButtonReleased() { - return getRawButtonReleased(Button.kMisc1.value); + return getButtonReleased(Button.MISC_1); } /** @@ -862,7 +861,7 @@ public class Gamepad extends GenericHID implements Sendable { * the given loop. */ public BooleanEvent misc1(EventLoop loop) { - return button(Button.kMisc1.value, loop); + return button(Button.MISC_1, loop); } /** @@ -871,7 +870,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getRightPaddle1Button() { - return getRawButton(Button.kRightPaddle1.value); + return getButton(Button.RIGHT_PADDLE_1); } /** @@ -880,7 +879,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getRightPaddle1ButtonPressed() { - return getRawButtonPressed(Button.kRightPaddle1.value); + return getButtonPressed(Button.RIGHT_PADDLE_1); } /** @@ -889,7 +888,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getRightPaddle1ButtonReleased() { - return getRawButtonReleased(Button.kRightPaddle1.value); + return getButtonReleased(Button.RIGHT_PADDLE_1); } /** @@ -900,7 +899,7 @@ public class Gamepad extends GenericHID implements Sendable { * the given loop. */ public BooleanEvent rightPaddle1(EventLoop loop) { - return button(Button.kRightPaddle1.value, loop); + return button(Button.RIGHT_PADDLE_1, loop); } /** @@ -909,7 +908,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getLeftPaddle1Button() { - return getRawButton(Button.kLeftPaddle1.value); + return getButton(Button.LEFT_PADDLE_1); } /** @@ -918,7 +917,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getLeftPaddle1ButtonPressed() { - return getRawButtonPressed(Button.kLeftPaddle1.value); + return getButtonPressed(Button.LEFT_PADDLE_1); } /** @@ -927,7 +926,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getLeftPaddle1ButtonReleased() { - return getRawButtonReleased(Button.kLeftPaddle1.value); + return getButtonReleased(Button.LEFT_PADDLE_1); } /** @@ -938,7 +937,7 @@ public class Gamepad extends GenericHID implements Sendable { * the given loop. */ public BooleanEvent leftPaddle1(EventLoop loop) { - return button(Button.kLeftPaddle1.value, loop); + return button(Button.LEFT_PADDLE_1, loop); } /** @@ -947,7 +946,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getRightPaddle2Button() { - return getRawButton(Button.kRightPaddle2.value); + return getButton(Button.RIGHT_PADDLE_2); } /** @@ -956,7 +955,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getRightPaddle2ButtonPressed() { - return getRawButtonPressed(Button.kRightPaddle2.value); + return getButtonPressed(Button.RIGHT_PADDLE_2); } /** @@ -965,7 +964,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getRightPaddle2ButtonReleased() { - return getRawButtonReleased(Button.kRightPaddle2.value); + return getButtonReleased(Button.RIGHT_PADDLE_2); } /** @@ -976,7 +975,7 @@ public class Gamepad extends GenericHID implements Sendable { * the given loop. */ public BooleanEvent rightPaddle2(EventLoop loop) { - return button(Button.kRightPaddle2.value, loop); + return button(Button.RIGHT_PADDLE_2, loop); } /** @@ -985,7 +984,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getLeftPaddle2Button() { - return getRawButton(Button.kLeftPaddle2.value); + return getButton(Button.LEFT_PADDLE_2); } /** @@ -994,7 +993,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getLeftPaddle2ButtonPressed() { - return getRawButtonPressed(Button.kLeftPaddle2.value); + return getButtonPressed(Button.LEFT_PADDLE_2); } /** @@ -1003,7 +1002,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getLeftPaddle2ButtonReleased() { - return getRawButtonReleased(Button.kLeftPaddle2.value); + return getButtonReleased(Button.LEFT_PADDLE_2); } /** @@ -1014,7 +1013,7 @@ public class Gamepad extends GenericHID implements Sendable { * the given loop. */ public BooleanEvent leftPaddle2(EventLoop loop) { - return button(Button.kLeftPaddle2.value, loop); + return button(Button.LEFT_PADDLE_2, loop); } /** @@ -1023,7 +1022,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getTouchpadButton() { - return getRawButton(Button.kTouchpad.value); + return getButton(Button.TOUCHPAD); } /** @@ -1032,7 +1031,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getTouchpadButtonPressed() { - return getRawButtonPressed(Button.kTouchpad.value); + return getButtonPressed(Button.TOUCHPAD); } /** @@ -1041,7 +1040,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getTouchpadButtonReleased() { - return getRawButtonReleased(Button.kTouchpad.value); + return getButtonReleased(Button.TOUCHPAD); } /** @@ -1052,7 +1051,7 @@ public class Gamepad extends GenericHID implements Sendable { * given loop. */ public BooleanEvent touchpad(EventLoop loop) { - return button(Button.kTouchpad.value, loop); + return button(Button.TOUCHPAD, loop); } /** @@ -1061,7 +1060,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getMisc2Button() { - return getRawButton(Button.kMisc2.value); + return getButton(Button.MISC_2); } /** @@ -1070,7 +1069,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getMisc2ButtonPressed() { - return getRawButtonPressed(Button.kMisc2.value); + return getButtonPressed(Button.MISC_2); } /** @@ -1079,7 +1078,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getMisc2ButtonReleased() { - return getRawButtonReleased(Button.kMisc2.value); + return getButtonReleased(Button.MISC_2); } /** @@ -1090,7 +1089,7 @@ public class Gamepad extends GenericHID implements Sendable { * the given loop. */ public BooleanEvent misc2(EventLoop loop) { - return button(Button.kMisc2.value, loop); + return button(Button.MISC_2, loop); } /** @@ -1099,7 +1098,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getMisc3Button() { - return getRawButton(Button.kMisc3.value); + return getButton(Button.MISC_3); } /** @@ -1108,7 +1107,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getMisc3ButtonPressed() { - return getRawButtonPressed(Button.kMisc3.value); + return getButtonPressed(Button.MISC_3); } /** @@ -1117,7 +1116,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getMisc3ButtonReleased() { - return getRawButtonReleased(Button.kMisc3.value); + return getButtonReleased(Button.MISC_3); } /** @@ -1128,7 +1127,7 @@ public class Gamepad extends GenericHID implements Sendable { * the given loop. */ public BooleanEvent misc3(EventLoop loop) { - return button(Button.kMisc3.value, loop); + return button(Button.MISC_3, loop); } /** @@ -1137,7 +1136,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getMisc4Button() { - return getRawButton(Button.kMisc4.value); + return getButton(Button.MISC_4); } /** @@ -1146,7 +1145,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getMisc4ButtonPressed() { - return getRawButtonPressed(Button.kMisc4.value); + return getButtonPressed(Button.MISC_4); } /** @@ -1155,7 +1154,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getMisc4ButtonReleased() { - return getRawButtonReleased(Button.kMisc4.value); + return getButtonReleased(Button.MISC_4); } /** @@ -1166,7 +1165,7 @@ public class Gamepad extends GenericHID implements Sendable { * the given loop. */ public BooleanEvent misc4(EventLoop loop) { - return button(Button.kMisc4.value, loop); + return button(Button.MISC_4, loop); } /** @@ -1175,7 +1174,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getMisc5Button() { - return getRawButton(Button.kMisc5.value); + return getButton(Button.MISC_5); } /** @@ -1184,7 +1183,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getMisc5ButtonPressed() { - return getRawButtonPressed(Button.kMisc5.value); + return getButtonPressed(Button.MISC_5); } /** @@ -1193,7 +1192,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getMisc5ButtonReleased() { - return getRawButtonReleased(Button.kMisc5.value); + return getButtonReleased(Button.MISC_5); } /** @@ -1204,7 +1203,7 @@ public class Gamepad extends GenericHID implements Sendable { * the given loop. */ public BooleanEvent misc5(EventLoop loop) { - return button(Button.kMisc5.value, loop); + return button(Button.MISC_5, loop); } /** @@ -1213,7 +1212,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return The state of the button. */ public boolean getMisc6Button() { - return getRawButton(Button.kMisc6.value); + return getButton(Button.MISC_6); } /** @@ -1222,7 +1221,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was pressed since the last check. */ public boolean getMisc6ButtonPressed() { - return getRawButtonPressed(Button.kMisc6.value); + return getButtonPressed(Button.MISC_6); } /** @@ -1231,7 +1230,7 @@ public class Gamepad extends GenericHID implements Sendable { * @return Whether the button was released since the last check. */ public boolean getMisc6ButtonReleased() { - return getRawButtonReleased(Button.kMisc6.value); + return getButtonReleased(Button.MISC_6); } /** @@ -1242,15 +1241,104 @@ public class Gamepad extends GenericHID implements Sendable { * the given loop. */ public BooleanEvent misc6(EventLoop loop) { - return button(Button.kMisc6.value, loop); + return button(Button.MISC_6, loop); } - private double getAxisForSendable(int axis) { - return DriverStation.getStickAxisIfAvailable(getPort(), axis).orElse(0.0); + /** + * Get the button value (starting at button 1). + * + *

This method returns true if the button is being held down at the time that this method is + * being called. + * + * @param button The button + * @return The state of the button. + */ + public boolean getButton(Button button) { + return getRawButton(button.value); } - private boolean getButtonForSendable(int button) { - return DriverStation.getStickButtonIfAvailable(getPort(), button).orElse(false); + /** + * Whether the button was pressed since the last check. + * + *

This method returns true if the button went from not pressed to held down since the last + * time this method was called. This is useful if you only want to call a function once when you + * press the button. + * + * @param button The button + * @return Whether the button was pressed since the last check. + */ + public boolean getButtonPressed(Button button) { + return getRawButtonPressed(button.value); + } + + /** + * Whether the button was released since the last check. + * + *

This method returns true if the button went from held down to not pressed since the last + * time this method was called. This is useful if you only want to call a function once when you + * release the button. + * + * @param button The button + * @return Whether the button was released since the last check. + */ + public boolean getButtonReleased(Button button) { + return getRawButtonReleased(button.value); + } + + /** + * Constructs an event instance around this button's digital signal. + * + * @param button the button + * @param loop the event loop instance to attach the event to. + * @return an event instance representing the button's digital signal attached to the given loop. + */ + public BooleanEvent button(Button button, EventLoop loop) { + return super.button(button.value, loop); + } + + /** + * Get the value of the axis. + * + * @param axis The axis to read + * @return The value of the axis. + */ + public double getAxis(Axis axis) { + return getRawAxis(axis.value); + } + + /** + * Constructs an event instance that is true when the axis value is less than {@code threshold}, + * attached to the given loop. + * + * @param axis The axis to read, starting at 0 + * @param threshold The value below which this event should return true. + * @param loop the event loop instance to attach the event to. + * @return an event instance that is true when the axis value is less than the provided threshold. + */ + public BooleanEvent axisLessThan(Axis axis, double threshold, EventLoop loop) { + return super.axisLessThan(axis.value, threshold, loop); + } + + /** + * Constructs an event instance that is true when the axis value is greater than {@code + * threshold}, attached to the given loop. + * + * @param axis The axis to read, starting at 0 + * @param threshold The value above which this event should return true. + * @param loop the event loop instance to attach the event to. + * @return an event instance that is true when the axis value is greater than the provided + * threshold. + */ + public BooleanEvent axisGreaterThan(Axis axis, double threshold, EventLoop loop) { + return super.axisGreaterThan(axis.value, threshold, loop); + } + + private double getAxisForSendable(Axis axis) { + return DriverStation.getStickAxisIfAvailable(getPort(), axis.value).orElse(0.0); + } + + private boolean getButtonForSendable(Button button) { + return DriverStation.getStickButtonIfAvailable(getPort(), button.value).orElse(false); } @Override @@ -1258,54 +1346,43 @@ public class Gamepad extends GenericHID implements Sendable { builder.setSmartDashboardType("HID"); builder.publishConstString("ControllerType", "Gamepad"); builder.addDoubleProperty( - "LeftTrigger Axis", () -> getAxisForSendable(Axis.kLeftTrigger.value), null); + "LeftTrigger Axis", () -> getAxisForSendable(Axis.LEFT_TRIGGER), null); builder.addDoubleProperty( - "RightTrigger Axis", () -> getAxisForSendable(Axis.kRightTrigger.value), null); - builder.addDoubleProperty("LeftX", () -> getAxisForSendable(Axis.kLeftX.value), null); - builder.addDoubleProperty("LeftY", () -> getAxisForSendable(Axis.kLeftY.value), null); - builder.addDoubleProperty("RightX", () -> getAxisForSendable(Axis.kRightX.value), null); - builder.addDoubleProperty("RightY", () -> getAxisForSendable(Axis.kRightY.value), null); + "RightTrigger Axis", () -> getAxisForSendable(Axis.RIGHT_TRIGGER), null); + builder.addDoubleProperty("LeftX", () -> getAxisForSendable(Axis.LEFT_X), null); + builder.addDoubleProperty("LeftY", () -> getAxisForSendable(Axis.LEFT_Y), null); + builder.addDoubleProperty("RightX", () -> getAxisForSendable(Axis.RIGHT_X), null); + builder.addDoubleProperty("RightY", () -> getAxisForSendable(Axis.RIGHT_Y), null); + builder.addBooleanProperty("SouthFace", () -> getButtonForSendable(Button.SOUTH_FACE), null); + builder.addBooleanProperty("EastFace", () -> getButtonForSendable(Button.EAST_FACE), null); + builder.addBooleanProperty("WestFace", () -> getButtonForSendable(Button.WEST_FACE), null); + builder.addBooleanProperty("NorthFace", () -> getButtonForSendable(Button.NORTH_FACE), null); + builder.addBooleanProperty("Back", () -> getButtonForSendable(Button.BACK), null); + builder.addBooleanProperty("Guide", () -> getButtonForSendable(Button.GUIDE), null); + builder.addBooleanProperty("Start", () -> getButtonForSendable(Button.START), null); + builder.addBooleanProperty("LeftStick", () -> getButtonForSendable(Button.LEFT_STICK), null); + builder.addBooleanProperty("RightStick", () -> getButtonForSendable(Button.RIGHT_STICK), null); + builder.addBooleanProperty("LeftBumper", () -> getButtonForSendable(Button.LEFT_BUMPER), null); builder.addBooleanProperty( - "SouthFace", () -> getButtonForSendable(Button.kSouthFace.value), null); + "RightBumper", () -> getButtonForSendable(Button.RIGHT_BUMPER), null); + builder.addBooleanProperty("DpadUp", () -> getButtonForSendable(Button.DPAD_UP), null); + builder.addBooleanProperty("DpadDown", () -> getButtonForSendable(Button.DPAD_DOWN), null); + builder.addBooleanProperty("DpadLeft", () -> getButtonForSendable(Button.DPAD_LEFT), null); + builder.addBooleanProperty("DpadRight", () -> getButtonForSendable(Button.DPAD_RIGHT), null); + builder.addBooleanProperty("Misc1", () -> getButtonForSendable(Button.MISC_1), null); builder.addBooleanProperty( - "EastFace", () -> getButtonForSendable(Button.kEastFace.value), null); + "RightPaddle1", () -> getButtonForSendable(Button.RIGHT_PADDLE_1), null); builder.addBooleanProperty( - "WestFace", () -> getButtonForSendable(Button.kWestFace.value), null); + "LeftPaddle1", () -> getButtonForSendable(Button.LEFT_PADDLE_1), null); builder.addBooleanProperty( - "NorthFace", () -> getButtonForSendable(Button.kNorthFace.value), null); - builder.addBooleanProperty("Back", () -> getButtonForSendable(Button.kBack.value), null); - builder.addBooleanProperty("Guide", () -> getButtonForSendable(Button.kGuide.value), null); - builder.addBooleanProperty("Start", () -> getButtonForSendable(Button.kStart.value), null); + "RightPaddle2", () -> getButtonForSendable(Button.RIGHT_PADDLE_2), null); builder.addBooleanProperty( - "LeftStick", () -> getButtonForSendable(Button.kLeftStick.value), null); - builder.addBooleanProperty( - "RightStick", () -> getButtonForSendable(Button.kRightStick.value), null); - builder.addBooleanProperty( - "LeftBumper", () -> getButtonForSendable(Button.kLeftBumper.value), null); - builder.addBooleanProperty( - "RightBumper", () -> getButtonForSendable(Button.kRightBumper.value), null); - builder.addBooleanProperty("DpadUp", () -> getButtonForSendable(Button.kDpadUp.value), null); - builder.addBooleanProperty( - "DpadDown", () -> getButtonForSendable(Button.kDpadDown.value), null); - builder.addBooleanProperty( - "DpadLeft", () -> getButtonForSendable(Button.kDpadLeft.value), null); - builder.addBooleanProperty( - "DpadRight", () -> getButtonForSendable(Button.kDpadRight.value), null); - builder.addBooleanProperty("Misc1", () -> getButtonForSendable(Button.kMisc1.value), null); - builder.addBooleanProperty( - "RightPaddle1", () -> getButtonForSendable(Button.kRightPaddle1.value), null); - builder.addBooleanProperty( - "LeftPaddle1", () -> getButtonForSendable(Button.kLeftPaddle1.value), null); - builder.addBooleanProperty( - "RightPaddle2", () -> getButtonForSendable(Button.kRightPaddle2.value), null); - builder.addBooleanProperty( - "LeftPaddle2", () -> getButtonForSendable(Button.kLeftPaddle2.value), null); - builder.addBooleanProperty( - "Touchpad", () -> getButtonForSendable(Button.kTouchpad.value), null); - builder.addBooleanProperty("Misc2", () -> getButtonForSendable(Button.kMisc2.value), null); - builder.addBooleanProperty("Misc3", () -> getButtonForSendable(Button.kMisc3.value), null); - builder.addBooleanProperty("Misc4", () -> getButtonForSendable(Button.kMisc4.value), null); - builder.addBooleanProperty("Misc5", () -> getButtonForSendable(Button.kMisc5.value), null); - builder.addBooleanProperty("Misc6", () -> getButtonForSendable(Button.kMisc6.value), null); + "LeftPaddle2", () -> getButtonForSendable(Button.LEFT_PADDLE_2), null); + builder.addBooleanProperty("Touchpad", () -> getButtonForSendable(Button.TOUCHPAD), null); + builder.addBooleanProperty("Misc2", () -> getButtonForSendable(Button.MISC_2), null); + builder.addBooleanProperty("Misc3", () -> getButtonForSendable(Button.MISC_3), null); + builder.addBooleanProperty("Misc4", () -> getButtonForSendable(Button.MISC_4), null); + builder.addBooleanProperty("Misc5", () -> getButtonForSendable(Button.MISC_5), null); + builder.addBooleanProperty("Misc6", () -> getButtonForSendable(Button.MISC_6), null); } } diff --git a/wpilibj/src/main/java/org/wpilib/driverstation/GenericHID.java b/wpilibj/src/main/java/org/wpilib/driverstation/GenericHID.java index afee0c9185..784ce697d3 100644 --- a/wpilibj/src/main/java/org/wpilib/driverstation/GenericHID.java +++ b/wpilibj/src/main/java/org/wpilib/driverstation/GenericHID.java @@ -4,6 +4,7 @@ package org.wpilib.driverstation; +import java.util.EnumSet; import java.util.HashMap; import java.util.Map; import org.wpilib.driverstation.DriverStation.POVDirection; @@ -24,39 +25,70 @@ public class GenericHID { /** Represents a rumble output on the Joystick. */ public enum RumbleType { /** Left rumble motor. */ - kLeftRumble, + LEFT_RUMBLE, /** Right rumble motor. */ - kRightRumble, + RIGHT_RUMBLE, /** Left trigger rumble motor. */ - kLeftTriggerRumble, + LEFT_TRIGGER_RUMBLE, /** Right trigger rumble motor. */ - kRightTriggerRumble, + RIGHT_TRIGGER_RUMBLE, + } + + /** Represents the various outputs that a HID may support. */ + public enum SupportedOutput { + /// No outputs supported. + NONE(0x0), + /// Mono LED support. + MONO_LED(0x1), + /// RGB LED support. + RGB_LED(0x2), + /// Player LED support. + PLAYER_LED(0x4), + /// Rumble support. + RUMBLE(0x8), + /// Trigger rumble support. + TRIGGER_RUMBLE(0x10); + + private final int value; + + SupportedOutput(int value) { + this.value = value; + } + + /** + * Get the bitfield value of this SupportedOutput. + * + * @return the bitfield value of this SupportedOutput. + */ + public int getValue() { + return value; + } } /** USB HID interface type. */ public enum HIDType { /** Unknown. */ - kUnknown(0), + UNKNOWN(0), /** Standard. */ - kStandard(1), + STANDARD(1), /** Xbox 360. */ - kXbox360(2), + XBOX_360(2), /** Xbox One. */ - kXboxOne(3), + XBOX_ONE(3), /** PS3. */ - kPS3(4), + PS3(4), /** PS4. */ - kPS4(5), + PS4(5), /** PS5. */ - kPS5(6), + PS5(6), /** Switch Pro. */ - kSwitchPro(7), + SWITCH_PRO(7), /** Switch Joycon Left. */ - kSwitchJoyconLeft(8), + SWITCH_JOYCON_LEFT(8), /** Switch Joycon Right. */ - kSwitchJoyconRight(9), + SWITCH_JOYCON_RIGHT(9), /** Switch Joycon Pair. */ - kSwitchJoyconPair(10); + SWITCH_JOYCON_PAIR(10); /** HIDType value. */ public final int value; @@ -433,8 +465,15 @@ public class GenericHID { * * @return the supported outputs for the HID. */ - public int getSupportedOutputs() { - return DriverStation.getJoystickSupportedOutputs(m_port); + public EnumSet getSupportedOutputs() { + int supported = DriverStation.getJoystickSupportedOutputs(m_port); + EnumSet outputs = EnumSet.noneOf(SupportedOutput.class); + for (SupportedOutput output : SupportedOutput.values()) { + if ((supported & output.getValue()) != 0) { + outputs.add(output); + } + } + return outputs; } /** @@ -479,10 +518,10 @@ public class GenericHID { value = Math.clamp(value, 0, 1); int rumbleValue = (int) (value * 65535); switch (type) { - case kLeftRumble -> this.m_leftRumble = rumbleValue; - case kRightRumble -> this.m_rightRumble = rumbleValue; - case kLeftTriggerRumble -> this.m_leftTriggerRumble = rumbleValue; - case kRightTriggerRumble -> this.m_rightTriggerRumble = rumbleValue; + case LEFT_RUMBLE -> this.m_leftRumble = rumbleValue; + case RIGHT_RUMBLE -> this.m_rightRumble = rumbleValue; + case LEFT_TRIGGER_RUMBLE -> this.m_leftTriggerRumble = rumbleValue; + case RIGHT_TRIGGER_RUMBLE -> this.m_rightTriggerRumble = rumbleValue; default -> { // no-op } diff --git a/wpilibj/src/main/java/org/wpilib/simulation/GamepadSim.java b/wpilibj/src/main/java/org/wpilib/simulation/GamepadSim.java index f6b7f773c5..21cff05c65 100644 --- a/wpilibj/src/main/java/org/wpilib/simulation/GamepadSim.java +++ b/wpilibj/src/main/java/org/wpilib/simulation/GamepadSim.java @@ -34,13 +34,33 @@ public class GamepadSim extends GenericHIDSim { setPOVsMaximumIndex(1); } + /** + * Set the value of a given button. + * + * @param button the button to set + * @param value the new value + */ + public void setButton(Gamepad.Button button, boolean value) { + setRawButton(button.value, value); + } + + /** + * Set the value of a given axis. + * + * @param axis the axis to set + * @param value the new value + */ + public void setAxis(Gamepad.Axis axis, double value) { + setRawAxis(axis.value, value); + } + /** * Change the left X value of the controller's joystick. * * @param value the new value */ public void setLeftX(double value) { - setRawAxis(Gamepad.Axis.kLeftX.value, value); + setAxis(Gamepad.Axis.LEFT_X, value); } /** @@ -49,7 +69,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setLeftY(double value) { - setRawAxis(Gamepad.Axis.kLeftY.value, value); + setAxis(Gamepad.Axis.LEFT_Y, value); } /** @@ -58,7 +78,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setRightX(double value) { - setRawAxis(Gamepad.Axis.kRightX.value, value); + setAxis(Gamepad.Axis.RIGHT_X, value); } /** @@ -67,7 +87,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setRightY(double value) { - setRawAxis(Gamepad.Axis.kRightY.value, value); + setAxis(Gamepad.Axis.RIGHT_Y, value); } /** @@ -76,7 +96,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setLeftTriggerAxis(double value) { - setRawAxis(Gamepad.Axis.kLeftTrigger.value, value); + setAxis(Gamepad.Axis.LEFT_TRIGGER, value); } /** @@ -85,7 +105,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setRightTriggerAxis(double value) { - setRawAxis(Gamepad.Axis.kRightTrigger.value, value); + setAxis(Gamepad.Axis.RIGHT_TRIGGER, value); } /** @@ -94,7 +114,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setSouthFaceButton(boolean value) { - setRawButton(Gamepad.Button.kSouthFace.value, value); + setButton(Gamepad.Button.SOUTH_FACE, value); } /** @@ -103,7 +123,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setEastFaceButton(boolean value) { - setRawButton(Gamepad.Button.kEastFace.value, value); + setButton(Gamepad.Button.EAST_FACE, value); } /** @@ -112,7 +132,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setWestFaceButton(boolean value) { - setRawButton(Gamepad.Button.kWestFace.value, value); + setButton(Gamepad.Button.WEST_FACE, value); } /** @@ -121,7 +141,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setNorthFaceButton(boolean value) { - setRawButton(Gamepad.Button.kNorthFace.value, value); + setButton(Gamepad.Button.NORTH_FACE, value); } /** @@ -130,7 +150,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setBackButton(boolean value) { - setRawButton(Gamepad.Button.kBack.value, value); + setButton(Gamepad.Button.BACK, value); } /** @@ -139,7 +159,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setGuideButton(boolean value) { - setRawButton(Gamepad.Button.kGuide.value, value); + setButton(Gamepad.Button.GUIDE, value); } /** @@ -148,7 +168,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setStartButton(boolean value) { - setRawButton(Gamepad.Button.kStart.value, value); + setButton(Gamepad.Button.START, value); } /** @@ -157,7 +177,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setLeftStickButton(boolean value) { - setRawButton(Gamepad.Button.kLeftStick.value, value); + setButton(Gamepad.Button.LEFT_STICK, value); } /** @@ -166,7 +186,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setRightStickButton(boolean value) { - setRawButton(Gamepad.Button.kRightStick.value, value); + setButton(Gamepad.Button.RIGHT_STICK, value); } /** @@ -175,7 +195,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setLeftBumperButton(boolean value) { - setRawButton(Gamepad.Button.kLeftBumper.value, value); + setButton(Gamepad.Button.LEFT_BUMPER, value); } /** @@ -184,7 +204,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setRightBumperButton(boolean value) { - setRawButton(Gamepad.Button.kRightBumper.value, value); + setButton(Gamepad.Button.RIGHT_BUMPER, value); } /** @@ -193,7 +213,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setDpadUpButton(boolean value) { - setRawButton(Gamepad.Button.kDpadUp.value, value); + setButton(Gamepad.Button.DPAD_UP, value); } /** @@ -202,7 +222,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setDpadDownButton(boolean value) { - setRawButton(Gamepad.Button.kDpadDown.value, value); + setButton(Gamepad.Button.DPAD_DOWN, value); } /** @@ -211,7 +231,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setDpadLeftButton(boolean value) { - setRawButton(Gamepad.Button.kDpadLeft.value, value); + setButton(Gamepad.Button.DPAD_LEFT, value); } /** @@ -220,7 +240,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setDpadRightButton(boolean value) { - setRawButton(Gamepad.Button.kDpadRight.value, value); + setButton(Gamepad.Button.DPAD_RIGHT, value); } /** @@ -229,7 +249,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setMisc1Button(boolean value) { - setRawButton(Gamepad.Button.kMisc1.value, value); + setButton(Gamepad.Button.MISC_1, value); } /** @@ -238,7 +258,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setRightPaddle1Button(boolean value) { - setRawButton(Gamepad.Button.kRightPaddle1.value, value); + setButton(Gamepad.Button.RIGHT_PADDLE_1, value); } /** @@ -247,7 +267,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setLeftPaddle1Button(boolean value) { - setRawButton(Gamepad.Button.kLeftPaddle1.value, value); + setButton(Gamepad.Button.LEFT_PADDLE_1, value); } /** @@ -256,7 +276,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setRightPaddle2Button(boolean value) { - setRawButton(Gamepad.Button.kRightPaddle2.value, value); + setButton(Gamepad.Button.RIGHT_PADDLE_2, value); } /** @@ -265,7 +285,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setLeftPaddle2Button(boolean value) { - setRawButton(Gamepad.Button.kLeftPaddle2.value, value); + setButton(Gamepad.Button.LEFT_PADDLE_2, value); } /** @@ -274,7 +294,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setTouchpadButton(boolean value) { - setRawButton(Gamepad.Button.kTouchpad.value, value); + setButton(Gamepad.Button.TOUCHPAD, value); } /** @@ -283,7 +303,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setMisc2Button(boolean value) { - setRawButton(Gamepad.Button.kMisc2.value, value); + setButton(Gamepad.Button.MISC_2, value); } /** @@ -292,7 +312,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setMisc3Button(boolean value) { - setRawButton(Gamepad.Button.kMisc3.value, value); + setButton(Gamepad.Button.MISC_3, value); } /** @@ -301,7 +321,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setMisc4Button(boolean value) { - setRawButton(Gamepad.Button.kMisc4.value, value); + setButton(Gamepad.Button.MISC_4, value); } /** @@ -310,7 +330,7 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setMisc5Button(boolean value) { - setRawButton(Gamepad.Button.kMisc5.value, value); + setButton(Gamepad.Button.MISC_5, value); } /** @@ -319,6 +339,6 @@ public class GamepadSim extends GenericHIDSim { * @param value the new value */ public void setMisc6Button(boolean value) { - setRawButton(Gamepad.Button.kMisc6.value, value); + setButton(Gamepad.Button.MISC_6, value); } } diff --git a/wpilibj/src/main/java/org/wpilib/simulation/GenericHIDSim.java b/wpilibj/src/main/java/org/wpilib/simulation/GenericHIDSim.java index cf39f8c9d6..27370faa2a 100644 --- a/wpilibj/src/main/java/org/wpilib/simulation/GenericHIDSim.java +++ b/wpilibj/src/main/java/org/wpilib/simulation/GenericHIDSim.java @@ -4,6 +4,7 @@ package org.wpilib.simulation; +import java.util.EnumSet; import org.wpilib.driverstation.DriverStation; import org.wpilib.driverstation.GenericHID; @@ -142,8 +143,12 @@ public class GenericHIDSim { * * @param supportedOutputs the new supported outputs */ - public void setSupportedOutputs(int supportedOutputs) { - DriverStationSim.setJoystickSupportedOutputs(m_port, supportedOutputs); + public void setSupportedOutputs(EnumSet supportedOutputs) { + int supportedOutputsInt = 0; + for (GenericHID.SupportedOutput output : supportedOutputs) { + supportedOutputsInt |= output.getValue(); + } + DriverStationSim.setJoystickSupportedOutputs(m_port, supportedOutputsInt); } /** @@ -173,10 +178,10 @@ public class GenericHIDSim { public double getRumble(GenericHID.RumbleType type) { int intType = switch (type) { - case kLeftRumble -> 0; - case kRightRumble -> 1; - case kLeftTriggerRumble -> 2; - case kRightTriggerRumble -> 3; + case LEFT_RUMBLE -> 0; + case RIGHT_RUMBLE -> 1; + case LEFT_TRIGGER_RUMBLE -> 2; + case RIGHT_TRIGGER_RUMBLE -> 3; }; int value = DriverStationSim.getJoystickRumble(m_port, intType); return value / 65535.0; diff --git a/wpilibj/src/test/java/org/wpilib/driverstation/GenericHIDTest.java b/wpilibj/src/test/java/org/wpilib/driverstation/GenericHIDTest.java index 8b24202d21..1eb3d34bfb 100644 --- a/wpilibj/src/test/java/org/wpilib/driverstation/GenericHIDTest.java +++ b/wpilibj/src/test/java/org/wpilib/driverstation/GenericHIDTest.java @@ -20,17 +20,17 @@ class GenericHIDTest { for (int i = 0; i <= 100; i++) { double rumbleValue = i / 100.0; - hid.setRumble(RumbleType.kLeftRumble, rumbleValue); - assertEquals(rumbleValue, sim.getRumble(RumbleType.kLeftRumble), kEpsilon); + hid.setRumble(RumbleType.LEFT_RUMBLE, rumbleValue); + assertEquals(rumbleValue, sim.getRumble(RumbleType.LEFT_RUMBLE), kEpsilon); - hid.setRumble(RumbleType.kRightRumble, rumbleValue); - assertEquals(rumbleValue, sim.getRumble(RumbleType.kRightRumble), kEpsilon); + hid.setRumble(RumbleType.RIGHT_RUMBLE, rumbleValue); + assertEquals(rumbleValue, sim.getRumble(RumbleType.RIGHT_RUMBLE), kEpsilon); - hid.setRumble(RumbleType.kLeftTriggerRumble, rumbleValue); - assertEquals(rumbleValue, sim.getRumble(RumbleType.kLeftTriggerRumble), kEpsilon); + hid.setRumble(RumbleType.LEFT_TRIGGER_RUMBLE, rumbleValue); + assertEquals(rumbleValue, sim.getRumble(RumbleType.LEFT_TRIGGER_RUMBLE), kEpsilon); - hid.setRumble(RumbleType.kRightTriggerRumble, rumbleValue); - assertEquals(rumbleValue, sim.getRumble(RumbleType.kRightTriggerRumble), kEpsilon); + hid.setRumble(RumbleType.RIGHT_TRIGGER_RUMBLE, rumbleValue); + assertEquals(rumbleValue, sim.getRumble(RumbleType.RIGHT_TRIGGER_RUMBLE), kEpsilon); } } @@ -40,45 +40,45 @@ class GenericHIDTest { final GenericHIDSim sim = new GenericHIDSim(0); // Make sure all are off - hid.setRumble(RumbleType.kLeftRumble, 0); - hid.setRumble(RumbleType.kRightRumble, 0); - hid.setRumble(RumbleType.kLeftTriggerRumble, 0); - hid.setRumble(RumbleType.kRightTriggerRumble, 0); - assertEquals(0, sim.getRumble(RumbleType.kLeftRumble), kEpsilon); - assertEquals(0, sim.getRumble(RumbleType.kRightRumble), kEpsilon); - assertEquals(0, sim.getRumble(RumbleType.kLeftTriggerRumble), kEpsilon); - assertEquals(0, sim.getRumble(RumbleType.kRightTriggerRumble), kEpsilon); + hid.setRumble(RumbleType.LEFT_RUMBLE, 0); + hid.setRumble(RumbleType.RIGHT_RUMBLE, 0); + hid.setRumble(RumbleType.LEFT_TRIGGER_RUMBLE, 0); + hid.setRumble(RumbleType.RIGHT_TRIGGER_RUMBLE, 0); + assertEquals(0, sim.getRumble(RumbleType.LEFT_RUMBLE), kEpsilon); + assertEquals(0, sim.getRumble(RumbleType.RIGHT_RUMBLE), kEpsilon); + assertEquals(0, sim.getRumble(RumbleType.LEFT_TRIGGER_RUMBLE), kEpsilon); + assertEquals(0, sim.getRumble(RumbleType.RIGHT_TRIGGER_RUMBLE), kEpsilon); // test left only - hid.setRumble(RumbleType.kLeftRumble, 1); - assertEquals(1, sim.getRumble(RumbleType.kLeftRumble), kEpsilon); - assertEquals(0, sim.getRumble(RumbleType.kRightRumble), kEpsilon); - assertEquals(0, sim.getRumble(RumbleType.kLeftTriggerRumble), kEpsilon); - assertEquals(0, sim.getRumble(RumbleType.kRightTriggerRumble), kEpsilon); - hid.setRumble(RumbleType.kLeftRumble, 0); + hid.setRumble(RumbleType.LEFT_RUMBLE, 1); + assertEquals(1, sim.getRumble(RumbleType.LEFT_RUMBLE), kEpsilon); + assertEquals(0, sim.getRumble(RumbleType.RIGHT_RUMBLE), kEpsilon); + assertEquals(0, sim.getRumble(RumbleType.LEFT_TRIGGER_RUMBLE), kEpsilon); + assertEquals(0, sim.getRumble(RumbleType.RIGHT_TRIGGER_RUMBLE), kEpsilon); + hid.setRumble(RumbleType.LEFT_RUMBLE, 0); // test right only - hid.setRumble(RumbleType.kRightRumble, 1); - assertEquals(0, sim.getRumble(RumbleType.kLeftRumble), kEpsilon); - assertEquals(1, sim.getRumble(RumbleType.kRightRumble), kEpsilon); - assertEquals(0, sim.getRumble(RumbleType.kLeftTriggerRumble), kEpsilon); - assertEquals(0, sim.getRumble(RumbleType.kRightTriggerRumble), kEpsilon); - hid.setRumble(RumbleType.kRightRumble, 0); + hid.setRumble(RumbleType.RIGHT_RUMBLE, 1); + assertEquals(0, sim.getRumble(RumbleType.LEFT_RUMBLE), kEpsilon); + assertEquals(1, sim.getRumble(RumbleType.RIGHT_RUMBLE), kEpsilon); + assertEquals(0, sim.getRumble(RumbleType.LEFT_TRIGGER_RUMBLE), kEpsilon); + assertEquals(0, sim.getRumble(RumbleType.RIGHT_TRIGGER_RUMBLE), kEpsilon); + hid.setRumble(RumbleType.RIGHT_RUMBLE, 0); // test left trigger only - hid.setRumble(RumbleType.kLeftTriggerRumble, 1); - assertEquals(0, sim.getRumble(RumbleType.kLeftRumble), kEpsilon); - assertEquals(0, sim.getRumble(RumbleType.kRightRumble), kEpsilon); - assertEquals(1, sim.getRumble(RumbleType.kLeftTriggerRumble), kEpsilon); - assertEquals(0, sim.getRumble(RumbleType.kRightTriggerRumble), kEpsilon); - hid.setRumble(RumbleType.kLeftTriggerRumble, 0); + hid.setRumble(RumbleType.LEFT_TRIGGER_RUMBLE, 1); + assertEquals(0, sim.getRumble(RumbleType.LEFT_RUMBLE), kEpsilon); + assertEquals(0, sim.getRumble(RumbleType.RIGHT_RUMBLE), kEpsilon); + assertEquals(1, sim.getRumble(RumbleType.LEFT_TRIGGER_RUMBLE), kEpsilon); + assertEquals(0, sim.getRumble(RumbleType.RIGHT_TRIGGER_RUMBLE), kEpsilon); + hid.setRumble(RumbleType.LEFT_TRIGGER_RUMBLE, 0); // test right trigger only - hid.setRumble(RumbleType.kRightTriggerRumble, 1); - assertEquals(0, sim.getRumble(RumbleType.kLeftRumble), kEpsilon); - assertEquals(0, sim.getRumble(RumbleType.kRightRumble), kEpsilon); - assertEquals(0, sim.getRumble(RumbleType.kLeftTriggerRumble), kEpsilon); - assertEquals(1, sim.getRumble(RumbleType.kRightTriggerRumble), kEpsilon); - hid.setRumble(RumbleType.kRightTriggerRumble, 0); + hid.setRumble(RumbleType.RIGHT_TRIGGER_RUMBLE, 1); + assertEquals(0, sim.getRumble(RumbleType.LEFT_RUMBLE), kEpsilon); + assertEquals(0, sim.getRumble(RumbleType.RIGHT_RUMBLE), kEpsilon); + assertEquals(0, sim.getRumble(RumbleType.LEFT_TRIGGER_RUMBLE), kEpsilon); + assertEquals(1, sim.getRumble(RumbleType.RIGHT_TRIGGER_RUMBLE), kEpsilon); + hid.setRumble(RumbleType.RIGHT_TRIGGER_RUMBLE, 0); } } diff --git a/wpilibjExamples/src/main/java/org/wpilib/examples/hatchbottraditional/RobotContainer.java b/wpilibjExamples/src/main/java/org/wpilib/examples/hatchbottraditional/RobotContainer.java index 798767cb57..395669067e 100644 --- a/wpilibjExamples/src/main/java/org/wpilib/examples/hatchbottraditional/RobotContainer.java +++ b/wpilibjExamples/src/main/java/org/wpilib/examples/hatchbottraditional/RobotContainer.java @@ -7,7 +7,7 @@ package org.wpilib.examples.hatchbottraditional; import static org.wpilib.driverstation.Gamepad.Button; import org.wpilib.command2.Command; -import org.wpilib.command2.button.JoystickButton; +import org.wpilib.command2.button.GamepadButton; import org.wpilib.driverstation.Gamepad; import org.wpilib.examples.hatchbottraditional.Constants.AutoConstants; import org.wpilib.examples.hatchbottraditional.Constants.OIConstants; @@ -83,13 +83,13 @@ public class RobotContainer { */ private void configureButtonBindings() { // Grab the hatch when the 'South Face' button is pressed. - new JoystickButton(m_driverController, Button.kSouthFace.value) + new GamepadButton(m_driverController, Button.SOUTH_FACE) .onTrue(new GrabHatch(m_hatchSubsystem)); // Release the hatch when the 'East Face' button is pressed. - new JoystickButton(m_driverController, Button.kEastFace.value) + new GamepadButton(m_driverController, Button.EAST_FACE) .onTrue(new ReleaseHatch(m_hatchSubsystem)); // While holding the bumper button, drive at half velocity - new JoystickButton(m_driverController, Button.kRightBumper.value) + new GamepadButton(m_driverController, Button.RIGHT_BUMPER) .whileTrue(new HalveDriveVelocity(m_robotDrive)); }