diff --git a/wpilibc/src/main/native/cpp/Timer.cpp b/wpilibc/src/main/native/cpp/Timer.cpp index 3863de4a0c..bf60021918 100644 --- a/wpilibc/src/main/native/cpp/Timer.cpp +++ b/wpilibc/src/main/native/cpp/Timer.cpp @@ -54,6 +54,14 @@ void Timer::Start() { } } +void Timer::Restart() { + if (m_running) { + Stop(); + } + Reset(); + Start(); +} + void Timer::Stop() { if (m_running) { m_accumulatedTime = Get(); diff --git a/wpilibc/src/main/native/include/frc/Timer.h b/wpilibc/src/main/native/include/frc/Timer.h index 14674ee740..e1639826c7 100644 --- a/wpilibc/src/main/native/include/frc/Timer.h +++ b/wpilibc/src/main/native/include/frc/Timer.h @@ -76,6 +76,14 @@ class Timer { */ void Start(); + /** + * Restart the timer by stopping the timer, if it is not already stopped, + * resetting the accumulated time, then starting the timer again. If you + * want an event to periodically reoccur at some time interval from the + * start time, consider using AdvanceIfElapsed() instead. + */ + void Restart(); + /** * Stop the timer. * 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 6f9a3614f8..4f948f5ae1 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Timer.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Timer.java @@ -100,6 +100,19 @@ public class Timer { } } + /** + * Restart the timer by stopping the timer, if it is not already stopped, resetting the + * accumulated time, then starting the timer again. If you want an event to periodically reoccur + * at some time interval from the start time, consider using advanceIfElapsed() instead. + */ + public void restart() { + if (m_running) { + stop(); + } + reset(); + start(); + } + /** * Stop the timer. This computes the time as of now and clears the running flag, causing all * subsequent time requests to be read from the accumulated time rather than looking at the system