[wpilib] Split DriverStation into smaller classes (#8628)

This commit is contained in:
Thad House
2026-04-18 19:56:45 -07:00
committed by GitHub
parent 58ad633ae2
commit ab2aef2c29
108 changed files with 4406 additions and 3211 deletions

View File

@@ -0,0 +1,13 @@
// 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.driverstation;
/** The robot alliance that the robot is a part of. */
public enum Alliance {
/** Red alliance. */
RED,
/** Blue alliance. */
BLUE
}

View File

@@ -4,6 +4,8 @@
package org.wpilib.driverstation;
import org.wpilib.driverstation.internal.DriverStationBackend;
/**
* A default implementation of UserControls that provides Gamepad instances for each of the 6
* joystick ports provided by the DS.
@@ -13,7 +15,7 @@ public class DefaultUserControls implements UserControls {
/** Constructs a DefaultUserControls instance with Gamepads for each port. */
public DefaultUserControls() {
m_gamepads = new Gamepad[DriverStation.kJoystickPorts];
m_gamepads = new Gamepad[DriverStationBackend.JOYSTICK_PORTS];
for (int i = 0; i < m_gamepads.length; i++) {
m_gamepads[i] = new Gamepad(i);
}

View File

@@ -0,0 +1,52 @@
// 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.driverstation;
import org.wpilib.driverstation.internal.DriverStationBackend;
/** Provides access to error and warning reporting functionality to the Driver Station. */
public final class DriverStationErrors {
private DriverStationErrors() {}
/**
* Report error to Driver Station. Optionally appends Stack trace to error message.
*
* @param error The error to report.
* @param printTrace If true, append stack trace to error string
*/
public static void reportError(String error, boolean printTrace) {
DriverStationBackend.reportError(error, printTrace);
}
/**
* Report error to Driver Station. Appends provided stack trace to error message.
*
* @param error The error to report.
* @param stackTrace The stack trace to append
*/
public static void reportError(String error, StackTraceElement[] stackTrace) {
DriverStationBackend.reportError(error, stackTrace);
}
/**
* Report warning to Driver Station. Optionally appends Stack trace to warning message.
*
* @param warning The warning to report.
* @param printTrace If true, append stack trace to warning string
*/
public static void reportWarning(String warning, boolean printTrace) {
DriverStationBackend.reportWarning(warning, printTrace);
}
/**
* Report warning to Driver Station. Appends provided stack trace to warning message.
*
* @param warning The warning to report.
* @param stackTrace The stack trace to append
*/
public static void reportWarning(String warning, StackTraceElement[] stackTrace) {
DriverStationBackend.reportWarning(warning, stackTrace);
}
}

View File

@@ -4,6 +4,7 @@
package org.wpilib.driverstation;
import org.wpilib.driverstation.internal.DriverStationBackend;
import org.wpilib.event.BooleanEvent;
import org.wpilib.event.EventLoop;
import org.wpilib.hardware.hal.HAL;
@@ -1334,11 +1335,11 @@ public class Gamepad extends GenericHID implements Sendable {
}
private double getAxisForSendable(Axis axis) {
return DriverStation.getStickAxisIfAvailable(getPort(), axis.value).orElse(0.0);
return DriverStationBackend.getStickAxisIfAvailable(getPort(), axis.value).orElse(0.0);
}
private boolean getButtonForSendable(Button button) {
return DriverStation.getStickButtonIfAvailable(getPort(), button.value).orElse(false);
return DriverStationBackend.getStickButtonIfAvailable(getPort(), button.value).orElse(false);
}
@Override

View File

@@ -7,8 +7,7 @@ package org.wpilib.driverstation;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import org.wpilib.driverstation.DriverStation.POVDirection;
import org.wpilib.driverstation.DriverStation.TouchpadFinger;
import org.wpilib.driverstation.internal.DriverStationBackend;
import org.wpilib.event.BooleanEvent;
import org.wpilib.event.EventLoop;
import org.wpilib.hardware.hal.DriverStationJNI;
@@ -150,7 +149,7 @@ public class GenericHID {
* @return The state of the button.
*/
public boolean getRawButton(int button) {
return DriverStation.getStickButton(m_port, (byte) button);
return DriverStationBackend.getStickButton(m_port, (byte) button);
}
/**
@@ -164,7 +163,7 @@ public class GenericHID {
* @return Whether the button was pressed since the last check.
*/
public boolean getRawButtonPressed(int button) {
return DriverStation.getStickButtonPressed(m_port, (byte) button);
return DriverStationBackend.getStickButtonPressed(m_port, (byte) button);
}
/**
@@ -178,7 +177,7 @@ public class GenericHID {
* @return Whether the button was released since the last check.
*/
public boolean getRawButtonReleased(int button) {
return DriverStation.getStickButtonReleased(m_port, button);
return DriverStationBackend.getStickButtonReleased(m_port, button);
}
/**
@@ -202,7 +201,7 @@ public class GenericHID {
* @return The value of the axis.
*/
public double getRawAxis(int axis) {
return DriverStation.getStickAxis(m_port, axis);
return DriverStationBackend.getStickAxis(m_port, axis);
}
/**
@@ -212,7 +211,7 @@ public class GenericHID {
* @return the angle of the POV.
*/
public POVDirection getPOV(int pov) {
return DriverStation.getStickPOV(m_port, pov);
return DriverStationBackend.getStickPOV(m_port, pov);
}
/**
@@ -394,7 +393,7 @@ public class GenericHID {
* @return the maximum axis index for the joystick
*/
public int getAxesMaximumIndex() {
return DriverStation.getStickAxesMaximumIndex(m_port);
return DriverStationBackend.getStickAxesMaximumIndex(m_port);
}
/**
@@ -403,7 +402,7 @@ public class GenericHID {
* @return the number of axis for the current HID
*/
public int getAxesAvailable() {
return DriverStation.getStickAxesAvailable(m_port);
return DriverStationBackend.getStickAxesAvailable(m_port);
}
/**
@@ -412,7 +411,7 @@ public class GenericHID {
* @return the maximum POV index for the joystick
*/
public int getPOVsMaximumIndex() {
return DriverStation.getStickPOVsMaximumIndex(m_port);
return DriverStationBackend.getStickPOVsMaximumIndex(m_port);
}
/**
@@ -421,7 +420,7 @@ public class GenericHID {
* @return the number of POVs for the current HID
*/
public int getPOVsAvailable() {
return DriverStation.getStickPOVsAvailable(m_port);
return DriverStationBackend.getStickPOVsAvailable(m_port);
}
/**
@@ -430,7 +429,7 @@ public class GenericHID {
* @return the maximum button index for the joystick
*/
public int getButtonsMaximumIndex() {
return DriverStation.getStickButtonsMaximumIndex(m_port);
return DriverStationBackend.getStickButtonsMaximumIndex(m_port);
}
/**
@@ -439,7 +438,7 @@ public class GenericHID {
* @return the number of buttons for the current HID
*/
public long getButtonsAvailable() {
return DriverStation.getStickButtonsAvailable(m_port);
return DriverStationBackend.getStickButtonsAvailable(m_port);
}
/**
@@ -448,7 +447,7 @@ public class GenericHID {
* @return true if the HID is connected
*/
public boolean isConnected() {
return DriverStation.isJoystickConnected(m_port);
return DriverStationBackend.isJoystickConnected(m_port);
}
/**
@@ -457,7 +456,7 @@ public class GenericHID {
* @return the type of the HID.
*/
public HIDType getGamepadType() {
return HIDType.of(DriverStation.getJoystickGamepadType(m_port));
return HIDType.of(DriverStationBackend.getJoystickGamepadType(m_port));
}
/**
@@ -466,7 +465,7 @@ public class GenericHID {
* @return the supported outputs for the HID.
*/
public EnumSet<SupportedOutput> getSupportedOutputs() {
int supported = DriverStation.getJoystickSupportedOutputs(m_port);
int supported = DriverStationBackend.getJoystickSupportedOutputs(m_port);
EnumSet<SupportedOutput> outputs = EnumSet.noneOf(SupportedOutput.class);
for (SupportedOutput output : SupportedOutput.values()) {
if ((supported & output.getValue()) != 0) {
@@ -482,7 +481,7 @@ public class GenericHID {
* @return the name of the HID.
*/
public String getName() {
return DriverStation.getJoystickName(m_port);
return DriverStationBackend.getJoystickName(m_port);
}
/**
@@ -543,7 +542,7 @@ public class GenericHID {
* @return true if the touchpad finger is available.
*/
public boolean getTouchpadFingerAvailable(int touchpad, int finger) {
return DriverStation.getStickTouchpadFingerAvailable(m_port, touchpad, finger);
return DriverStationBackend.getStickTouchpadFingerAvailable(m_port, touchpad, finger);
}
/**
@@ -554,6 +553,6 @@ public class GenericHID {
* @return The touchpad finger data.
*/
public TouchpadFinger getTouchpadFinger(int touchpad, int finger) {
return DriverStation.getStickTouchpadFinger(m_port, touchpad, finger);
return DriverStationBackend.getStickTouchpadFinger(m_port, touchpad, finger);
}
}

View File

@@ -0,0 +1,104 @@
// 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.driverstation;
import java.util.Optional;
import java.util.OptionalInt;
import org.wpilib.driverstation.internal.DriverStationBackend;
/** Provides access to match state information from the Driver Station. */
public final class MatchState {
private MatchState() {}
/**
* Return the approximate match time. The FMS does not send an official match time to the robots,
* but does send an approximate match time. The value will count down the time remaining in the
* current period (auto or teleop). Warning: This is not an official time (so it cannot be used to
* dispute ref calls or guarantee that a function will trigger before the match ends).
*
* <p>When connected to the real field, this number only changes in full integer increments, and
* always counts down.
*
* <p>When the DS is in practice mode, this number is a floating point number, and counts down.
*
* <p>When the DS is in teleop or autonomous mode, this number returns -1.0.
*
* <p>Simulation matches DS behavior without an FMS connected.
*
* @return Time remaining in current match period (auto or teleop) in seconds
*/
public static double getMatchTime() {
return DriverStationBackend.getMatchTime();
}
/**
* Get the current alliance from the FMS.
*
* <p>If the FMS is not connected, it is set from the team alliance setting on the driver station.
*
* @return The alliance (red or blue) or an empty optional if the alliance is invalid
*/
public static Optional<Alliance> getAlliance() {
return DriverStationBackend.getAlliance();
}
/**
* Gets the location of the team's driver station controls from the FMS.
*
* <p>If the FMS is not connected, it is set from the team alliance setting on the driver station.
*
* @return the location of the team's driver station controls: 1, 2, or 3
*/
public static OptionalInt getLocation() {
return DriverStationBackend.getLocation();
}
/**
* Get the replay number from the FMS.
*
* @return the replay number
*/
public static int getReplayNumber() {
return DriverStationBackend.getReplayNumber();
}
/**
* Get the match number from the FMS.
*
* @return the match number
*/
public static int getMatchNumber() {
return DriverStationBackend.getMatchNumber();
}
/**
* Get the match type from the FMS.
*
* @return the match type
*/
public static MatchType getMatchType() {
return DriverStationBackend.getMatchType();
}
/**
* Get the event name from the FMS.
*
* @return the event name
*/
public static String getEventName() {
return DriverStationBackend.getEventName();
}
/**
* Get the game specific message from the FMS.
*
* <p>If the FMS is not connected, it is set from the game data setting on the driver station.
*
* @return the game specific message
*/
public static Optional<String> getGameData() {
return DriverStationBackend.getGameData();
}
}

View File

@@ -0,0 +1,17 @@
// 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.driverstation;
/** The type of robot match that the robot is part of. */
public enum MatchType {
/** None. */
NONE,
/** Practice. */
PRACTICE,
/** Qualification. */
QUALIFICATION,
/** Elimination. */
ELIMINATION
}

View File

@@ -0,0 +1,82 @@
// 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.driverstation;
import java.util.Optional;
import org.wpilib.math.geometry.Rotation2d;
import org.wpilib.system.Timer;
/** A controller POV direction. */
public enum POVDirection {
/** POV center. */
CENTER(0x00),
/** POV up. */
UP(0x01),
/** POV up right. */
UP_RIGHT(0x01 | 0x02),
/** POV right. */
RIGHT(0x02),
/** POV down right. */
DOWN_RIGHT(0x02 | 0x04),
/** POV down. */
DOWN(0x04),
/** POV down left. */
DOWN_LEFT(0x04 | 0x08),
/** POV left. */
LEFT(0x08),
/** POV up left. */
UP_LEFT(0x01 | 0x08);
private static final double INVALID_POV_VALUE_INTERVAL = 1.0;
private static double s_nextMessageTime;
/**
* Converts a byte value into a POVDirection enum value.
*
* @param value The byte value to convert.
* @return The corresponding POVDirection enum value.
* @throws IllegalArgumentException If value does not correspond to a POVDirection.
*/
public static POVDirection of(byte value) {
for (var direction : values()) {
if (direction.value == value) {
return direction;
}
}
double currentTime = Timer.getTimestamp();
if (currentTime > s_nextMessageTime) {
DriverStationErrors.reportError("Invalid POV value " + value + "!", false);
s_nextMessageTime = currentTime + INVALID_POV_VALUE_INTERVAL;
}
return CENTER;
}
/** The corresponding HAL value. */
public final byte value;
POVDirection(int value) {
this.value = (byte) value;
}
/**
* Gets the angle of a POVDirection.
*
* @return The angle clockwise from straight up, or Optional.empty() if this POVDirection is
* CENTER.
*/
public Optional<Rotation2d> getAngle() {
return switch (this) {
case CENTER -> Optional.empty();
case UP -> Optional.of(Rotation2d.fromDegrees(0));
case UP_RIGHT -> Optional.of(Rotation2d.fromDegrees(45));
case RIGHT -> Optional.of(Rotation2d.fromDegrees(90));
case DOWN_RIGHT -> Optional.of(Rotation2d.fromDegrees(135));
case DOWN -> Optional.of(Rotation2d.fromDegrees(180));
case DOWN_LEFT -> Optional.of(Rotation2d.fromDegrees(225));
case LEFT -> Optional.of(Rotation2d.fromDegrees(270));
case UP_LEFT -> Optional.of(Rotation2d.fromDegrees(315));
};
}
}

View File

@@ -0,0 +1,270 @@
// 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.driverstation;
import org.wpilib.driverstation.internal.DriverStationBackend;
import org.wpilib.hardware.hal.RobotMode;
import org.wpilib.util.Color;
/** Provides access to robot state information from the Driver Station. */
public final class RobotState {
private RobotState() {}
/**
* Gets a value indicating whether the Driver Station requires the robot to be enabled.
*
* @return True if the robot is enabled, false otherwise.
*/
public static boolean isEnabled() {
return DriverStationBackend.isEnabled();
}
/**
* Gets a value indicating whether the Driver Station requires the robot to be disabled.
*
* @return True if the robot should be disabled, false otherwise.
*/
public static boolean isDisabled() {
return DriverStationBackend.isDisabled();
}
/**
* Gets a value indicating whether the Robot is e-stopped.
*
* @return True if the robot is e-stopped, false otherwise.
*/
public static boolean isEStopped() {
return DriverStationBackend.isEStopped();
}
/**
* Gets the current robot mode.
*
* <p>Note that this does not indicate whether the robot is enabled or disabled.
*
* @return robot mode
*/
public static RobotMode getRobotMode() {
return DriverStationBackend.getRobotMode();
}
/**
* Gets a value indicating whether the Driver Station requires the robot to be running in
* autonomous mode.
*
* @return True if autonomous mode should be enabled, false otherwise.
*/
public static boolean isAutonomous() {
return DriverStationBackend.isAutonomous();
}
/**
* Gets a value indicating whether the Driver Station requires the robot to be running in
* autonomous mode and enabled.
*
* @return True if autonomous should be set and the robot should be enabled.
*/
public static boolean isAutonomousEnabled() {
return DriverStationBackend.isAutonomousEnabled();
}
/**
* Gets a value indicating whether the Driver Station requires the robot to be running in
* operator-controlled mode.
*
* @return True if operator-controlled mode should be enabled, false otherwise.
*/
public static boolean isTeleop() {
return DriverStationBackend.isTeleop();
}
/**
* Gets a value indicating whether the Driver Station requires the robot to be running in
* operator-controller mode and enabled.
*
* @return True if operator-controlled mode should be set and the robot should be enabled.
*/
public static boolean isTeleopEnabled() {
return DriverStationBackend.isTeleopEnabled();
}
/**
* Gets a value indicating whether the Driver Station requires the robot to be running in Test
* mode.
*
* @return True if test mode should be enabled, false otherwise.
*/
public static boolean isTest() {
return DriverStationBackend.isTest();
}
/**
* Gets a value indicating whether the Driver Station requires the robot to be running in Test
* mode and enabled.
*
* @return True if test mode should be set and the robot should be enabled.
*/
public static boolean isTestEnabled() {
return DriverStationBackend.isTestEnabled();
}
/**
* Adds an operating mode option. It's necessary to call publishOpModes() to make the added modes
* visible to the driver station.
*
* @param mode robot mode
* @param name name of the operating mode
* @param group group of the operating mode
* @param description description of the operating mode
* @param textColor text color, or null for default
* @param backgroundColor background color, or null for default
* @return unique ID used to later identify the operating mode
* @throws IllegalArgumentException if name is empty or an operating mode with the same robot mode
* and name already exists
*/
public static long addOpMode(
RobotMode mode,
String name,
String group,
String description,
Color textColor,
Color backgroundColor) {
return DriverStationBackend.addOpMode(
mode, name, group, description, textColor, backgroundColor);
}
/**
* Adds an operating mode option. It's necessary to call publishOpModes() to make the added modes
* visible to the driver station.
*
* @param mode robot mode
* @param name name of the operating mode
* @param group group of the operating mode
* @param description description of the operating mode
* @return unique ID used to later identify the operating mode
* @throws IllegalArgumentException if name is empty or an operating mode with the same name
* already exists
*/
public static long addOpMode(RobotMode mode, String name, String group, String description) {
return DriverStationBackend.addOpMode(mode, name, group, description);
}
/**
* Adds an operating mode option. It's necessary to call publishOpModes() to make the added modes
* visible to the driver station.
*
* @param mode robot mode
* @param name name of the operating mode
* @param group group of the operating mode
* @return unique ID used to later identify the operating mode
* @throws IllegalArgumentException if name is empty or an operating mode with the same name
* already exists
*/
public static long addOpMode(RobotMode mode, String name, String group) {
return DriverStationBackend.addOpMode(mode, name, group);
}
/**
* Adds an operating mode option. It's necessary to call publishOpModes() to make the added modes
* visible to the driver station.
*
* @param mode robot mode
* @param name name of the operating mode
* @return unique ID used to later identify the operating mode
* @throws IllegalArgumentException if name is empty or an operating mode with the same name
* already exists
*/
public static long addOpMode(RobotMode mode, String name) {
return DriverStationBackend.addOpMode(mode, name);
}
/**
* Removes an operating mode option. It's necessary to call publishOpModes() to make the removed
* mode no longer visible to the driver station.
*
* @param mode robot mode
* @param name name of the operating mode
* @return unique ID for the opmode, or 0 if not found
*/
public static long removeOpMode(RobotMode mode, String name) {
return DriverStationBackend.removeOpMode(mode, name);
}
/** Publishes the operating mode options to the driver station. */
public static void publishOpModes() {
DriverStationBackend.publishOpModes();
}
/** Clears all operating mode options and publishes an empty list to the driver station. */
public static void clearOpModes() {
DriverStationBackend.clearOpModes();
}
/**
* Gets the operating mode selected on the driver station. Note this does not mean the robot is
* enabled; use isEnabled() for that. In a match, this will indicate the operating mode selected
* for auto before the match starts (i.e., while the robot is disabled in auto mode); after the
* auto period ends, this will change to reflect the operating mode selected for teleop.
*
* @return the unique ID provided by the addOpMode() function; may return 0 or a unique ID not
* added, so callers should be prepared to handle that case
*/
public static long getOpModeId() {
return DriverStationBackend.getOpModeId();
}
/**
* Gets the operating mode selected on the driver station. Note this does not mean the robot is
* enabled; use isEnabled() for that. In a match, this will indicate the operating mode selected
* for auto before the match starts (i.e., while the robot is disabled in auto mode); after the
* auto period ends, this will change to reflect the operating mode selected for teleop.
*
* @return Operating mode string; may return a string not in the list of options, so callers
* should be prepared to handle that case
*/
public static String getOpMode() {
return DriverStationBackend.getOpMode();
}
/**
* Check to see if the selected operating mode is a particular value. Note this does not mean the
* robot is enabled; use isEnabled() for that.
*
* @param id operating mode unique ID
* @return True if that mode is the current mode
*/
public static boolean isOpMode(long id) {
return DriverStationBackend.isOpMode(id);
}
/**
* Check to see if the selected operating mode is a particular value. Note this does not mean the
* robot is enabled; use isEnabled() for that.
*
* @param mode operating mode
* @return True if that mode is the current mode
*/
public static boolean isOpMode(String mode) {
return DriverStationBackend.isOpMode(mode);
}
/**
* Gets a value indicating whether the Driver Station is attached.
*
* @return True if Driver Station is attached, false otherwise.
*/
public static boolean isDSAttached() {
return DriverStationBackend.isDSAttached();
}
/**
* Gets if the driver station attached to a Field Management System.
*
* @return true if the robot is competing on a field being controlled by a Field Management System
*/
public static boolean isFMSAttached() {
return DriverStationBackend.isFMSAttached();
}
}

View File

@@ -0,0 +1,31 @@
// 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.driverstation;
/** Represents a finger on a touchpad. */
@SuppressWarnings("MemberName")
public final class TouchpadFinger {
/** Whether the finger is touching the touchpad. */
public final boolean down;
/** The x position of the finger. 0 is at top left. */
public final float x;
/** The y position of the finger. 0 is at top left. */
public final float y;
/**
* Creates a TouchpadFinger object.
*
* @param down Whether the finger is touching the touchpad.
* @param x The x position of the finger.
* @param y The y position of the finger.
*/
public TouchpadFinger(boolean down, float x, float y) {
this.x = x;
this.y = y;
this.down = down;
}
}

File diff suppressed because it is too large Load Diff