diff --git a/wpilibc/src/main/native/cpp/DriverStation.cpp b/wpilibc/src/main/native/cpp/DriverStation.cpp index daa57c956f..b0a16bf182 100644 --- a/wpilibc/src/main/native/cpp/DriverStation.cpp +++ b/wpilibc/src/main/native/cpp/DriverStation.cpp @@ -568,6 +568,20 @@ int DriverStation::GetLocation() { } } +bool DriverStation::WaitForDsConnection(units::second_t timeout) { + wpi::Event event{true, false}; + HAL_ProvideNewDataEventHandle(event.GetHandle()); + bool result = false; + if (timeout == 0_s) { + result = wpi::WaitForObject(event.GetHandle()); + } else { + result = wpi::WaitForObject(event.GetHandle(), timeout.value(), nullptr); + } + + HAL_RemoveNewDataEventHandle(event.GetHandle()); + return result; +} + double DriverStation::GetMatchTime() { int32_t status = 0; return HAL_GetMatchTime(&status); diff --git a/wpilibc/src/main/native/include/frc/DriverStation.h b/wpilibc/src/main/native/include/frc/DriverStation.h index 28b1cce17a..ce3a7fe45d 100644 --- a/wpilibc/src/main/native/include/frc/DriverStation.h +++ b/wpilibc/src/main/native/include/frc/DriverStation.h @@ -296,6 +296,14 @@ class DriverStation final { */ static int GetLocation(); + /** + * Wait for a DS connection. + * + * @param timeout timeout in seconds. 0 for infinite. + * @return true if connected, false if timeout + */ + static bool WaitForDsConnection(units::second_t timeout); + /** * Return the approximate match time. * diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/DriverStation.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/DriverStation.java index 0db40f8587..355f9d01f6 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/DriverStation.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/DriverStation.java @@ -1145,6 +1145,33 @@ public final class DriverStation { } } + /** + * Wait for a DS connection. + * + * @param timeoutSeconds timeout in seconds. 0 for infinite. + * @return true if connected, false if timeout + */ + public static boolean waitForDsConnection(double timeoutSeconds) { + int event = WPIUtilJNI.createEvent(true, false); + DriverStationJNI.provideNewDataEventHandle(event); + boolean result; + try { + if (timeoutSeconds == 0) { + WPIUtilJNI.waitForObject(event); + result = true; + } else { + result = !WPIUtilJNI.waitForObjectTimeout(event, timeoutSeconds); + } + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + result = false; + } finally { + DriverStationJNI.removeNewDataEventHandle(event); + WPIUtilJNI.destroyEvent(event); + } + return result; + } + /** * 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