[wpilibj] Allow passing DS Instance to Robot and OpModes (#8626)

Some discussion with the tech team showed that there were some real
advantages to being able to pass a 2nd type. It allows separating the DS
and Robot. Additionally, we can make the DriverStationBase class
actually usable instead of the existing DriverStation class which is
impossible to handle in intellisense because it has too much.

This won't fully be doable in C++, but we will need to implement
something similar in python.
This commit is contained in:
Thad House
2026-03-20 13:05:48 -07:00
committed by GitHub
parent b86204bf45
commit fb4bcefabc
7 changed files with 620 additions and 61 deletions

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;
/**
* A default implementation of UserControls that provides Gamepad instances for each of the 6
* joystick ports provided by the DS.
*/
public class DefaultUserControls implements UserControls {
private final Gamepad[] m_gamepads;
/** Constructs a DefaultUserControls instance with Gamepads for each port. */
public DefaultUserControls() {
m_gamepads = new Gamepad[DriverStation.kJoystickPorts];
for (int i = 0; i < m_gamepads.length; i++) {
m_gamepads[i] = new Gamepad(i);
}
}
/**
* Returns the Gamepad instance for the specified port.
*
* @param port The joystick port number.
* @return The Gamepad instance for the given port.
*/
public Gamepad getGamepad(int port) {
return m_gamepads[port];
}
}

View File

@@ -0,0 +1,15 @@
// 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;
/**
* An interface representing user controls such as gamepads or joysticks. If your main robot class
* has a UserControlsInstance attribute with a class implementing this interface, the constructor is
* able to receive an instance of that class. Additionally, any OpModes can also receive that same
* instance.
*
* <p>The implementation of this class must have a default constructor
*/
public interface UserControls {}

View File

@@ -0,0 +1,26 @@
// 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.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* An annotation to specify the UserControls implementation class to be used for a robot. Apply this
* annotation to your main robot class, providing a class that implements the UserControls
* interface.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface UserControlsInstance {
/**
* The UserControls implementation class to be used.
*
* @return The class that implements UserControls.
*/
Class<? extends UserControls> value();
}