[hal,wpilib,cmd] Update POVs to use enums (#7978)

This commit is contained in:
Joseph Eng
2025-06-29 18:32:26 -07:00
committed by GitHub
parent e13d237390
commit f55564729b
30 changed files with 538 additions and 378 deletions

View File

@@ -5,6 +5,7 @@
package edu.wpi.first.wpilibj2.command.button;
import edu.wpi.first.math.Pair;
import edu.wpi.first.wpilibj.DriverStation.POVDirection;
import edu.wpi.first.wpilibj.GenericHID;
import edu.wpi.first.wpilibj.event.EventLoop;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
@@ -74,121 +75,115 @@ public class CommandGenericHID {
* attached to {@link CommandScheduler#getDefaultButtonLoop() the default command scheduler button
* loop}.
*
* <p>The POV angles start at 0 in the up direction, and increase clockwise (e.g. right is 90,
* upper-left is 315).
*
* @param angle POV angle in degrees, or -1 for the center / not pressed.
* @param angle POV angle.
* @return a Trigger instance based around this angle of a POV on the HID.
*/
public Trigger pov(int angle) {
public Trigger pov(POVDirection angle) {
return pov(0, angle, CommandScheduler.getInstance().getDefaultButtonLoop());
}
/**
* Constructs a Trigger instance based around this angle of a POV on the HID.
*
* <p>The POV angles start at 0 in the up direction, and increase clockwise (e.g. right is 90,
* upper-left is 315).
*
* @param pov index of the POV to read (starting at 0). Defaults to 0.
* @param angle POV angle in degrees, or -1 for the center / not pressed.
* @param angle POV angle.
* @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 based around this angle of a POV on the HID.
*/
public Trigger pov(int pov, int angle, EventLoop loop) {
public Trigger pov(int pov, POVDirection angle, EventLoop loop) {
var cache = m_povCache.computeIfAbsent(loop, k -> new HashMap<>());
// angle can be -1, so use 3600 instead of 360
// angle.value is a 4 bit bitfield
return cache.computeIfAbsent(
pov * 3600 + angle, k -> new Trigger(loop, () -> m_hid.getPOV(pov) == angle));
pov * 16 + angle.value, k -> new Trigger(loop, () -> m_hid.getPOV(pov) == angle));
}
/**
* Constructs a Trigger instance based around the 0 degree angle (up) of the default (index 0) POV
* Constructs a Trigger instance based around the up direction of the default (index 0) POV on the
* HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default command scheduler
* button loop}.
*
* @return a Trigger instance based around the up direction of a POV on the HID.
*/
public Trigger povUp() {
return pov(POVDirection.Up);
}
/**
* Constructs a Trigger instance based around the up right direction of the default (index 0) POV
* on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default command
* scheduler button loop}.
*
* @return a Trigger instance based around the 0 degree angle of a POV on the HID.
*/
public Trigger povUp() {
return pov(0);
}
/**
* Constructs a Trigger instance based around the 45 degree angle (right up) of the default (index
* 0) POV on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default
* command scheduler button loop}.
*
* @return a Trigger instance based around the 45 degree angle of a POV on the HID.
* @return a Trigger instance based around the up right direction of a POV on the HID.
*/
public Trigger povUpRight() {
return pov(45);
return pov(POVDirection.UpRight);
}
/**
* Constructs a Trigger instance based around the 90 degree angle (right) of the default (index 0)
* POV on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default command
* Constructs a Trigger instance based around the right direction of the default (index 0) POV on
* the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default command
* scheduler button loop}.
*
* @return a Trigger instance based around the 90 degree angle of a POV on the HID.
* @return a Trigger instance based around the right direction of a POV on the HID.
*/
public Trigger povRight() {
return pov(90);
return pov(POVDirection.Right);
}
/**
* Constructs a Trigger instance based around the 135 degree angle (right down) of the default
* (index 0) POV on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the
* default command scheduler button loop}.
* Constructs a Trigger instance based around the down right direction of the default (index 0)
* POV on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default command
* scheduler button loop}.
*
* @return a Trigger instance based around the 135 degree angle of a POV on the HID.
* @return a Trigger instance based around the down right direction of a POV on the HID.
*/
public Trigger povDownRight() {
return pov(135);
return pov(POVDirection.DownRight);
}
/**
* Constructs a Trigger instance based around the 180 degree angle (down) of the default (index 0)
* POV on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default command
* Constructs a Trigger instance based around the down direction of the default (index 0) POV on
* the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default command
* scheduler button loop}.
*
* @return a Trigger instance based around the 180 degree angle of a POV on the HID.
* @return a Trigger instance based around the down direction of a POV on the HID.
*/
public Trigger povDown() {
return pov(180);
return pov(POVDirection.Down);
}
/**
* Constructs a Trigger instance based around the 225 degree angle (down left) of the default
* (index 0) POV on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the
* default command scheduler button loop}.
*
* @return a Trigger instance based around the 225 degree angle of a POV on the HID.
*/
public Trigger povDownLeft() {
return pov(225);
}
/**
* Constructs a Trigger instance based around the 270 degree angle (left) of the default (index 0)
* Constructs a Trigger instance based around the down left POV direction of the default (index 0)
* POV on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default command
* scheduler button loop}.
*
* @return a Trigger instance based around the 270 degree angle of a POV on the HID.
* @return a Trigger instance based around the down left POV direction of a POV on the HID.
*/
public Trigger povLeft() {
return pov(270);
public Trigger povDownLeft() {
return pov(POVDirection.DownLeft);
}
/**
* Constructs a Trigger instance based around the 315 degree angle (left up) of the default (index
* 0) POV on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default
* command scheduler button loop}.
* Constructs a Trigger instance based around the left direction of the default (index 0) POV on
* the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default command
* scheduler button loop}.
*
* @return a Trigger instance based around the 315 degree angle of a POV on the HID.
* @return a Trigger instance based around the left direction of a POV on the HID.
*/
public Trigger povLeft() {
return pov(POVDirection.Left);
}
/**
* Constructs a Trigger instance based around the up left direction of the default (index 0) POV
* on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default command
* scheduler button loop}.
*
* @return a Trigger instance based around the up left direction of a POV on the HID.
*/
public Trigger povUpLeft() {
return pov(315);
return pov(POVDirection.UpLeft);
}
/**
@@ -199,7 +194,7 @@ public class CommandGenericHID {
* @return a Trigger instance based around the center position of a POV on the HID.
*/
public Trigger povCenter() {
return pov(-1);
return pov(POVDirection.Center);
}
/**

View File

@@ -6,6 +6,7 @@ package edu.wpi.first.wpilibj2.command.button;
import static edu.wpi.first.util.ErrorMessages.requireNonNullParam;
import edu.wpi.first.wpilibj.DriverStation.POVDirection;
import edu.wpi.first.wpilibj.GenericHID;
/**
@@ -18,10 +19,10 @@ public class POVButton extends Trigger {
* Creates a POV button for triggering commands.
*
* @param joystick The GenericHID object that has the POV
* @param angle The desired angle in degrees (e.g. 90, 270)
* @param angle The desired angle
* @param povNumber The POV number (see {@link GenericHID#getPOV(int)})
*/
public POVButton(GenericHID joystick, int angle, int povNumber) {
public POVButton(GenericHID joystick, POVDirection angle, int povNumber) {
super(() -> joystick.getPOV(povNumber) == angle);
requireNonNullParam(joystick, "joystick", "POVButton");
}
@@ -30,9 +31,9 @@ public class POVButton extends Trigger {
* Creates a POV button for triggering commands. By default, acts on POV 0
*
* @param joystick The GenericHID object that has the POV
* @param angle The desired angle (e.g. 90, 270)
* @param angle The desired angle
*/
public POVButton(GenericHID joystick, int angle) {
public POVButton(GenericHID joystick, POVDirection angle) {
this(joystick, angle, 0);
}
}

View File

@@ -16,49 +16,51 @@ Trigger CommandGenericHID::Button(int button, frc::EventLoop* loop) const {
return Trigger(loop, [this, button] { return m_hid.GetRawButton(button); });
}
Trigger CommandGenericHID::POV(int angle, frc::EventLoop* loop) const {
Trigger CommandGenericHID::POV(frc::DriverStation::POVDirection angle,
frc::EventLoop* loop) const {
return POV(0, angle, loop);
}
Trigger CommandGenericHID::POV(int pov, int angle, frc::EventLoop* loop) const {
Trigger CommandGenericHID::POV(int pov, frc::DriverStation::POVDirection angle,
frc::EventLoop* loop) const {
return Trigger(loop,
[this, pov, angle] { return m_hid.GetPOV(pov) == angle; });
}
Trigger CommandGenericHID::POVUp(frc::EventLoop* loop) const {
return POV(0, loop);
return POV(frc::DriverStation::POVDirection::kUp, loop);
}
Trigger CommandGenericHID::POVUpRight(frc::EventLoop* loop) const {
return POV(45, loop);
return POV(frc::DriverStation::POVDirection::kUpRight, loop);
}
Trigger CommandGenericHID::POVRight(frc::EventLoop* loop) const {
return POV(90, loop);
return POV(frc::DriverStation::POVDirection::kRight, loop);
}
Trigger CommandGenericHID::POVDownRight(frc::EventLoop* loop) const {
return POV(135, loop);
return POV(frc::DriverStation::POVDirection::kDownRight, loop);
}
Trigger CommandGenericHID::POVDown(frc::EventLoop* loop) const {
return POV(180, loop);
return POV(frc::DriverStation::POVDirection::kDown, loop);
}
Trigger CommandGenericHID::POVDownLeft(frc::EventLoop* loop) const {
return POV(225, loop);
return POV(frc::DriverStation::POVDirection::kDownLeft, loop);
}
Trigger CommandGenericHID::POVLeft(frc::EventLoop* loop) const {
return POV(270, loop);
return POV(frc::DriverStation::POVDirection::kLeft, loop);
}
Trigger CommandGenericHID::POVUpLeft(frc::EventLoop* loop) const {
return POV(315, loop);
return POV(frc::DriverStation::POVDirection::kUpLeft, loop);
}
Trigger CommandGenericHID::POVCenter(frc::EventLoop* loop) const {
return POV(360, loop);
return POV(frc::DriverStation::POVDirection::kCenter, loop);
}
Trigger CommandGenericHID::AxisLessThan(int axis, double threshold,

View File

@@ -3,6 +3,8 @@
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <frc/DriverStation.h>
#include <frc/GenericHID.h>
#include "Trigger.h"
@@ -49,133 +51,127 @@ class CommandGenericHID {
/**
* Constructs a Trigger instance based around this angle of a POV on the HID.
*
* <p>The POV angles start at 0 in the up direction, and increase clockwise
* (eg right is 90, upper-left is 315).
*
* @param loop the event loop instance to attach the event to. Defaults to
* {@link CommandScheduler::GetDefaultButtonLoop() the default command
* scheduler button loop}.
* @param angle POV angle in degrees, or -1 for the center / not pressed.
* @param angle POV angle.
* @return a Trigger instance based around this angle of a POV on the HID.
*/
Trigger POV(int angle,
Trigger POV(frc::DriverStation::POVDirection angle,
frc::EventLoop* loop =
CommandScheduler::GetInstance().GetDefaultButtonLoop()) const;
/**
* Constructs a Trigger instance based around this angle of a POV on the HID.
*
* <p>The POV angles start at 0 in the up direction, and increase clockwise
* (eg right is 90, upper-left is 315).
*
* @param loop the event loop instance to attach the event to. Defaults to
* {@link CommandScheduler::GetDefaultButtonLoop() the default command
* scheduler button loop}.
* @param pov index of the POV to read (starting at 0). Defaults to 0.
* @param angle POV angle in degrees, or -1 for the center / not pressed.
* @param angle POV angle.
* @return a Trigger instance based around this angle of a POV on the HID.
*/
Trigger POV(int pov, int angle,
Trigger POV(int pov, frc::DriverStation::POVDirection angle,
frc::EventLoop* loop =
CommandScheduler::GetInstance().GetDefaultButtonLoop()) const;
/**
* Constructs a Trigger instance based around the 0 degree angle (up) of the
* Constructs a Trigger instance based around the up direction of the
* default (index 0) POV on the HID.
*
* @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 based around the 0 degree angle of a POV on the
* @return a Trigger instance based around the up direction of a POV on the
* HID.
*/
Trigger POVUp(frc::EventLoop* loop = CommandScheduler::GetInstance()
.GetDefaultButtonLoop()) const;
/**
* Constructs a Trigger instance based around the 45 degree angle (right up)
* Constructs a Trigger instance based around the up right direction
* of the default (index 0) POV on the HID.
*
* @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 based around the 45 degree angle of a POV on the
* HID.
* @return a Trigger instance based around the up right direction of a POV on
* the HID.
*/
Trigger POVUpRight(frc::EventLoop* loop = CommandScheduler::GetInstance()
.GetDefaultButtonLoop()) const;
/**
* Constructs a Trigger instance based around the 90 degree angle (right) of
* Constructs a Trigger instance based around the right direction of
* the default (index 0) POV on the HID.
*
* @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 based around the 90 degree angle of a POV on the
* @return a Trigger instance based around the right direction of a POV on the
* HID.
*/
Trigger POVRight(frc::EventLoop* loop = CommandScheduler::GetInstance()
.GetDefaultButtonLoop()) const;
/**
* Constructs a Trigger instance based around the 135 degree angle (right
* down) of the default (index 0) POV on the HID.
* Constructs a Trigger instance based around the down right direction
* of the default (index 0) POV on the HID.
*
* @return a Trigger instance based around the 135 degree angle of a POV on
* the HID.
* @return a Trigger instance based around the down right direction of a POV
* on the HID.
*/
Trigger POVDownRight(
frc::EventLoop* loop =
CommandScheduler::GetInstance().GetDefaultButtonLoop()) const;
/**
* Constructs a Trigger instance based around the 180 degree angle (down) of
* Constructs a Trigger instance based around the down direction of
* the default (index 0) POV on the HID.
*
* @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 based around the 180 degree angle of a POV on
* @return a Trigger instance based around the down direction of a POV on
* the HID.
*/
Trigger POVDown(frc::EventLoop* loop = CommandScheduler::GetInstance()
.GetDefaultButtonLoop()) const;
/**
* Constructs a Trigger instance based around the 225 degree angle (down left)
* Constructs a Trigger instance based around the down left direction
* of the default (index 0) POV on the HID.
*
* @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 based around the 225 degree angle of a POV on
* @return a Trigger instance based around the down left direction of a POV on
* the HID.
*/
Trigger POVDownLeft(frc::EventLoop* loop = CommandScheduler::GetInstance()
.GetDefaultButtonLoop()) const;
/**
* Constructs a Trigger instance based around the 270 degree angle (left) of
* Constructs a Trigger instance based around the left direction of
* the default (index 0) POV on the HID.
*
* @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 based around the 270 degree angle of a POV on
* @return a Trigger instance based around the left direction of a POV on
* the HID.
*/
Trigger POVLeft(frc::EventLoop* loop = CommandScheduler::GetInstance()
.GetDefaultButtonLoop()) const;
/**
* Constructs a Trigger instance based around the 315 degree angle (left up)
* Constructs a Trigger instance based around the up left direction
* of the default (index 0) POV on the HID.
*
* @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 based around the 315 degree angle of a POV on
* @return a Trigger instance based around the up left direction of a POV on
* the HID.
*/
Trigger POVUpLeft(frc::EventLoop* loop = CommandScheduler::GetInstance()

View File

@@ -4,6 +4,7 @@
#pragma once
#include <frc/DriverStation.h>
#include <frc/GenericHID.h>
#include "Trigger.h"
@@ -26,7 +27,8 @@ class POVButton : public Trigger {
* @param angle The angle of the POV corresponding to a button press.
* @param povNumber The number of the POV on the joystick.
*/
POVButton(frc::GenericHID* joystick, int angle, int povNumber = 0)
POVButton(frc::GenericHID* joystick, frc::DriverStation::POVDirection angle,
int povNumber = 0)
: Trigger([joystick, angle, povNumber] {
return joystick->GetPOV(povNumber) == angle;
}) {}