File tree Expand file tree Collapse file tree 8 files changed +137
-7
lines changed
main/kotlin/org/team5499/monkeyLib Expand file tree Collapse file tree 8 files changed +137
-7
lines changed Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ dependencies {
3232}
3333
3434group = ' org.team5499'
35- version = ' 2.3 .0'
35+ version = ' 2.4 .0'
3636
3737task sourcesJar (type : Jar ) {
3838 from sourceSets. main. allJava
Original file line number Diff line number Diff line change 11package org.team5499.monkeyLib.auto
22
3- import edu.wpi.first.wpilibj.Timer
3+ import org.team5499.monkeyLib.util.time.ITimer
4+ import org.team5499.monkeyLib.util.time.WPITimer
45
5- public open class Action (timeoutSeconds : Double ) {
6- private val mTimer: Timer
6+ public open class Action (timeoutSeconds : Double , timer : ITimer = WPITimer ()) {
7+
8+ private val mTimer: ITimer
79 private val mTimeoutSeconds: Double
810
911 init {
10- mTimer = Timer ()
12+ mTimer = timer
1113 mTimeoutSeconds = timeoutSeconds
1214 }
1315
Original file line number Diff line number Diff line change 11package org.team5499.monkeyLib.auto
22
3- public class NothingAction (timeout : Double ) : Action(timeout)
3+ import org.team5499.monkeyLib.util.time.ITimer
4+ import org.team5499.monkeyLib.util.time.WPITimer
5+
6+ public class NothingAction (timeout : Double , timer : ITimer = WPITimer ()) : Action(timeout, timer)
Original file line number Diff line number Diff line change 11package org.team5499.monkeyLib.auto
22
3- class ParallelAction (vararg actions : Action ) : Action(0.0 ) {
3+ import org.team5499.monkeyLib.util.time.ITimer
4+ import org.team5499.monkeyLib.util.time.WPITimer
5+
6+ class ParallelAction (vararg actions : Action , timer : ITimer = WPITimer ()) : Action(0.0 , timer) {
47
58 private val mActions: Array <out Action >
69
Original file line number Diff line number Diff line change 1+ package org.team5499.monkeyLib.util.time
2+
3+ /* *
4+ * Timer abstraction layer to allow unit testing
5+ */
6+ public interface ITimer {
7+
8+ /* *
9+ * Function to get time since timer started
10+ * @return number of ellapsed seconds
11+ */
12+ public fun get (): Double
13+
14+ /* *
15+ * Starts the timer
16+ */
17+ public fun start (): Unit
18+
19+ /* *
20+ * stops the timer
21+ */
22+ public fun stop (): Unit
23+
24+ /* *
25+ * resets the timer to 0.
26+ */
27+ public fun reset (): Unit
28+ }
Original file line number Diff line number Diff line change 1+ package org.team5499.monkeyLib.util.time
2+
3+ public class SystemTimer : ITimer {
4+
5+ private var mStartTime: Long
6+ private var mTotalPauseTime: Long
7+ private var mStartPauseTime: Long
8+ private var mPaused: Boolean
9+
10+ init {
11+ mStartTime = 0L
12+ mTotalPauseTime = 0L
13+ mStartPauseTime = 0L
14+
15+ mPaused = false
16+ }
17+
18+ public override fun get (): Double {
19+ @Suppress(" MagicNumber" )
20+ return (mStartTime - System .currentTimeMillis() - mTotalPauseTime) / 1000.0
21+ }
22+
23+ public override fun start () {
24+ if (mPaused) {
25+ mTotalPauseTime + = System .currentTimeMillis() - mStartPauseTime
26+ mPaused = false
27+ }
28+ mStartTime = System .currentTimeMillis()
29+ }
30+
31+ public override fun stop () {
32+ if (! mPaused) {
33+ mStartPauseTime = System .currentTimeMillis()
34+ mPaused = true
35+ }
36+ }
37+
38+ public override fun reset () {
39+ mStartTime = 0L
40+ mTotalPauseTime = 0L
41+ mTotalPauseTime = 0L
42+ mPaused = false
43+ }
44+ }
Original file line number Diff line number Diff line change 1+ package org.team5499.monkeyLib.util.time
2+
3+ import edu.wpi.first.wpilibj.Timer
4+
5+ public class WPITimer (timer : Timer = Timer ()) : ITimer {
6+
7+ private val mTimer: Timer
8+
9+ init {
10+ mTimer = timer
11+ }
12+
13+ public override fun get () = mTimer.get()
14+
15+ public override fun start () = mTimer.start()
16+
17+ public override fun stop () = mTimer.stop()
18+
19+ public override fun reset () = mTimer.reset()
20+ }
Original file line number Diff line number Diff line change 1+ package tests.utils
2+
3+ import org.junit.Test
4+ import org.junit.Assert.assertEquals
5+
6+ import org.team5499.monkeyLib.util.time.ITimer
7+ import org.team5499.monkeyLib.util.time.SystemTimer
8+
9+ public class TimerTest {
10+
11+ private val kEpsilon = 1E6
12+
13+ @Test
14+ fun timerSystemTest () {
15+ val timer: ITimer = SystemTimer ()
16+ timer.start()
17+ assertEquals(timer.get(), 0.0 , kEpsilon)
18+ Thread .sleep(2000L )
19+ assertEquals(timer.get(), 2.0 , kEpsilon)
20+ timer.stop()
21+ Thread .sleep(1000L )
22+ timer.start()
23+ Thread .sleep(1000L )
24+ assertEquals(timer.get(), 3.0 , kEpsilon)
25+ timer.stop()
26+ timer.reset()
27+ timer.start()
28+ assertEquals(timer.get(), 0.0 , kEpsilon)
29+ }
30+ }
You can’t perform that action at this time.
0 commit comments