Timer: add hasElapsed, advanceIfElapsed (#2322)

The current hasPeriodPassed() function is confusing.  In preparation for deprecating it,
add new advanceIfElapsed() function with same functionality and hasElapsed() function
which only checks that the time period has elapsed and does not advance the timer.

Also fix a couple of incorrect usages of hasPeriodPassed().
This commit is contained in:
Oblarg
2020-02-08 13:23:29 -05:00
committed by GitHub
parent 29c82527a5
commit f0a18f31e7
13 changed files with 72 additions and 24 deletions

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */
/* Copyright (c) 2016-2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -104,21 +104,44 @@ public class Timer {
m_running = false;
}
/**
* Check if the period specified has passed.
*
* @param seconds The period to check.
* @return Whether the period has passed.
*/
public synchronized boolean hasElapsed(double seconds) {
return get() > seconds;
}
/**
* 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.
* @return Whether the period has passed.
*/
public synchronized boolean hasPeriodPassed(double period) {
if (get() > period) {
return advanceIfElapsed(period);
}
/**
* 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 seconds The period to check.
* @return Whether the period has passed.
*/
public synchronized boolean advanceIfElapsed(double seconds) {
if (get() > seconds) {
// Advance the start time by the period.
// Don't set it to the current time... we want to avoid drift.
m_startTime += period * 1000;
m_startTime += seconds * 1000;
return true;
} else {
return false;
}
return false;
}
}