diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/CommandGenericHID.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/CommandGenericHID.java index 392471fb020..e22d6f208ee 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/CommandGenericHID.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/button/CommandGenericHID.java @@ -26,6 +26,7 @@ public class CommandGenericHID { private final Map, Trigger>> m_axisMagnitudeGreaterThanCache = new HashMap<>(); private final Map> m_povCache = new HashMap<>(); + private final Map m_isConnectedCache = new HashMap<>(); /** * Construct an instance of a device. @@ -322,4 +323,25 @@ public void setRumble(GenericHID.RumbleType type, double value) { public boolean isConnected() { return m_hid.isConnected(); } + + /** + * Constructs a Trigger instance that is true when the HID is connected, attached to the given + * loop. + * + * @param loop the event loop instance to attach the Trigger to. + * @return a Trigger instance that is true when the HID is connected. + */ + public Trigger connected(EventLoop loop) { + return m_isConnectedCache.computeIfAbsent(loop, k -> new Trigger(loop, this::isConnected)); + } + + /** + * Constructs a Trigger instance that is true when the HID is connected, attached to {@link + * CommandScheduler#getDefaultButtonLoop() the default command scheduler button loop}. + * + * @return a Trigger instance that is true when the HID is connected. + */ + public Trigger connected() { + return connected(CommandScheduler.getInstance().getDefaultButtonLoop()); + } }