From 796dbd3b864a4d251acf7ea427a51fd6d0f6ff62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Beaulac?= Date: Thu, 17 Oct 2024 20:03:40 -0400 Subject: [PATCH] [wpilib] Add Timer.isRunning() method (#7220) --- wpilibc/src/main/native/cpp/Timer.cpp | 4 ++++ wpilibc/src/main/native/include/frc/Timer.h | 7 +++++++ wpilibc/src/test/native/cpp/TimerTest.cpp | 4 ++++ wpilibj/src/main/java/edu/wpi/first/wpilibj/Timer.java | 9 +++++++++ .../src/test/java/edu/wpi/first/wpilibj/TimerTest.java | 4 ++++ 5 files changed, 28 insertions(+) diff --git a/wpilibc/src/main/native/cpp/Timer.cpp b/wpilibc/src/main/native/cpp/Timer.cpp index dfad62071ba..02dd9bad6ab 100644 --- a/wpilibc/src/main/native/cpp/Timer.cpp +++ b/wpilibc/src/main/native/cpp/Timer.cpp @@ -84,6 +84,10 @@ bool Timer::AdvanceIfElapsed(units::second_t period) { } } +bool Timer::IsRunning() const { + return m_running; +} + units::second_t Timer::GetFPGATimestamp() { // FPGA returns the timestamp in microseconds return units::second_t{frc::RobotController::GetFPGATime() * 1.0e-6}; diff --git a/wpilibc/src/main/native/include/frc/Timer.h b/wpilibc/src/main/native/include/frc/Timer.h index e1639826c71..37fe5e0c646 100644 --- a/wpilibc/src/main/native/include/frc/Timer.h +++ b/wpilibc/src/main/native/include/frc/Timer.h @@ -111,6 +111,13 @@ class Timer { */ bool AdvanceIfElapsed(units::second_t period); + /** + * Whether the timer is currently running. + * + * @return true if running. + */ + bool IsRunning() const; + /** * Return the FPGA system clock time in seconds. * diff --git a/wpilibc/src/test/native/cpp/TimerTest.cpp b/wpilibc/src/test/native/cpp/TimerTest.cpp index fd66541e90d..badeacbca9e 100644 --- a/wpilibc/src/test/native/cpp/TimerTest.cpp +++ b/wpilibc/src/test/native/cpp/TimerTest.cpp @@ -28,18 +28,22 @@ TEST_F(TimerTest, StartStop) { // Verify timer is initialized as stopped EXPECT_EQ(timer.Get(), 0_s); + EXPECT_FALSE(timer.IsRunning()); frc::sim::StepTiming(500_ms); EXPECT_EQ(timer.Get(), 0_s); + EXPECT_FALSE(timer.IsRunning()); // Verify timer increments after it's started timer.Start(); frc::sim::StepTiming(500_ms); EXPECT_EQ(timer.Get(), 500_ms); + EXPECT_TRUE(timer.IsRunning()); // Verify timer stops incrementing after it's stopped timer.Stop(); frc::sim::StepTiming(500_ms); EXPECT_EQ(timer.Get(), 500_ms); + EXPECT_FALSE(timer.IsRunning()); } TEST_F(TimerTest, Reset) { diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Timer.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Timer.java index 63e5ed4c99a..3e0aaf522b0 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Timer.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Timer.java @@ -151,4 +151,13 @@ public boolean advanceIfElapsed(double seconds) { return false; } } + + /** + * Whether the timer is currently running. + * + * @return true if running. + */ + public boolean isRunning() { + return m_running; + } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/TimerTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/TimerTest.java index 89619dd6335..aa64c6d0979 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/TimerTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/TimerTest.java @@ -35,18 +35,22 @@ void startStopTest() { // Verify timer is initialized as stopped assertEquals(timer.get(), 0.0); + assertFalse(timer.isRunning()); SimHooks.stepTiming(0.5); assertEquals(timer.get(), 0.0); + assertFalse(timer.isRunning()); // Verify timer increments after it's started timer.start(); SimHooks.stepTiming(0.5); assertEquals(timer.get(), 0.5, 1e-9); + assertTrue(timer.isRunning()); // Verify timer stops incrementing after it's stopped timer.stop(); SimHooks.stepTiming(0.5); assertEquals(timer.get(), 0.5, 1e-9); + assertFalse(timer.isRunning()); } @Test