From 8e707169a118ad0808f6ff2da80cb17161030258 Mon Sep 17 00:00:00 2001 From: Dustin Spicuzza Date: Sun, 30 Nov 2014 23:51:01 -0500 Subject: [PATCH] Add hasPeriodPassed function to java, for parity with C++ Timer API Change-Id: I0f9a2714f20deaaccce610bd3eec58409eac3104 --- .../java/edu/wpi/first/wpilibj/Timer.java | 24 +++++++++++++++++++ .../first/wpilibj/internal/HardwareTimer.java | 18 ++++++++++++++ .../wpi/first/wpilibj/internal/SimTimer.java | 18 ++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/wpilibj/wpilibJava/src/main/java/edu/wpi/first/wpilibj/Timer.java b/wpilibj/wpilibJava/src/main/java/edu/wpi/first/wpilibj/Timer.java index a35be8c397..8a1391b859 100644 --- a/wpilibj/wpilibJava/src/main/java/edu/wpi/first/wpilibj/Timer.java +++ b/wpilibj/wpilibJava/src/main/java/edu/wpi/first/wpilibj/Timer.java @@ -113,6 +113,19 @@ public class Timer { timer.stop(); } + /** + * Check if the period specified has passed and if it has, advance the start + * time by that period. This is useful to decide if it's time to do periodic + * work without drifting later by the time it took to get around to checking. + * + * @param period The period to check for (in seconds). + * @return If the period has passed. + */ + public boolean hasPeriodPassed(double period) + { + return timer.hasPeriodPassed(period); + } + public interface Interface { /** * Get the current time from the timer. If the clock is running it is derived from @@ -143,5 +156,16 @@ public class Timer { * looking at the system clock. */ public void stop(); + + + /** + * Check if the period specified has passed and if it has, advance the start + * time by that period. This is useful to decide if it's time to do periodic + * work without drifting later by the time it took to get around to checking. + * + * @param period The period to check for (in seconds). + * @return If the period has passed. + */ + public boolean hasPeriodPassed(double period); } } diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/internal/HardwareTimer.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/internal/HardwareTimer.java index b5a301a057..0ba6339930 100644 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/internal/HardwareTimer.java +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/internal/HardwareTimer.java @@ -120,5 +120,23 @@ public class HardwareTimer implements Timer.StaticInterface { m_accumulatedTime = temp; m_running = false; } + + /** + * Check if the period specified has passed and if it has, advance the start + * time by that period. This is useful to decide if it's time to do periodic + * work without drifting later by the time it took to get around to checking. + * + * @param period The period to check for (in seconds). + * @return If the period has passed. + */ + public synchronized boolean hasPeriodPassed(double period) { + if (get() > period) { + // Advance the start time by the period. + // Don't set it to the current time... we want to avoid drift. + m_startTime += period; + return true; + } + return false; + } } } diff --git a/wpilibj/wpilibJavaSim/src/main/java/edu/wpi/first/wpilibj/internal/SimTimer.java b/wpilibj/wpilibJavaSim/src/main/java/edu/wpi/first/wpilibj/internal/SimTimer.java index 8314a3ffbe..cbc75eb668 100644 --- a/wpilibj/wpilibJavaSim/src/main/java/edu/wpi/first/wpilibj/internal/SimTimer.java +++ b/wpilibj/wpilibJavaSim/src/main/java/edu/wpi/first/wpilibj/internal/SimTimer.java @@ -149,5 +149,23 @@ public class SimTimer implements Timer.StaticInterface { m_accumulatedTime = temp; m_running = false; } + + /** + * Check if the period specified has passed and if it has, advance the start + * time by that period. This is useful to decide if it's time to do periodic + * work without drifting later by the time it took to get around to checking. + * + * @param period The period to check for (in seconds). + * @return If the period has passed. + */ + public synchronized boolean hasPeriodPassed(double period) { + if (get() > period) { + // Advance the start time by the period. + // Don't set it to the current time... we want to avoid drift. + m_startTime += period; + return true; + } + return false; + } } }